编译原理课后习题答案

B B’

(5) (4) (6) aade#的分析过程如下 分析栈 A# aA’ # A’ # ABe# aA’Be# A’Be# Be# dB’e# B’e# e# #

输入流 aade# aade# ade# ade# ade# de# de# de# e# e# # 动作 替换(1) 匹配 替换(2) 替换(1) 匹配 替换(3) 替换(4) 匹配 替换 匹配 成功 9

第五章(这章答案是错的) 1. 设有下列文法:

(1) S?aA

A?Ab A?b S?aSSS S?c S?b A?SA A?a S?ccB B?ccB B?b A?cA A?a

(2) S?aSSb

(3) S?AS

(4) S?cA

构造上述文法的LR(0)归约活前缀状态机,并给出LR(0)分析表。 答:

(1)

Ch05-1-(1)0Z→.SS→.aAA→.AbA→.b3A→b.

(2)

有移入、规约冲突1SaZ→S.2S→a.AA→.AbA→.bbA4S→aA.A→A.bb5A→Ab.

10

Ch05-1-(2)1Z→S.S0Z→.SS→.aSSbS→.aSSSS→.cc5S→c.c2S→a.SSbS→a.SSSS→.cS→.aSSbS→.aSSSacaS3S→aS.SbS→aS.SSS→.cS→.aSSbS→.aSSSa4S→aSS.bS→aSS.SS→.cS→.aSSbS→.aSSScbS6S→aSSb.7S→aSSS.aS

actionaS0Z→.S (1)S→.aSSb (2)S→.aSSS (3)S→.c (4)S1S2S3S4S5S6S7

(3)

gotocs5Accs5s5#S1347R4R2R3

bs2s2s2s2R4R2R3s6R4R2R3s5R4R2R3 11

Ch05-1-(3)有移入、规约冲突2A→SA.1Z→S.A→S.AA→.SAA→.aSa4aA→a.5bS→b.bS7S→AS.AS3A→S.AA→.aA→.SAS→.ASS→.bb8A→SA.S→A.SS→.ASS→.bA9SS→AS.S10AS→.ASS→A.SS→.bASbb0Z→.SS→.ASS→.bA→.SAA→.aA6S→A.SS→.ba

(4)

Ch05-1-(4)0Z→.SS→.cAS→.ccB2Z→S.S4S→cA.A1S→c.AcS→c.cBcA→.cAA→.aaa3A→a.6S→ccB.B5S→cc.BB→.ccBB→.bA→c.AA→.cAA→.a8B→c.cBA→c.AA→.cAA→.acA7AA→cA.b9B→b.cc11B→ccB.B10B→cc.BB→.ccBB→.bA→c.AA→.cAA→.aAbaa

12

联系客服:779662525#qq.com(#替换为@)