




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国汁斗市场调查研究报告
- 2025年中国悬臂式尾板市场调查研究报告
- 2025年中国天然海带市场调查研究报告
- 2025年中国多功能全自动钢圈换顶机市场调查研究报告
- 2025年中国双位浪板市场调查研究报告
- 科技人才引进协议合同
- 保险数字化理赔服务与客户满意度提升报告2025
- 小劳保买卖合同协议
- 喷粉设备合同协议
- 私人建房监理合同协议
- 2025地质勘察合同范本
- 2025年时政政治试题库及答案
- 抗帕金森病试题及答案
- 2025-2030中国钢结构行业现状供需分析及市场深度研究发展前景及规划可行性分析研究报告
- 2025年河南省中考数学二轮复习压轴题:动态几何问题专练
- 《知识产权保护》课件
- 北京市东城区2024-2025学年度第二学期高三综合练习(一)(东城高三一模)【历史试卷+答案】
- 2025-2030中国制造运营管理(MOM)软件行业市场现状供需分析及投资评估规划分析研究报告
- 少尿与无尿的急诊处理
- 血管导管相关血流感染预防控制措施
- 非计划拔管的预防及处理
评论
0/150
提交评论