C-Minus 的词法规则
(1)关键字: if else int return void while
(2)专用符号: + - * / < >= == ~= = ; , ( ) [ ] { } /* */
(3)其他标记为 ID 和 NUM ,通过下列正则表达式定义:
ID = letter letter*
NUM = digit digit*
letter = a|..|z|A|..|Z
digit = 0|..|9
(4)空格由空白、换行符、制表符组成。
(5)注释由 /*...*/ 围起来,不能嵌套。
C-Minus 的语法规则
C-Minus 的 BNF 语法如下:
1. program -> declaration_list
2. declaration_list -> declaration_list declaration | declaration
3. declaration -> var_declaration | fun_declaration
4. var_declaration -> type_specifier ID; | type_specifier ID [ NUM ];
5. type_specifier -> int | void
6. fun_declaration -> type_specifier ID ( params ) compound_stmt
7. params -> param_list | void
8. param_list -> param_list , param | param
9. param -> type_specifier ID | type_specifier ID [ ]
10. compound_stmt -> { local_declarations statement_list }
11. local_declarations -> local_declarations var_declaration | empty
12. statement_list -> statement_list statement | empty
13. statement -> expression_stmt | compound_stmt | selection_stmt | iteration_stmt | return_stmt
14. expression_stmt -> expression ; | ;
15. selection_stmt -> if ( expression ) statement | if ( expression ) statement else statement
16. iteration_stmt -> while ( expression ) statement
17. return_stmt -> return | return expression
18. expression -> var = expression | simple_expression
19. var -> ID | ID [ expression ]
20. simple_expression -> additive_expression relop additive_expression | additive_expression
21. relop -> <= | | >= | == | ~=
22. additive_expression -> additive_expression addop term | term
23. addop -> + | -
24. term -> term mulop factor | factor
25. mulop -> * | /
26. factor -> ( expression ) | var | call | NUM
27. call -> ID ( args )
28. args -> arg_list | empty
29. arg8list -> arg_list , expression | expression
对以上每条文法规则,给出了相关语义的简短解释。
1. program -> declaration_list
2. declaration_list -> declaration_list declaration | declaration
3. dec
2021-04-08 18:59:04
9KB
编译原理实验
1