




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 新疆機電職業(yè)技術學院《創(chuàng)業(yè)管理》2023-2024學年第二學期期末試卷
- 蘭州職業(yè)技術學院《道路景觀設計》2023-2024學年第一學期期末試卷
- 昆明冶金高等??茖W校《裝飾圖案基礎》2023-2024學年第二學期期末試卷
- 日照航海工程職業(yè)學院《首飾設計與制作》2023-2024學年第二學期期末試卷
- 西藏民族大學《醫(yī)學免疫學研究進展》2023-2024學年第二學期期末試卷
- 吉林電子信息職業(yè)技術學院《軟件設計開發(fā)綜合實訓》2023-2024學年第二學期期末試卷
- 銅仁職業(yè)技術學院《生物質廢棄物資源化利用》2023-2024學年第二學期期末試卷
- 上海杉達學院《細胞及分子生物學實驗》2023-2024學年第二學期期末試卷
- 江海職業(yè)技術學院《天然藥物化學》2023-2024學年第一學期期末試卷
- 延安職業(yè)技術學院《高頻電子電路》2023-2024學年第二學期期末試卷
- 外國建筑賞析:文藝復興建筑課件
- 2025年中國稀土集團招聘筆試參考題庫含答案解析
- 部編版五年級下冊詞句段運用練習
- photoshop圖形圖像處理-中國院子知到智慧樹章節(jié)測試課后答案2024年秋青島西海岸新區(qū)職業(yè)中等專業(yè)學校
- 道路勘測設計-平縱線形組合設83課件講解
- 設施農業(yè)課件
- 啟事(教學課件)-中職高考語文二輪應用文寫作專項突破
- 《DBJT45-T 047.2-2022旅游公路設計指南 第2部分:設計要求》
- 中國建筑校招二輪測試題庫
- 第46屆世界技能大賽河南省選拔賽-3D數(shù)字游戲藝術項目-樣題
- 《職場溝通技巧》(第三版)課件全套 陶莉 項目1-9 有效溝通基本功 - 有效溝通綜合實訓
評論
0/150
提交評論