编制一个能够进行语法分析并生成三地址代码的微型编译程序。1、考虑给定的文法,消除左递归,提取左因子; 2、编制并化简语法图;3、编制各个递归子程序的算法; 4、编制各个递归子程序的函数; 5、连接实验一的词法分析函数scan( ),进行测试6、设计三地址代码生成的数据结构和算法.输入示例:while (a3+15)>0xa do if x2 = 07 then while y<z do y =x * y / z;
时间: 2024-03-18 21:39:19 浏览: 56
好的,我可以回答你的问题!为了实现语法分析并生成三地址代码,我们需要按照如下步骤进行:
1. 消除左递归,提取左因子。给定的文法如下:
```
<stmt> ::= <while_stmt> | <if_stmt> | <assign_stmt>
<while_stmt> ::= while <bool_expr> do <stmt>
<if_stmt> ::= if <bool_expr> then <stmt> | if <bool_expr> then <stmt> else <stmt>
<bool_expr> ::= <expr> <rel_op> <expr> | not <bool_expr>
<expr> ::= <term> | <expr> <add_op> <term>
<term> ::= <factor> | <term> <mul_op> <factor>
<factor> ::= id | num | ( <expr> )
<rel_op> ::= < | > | <= | >= | == | !=
<add_op> ::= + | -
<mul_op> ::= * | /
```
我们可以先消除左递归,得到如下文法:
```
<stmt> ::= <while_stmt> | <if_stmt> | <assign_stmt>
<while_stmt> ::= while <bool_expr> do <stmt>
<if_stmt> ::= if <bool_expr> then <stmt> <else_part>
<else_part> ::= else <stmt> | ε
<bool_expr> ::= <expr> <rel_op> <expr> | not <bool_expr>
<expr> ::= <term> <expr_tail>
<expr_tail> ::= <add_op> <term> <expr_tail> | ε
<term> ::= <factor> <term_tail>
<term_tail> ::= <mul_op> <factor> <term_tail> | ε
<factor> ::= id | num | ( <expr> )
<rel_op> ::= < | > | <= | >= | == | !=
<add_op> ::= + | -
<mul_op> ::= * | /
```
然后,我们可以提取左因子,得到如下文法:
```
<stmt> ::= <while_stmt> | <if_stmt> | <assign_stmt>
<while_stmt> ::= while <bool_expr> do <stmt>
<if_stmt> ::= if <bool_expr> then <stmt> <else_part>
<else_part> ::= else <stmt> | ε
<bool_expr> ::= <expr> <bool_expr_tail>
<bool_expr_tail> ::= <rel_op> <expr> | not <bool_expr>
<expr> ::= <term> <expr_tail>
<expr_tail> ::= <add_op> <term> <expr_tail> | ε
<term> ::= <factor> <term_tail>
<term_tail> ::= <mul_op> <factor> <term_tail> | ε
<factor> ::= id | num | ( <expr> )
<rel_op> ::= < | > | <= | >= | == | !=
<add_op> ::= + | -
<mul_op> ::= * | /
```
2. 编制并化简语法图。根据上述文法,可以得到如下的语法图:
```
<stmt>
↓
<while_stmt>
↓
while <bool_expr> do <stmt>
↓
while <expr> <bool_expr_tail> do <stmt>
↓
while <term> <expr_tail> <bool_expr_tail> do <stmt>
↓
while <factor> <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id <mul_op> <factor> <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * <factor> <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( <expr> ) <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( <term> <expr_tail> ) <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( <factor> <term_tail> <expr_tail> ) <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( id <term_tail> <expr_tail> ) <term_tail> <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( id <mul_op> <factor> <term_tail> <expr_tail> ) <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( id * <factor> <term_tail> <expr_tail> ) <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( id * ( <expr> ) <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( <term> <expr_tail> ) <term_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( <factor> <term_tail> <expr_tail> ) <term_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id <term_tail> <expr_tail> ) <term_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id <mul_op> <factor> <term_tail> ) <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * <factor> <term_tail> ) <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * ( <expr> ) <term_tail> ) ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * ( <term> <term_tail> ) ) ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * ( <factor> <term_tail> ) ) ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id <term_tail> ) ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) ) <rel_op> <expr> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> <term> <expr_tail> ) do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> <factor> <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * <factor> <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * ( <expr> ) <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * ( <term> <expr_tail> ) <term_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * ( <factor> <term_tail> <expr_tail> ) <term_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id ) <add_op> <term> <expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id + <factor> <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id + id <term_tail> <expr_tail> ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id + id * <factor> <term_tail> ) <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id + id * ( <expr> ) <term_tail> ) <expr_tail> <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id + id * ( <term> <term_tail> ) ) <bool_expr_tail> do <stmt>
↓
while id * ( id * ( id * id ) <rel_op> id * id + id * ( <factor> <term_tail> ) )
阅读全文