编译原理作业3
答案仅供参考,若有问题欢迎评论区讨论~
4.2.1
考虑上下文无关文法:S ->SS+|SS*|a,以及串aa + a*
(1)给出这个串的一个最左推导。
(2)给出这个串的一个最右推导。
(3)给出这个串的一棵语法分析树。
(1)最左推导:
S->SS*
->SS+S*
->aS+S*
->aa+S*
->aa+a*
(2)最右推导:
S->SS*
->Sa*
->SS+a*
->Sa+a*
->aa+a*
(3)语法分析树
4.2.2
对下列的每一对文法和串重复练习4.2.1
-
S->0S1 | 01和串000111。
-
S->+SS | *SS |a和串+*aaa。
-
S->S(S)S | ε 和串(()())
-
S->(L) | a以及L->L,S | S和串((a, a), a, (a))。
- 1. S->0S1 | 01和串000111
(1)最左推导:
S->0S1
->00S11
->000111
(2)最右推导:
S->0S1
->00S11
->000111
(3)语法分析树
- S->+SS | *SS |a和串+*aaa
(1)最左推导:
S->+SS
->+*SSS
->+*aSS
->+*aaS
->+*aaa
(2)最右推导:
S->+SS
->+Sa
->+*SSa
->+*Saa
->+*aaa
(3)语法分析树
- S->S(S)S | ε 和串(()())
(1)最左推导:
S->S(S)S
->(S)S
->(S(S)S)S
->((S)S)S
->(()S)S
->(()S(S)S)S
->(()(S)S)S
->(()()S)S
->(()())S
->(()())
(2)最右推导:
S->S(S)S
->S(S)
->S(S(S)S)
->S(S(S))
->S(S())
->S(S(S)S())
->S(S(S)())
->S(S()())
->S(()())
->(()())
(3)语法分析树:
- S->(L) | a以及L->L,S | S和串((a, a), a, (a))
(1)最左推导:
S->(L)
->(L,S)
->(L,S,S)
->(S,S,S)
->((L),S,S)
->((L,S),S,S)
->((S,S),S,S)
->((a,S),S,S)
->((a,a),S,S)
->((a,a),a,S)
->((a,a),a,(L))
->((a,a),a,(s))
->((a,a),a,(a))
(2)最右推导:
S->(L)
->(L,S)
->(L,(L))
->(L,(S))
->(L,(a))
->(L,S,(a))
->(L,a,(a))
->(S,a,(a))
->((L),a,(a))
->((L,S),a,(a))
->((L,a),a,(a))
->((S,a),a,(a))
->((a,a),a,(a))
(3)语法分析树
4.2.3
为下面的语言设计文法
(1)所有由0和1组成的并且每个0之后都至少跟着一个1的串的集合。
(2)所有由0和1组成的回文的集合,也就是从前面和从后面读结果都相同的串的集合。
(1)
很容易想到正则表达式为:(0?1)*
但文法应该是:
S->ST
|ε
T->01
|1
这个串可以为空,或者是若干个T相连,每个T是01或者1,刚好符合题意。
(2)
s-> 0s0
| 1s1
| 0
| 1
| ε
4.2.5
使用练习4.2.4中描述的括号表示法来简化如下的关于语句块和条件语句的文法。
stmt-> if expr then stmt else stmt
| if expr then stmt
| begin stmtList end
stmtList-> stmt ; stmtList | stmt
stmt-> if expr then stmt [else stmt]
| begin stmtList end
stmtList-> stmt[; stmtList]
或
stmtList->stmt{;stmt}
4.4.1
为下面的每一个文法设计一个预测分析器,并给出预测分析表。你可能先要对文法进行提取左公因子或消除左递归的操作。
1. 练习4.2.2(1)中的文法。
2. 练习4.2.2(2)中的文法。
3. 练习4.2.2(3)中的文法。
4. 练习4.2.2(5)中的文法。
- S->0S1 | 01
提取左公因子:
S->0S’
S’->S1
| 1
FIRST/FOLLOW/NULLABLE集:
FIRST | FOLLOW | NULLABLE | |
---|---|---|---|
S | {0} | {1} | FALSE |
S’ | {0,1} | {1} | FALSE |
预测分析表:
0 | 1 | |
---|---|---|
S | S->0S’ | |
S’ | S’->S1 | S’->1 |
- S->+SS | *SS |a
FIRST/FOLLOW/NULLABLE集:
FIRST | FOLLOW | NULLABLE | |
---|---|---|---|
S | {+,*,a} | {+,*,a} | FALSE |
预测分析表:
+ | * | a | |
---|---|---|---|
S | S->+SS | S->*SS | S->a |
- S->S(S)S | ε
消除左递归:
S->S’
S’->(S)SS’
| ε
FIRST/FOLLOW/NULLABLE集:
FIRST | FOLLOW | NULLABLE | |
---|---|---|---|
S | {(,),$,ε} | {(,),$,ε} | TRUE |
S’ | {(,),$,ε} | {(,),$,ε} | TRUE |
预测分析表:
( | ) | $ | |
---|---|---|---|
S | S->S’ | S->S’ | S->S’ |
S’ | S’->(S)SS’ S’->ε | S’->ε | S’->ε |
- S->(L) | a以及L->L,S | S
消除左递归:
S->(L)
|a
L->SL’
L’->,SL’
|ε
FIRST/FOLLOW/NULLABLE集:
FIRST | FOLLOW | NULLABLE | |
---|---|---|---|
S | {(,a} | {,,),ε}(第一个是逗号) | FALSE |
L | {(,a} | {)} | FALSE |
L’ | {,,),ε} | {)} | TRUE |
预测分析表:
( | ) | , | a | |
---|---|---|---|---|
S | S->(L) | S->a | ||
L | L->SL’ | L->SL’ | ||
L’ | L’->ε | L’->,SL’ |
4.4.4
计算练习4.2.2中各个文法的FIRST和FOLLOW集合。
- S->0S1 | 01
FIRST | FOLLOW | |
---|---|---|
S | {0} | {1,$} |
- S->+SS | *SS |a
FIRST | FOLLOW | |
---|---|---|
S | {+,*,a} | {+,*,a,$} |
- S->S(S)S | ε
FIRST | FOLLOW | |
---|---|---|
S | {(,ε} | {(,),$} |
- S->(L) | a以及L->L,S | S
FIRST | FOLLOW | |
---|---|---|
S | {(,a} | {,,),$}(第一个是逗号) |
L | {(,a} | {,,)}(第一个是逗号) |
4.6.5
说明下面的文法
S->AaAb | BbBa
A->ε
B->ε
是LL(1)的,但不是SLR(1)的。
(1)该文法是LL(1)的
文法G⇔LL(1),当且仅当G中的任意两条产生式A->α|β满足下列条件
- 当α与β不能都推导出ε,则FIRST(α)与FIRST(β)不相交
- 当α或β能推导出ε
- 当α能推导出ε,则FIRST(β)与FOLLOW(A)不相交
- 当β能推导出ε,则FIRST(α)与FOLLOW(A)不相交
AaAb与BbBa不能都推导出ε,FIRST(AaAb) = {a},FIRST(BbBa) = {b},不相交,所以该文法是LL(1)的。
(2)该文法不是SLR(1)的
S_0状态:
S-> · AaAb
S-> · BbBa
A-> · ε
B-> · ε
接受一个ε,可以直接到达S_1:
A->ε·
B->ε·
此时就会发生规约冲突,所以该文法不是SLR(1)的。
4.6.6
说明下面的文法
S->SA | A
A->a
是SLR(1)的,但不是LL(1)的。
(1)该文法是SLR(1)的
构造增广文法:
0: S’->S$
1: S->SA
2: | A
3: A->a
S_0状态:
S’->·S$
S->·SA
S->·A
A->·a
S_0状态吃入S到达S_1状态:
S’->S·$
S->S·A
A->·a
S_0状态吃入A到达S_2状态:
S->A·
S_0状态吃入a到达S_3状态:
A->a·
S_1状态吃入A到达S_4状态:
S->SA·
S_1状态吃入a到达S_3状态:
A->a·
FOLLOW(S) = {a,$},FOLLOW(A) = {$}。
该文法的SLR分析表如下:
动作 | 动作 | 转移 | 转移 | |
---|---|---|---|---|
状态/符号 | a | $ | S | A |
S_0 | s3 | g1 | g2 | |
S_1 | s3 | Accept | g4 | |
S_2 | r2 | r2 | ||
S_3 | r3 | |||
S_4 | r1 | r1 |
该文法的SLR分析表没有冲突,所以该文法是SLR(1)的
(2)该文法不是LL(1)的
文法G是LL(1)的条件已经在4.6.5中给出。
对于产生式S->SA|A,SA与A不能都推导出ε,FIRST(SA) = {a},FIRST(A) = {a},相交,所以该文法不是LL(1)的。
4.7.2
为练习4.2.2(5)的文法
S->(L)
|a
L->L,S
|S
构造 1)规范LR项集族;2)LALR项集族。
构造增广文法:
S’->S$
S->(L)
|a
L->L,S
|S
(1)规范LR项集族
(2)LALR项集族
LALR即对LR(1)进行合并同心项。先看一下LR(1)中可以合并的产生式集(已经用相同的颜色标出):
2-6,3-7,4-10,8-12可合并。
合并之后得LALR项集族:
4.7.5
说明下面的文法
S->Aa | bAc | Bc | bBa
A->d
B->d
是LR(1)的,但不是LALR(1)的。
构造增广文法:
S’->S$
S->Aa
|bAc
|Bc
|bBa
A->d
B->d
(1)LR(1)项集族如图:
没有冲突,故该文法是LR(1)的。
(2)合并前:
LALR(1)项集族如图:
笔记:
-
自底向上的语法分析都不用进行消除左递归/提取左公因子的处理。
-
语义分析要注意构造增广文法(S’->S$)
PS:(所有题目均为自己所做,图和表均为自己所画,为了美观草稿纸就不往上贴了~)