mini-c文法规则
时间: 2023-12-14 17:36:44 浏览: 137
TINY+文法规则
以下是一种简单的Mini-C文法规则:
```
<program> ::= <declaration-list>
<declaration-list> ::= <declaration> | <declaration> <declaration-list>
<declaration> ::= <var-declaration> | <fun-declaration>
<var-declaration> ::= <type-specifier> <id-list> ;
<id-list> ::= <identifier> | <identifier>, <id-list>
<fun-declaration> ::= <type-specifier> <identifier> ( <params> ) <compound-stmt>
<params> ::= <param-list> | VOID
<param-list> ::= <param> | <param>, <param-list>
<param> ::= <type-specifier> <identifier>
<compound-stmt> ::= { <local-declarations> <statement-list> }
<local-declarations> ::= <var-declaration> | <local-declarations> <var-declaration>
<statement-list> ::= <statement> | <statement> <statement-list>
<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> ::= <identifier> | <identifier> [ <expression> ]
<simple-expression> ::= <additive-expression> <relop> <additive-expression> | <additive-expression>
<additive-expression> ::= <term> <addop> <term> | <term>
<term> ::= <factor> <mulop> <factor> | <factor>
<factor> ::= ( <expression> ) | <var> | <call> | NUM
<call> ::= <identifier> ( <args> )
<args> ::= <arg-list> | ε
<arg-list> ::= <expression> | <expression>, <arg-list>
<relop> ::= <= | < | > | >= | == | !=
<addop> ::= + | -
<mulop> ::= * | /
```
其中,`<identifier>`代表变量名,`NUM`代表数字常量,`<type-specifier>`代表数据类型(如int、float等)。这个文法规则支持函数定义、变量声明、复合语句、选择语句、循环语句和返回语句等基本语法结构。
阅读全文