已知某算术表达式的文法G为: (1) <AEXPR> -<AEXPR> + <TERM><TERM>(2) <TERM>-> <TERM> *<FACTOR> <FACTOR>(3) <FACTOR> i(<AEXPR>给出i+i+i和i+i*i的最左推导、最右推导和语法树
时间: 2023-12-15 12:06:14 浏览: 140
最左推导、最右推导和语法树如下所示:
1. i+i+i的最左推导、最右推导和语法树:
最左推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<FACTOR>
+
<FACTOR>
i
<TERM>
+
<TERM>
<FACTOR>
i
```
最右推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<TERM>
<FACTOR>
+
<FACTOR>
i
<TERM>
+
<FACTOR>
i
```
语法树:
```
AEXPR
|
TERM
/ \
FACTOR TERM
| / \
i FACTOR
/ \
+ FACTOR
|
i
```
2. i+i*i的最左推导、最右推导和语法树:
最左推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<FACTOR>
+
<FACTOR>
i
<TERM>
*
<TERM>
<FACTOR>
i
```
最右推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<TERM>
<FACTOR>
*
<FACTOR>
i
```
语法树:
```
AEXPR
|
TERM
/ \
FACTOR TERM
| / \
i FACTOR
/ \
+ FACTOR
|
i
```
相关问题
已知某算术表达式的文法G为: (1) <AEXPR>--<AEXPR> + <TERM> <TERM>(2) <TERM-- <TERM> * FACTOR> <FACTOR>(3) <FACTOR>- (<AEXPR>)给出ii+i和iti*的最左推导、最右推导和语法树
最左推导:
```
<AEXPR>
<AEXPR> + <TERM>
<TERM> + <TERM> * <FACTOR>
<FACTOR> + i
i + i
```
最右推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<FACTOR> * <TERM>
i * <TERM>
<TERM> + <FACTOR>
i + i
```
语法树如下:
```
+
/ \
i *
/ \
i i
```
已知某算术表达式的文法G为: (1)<AEXPR>→<AEXPR>+<TERM>丨<TERM> (2)<TERM>→<TERM>*<FACTOR>丨<FACTOR>(3)<FACTOR>→i丨(<AEXPR>) 给出i+i+i和i+i*I的最左推导、最右推导
最左推导和最右推导的概念已经在引用中给出了定义,下面分别给出i+i+i和i+i*I的最左推导和最右推导。
i+i+i的最左推导:
```
<AEXPR>
<AEXPR>+<TERM>
<TERM>+<TERM>
<FACTOR>+<TERM>
i+<TERM>
i+<FACTOR>
i+i
```
i+i+i的最右推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<FACTOR>+<TERM>
<i>+<TERM>
<i>+<FACTOR>
i+i
<TERM>+<FACTOR>
<i>+<FACTOR>
i+i+i
```
i+i*I的最左推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<FACTOR>*<FACTOR>
<i>*<FACTOR>
<i>+<FACTOR>
<FACTOR>+<TERM>
i+<TERM>
i+<FACTOR>
i+i
```
i+i*I的最右推导:
```
<AEXPR>
<TERM>
<FACTOR>
i
<FACTOR>*<FACTOR>
<i>*<FACTOR>
<i>*<FACTOR>
<FACTOR>
(<AEXPR>)
<TERM>*<FACTOR>
<FACTOR>*<FACTOR>
i*<FACTOR>
i*<AEXPR>
<FACTOR>*<AEXPR>
<i>+<AEXPR>
<FACTOR>+<TERM>
i+<TERM>
i+<FACTOR>
i+i*I
```
阅读全文