版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
編譯原理與實踐第四章答案編譯原理與實踐第四章答案編譯原理與實踐第四章答案資料僅供參考文件編號:2022年4月編譯原理與實踐第四章答案版本號:A修改號:1頁次:1.0審核:批準(zhǔn):發(fā)布日期:TheexercisesofChapterFourGrammar:A→(A)A|εAssumewehavelookaheadofonetokenasintheexampleonp.144inthetextbook.ProcedureA()if(LookAhead()∈{‘(‘})thenCallExpect(‘(‘)CallA()CallExpect(‘)’)CallA()elseif(LookAhead()∈{‘)‘,$})thenreturn()else/*error*/fifiendGiventhegrammar statement→assign-stmt|call-stmt|other assign-stmt→identifier:=exp call-stmt→identifier(exp-list)[Solution] First,convertthegrammarintofollowingforms: statement→identifier:=exp|identifier(exp-list)|other Then,thepseudocodetoparsethisgrammar: Procedurestatement Begin Casetokenof (identifer:match(identifer); casetokenof (:=:match(:=); exp; ((:match((); exp-list; match()); elseerror; endcase (other:match(other); elseerror; endcase; endstatementaGrammar:A→(A)A|εFirst(A)={(,ε}Follow(A)={$,)}bSeetheoremoninthetextbookFirst{(}∩First{ε}=Φε∈Fist(A),First(A)∩Follow(A)=Φbothconditionsofthetheoremaresatisfied,hencegrammarisLL(1)Considerthefollowinggrammar: lexp→atom|list atom →number|identifier list→(lexp-seq) lexp-seq→lexp,lexp-seq|lexpa.Leftfactorthisgrammar.b.ConstructFirstandFollowsetsforthenonterminalsoftheresultinggrammar.c.ShowthattheresultinggrammarisLL(1).d.ConstructtheLL(1)parsingtablefortheresultinggrammar.e.ShowtheactionsofthecorrespondingLL(1)parser,giventheinputstring(a,(b,(2)),(c)).[Solution]a. lexp→atom|list atom →number|identifier list→(lexp-seq) lexp-seq→lexplexp-seq’ lexp-seq’→,lexp-seq|εb. First(lexp)={number,identifier,(} First(atom)={number,identifier} First(list)={(} First(lexp-seq)={number,identifier,(} First(lexp-seq’)={,,ε} Follow(lexp)={,$,}} Follow(atom)={,$,}} Follow(list)={,$,}} Follow(lexp-seq)={$,}} Follow(lexp-seq’)={$,}}c.AccordingtothedefinationofLL(1)grammar(Page155),theresultinggrammarisLL(1)aseachtableentryhasatmostoneproductionasshownin(d).d.TheLL(1)parsingtablefortheresultinggrammarM[N,T]numberidentifer(),$Lexplexp→atomlexp→atomlexp→listAtomatom →numberatom →identifierListlist→(lexp-seq)Lexp-seqlexp-seq→lexplexp-seq’lexp-seq→lexplexp-seq’lexp-seq→lexplexp-seq’Lexp-seq’lexp-seq’→εlexp-seq’→,lexp-seqlexp-seq’→εe.Theactionsoftheparsergiventhestring(a,(b,(2)),(c))ParsingstackInputstringAction$lexp-seq(a,(b,(2)),(c))$lexp-seq→lexplexp-seq’$lexp-seq’lexp(a,(b,(2)),(c))$lexp→list$lexp-seq’list(a,(b,(2)),(c))$list→(lexp-seq)$lexp-seq’)lexp-seq((a,(b,(2)),(c))$match$lexp-seq’)lexp-seqa,(b,(2)),(c))$lexp-seq→lexplexp-seq’$lexp-seq’)lexp-seq’lexpa,(b,(2)),(c))$lexp→atom$lexp-seq’)lexp-seq’atoma,(b,(2)),(c))$atom →identifier$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’)lexp-seq(b,(2)),(c))$lexp-seq→lexplexp-seq’$lexp-seq’)lexp-seq’lexp(b,(2)),(c))$lexp→list$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-seq→lexplexp-seq’$lexp-seq’)lexp-seq’)lexp-seq’lexpb,(2)),(c))$lexp→atom$lexp-seq’)lexp-seq’)lexp-seq’atomb,(2)),(c))$atom →identifier$lexp-seq’)lexp-seq’)lexp-seq’identifierb,(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-seq→lexplexp-seq’$lexp-seq’)lexp-seq’)lexp-seq’lexp(2)),(c))$lexp→list$lexp-seq’)lexp-seq’)lexp-seq’list(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-seq→lexplexp-seq’$lexp-seq’)lexp-seq’)lexp-seq’)lexp-seq’lexp2)),(c))$lexp→atom$lexp-seq’)lexp-seq’)lexp-seq’)lexp-seq’atom2)),(c))$atom →number$lexp-seq’)lexp-seq’)lexp-seq’)lexp-seq’number2)),(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’)lexp-seq,,(c))$match$lexp-seq’)lexp-seq(c))$lexp-seq→lexplexp-seq’$lexp-seq’)lexp-seq’lexp(c))$lexp→list$lexp-seq’)lexp-seq’list(c))$list→(lexp-seq)$lexp-seq’)lexp-seq’)lexp-seq((c))$match$lexp-seq’)lexp-seq’)lexp-seqc))$lexp-seq→lexplexp-seq’$lexp-seq’)lexp-seq’)lexp-seq’lexpc))$lexp→atom$lexp-seq’)lexp-seq’)lexp-seq’atomc))$atom →identifier$lexp-seq’)lexp-seq’)lexp-seq’identifierc))$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’→ε$$acceptaLeftfactoredgrammar:1.decl→typevar-list2.type→int3.type→float4.var-list→identifierB5.B→,var-list6.B→εbcdM[N,T]intfloatidentifier,$decl11type23var-list4B56eSampleinputstring:intx,y,zParsingstackInputAction$declintx,y,z$decl→typevar-list$var-listtypeintx,y,z$type→int$var-listintintx,y,z$matchint$var-listx,y,z$var-list→identiferB$Bidentifierx,y,z$matchidentifierw/x$B,y,z$B→,var-list$var-list,,y,z$match,$var-listy,z$var-list→identiferB$Bidentifiery,z$matchidentifierw/y$B,z$B→,var-list$var-list,,z$match,$var-listz$var-list→identiferB$Bidentifierz$matchidentifierw/z$B$B→ε$$Accepta.CananLL(1)grammarbeambigousWhyorWhynotb.CananambigousgrammarbeLL(1)WhyorWhynotc.MustanunambigousgrammarbeLL(1)WhyorWhynot[Solution] Definationofanambiguousgrammar:Agrammarthatgeneratesastringwithtwodistinctparsetrees.(Page116) DefinationofanLL(1)grammar:AgrammarisanLL(1)grammariftheassociatiedLL(1)parsingtablehasatmostonepriductionineachtableentry.AnLL(1)grammarcannotbeambiguous,sincethedefinationimpliesthatanunambiguousparsecanbeconstructedusingtheLL(1)parsingtableAnambiguousgrammarcannotbeLL(1)grammar,butcanbeconverttobeambiguousbyusingdisambiguatingrule.Anunambiguousgrammarm
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年廚房設(shè)計服務(wù)合同
- 2025年江蘇省啟東市委組織部引進(jìn)國企專業(yè)化人才備考題庫及答案詳解一套
- 2025年中國兵器工業(yè)集團(tuán)航空彈藥研究院有限公司公開招聘安全總監(jiān)備考題庫及一套參考答案詳解
- 2025年云南大學(xué)附屬中學(xué)星耀學(xué)校招聘備考題庫及一套答案詳解
- 云南昭通檢察系統(tǒng)招聘考試真題2024
- 2025年新疆兵團(tuán)第九師白楊市公安局面向社會招錄警務(wù)輔助人員30人備考題庫及1套參考答案詳解
- 2025年福清市人民法院關(guān)于公開招聘勞務(wù)派遣人員的備考題庫及答案詳解1套
- 2025年廈門銀行南平分行招聘備考題庫及一套答案詳解
- 2025廣東佛山市順德區(qū)杏壇中心小學(xué)后勤服務(wù)人員招聘1人考試核心題庫及答案解析
- 2025青海西寧市藝術(shù)實驗中學(xué)招聘2人備考核心試題附答案解析
- 學(xué)堂在線 雨課堂 學(xué)堂云 藝術(shù)的啟示 期末考試答案
- 共創(chuàng)賬號合同協(xié)議
- 2026年贛州職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫帶答案詳解
- 2025年區(qū)域經(jīng)濟(jì)一體化戰(zhàn)略可行性研究報告
- 港口安全規(guī)劃與應(yīng)急預(yù)案
- 2025廣東廣州琶洲街道招聘雇員(協(xié)管員)5人筆試考試參考題庫及答案解析
- 山東師范大學(xué)《基礎(chǔ)大學(xué)英語》2025-2026學(xué)年第一學(xué)期期末試卷
- 2026年江西萍實鐵路發(fā)展股份有限公司校園招聘4人筆試考試參考試題及答案解析
- 2025專精特新小巨人打分表(密件)
- 國家自然科學(xué)基金申報培訓(xùn)
- MOOC 光學(xué)發(fā)展與人類文明-華南師范大學(xué) 中國大學(xué)慕課答案
評論
0/150
提交評論