![編譯原理與實(shí)踐第四章答案_第1頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/26/ed76e50a-26bc-4f1a-bcf8-56a6ab604d10/ed76e50a-26bc-4f1a-bcf8-56a6ab604d101.gif)
![編譯原理與實(shí)踐第四章答案_第2頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/26/ed76e50a-26bc-4f1a-bcf8-56a6ab604d10/ed76e50a-26bc-4f1a-bcf8-56a6ab604d102.gif)
![編譯原理與實(shí)踐第四章答案_第3頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/26/ed76e50a-26bc-4f1a-bcf8-56a6ab604d10/ed76e50a-26bc-4f1a-bcf8-56a6ab604d103.gif)
![編譯原理與實(shí)踐第四章答案_第4頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/26/ed76e50a-26bc-4f1a-bcf8-56a6ab604d10/ed76e50a-26bc-4f1a-bcf8-56a6ab604d104.gif)
![編譯原理與實(shí)踐第四章答案_第5頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/26/ed76e50a-26bc-4f1a-bcf8-56a6ab604d10/ed76e50a-26bc-4f1a-bcf8-56a6ab604d105.gif)
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、The exercises of Chapter FourGrammar: A f (A) / &Assume we have lookahead of one token as in the example on p 144 in the text book Procedure A()if (LookAhead() '(') thenCall Expect('(')Call A()Call Expect (')')Call A()elseif (LookAhead() $) thenretum()else/* error */fifie
2、nd4 3 Given the grammarstatementf assign-stmt|call-stmt|other assign-stmtf identifier :=exp call-stmtfidentifier (exp-list)SolutionFirst, con vert the grammar into following forms:state me ntf ide ntifier :=exp | ide ntifier (explist)| otherThen, the pseudocode to parse this grammar:Procedure statem
3、entBegi nCase token of(identifer: match(identifer);case token of(:=:match(:=);exp;(:match();exp-list;match();else error;endcase(other: match(other); else error;endcase;end statement4、7aGrammar: A f (A) / &First(A)=(, £ Follow(A)=$,)4、7bSee theorem on P 178 in the text book1. First( n First
4、£ =2. & Fist(A), First(A)n Follow(A)=both conditions of the theorem are satisfied, hence grammar is LL(1)4、9 Consider the following grammar: lexpfatom|list atomfnumber|identifier listf(lexp-seq)lexp-seqflexp, lexp-seq|lexpa、Left factor this grammar 、b、 Con struct First and Follow sets for t
5、he non terminals of the resulti ng grammar c Show that the resulting grammar is LL(1)d、Con struct the LL(1) parsi ng table for the resulti ng grammar 、e、 Show the acti ons of the corresp onding LL(1) parser, give n the in put string (a,(b,(2),(c)Solutiona、lexpfatom|list atomfnumber|identifier listf(
6、lexp-seq)lexp-seqflexp lexp- seq 'lexp-seq ; lexpseq| &b、First(lexp)=number, identifier, (First(atom)=number, identifierFirst(list)=(First(lexp-seq)= number, identifier, (First(lexp- seq ')=,&Follow(lexp)=, $,Follow(atom)= ,$,Follow(list)= ,$,Follow(lexp-seq)=$,Follow(lexp- seq')
7、=$,c According to the defination of LL(1) grammar (Page 155), the resulting grammar is LL(1) as each tableentry has at most one production as shown in (d)、d The LL(1) pars ing table for the result ing grammarMN,Tnu mberide ntifer()$Lexplexp T atomlexp T atomlexp T listAtomatom T numberatom T identif
8、ierListlistT(lexp-seq)Lexp-seqlexp-seq T lexplexp- seq,lexp-seq T lexplexp- seq,lexp-seq T lexplexp seqrLexp-seq 'lexp- seqrt£lexp-seq 'T,lexp-seqlexp- seq' T£e The actions of the parser give n the stri ng (a,(b,(2),(c)Parsing stackIn put stri ngActio n$ lexp-seq(a,(b,),(c)$ 1l
9、exp-seqTlexp lexp- seq*$ lexp-seq r lexp(a,(b,),(c)$lexp T list$ lexp-seq ' list(a,(b,),(c)$list T (lexp-seq)$ lexp-seq ' ) lexpeq (a,(b,),(c)$ 1match$ lexp-seq 1 ) lexpeqa,(b,),(c)$lexp-seqTlexp lexp seq'$ lexp-seq r ) lexpeq 1 lexpa,(b,(2),(c)$lexp T atom$ lexp-seq ' ) lexpeq 1 ato
10、ma,(b,(2),(c)$ :atom T identifier$ lexp-seq ' ) lexpeq 1 identifiera,(b,(2),(c)$match$ lexp-seq r )xp-seq 1,(b,(2),(c)$lexp- seq T, lexp-seq$ lexp-seq 1 ) lexpeq ,(b,),(c)$match$ lexp-seq ' ) lexpeq(b,),(c)$lexp-seqTlexp lexp- seq*$ lexp-seq r ) lexpeq 1 lexp(b,),(c)$lexp T list$ lexp-seq r
11、) lexpeq ' list(b,),(c)$list T (lexp-seq)$ lexp-seq ' ) lexpeq 1 )lexseq(b,(2),(c)$match$ lexp-seq ' ) lexpeq 1 )lexseqb,),(c)$lexp-seqTlexp lexp- seq'$ lexp-seq 1 ) lexpeq 1 )lexpeq 1 lexpb,(2),(c)$lexp T atom$ lexp-seq ' ) lexpeq 1 )lexpeq 1 atomb,(2),(c)$atom T identifier$ lex
12、p-seq ' ) lexpeq 1 )lexpeq 1 identifierb,(2),(c)$match$ lexp-seq r ) lexpeq 1 )lexpeq 1,),(c)$lexp- seq T, lexp-seq$ lexp-seq ' ) lexpeq 1 )lexseq,,),(c)$match$ lexp-seq ' ) lexpeq 1 )lexseq(2),(c)$lexp-seqTlexp lexp- seq*$ lexp-seq ' ) lexpeq 1 )lexpeq 1 lexp(2),(c)$lexp T list$ lex
13、p-seq ' ) lexpeq 1 )lexpeq 1 list(2),(c)$list T (lexp-seq)$ lexp-seq ' ) lexpeq 1 )lexpeq 1 )lexpeq(2),(c)$match$ lexp-seq ' ) lexpeq 1 )lexpeq 1 )lexpeq2),(c)$lexp-seqTlexp lexp seq'$ lexp-seq ' ) lexpeq 1 )lexpeq 1 )lexpeq 1 lexp2),(c)$lexp T atom$ lexp-seq ' ) lexpeq 1 )le
14、xpeq 1 )lexpeq 1 atom2),(c)$atom T number$ lexp-seq ' ) lexpeq 1 )lexpeq 1 )lexpeq 1 number2),(c)$match$ lexp-seq ' ) lexpeq 1 )lexseq r )lexpeq 1),(c)$lexp- seq Tx$ lexp-seq ' ) lexpeq 1 )lexpeq 1),(c)$match$ lexp-seq ' ) lexpeq 1 )lexpeq 1),(c)$lexp- seq Tz$ lexp-seq ' ) lexpeq
15、 1),(c)$match$ lexp-seq ' ) lexpeq 1,(c)$lexp- seq T, lexp-seq$ lexp-seq ' ) lexpeq,(c)$match$ lexp-seq ' ) lexpeq(c)$lexp-seqTlexp lexp- seqr$ lexp-seq r ) lexpeq 1 lexp(c)$lexp T list$ lexp-seq r ) lexpeq ' list(c)$list T (lexp-seq)$ lexp-seq ' ) lexpeq 1 )lexseq(c)$match$ lexp
16、-seq ' ) lexpeq 1 )lexseqc)$lexp-seqTlexp lexp- seqr$ lexp-seq ' ) lexpeq 1 )lexpeq 1 lexpc)$:lexp T atom$ lexp-seq ' ) lexpeq 1 )lexpeq 1 atomc)$atom T identifier$ lexp-seq ' ) lexpeq * )lexpeq 1 identifierc)$match$ lexp-seq ' ) lexpeq 1 )lexpeq 1)$:lexp- seq Tz$ lexp-seq1 ) lex
17、-seq 1)$match$ lexp-seq ' ) lexpeq 1)$lexp- seq Tx$ lexp-seq f)$match$ lexp-seq r$lexp- seq Tt$accept4、10 aLeft factored grammar:1、decl T type var-list2、type T int3、type T float4、var-list T identifier B5、B T, var-list6、B T e4、10b/ irsl(L/ecl 7) = : i 1 it)/ trsl(type) /'A(intjo /、帀侄ij(float)
18、 Jim. lloul ;First (var一 list) = F>$t(iderM i Iler) = pdenti tier卜 irsf(出)一尸 A7r,v/一占;I iiofdecl:FolloulypeJ = Firi/fvar- list)二ideriljllcr!Folk ywr list) = Foliw(fi/ecl) = J$iFnllo vABA = / x ; ir /Av =月【JAfr.y/(int) C / float )=0戶 *imf (rCZrJT 伝)=Q2. e e A irsf(/iFfW(甘)c FO/OH (B) =0Boili condi
19、tions of the theorem are istisf沱d n grammar is IJ.( I)_4 10dMN,Tintfloatide ntifierJ$decl11type23var-list4B564、10eSample in put stri ng: int x, y, zPars ing stackIn putActio n$ declint x, y, z $decl T type var-list$ var-list typeint x, y, z $type T int$ var-list intint x, y, z $match int$ var-listX,
20、 y, z $var-list T identifer B$ B ide ntifierx, y, z $match ide ntifier w/ x$B,y, z$B T, var-list$ var-list,y, z$match,$ var-listy, z$var-list T identifer B$ B ide ntifiery, z$match ide ntifier w/ y$B,z$B T, var-list$ var-list,z$match ,$ var-listz$var-list T identifer B$ B ide ntifierz$match ide ntif
21、ier w/ z$B$B“ £$Accept4、12a、Can an LL(1) grammar be ambigous? Why or Why not?b、Can an ambigous grammar be LL(1)? Why or Why not?c、Must an un ambigous grammar be LL(1)? Why or Why not?Solutio nDefin ati on of an ambiguous grammar: A grammar that gen erates a stri ng with two disti net parse tree
22、s (Page 116)Defination of an LL(1) grammar: A grammar is an LL(1) grammar if the associatied LL(1) parsing table has at most one priduction in each table entry a. An LL(1) grammar can not be ambiguous, since the defination implies that an unambiguous parse can becon structed using the LL(1) pars ing tableb. An ambiguous grammar can not be LL(1) grammar, but can be con vert to be ambiguous by using disambiguating rule c. An unambiguous grammar
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 讓課堂充滿生機(jī)與活力
- 2025年槍托項(xiàng)目可行性研究報(bào)告
- 2025年度航空航天裝備研發(fā)合作合同
- 信用社終止貸款合同范本
- 儲(chǔ)值合同范本
- 保時(shí)捷買賣合同范本
- 公司對(duì)個(gè)人轉(zhuǎn)讓合同范例
- 優(yōu)信網(wǎng)出租車合同范例
- 交通管制合同范本
- 企業(yè)公司聘用合同范本
- 高職應(yīng)用語(yǔ)文教程(第二版)教案 3管晏列傳
- 高中物理《光電效應(yīng)》
- 烹飪實(shí)訓(xùn)室安全隱患分析報(bào)告
- 《金屬加工的基礎(chǔ)》課件
- 運(yùn)輸行業(yè)春節(jié)安全生產(chǎn)培訓(xùn) 文明駕駛保平安
- 體驗(yàn)式沙盤-收獲季節(jié)
- 找人辦事協(xié)議
- 老年護(hù)理陪護(hù)培訓(xùn)課件
- 醬香型白酒工廠設(shè)計(jì)
- 第3章 環(huán)境感知技術(shù)
- 牽引管道孔壁與管道外壁之間注漿技術(shù)方案
評(píng)論
0/150
提交評(píng)論