




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、總復習Target codeSource codeScannerParserSemantic analyzerSource code optimizer/Intermediate code generatorCode generatorTarget code optimizerTokensSyntax TreeAnnotated TreeIntermediate codeTarget codeIdentify logical pieces of the description.Identify how those pieces relate to each other.Identify the
2、 meaning of the overall structure.Design one possible structure and Simplify the intended structureFabricate the structureImprove the resulting structureLiteral tableSymbol tableError handlerAuxiliary components that interact with some phases2Lexical AnalysisLexical Analysis1.Define the tokens with
3、a Regular Expression2.Create the equivalent NFA3.Construct the equivalent DFA4.Minimizing the equivalent DFA4Example1.Please write a regular expression for all strings of as and bs which contain two consecutive as or bs.(a|b)*(aa|bb) (a|b)*5nConvert the regular express into NFA4f35621iaaaabbbb62.Con
4、vert the NFA into DFA by subset construction. The Transition table is required.i,1,2IaIb1,2,31,2,31,2,41,2,41, 2, 3, 5, 6, f1, 2, 3, 5, 6, f1, 2, 3, 5, 6, f1, 2, 3, 6, f1, 2, 3, 6, f1, 2, 4, 5, 6, f1, 2, 4, 6, f1, 2, 4, 5, 6, f1, 2, 4, 5, 6, f1, 2, 4, 6, fS1,2,3A1,2,4B1, 2, 3, 5, 6, fC1, 2, 4, 5, 6,
5、 fD1, 2, 4, 6, fE1, 2, 3, 6, fFACACFFCBBDEDDE4f35621iaaaabbbb7SbAaaCaaBbbDbEbbabFaFa83.Minimizing the Number of States in a DFAaCDBAEFSbaaaaabbbbbabF1. Split into accepting states sets and Nonaccepting states setsS,A,B C,D,E,F2. Continue to split a: S,A,B=S,B AC,D,E,F = C,D,E,F b: S,B A=S A BC,D,E,F
6、 = C,D,E,F 3. Let D represents C,D,E,F Minimized DFA P=S,A,B,DDBASaaabbbba9Context-Free Grammars and ParsingProf. CHEN JSouth China University of TechnologyContext-Free Grammars and ParsingnThe Parsing ProcessnContext-Free GrammarsnParse Trees and Abstract Syntax TreenAmbiguity1112Context-Free Gramm
7、arsnFunctionqA context-free grammar is a specification for the syntactic structure of a programming languageqIt is similar to regular expressions except that a context-free grammar involves recursive rules.nExamplecontext-free grammar for integer arithmetic expressionexpexp op exp | (exp) | numberop
8、+ | - | *Example of Parsing Processexp exp op exp | (exp) | numop + | - | *na derivation for (34-3)*42 is:(1) exp=exp op exp(expexp op exp)(2) =exp op num(expnum)(3) =exp * num(op*)(4) =(exp) * num(exp(exp)(5) =(exp op exp) * num(expexp op exp)(6) =(exp op num) * num (expnum)(7) =(exp-num) * num (op
9、-)(8) =(num-num) * num (expnum)13Example (leftmost)expexp op exp | (exp) | numop + | - | *vThe leftmost derivation of “num+num” is:exp=exp op exp=num op exp =num + exp=num+num1exp2exp4exp3opnumnum+1415Example (rightmost)expexp op exp | (exp) | numop + | - | *vThe rightmost derivation of “num+num” is
10、:exp=exp op exp= exp op num = exp + num =num+num1exp2exp4exp3opnumnum+1. Leftmost and rightmost derivation are unique for the string they construct2. They are uniquely associated with the parse treeAbstract Syntax TreesnThe need of abstract syntax treeqA parse tree contains much more information tha
11、n is absolutely necessary for a compiler to produce executable codenExample:expexpexpopnum(3)+num(4)+34Abstract syntax tree16ExamplenThe parse tree and abstract syntax tree for expression (34-3)*4217AmbiguitynIn general ,a string of tokens has one parse tree, which corresponds to more than one deriv
12、ations of the stringnthe derivation of “” and the parse treeEET+TFTiiiE=E+TE=E+T =E+TF =T+TF=T+T=T+TF=i+ii=i+ii18It is possible for some grammars to permit a string to have more than one parse tree (or leftmost/rightmost derivation). For Example:nInteger arithmetic grammar: nString “” has two differ
13、ent parse trees:Corresponding to two leftmost derivations:1:E E+E EE+E iE+E ii+E ii+i2:E EE iE iE+E ii+E ii+iiEE+EEEiiEEEiEE+ii1920AmbiguitynA grammar G is ambiguous if there exists a string w L(G) such that w has two distinct parse trees (or leftmost /rightmost derivations)Top-Down ParsingProf. CHE
14、N JSouth China University of TechnologyTop-Down Parsing1.Non LL(1) grammar to LL(1) grammarqleft factor qleft recursion 2.Recognition of LL(1) GrammarqFirst SetsqFollow Sets3.LL(1) Parsing22Step 1: Determine whether the grammar is LL(1)G:EE+TTTT*FFF(E)iExample: LL(1) Parsing qThe grammar has left re
15、cursion, so it is not LL(1) grammar. Consider left recursion removal.qAfter left recursion removal getting G: ETE E+TE TFT T*FTF(E)i23Computer First and Follow Sets for each nonterminal nullableFirstFollowEno(,i),$Eyes+| ),$Tno(,i+,),$Tyes*| +,),$Fno(|i*,+,),$G: ETE E+TE TFT T*FTF(E)iG is a LL(1) gr
16、ammarStep 2: Determine whether G is LL(1)24Step 3: Construct the parsing tablenullableFirstFollowEno(,i ), $ Eyes+| ), $ Tno(,i +, ), $ Tyes*| +, ), $Fno(|i *, + , ), $G: ETE E+TE TFT T*FT F(E)i i+*()$E E T T F TETE+TEFTFTi*FT(E)25stepstacktopinputremainerMX,b12345678910111213Parsing process of stri
17、ng $i+i$ i+*()$ETE TE E +TE TFT FT T *FT Fi (E) $E$ET$ETF$ETi$ET $E $ET+$ET $ETF$ETi$ET $E $ETFiT E +T FiT E $iiii+ +i ii$ $ $+i$+i$+i$+i$i$ i$i$ $ETETFTF iT E +TET FTF iE T Step 4: LL(1) parsing26Bottom-Up ParsingProf. CHEN JSouth China University of TechnologyExampleG: (0) SS(1) SrD(2) DD,i(3) Di1
18、. Construct the DFA of LR(0) items for this grammar2. Is this grammar the LR(0) or SLR(1) grammar ? Give your reason. If so, Construct the parsing table.3. Show the parsing stack and the action of the parser for the input string “r i,i”.28I0:S SS rDI1: S S SI2: S rDD D,iD irI4: D i iI3: S rD D D ,iD
19、I5: D D,i,I6: D D,i i1.292. 1.In state I3, SrD is a complete item, DD,i is a shift item, there is shift-reduce conflict, so G is not LR(0) grammar. 2.Eliminating Conflicts I0:S SS rDI1: S S SI2: S rDD D,iD irI4: D i iI3: S rD D D ,iDI5: D D,i,I6: D D,i iFOLLOWS$S$D$ ,For state I3nIf the next token i
20、s $, then reducenIf the next token is , , then shiftConflict can be solvedG: (0) SS(1) SrD(2) DD,i(3) Di303. Construction of SLR(1) Parse TableACTION GOTO r,i$SD0123456S21accS43S5r1r3r3S6r2r2FOLLOW$ ,G: (0) SS(1) SrD(2) DD,i(3) Di31Parsing “r i,i”ACTION GOTO r,i$SD0S2 1 1 acc2 S4 33S5 r14r3 r35 S6 6
21、 r2 r2StackInputACTIONGOTO12345678$0ri,i$S2$0r2i,i$S4$0r2i4,i$r3$0r2D3,i$S5$0r2D3,5i$S6$0r2D3,5i6r23$0r2D3r11$0S1$acc$3G: (0) SS(1) SrD(2) DD,i(3) Di321.Right Sentential Form2.Viable Prefix3.HandleParsing “abbcde”reduce AAbcde$aAb5shiftcde$aA6shiftbcde$aA4reduce Abbcde$ab3shiftbbcde$a2shiftabbcde$1A
22、ctionInputStackS aAcBe A b A Ab B d331.Right Sentential Form2.Viable Prefix3.HandleParsing “abbcde”reduce AAbcde$aAb5shiftcde$aA6shiftbcde$aA4reduce Abbcde$ab3shiftbbcde$a2shiftabbcde$1ActionInputStackS aAcBe A b A Ab B dare all viable prefix of aAcde341.Right Sentential Form2.Viable Prefix3.HandleP
23、arsing “abbcde”reduce AAbcde$aAb5shiftcde$aA6shiftbcde$aA4reduce Abbcde$ab3shiftbbcde$a2shiftabbcde$1ActionInputStackS aAcBe A b A Ab B dn“b” is the handle of “abbcde”n“Ab” is the handle of “aAbcde”35Semantic AnalysisProf. CHEN JSouth China University of Technology37Consider the following grammar fo
24、r simple integer arithmetic expressions:exp exp + term | exp-term | termterm term*factor | factorfactor (exp)| number The principal attribute of an exp (or term or factor) is its numeric value (write as val) and the attribute equations for the val attribute are given as followsGrammar RuleSemantic R
25、ulesexp1exp2+termexp1.val=exp2.val+term.valexp1exp2-termexp1.val=exp2.val-erm.valexp1 termexp1.val= term.valterm1term2*factorterm1.val=term2.val*factor.val termfactorterm.val=factor.val factor(exp)factor.val=exp.val factornumberfactor.val=number.valTable 6.2Given the expression (34-3)*42 , the compu
26、tations implied by this attribute grammar by attaching equations to nodes in a parse tree is as followsexp(val = 1302)term(val = 31*42=1302)term(val = 31)factor(val = 42)( exp ) (val = 34-3=31)exp(val = 34)term(val = 3)factor(val = 31)number(val = 42)term(val = 34)factor(val =3)factor(val = 34)number(val = 3)number(val = 34)-*Dependency graphs and evaluation order38Code GenerationProf. CHEN JSouth China University of Technology40Instructions of three-address codenThree-address code for each construction of a standard programming la
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆安徽省蚌埠局屬學校數(shù)學七下期末復習檢測試題含解析
- 貴州省黔東南州麻江縣2025年八年級數(shù)學第二學期期末經(jīng)典試題含解析
- 工業(yè)和信息化領域數(shù)據(jù)安全事件上報(模板)
- 2025屆浙江省江北區(qū)七校聯(lián)考七年級數(shù)學第二學期期末質(zhì)量檢測試題含解析
- 法律科學的分類及應用試題及答案
- 戰(zhàn)略性儲蓄的思維與方法計劃
- 江蘇省南京市南航附中2025屆八下數(shù)學期末學業(yè)水平測試模擬試題含解析
- 2025年市場需求分析與預測試題及答案
- 網(wǎng)絡管理員考試知識結構試題及答案細解
- 城市交通環(huán)境影響評價師重點基礎知識點
- 《意大利美食文化》課件
- 綠色中國智慧樹知到課后章節(jié)答案2023年下華東理工大學
- 《施之以愛報之以恩》的主題班會
- 茶葉食用農(nóng)產(chǎn)品承諾書(八篇)
- 組織行為學全套課件(羅賓斯版)
- 數(shù)據(jù)治理咨詢項目投標文件技術方案
- 單梁起重機安全操作培訓課件
- 動火證施工現(xiàn)場動火證申請書
- 安保安全隱患排查記錄表
- 2022年05月四川省涼山州國有工業(yè)投資發(fā)展集團有限責任公司專業(yè)技術人員及管理人員筆試題庫含答案解析
- 2023年全國測繪生產(chǎn)成本費用定額
評論
0/150
提交評論