资源描述:
《编译原理2E全套配套课件龙书 Chapter_three(2).ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、COMPILERCONSTRUCTIONPrinciplesandPracticeKennethC.Louden3.Context-FreeGrammarsandParsingPARTTWOContentsPARTONE3.1TheParsingProcess3.2Context-FreeGrammars3.3ParseTreesandAbstract3.4AmbiguityPARTTWO3.5ExtendedNotations:EBNFandSyntaxDiagrams[More]3.6Formal
2、PropertiesofContext-FreeLanguages[More]3.7SyntaxoftheTINYLanguage[More]3.5ExtendedNotations:EBNFandSyntaxDiagrams3.5.1EBNFNotationSpecialNotationsforRepetitiveConstructsRepetitionAA
3、(leftrecursive),andAA
4、(rightrecursive)whereandarearbitrarystrin
5、gsofterminalsandnon-terminals,andInthefirstruledoesnotbeginwithAandIntheseconddoesnotendwithASpecialNotationsforRepetitiveConstructsNotationforrepetitionasregularexpressionsuse,theasterisk*.A*,andA*EBNFoptstousecurlybrackets{...}toexpressrepetit
6、ionA{},andA{}Theproblemwithanyrepetitionnotationisthatitobscureshowtheparsetreeistobeconstructed,but,aswehaveseen,weoftendonotcare.ExamplesExample:ThecaseofstatementsequencesThegrammarasfollows,inrightrecursiveform:stmt-Sequencestmt;stmt-Sequence
7、
8、stmtstmtsInEBNFthiswouldappearasstmt-sequence{stmt;}stmt(rightrecursiveform)stmt-sequencestmt{;stmt}(leftrecursiveform)ExamplesAmoresignificantproblemoccurswhentheassociativitymattersexpexpaddopterm
9、termexpterm{addopterm}(implyleftassociativity)ex
10、p{termaddop}term(implyrightassociativity)SpecialNotationsforOptionalConstructsOptionalconstructareindicatedbysurroundingthemwithsquarebrackets[...].Thegrammarrulesforif-statementswithoptionalelse-partswouldbewrittenasfollowsinEBNF:statementif-stmt
11、oth
12、erif-stmtif(exp)statement[elsestatement]exp0
13、1stmt-sequencestmt;stmt-sequence
14、stmtiswrittenasstmt-sequencestmt[;stmt-sequence]3.5.2SyntaxDiagramsSyntaxDiagramsSyntaxDiagrams:GraphicalrepresentationsforvisuallyrepresentingEBNFrules.Anexample:consider
15、thegrammarrulefactor(exp)
16、numberThesyntaxdiagram:factornumber()expSyntaxDiagramsBoxesrepresentingterminalsandnon-terminals.Arrowedlinesrepresentingsequencingandchoices.Non-terminallabelsforeachdiagramrepresentingthegrammarrulede