根据以上C语言代码生成其文法
时间: 2024-04-30 11:18:43 浏览: 52
以下是基于该 C 代码的文法:
```
program -> declaration_list
declaration_list -> declaration
| declaration_list declaration
declaration -> var_declaration
| fun_declaration
var_declaration -> type_specifier ID ;
| type_specifier ID [ NUM ] ;
type_specifier -> int
| void
fun_declaration -> type_specifier ID ( params ) compound_stmt
params -> param_list
| void
param_list -> param
| param_list , param
param -> type_specifier ID
| type_specifier ID []
compound_stmt -> { local_declarations statement_list }
local_declarations -> local_declarations var_declaration
| empty
statement_list -> statement
| statement_list statement
statement -> expression_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt
expression_stmt -> expression ;
| ;
selection_stmt -> if ( expression ) statement
| if ( expression ) statement else statement
iteration_stmt -> while ( expression ) statement
return_stmt -> return ;
| return expression ;
expression -> var = expression
| simple_expression
var -> ID
| ID [ expression ]
simple_expression -> additive_expression relop additive_expression
| additive_expression
additive_expression -> additive_expression addop term
| term
term -> term mulop factor
| factor
factor -> ( expression )
| var
| call
| NUM
call -> ID ( args )
args -> arg_list
| empty
arg_list -> expression
| arg_list , expression
empty -> ε
```
其中,大写字母表示非终结符,小写字母表示终结符,ε 表示空串。
阅读全文