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

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework:
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
– 1 – CSCE 531 Spring 2006 Lecture 7 Predictive Parsing Topics Review Top Down Parsing First Follow LL (1) Table construction Readings: 4.4 Homework: Program.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Parsing IV Bottom-up Parsing. Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves.
Syntax and Semantics Structure of programming languages.
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
Bottom-up Parsing, Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Parsing IV Bottom-up Parsing
Fall Compiler Principles Lecture 4: Parsing part 3
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
Lecture 9 SLR Parse Table Construction
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Bottom Up Parsing.
Compiler Construction
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Lecture 7 Predictive Parsing
Compiler Construction
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

– 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 – 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 handout) grammar Test 1 Feb 15

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

– 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – 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 – CSCE 531 Spring 2006 LR Parsing Algorithm

– 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 – CSCE 531 Spring 2006 LR Parse of id * ( id + id ) + id StackInputAction 0 id * ( id + id ) + id $ Shift id Shift id