Presentation is loading. Please wait.

Presentation is loading. Please wait.

– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:

Similar presentations


Presentation on theme: "– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:"— Presentation transcript:

1 – 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings: 4.6 Homework: Test 1 – Feb 15 February 6, 2006 CSCE 531 Compiler Construction

2 – 2 – CSCE 531 Spring 2006 Overview Last Time First and Follow LL(1) property Today’s Lecture Panic mode error recovery in Predictive parsing Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsingHomework: LL(1) table for core (pdf email handout) grammar Test 1 Feb 15

3 – 3 – CSCE 531 Spring 2006 Panic Mode Error Recovery for LL(1)

4 – 4 – CSCE 531 Spring 2006 Recall Bottom-up Parsing Bottom-up parsers Start at the leaves and grow tree toward root As input is consumed, encode possibilities on a stack

5 – 5 – CSCE 531 Spring 2006 Bottom-up Parsing: Terminology A derivation consists of a series of rewrite steps S   0   1   2  …   n–1   n Each  i is a sentential form and if  n  T* ({ tokens}* ) then it is a sentence To get  i from  i–1, expand some A   i–1 by using A  i.e., Replace the occurrence of A   i–1 with  to get  i In a rightmost derivation, A would be the last nonterminal in  i–1

6 – 6 – CSCE 531 Spring 2006 Bottom-up Parsing In finding a rightmost derivation S   0   1 …   i–1   i …   n–1   n At each step we need to find a place where the right-hand side of a production occurs and replace it with the non- terminal on the LHS. Again we are building the parse tree from leaves to root As we are constructing it is not always connected As we are constructing it is not always connected Nodes with no parent in the partially constructed tree form its upper fringe Nodes with no parent in the partially constructed tree form its upper fringe Since each replacement of  with A shrinks the upper fringe, we call it a reduction. Since each replacement of  with A shrinks the upper fringe, we call it a reduction.

7 – 7 – CSCE 531 Spring 2006 Example of Indentifying Reductions E  E+E | E * E | ( E ) | id | num Rightmost derivation of x * (z + w) with token sequence id * ( id + id ) E  E * E  E * ( E )  E * ( E + E )  E * ( E + id )  E * (id+id)  id * ( id + id ) Frontier id * ( id + id )

8 – 8 – CSCE 531 Spring 2006 Finding Handles The parser must find a substring  in the tree’s frontier that matches some production A   that occurs as one step in the rightmost derivation We call this substring  a handle S  rm  1 …  rm  i–1 =αAw  rm α  w =  i  rm …  rm  n Because  i is a right-sentential form, the substring to the right of a handle (w) contains only tokens

9 – 9 – CSCE 531 Spring 2006 Figure 4.20 – a Tree with Handle A  β A β α = α 1 α 2 … α k S w = w 1 w 2 … w s …

10 – 10 – CSCE 531 Spring 2006 Grammar Unambiguous  Unique Handles Sketch of Proof: 1. G is unambiguous implies 2. rightmost derivation is unique, which implies 3. there is a unique production A   applied to derive  i from  i–1 4. and a unique position k at which A   is applied 5. thus the handle is unique This all follows from the definitions

11 – 11 – CSCE 531 Spring 2006 Shift-reduce Parsing A parser based on recognizing handles is called a Shift Reduce parsers A shift-reduce parser has just four actions Shift: the next token is pushed (shifted) onto the stack Shift: the next token is pushed (shifted) onto the stack Reduce: the right end of a handle is on the top of stack Reduce: the right end of a handle is on the top of stack Locate the left end of the handle within the stack Pop the handle off stack & push appropriate lhs Accept — stop parsing & report success Accept — stop parsing & report success Error — call an error handling/recovery routine Error — call an error handling/recovery routine

12 – 12 – CSCE 531 Spring 2006 Shift-reduce Parsing Accept - finish up Accept - finish up Error - error recovery, panic mode again Error - error recovery, panic mode again Shift Shift push the current token call to the scanner to get the next token Reducing by A  β requires Reducing by A  β requires Popping β from the stack Push A

13 – 13 – CSCE 531 Spring 2006 Stack Implementation of Shift-Reduce Parsers StackInputAction $ id * ( id + id ) $ Shift id Shift id $ id * ( id + id ) $ Reduce E  id Reduce E  id $ E * ( id + id ) $ Shift * Shift * $ E * ( id + id ) $ Shift ( Shift ( $ E * ( id + id ) $ id + id ) $ Shift id Shift id $ E * ( id + id ) $ Reduce E  id Reduce E  id $ E * ( E + id ) $ Shift + Shift + $ E * ( E + id ) $ id ) $ Shift id Shift id $ E * ( E + id ) $ Reduce E  id Reduce E  id $ E * ( E + E ) $ Reduce E  E+E Reduce E  E+E $ E * ( E ) $ Shift ) Shift ) $ E * ( E ) $ Reduce E  ( E ) $ E * E $ Reduce E  ( E )

14 – 14 – CSCE 531 Spring 2006 More Examples Now what about id * ( id + id ) + id id * ( id + id ) + id See fig 4.22 for another example See fig 4.22 for another example

15 – 15 – CSCE 531 Spring 2006 Justification of the Stack Implementation Consider two derivations  S  …  αAz  αβByz  …  αβμyz Rewriting by A  βBy  S  …  αBxAz  αBxyz  …  αμxyz Rewriting by A  y

16 – 16 – CSCE 531 Spring 2006 Justification of the Stack Implementation Consider the first derivation  S  …  αAz  αβByz  αβμyz Rewriting by A  βBy, and then B  μ StackInputActions $ α β μ yz$ Reduce by B  μ $ α β B yz$ Since B is the rightmost nonterminal in αβByz, the handle cannot be all in the stack Since B is the rightmost nonterminal in αβByz, the handle cannot be all in the stack So the parser can shift the string of tokens y onto the stack So the parser can shift the string of tokens y onto the stackStackInputActions $ α β B y z$ Reduce by A  βBy $ α A z$

17 – 17 – CSCE 531 Spring 2006 Justification of the Stack Implementation Consider the second derivation (2) S  …  αBxAz  αBxyz  αμxyz Rewriting by A  y and then B  μ StackInputActions $ α μ xyz$ Reduce by B  μ $ α B xyz$ Now the parser can shift the string of tokens xy onto the stack Now the parser can shift the string of tokens xy onto the stackStackInputActions $ α B x y z$ Reduce by A  y $ α B x A z$

18 – 18 – CSCE 531 Spring 2006 An Important Lesson about Handles To be a handle, a substring of a sentential form  must have two properties: It must match the right hand side  of some rule A   There must be some rightmost derivation from the goal symbol that produces the sentential form  with A   as the last production applied Simply looking for right hand sides that match strings is not good enough Critical Question: How can we know when we have found a handle without generating lots of different derivations? Answer: we use look ahead in the grammar along with tables produced as the result of analyzing the grammar. L R(1) parsers build a DFA that runs over the stack & finds them

19 – 19 – CSCE 531 Spring 2006 An Important Lesson about Handles To be a handle, a substring of a sentential form  must have two properties: It must match the right hand side  of some rule A   There must be some rightmost derivation from the goal symbol that produces the sentential form  with A   as the last production applied We have seen that simply looking for right hand sides that match strings is not good enough Critical Question: How can we know when we have found a handle without generating lots of different derivations? Answer: we use look ahead in the grammar along with tables produced as the result of analyzing the grammar. o There are a number of different ways to do this. o We will look at two: operator precedence and LR parsing

20 – 20 – CSCE 531 Spring 2006 Model of an LR Parser a1a1a1a1… aiaiaiai… an$an$an$an$ smsmsmsm XmXmXmXm s m-1 X m-1 … s0s0s0s0 Stack input output LR Parsing Program Parsing Table Action goto

21 – 21 – CSCE 531 Spring 2006 LR Parsing Algorithm

22 – 22 – CSCE 531 Spring 2006 Expression LR-Parsing Table fig 4.31 StateActiongoto Id+*()$ETF0S5S4123 1S6accept 2R2S7R2 3R4R4R4 4S5S4823 5R6R6R6 6S5S493 7S5S410 8S6S11 9R1S7R1R1 10R3R3R3R3 11R5R5R5R5

23 – 23 – CSCE 531 Spring 2006 LR Parse of id * ( id + id ) + id StackInputAction 0 id * ( id + id ) + id $ Shift id Shift id


Download ppt "– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:"

Similar presentations


Ads by Google