给出Cpp 的文法,要求不含左递归和公共子表达式
时间: 2024-05-28 18:09:40 浏览: 20
对于C++的文法,不含左递归和公共子表达式的规则可以如下:
<program> -> <declaration-list>
<declaration-list> -> <declaration> | <declaration-list> <declaration>
<declaration> -> <var-declaration> | <fun-declaration>
<var-declaration> -> <type-specifier> <id-list> ;
<type-specifier> -> int | float | char | double | void
<id-list> -> <id> | <id-list> , <id>
<id> -> <identifier>
<fun-declaration> -> <type-specifier> <identifier> ( <params> ) <compound-stmt>
<params> -> <param-list> | void
<param-list> -> <param> | <param-list> , <param>
<param> -> <type-specifier> <identifier>
<compound-stmt> -> { <local-declarations> <statement-list> }
<local-declarations> -> <var-declaration> | <local-declarations> <var-declaration>
<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> -> <identifier> | <identifier> [ <expression> ]
<simple-expression> -> <additive-expression> <relop> <additive-expression> | <additive-expression>
<relop> -> < | > | <= | >= | == | !=
<additive-expression> -> <term> <addop> <additive-expression> | <term>
<addop> -> + | -
<term> -> <factor> <mulop> <term> | <factor>
<mulop> -> * | /
<factor> -> ( <expression> ) | <var> | <call> | <num>
<num> -> <integer> | <float-point>
<call> -> <identifier> ( <args> )
<args> -> <arg-list> | void
<arg-list> -> <expression> | <arg-list> , <expression>
<identifier> -> letter { letter | digit }
<integer> -> digit { digit }
<float-point> -> <integer> . <integer>
注意,以上只是C++语言的简化版本,实际的语法会更加复杂。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)