




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、The exercises of Chapter Four4.2 Grammar: A ( A ) A | Assume we have lookahead of one token as in the example on p. 144 in the text book. Procedure A() if (LookAhead() () then Call Expect() Call A() Call Expect () Call A() else if (LookAhead() ), $) then return() else /* error */ fi fi end 4.3 Given
2、 the grammar statement assign-stmt|call-stmt|otherassign-stmtidentifier:=expcall-stmtidentifier(exp-list)SolutionFirst, convert the grammar into following forms:statement identifier:=exp | identifier(exp-list)|otherThen, the pseudocode to parse this grammar:Procedure statementBeginCase token of( ide
3、ntifer : match(identifer);case token of( := : match(:=); exp;( (: match(); exp-list;match();else error;endcase(other: match(other);else error;endcase;end statement4.7 a Grammar: A ( A ) A | First(A)=(, Follow(A)=$,)4.7 bSee theorem on P.178 in the text book1. First(First=2. Fist(A), First(A) Follow(
4、A)= both conditions of the theorem are satisfied, hence grammar is LL(1)4.9 Consider the following grammar:lexpatom|listatomnumber|identifierlist(lexp-seq)lexp-seqlexp, lexp-seq|lexpa. Left factor this grammar.b. Construct First and Follow sets for the nonterminals of the resulting grammar.c. Show t
5、hat the resulting grammar is LL(1).d. Construct the LL(1) parsing table for the resulting grammar.e. Show the actions of the corresponding LL(1) parser, given the input string (a,(b,(2),(c).Solutiona.lexpatom|listatomnumber|identifierlist(lexp-seq)lexp-seqlexp lexp-seqlexp-seq, lexp-seq|b.First(lexp
6、)=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)=$, c. According to the defination of LL(1) grammar (Page 155), the resulting gra
7、mmar is LL(1) as each table entry has at most one production as shown in (d).d. The LL(1) parsing table for the resulting grammarMN,Tnumberidentifer(),$LexplexpatomlexpatomlexplistAtomatomnumberatomidentifierListlist(lexp-seq)Lexp-seqlexp-seqlexp lexp-seqlexp-seqlexp lexp-seqlexp-seqlexp lexp-seqLex
8、p-seqlexp-seqlexp-seq, lexp-seqlexp-seqe. The actions of the parser given the string (a,(b,(2),(c)Parsing stackInput stringAction$ lexp-seq(a,(b,(2),(c)$lexp-seqlexp lexp-seq$ lexp-seq lexp(a,(b,(2),(c)$lexplist$ lexp-seq list(a,(b,(2),(c)$list(lexp-seq)$ lexp-seq ) lexp-seq (a,(b,(2),(c)$match$ lex
9、p-seq ) lexp-seqa,(b,(2),(c)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seq lexpa,(b,(2),(c)$lexpatom$ lexp-seq ) lexp-seq atoma,(b,(2),(c)$atomidentifier$ lexp-seq ) lexp-seq identifiera,(b,(2),(c)$match$ lexp-seq ) lexp-seq,(b,(2),(c)$lexp-seq, lexp-seq$ lexp-seq ) lexp-seq ,(b,(2),(c)$match$ lexp-seq
10、 ) lexp-seq(b,(2),(c)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seq lexp(b,(2),(c)$lexplist$ lexp-seq ) lexp-seq list(b,(2),(c)$list(lexp-seq)$ lexp-seq ) lexp-seq)lexp-seq(b,(2),(c)$match$ lexp-seq ) lexp-seq)lexp-seqb,(2),(c)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seq)lexp-seqlexpb,(2),(c)$lexpatom$
11、lexp-seq ) lexp-seq)lexp-seqatomb,(2),(c)$atomidentifier$ lexp-seq ) lexp-seq)lexp-seqidentifierb,(2),(c)$match$ lexp-seq ) lexp-seq)lexp-seq,(2),(c)$lexp-seq, lexp-seq$ lexp-seq ) lexp-seq)lexp-seq,(2),(c)$match$ lexp-seq ) lexp-seq)lexp-seq(2),(c)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seq)lexp-se
12、qlexp(2),(c)$lexplist$ lexp-seq ) lexp-seq)lexp-seqlist(2),(c)$list(lexp-seq)$ lexp-seq ) lexp-seq)lexp-seq)lexp-seq(2),(c)$match$ lexp-seq ) lexp-seq)lexp-seq)lexp-seq2),(c)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seq)lexp-seq)lexp-seqlexp2),(c)$lexpatom$ lexp-seq ) lexp-seq)lexp-seq)lexp-seqatom2),
13、(c)$atomnumber$ lexp-seq ) lexp-seq)lexp-seq)lexp-seqnumber2),(c)$match$ lexp-seq ) lexp-seq)lexp-seq)lexp-seq),(c)$lexp-seq$ lexp-seq ) lexp-seq)lexp-seq),(c)$match$ lexp-seq ) lexp-seq)lexp-seq),(c)$lexp-seq$ lexp-seq ) lexp-seq),(c)$match$ lexp-seq ) lexp-seq,(c)$lexp-seq, lexp-seq$ lexp-seq ) le
14、xp-seq,(c)$match$ lexp-seq ) lexp-seq(c)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seqlexp(c)$lexplist$ lexp-seq ) lexp-seqlist(c)$list(lexp-seq)$ lexp-seq ) lexp-seq)lexp-seq(c)$match$ lexp-seq ) lexp-seq)lexp-seqc)$lexp-seqlexp lexp-seq$ lexp-seq ) lexp-seq)lexp-seqlexpc)$lexpatom$ lexp-seq ) lexp-se
15、q)lexp-seqatomc)$atomidentifier$ lexp-seq ) lexp-seq)lexp-seqidentifierc)$match$ lexp-seq ) lexp-seq)lexp-seq)$lexp-seq$ lexp-seq ) lexp-seq)$match$ lexp-seq ) lexp-seq)$lexp-seq$ lexp-seq ) )$match$ lexp-seq$lexp-seq$accept4.10 aLeft factored grammar: 1. decl type var-list 2. type int 3. type float
16、 4. var-list identifier B 5. B , var-list 6. B 4.10 b4.10 c4.10 dMN, T int float identifier ,$ decl 1 1 type 2 3 var-list 4 B 5 64.10 eSample input string: int x, y, z Parsing stack Input Action $ decl int x, y, z $ decl type var-list $ var-list type int x, y, z $ type int $ var-list int int x, y, z
17、 $ match int $ var-list x, y, z $ var-list identifer B $ B identifier x, y, z $ match identifier w/ x $ B , y, z $ B , var-list $ var-list , , y, z $ match , $ var-list y, z $ var-list identifer B $ B identifier y, z $ match identifier w/ y $ B , z $ B , var-list $ var-list , , z $ match , $ var-lis
18、t z $ var-list identifer B $ B identifier z $ match identifier w/ z $ B $ B $ $ Accept 4.12 a. 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 unambigous grammar be LL(1)? Why or Why not?SolutionDefination of an ambiguous grammar: A gra
19、mmar that generates a string with two distinct parse trees. (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 be constructed using the LL(1) parsing tableb. An ambiguous grammar can not be LL(1) grammar, but can be convert to be ambiguous by using disambiguating rule.c. An unambiguous grammar may be not an LL(1) grammar, since some ambiuous grammar can be parsed using LL(K) table, where
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 羊奶教學(xué)課件
- 教育法視頻課件下載
- 人稱代詞教學(xué)課件
- 題型2 圖片表述型計(jì)算-備戰(zhàn)2020年中考化學(xué)計(jì)算題型特訓(xùn)(原卷版)
- 教學(xué)課件設(shè)計(jì)評(píng)價(jià)
- 新年公司套錢活動(dòng)方案
- 日產(chǎn)促銷活動(dòng)方案
- 春晚幼兒線上活動(dòng)方案
- 新年插花活動(dòng)方案
- 明一鮮奶開業(yè)活動(dòng)方案
- 我的妹妹高中 我的妹妹初中800字(三篇)
- 昆明天大礦業(yè)有限公司尋甸縣金源磷礦老廠箐-小凹子礦段(擬設(shè))采礦權(quán)出讓收益評(píng)估報(bào)告
- -衛(wèi)生資格-副高-內(nèi)科護(hù)理-副高-章節(jié)練習(xí)-護(hù)理學(xué)總論-醫(yī)院感染護(hù)理(案例分析題)(共6題)
- 尿動(dòng)力學(xué)檢查操作指南2023版
- 夢(mèng)幻西游古龍服務(wù)端安裝教程
- 食品安全地方標(biāo)準(zhǔn) 預(yù)制菜生產(chǎn)衛(wèi)生規(guī)范
- 亮化工程竣工驗(yàn)收?qǐng)?bào)告
- 《出生醫(yī)學(xué)證明》單親母親情況聲明
- PCS-915母差保護(hù)裝置介紹
- 第一套路面工程考試試題及答案
- GB∕T 22590-2021 軋鋼加熱爐用耐火澆注料
評(píng)論
0/150
提交評(píng)論