消除其中的左递归和公共子表达式
时间: 2024-05-23 15:09:48 浏览: 20
消除左递归和公共子表达式是常见的优化手段,可以有效地降低语法分析和代码生成的复杂度。对于左递归,可以采用以下步骤进行消除:
1. 将所有的左递归规则转换为右递归规则;
2. 对右递归规则进行提取公因子,得到一个非左递归的等价文法。
对于公共子表达式,可以采用以下步骤进行消除:
1. 对于每个表达式,建立它的语法树;
2. 在语法树中,查找所有可能存在的公共子树;
3. 将公共子树提取出来,分别存储在临时变量中;
4. 将原表达式中的公共子树替换为对应的临时变量。
这样,我们就可以消除左递归和公共子表达式,从而使得程序的执行速度更快、更高效。
相关问题
给出Cpp 的文法,要求不含左递归和公共子表达式
对于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++语言的简化版本,实际的语法会更加复杂。
消除左递归和消除直接左递归
消除左递归是一种常用的语法转换技术,它可以将一个文法转换为不含左递归的等价文法。消除左递归是为了避免在语法分析时出现无限递归的情况。
消除直接左递归是一种特殊的左递归情况,它指的是存在形如 A -> Aα | β 的产生式,其中 α 和 β 都是由非终结符和终结符组成的符号串。消除直接左递归的方法是将这个产生式拆分成两个产生式:A -> βA' 和 A' -> αA' | ε,其中 A' 是一个新的非终结符。
通过消除左递归和消除直接左递归,可以将一个文法转换为不含左递归的等价文法,从而方便地进行语法分析和语义分析。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)