哈工大编译原理实验2——语法分析

这篇博客介绍了哈工大编译原理实验的第二部分,主要涉及语法分析。由于实验文法简单,作者采用了学长提供的文法,并增加了数组判断功能。完整工程源代码可在GitHub上找到。
摘要由CSDN通过智能技术生成

此次试验由于指导书的文法比较简单,且没有消除左递归(我懒),因此使用学长的文法,并加上了数组判断。

S -> func funcs
funcs -> func funcs
funcs -> $
func -> type IDN ( args ) func_body
type -> int
type -> short
type -> long
type -> char
type -> float
type -> double
type -> void
type -> unsigned type
args -> type IDN arg
args -> $
arg -> , type IDN arg
arg -> $
func_body -> ;
func_body -> block
block -> { define_stmts stmts }
define_stmts -> define_stmt define_stmts
define_stmts -> $
define_stmt -> type IDN init vars ;
init -> = expression
init -> [ INT10 ]
init -> $
vars -> , IDN init vars
vars -> $
stmts -> stmt stmts
stmts -> $
stmt -> assign_stmt
stmt -> jump_stmt
stmt -> iteration_stmt
stmt -> branch_stmt
assign_stmt -> expression ;
jump_stmt -> continue ;
jump_stmt -> break ;
jump_stmt -> return isnull_expr ;
iteration_stmt -> while ( logical_expression ) block_stmt
iteration_stmt -> for ( isnull_expr ; isnull_expr ; isnull_expr ) block_stmt
iteration_stmt -> do block_stmt while ( logical_expression ) ;
branch_stmt -> if ( logical_expression ) block_stmt result
result -> else block_stmt
result -> $
logical_expression -> ! expression bool_expression
logical_expression -> expression bool_expression
bool_expression -> lop expression bool_expression
bool_expression -> $
lop -> &&
lop -> ||
branch_stmt -> switch ( IDN ) { case_stmt case_stmts default_stmt }
case_stmts -> case_stmt case_stmts
case_stmts -> $
case_stmt -> case const : stmts
default_stmt -> default : stmts
block_stmt -> { stmts }
isnull_expr -> expression
isnull_expr -> $
expression -> value operation
operation -> compare_op value
operation -> equal_op value
operation -> $
compare_op -> >
compare_op -> >=
compare_op -> <
compare_op -> <=
compare_op -> ==
compare_op -> !=
equal_op -> =
equal_op -> +=
equal_op -> -=
equal_op -> *=
equal_op -> /=
equal_op -> %=
value -> item 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值