Chapter 4: Syntax Analysis Part 2: Top-Down ParsingMotivationBasic intuitionLookaheadPredictive ParsingExampleKey IdeaMatchingNo Matching?Dealing with non-terminalsOn the ExampleGain?Final outcomeNegative outcomeOverall Top-Down AlgorithmAre We Done ?The Lookahead WindowLookahead Size and LanguagesTop-Down ParsingSlide 20Recursive Descent Parsing ConceptsPredictive Parsing : RecursiveTransition Diagrams (TDs)How are Transition Diagrams Used ?How can Transition Diagrams be Simplified ?How can Transition Diagrams be Simplified ? (2)How can Transition Diagrams be Simplified ? (3)How can Transition Diagrams be Simplified ? (4)How can Transition Diagrams be Simplified ? (5)How can Transition Diagrams be Simplified ? (6)Additional Transition Diagram SimplificationsMotivating Table-Driven ParsingNon-Recursive / Table DrivenAlgorithm for Non-Recursive ParsingSlide 35Trace of ExampleLeftmost Derivation for the ExampleWhat’s the Missing Puzzle Piece ?Computing First(X) : All Grammar SymbolsComputing First(X) : All Grammar Symbols - continuedConceptually: What is First (E, T, …) in Derivation?Slide 42Example 2Computing Follow(A) : All Non-TerminalsConceptually: What is Follow in Derivation?Slide 46Computing Follow : 2nd ExampleFirst & Follow – One More LookSlide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Motivation Behind First & FollowConstructing Parsing TableConstructing Parsing Table - ExampleConstructing Parsing Table – Example 2Slide 67Slide 68Parsing Process Over TimeSlide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Slide 80Slide 81Slide 82Slide 83Slide 84Slide 85Slide 86Slide 87Slide 88LL(1) GrammarsError RecoveryPanic Mode RecoveryRevised Parsing Table / ExampleSkip & SynchRevised Parsing Table / Example(2)Phase-Level RecoveryHow Would You Implement TD ParserRevised Parsing Table:Revised Parsing Table: (2)How is Parser Constructed ?Final Comments – Top-Down ParsingCH4p2.1CSE4100Chapter 4: Syntax AnalysisChapter 4: Syntax Analysis Part 2: Top-Down Parsing Part 2: Top-Down ParsingProf. Steven A. DemurjianComputer Science & Engineering DepartmentThe University of Connecticut371 Fairfield Way, Unit 2155Storrs, CT [email protected]://www.engr.uconn.edu/~steve(860) 486 - 4818Material for course thanks to:Laurent MichelAggelos KiayiasRobert LeBarreCH4p2.2CSE4100MotivationMotivationSourceSourceWe have a grammarProductProductWe want a parsing algorithmIdeaIdeaSynthesize the algorithm from the grammarProblemProblemHow are we going to use the grammar ???Hint...Hint...We can look at the beginning of the input...CH4p2.3CSE4100Use a sliding window over the input streamUse a sliding window over the input streamBenefitBenefitReveal the input SlowlyA little bit at a time–1 token at a time–Maybe 2 at a time ?But systematically–Left to RightWhat kind of derivation can use tokens in this way?What kind of derivation can use tokens in this way?Basic intuitionBasic intuitionThe InputCH4p2.4CSE4100LookaheadLookaheadTechniqueTechniqueUse a window of lookaheadGuide a LEFTMOST derivation with the window contentSo...So...How to guide ?CH4p2.5CSE4100Predictive ParsingPredictive ParsingLookaheadLookaheadPredictive tool!Helps to select the right productionQuestionQuestionHow?Answered with an example...Answered with an example...CH4p2.6CSE4100ExampleExampleConsider the grammarConsider the grammarAnd the input fragmentAnd the input fragmentWhich production should we start with ?Which production should we start with ?Why?stmt → if expr then stmt else stmt| while expr do stmt| for ( expr ; expr ; expr) stmt| { stmtList } | lvalue = exprlvalue → idexpr → id | integerwhile x do { if x then ....CH4p2.7CSE4100Key IdeaKey IdeaUse the production bodyUse the production bodyTo know what can start a productionSelecting a productionSelecting a productionPick the rule that can start with the symbol in the lookahead window!Using the productionUsing the productionWhat should we do with chosen production? while expr do stmtProductionInputwhile x do { if x then ....MatchingMatchingCH4p2.8CSE4100MatchingMatchingWhen input matchesWhen input matchesEat the matched symbolsIn the productionIn the inputMove the window further downDeal with the rest of the productionHow?while expr do stmtProductionInputwhile x do { if x then ....CH4p2.9CSE4100No Matching?No Matching?What does it mean?What does it mean?Example belowExample belowIn production:NON-TERMINAL exprIn window:TERMINAL x [an identifier token]What should we do ?What should we do ?while expr do stmtProductionInputwhile x do { if x then ....CH4p2.10CSE4100Dealing with non-terminalsDealing with non-terminalsSimple...Simple...The non-terminal is defined somewhereThe non-terminal is defined by a set of productionsCorollaryCorollaryPop the non-terminalUse the lookahead to choose a production for NT.Push and RecurCH4p2.11CSE4100On the ExampleOn the ExampleRecall the grammar and the current stateRecall the grammar and the current statewhile expr do stmtProductionInputwhile x do { if x then ....stmt → if expr then stmt else stmt.... lvalue → idexpr → id | integer2. Choose production for expr result: expr → id3. Push & Recur...result:while id do stmtProductionInputwhile x do { if x then ....1. Pop result: exprCH4p2.12CSE4100Gain?Gain?RecallRecallThe topmost symbol ( id )The lookahead x [an instance of id]Gain?Gain?The top symbol and lookahead match!So....So....RecurRecursive call will match them and pop themKeep on goingCH4p2.13CSE4100Final outcomeFinal outcomeWhat can happen in the end ?What can happen in the end ?We “eat” (match) the entire inputMeaning ?We get “stuck” at some pointWhat does it mean (to be stuck)How can we get stuck ?CH4p2.14CSE4100Negative outcomeNegative outcomeWe can get stuck We can get stuck Lookahead window and topmost non-terminal yield...An empty prediction!This is the “classic” syntax errorExpecting xyz–A list of tokens predicting the productions of the current non-terminalGot abc–“abc” is the actual content of the lookahead windowSyntax error at file:line. Expecting xyz got abcCH4p2.15CSE4100Overall Top-Down AlgorithmOverall Top-Down
View Full Document