CS 536 Spring 20011 Introduction to Bottom-Up Parsing Lecture 11.

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.
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.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
Mooly Sagiv and Roman Manevich School of Computer Science
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Bottom Up Parsing. 2 Bottom-Up Parsing l Bottom-up parsing is more general than top-down parsing »And just as efficient »Builds on ideas in top-down.
Context-Free Grammars Lecture 7
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Bottom Up Parsing.
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.
Prof. Bodik CS 164 Lecture 91 Bottom-up parsing CS164 3:30-5:00 TT 10 Evans.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
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.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
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.
11/22/1999 JHU CS /Jan Hajic 1 Introduction to Natural Language Processing ( ) Shift-Reduce Parsing in Detail Dr. Jan Hajič CS Dept., Johns.
Syntax and Semantics Structure of programming languages.
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
Top-Down Parsing - recursive descent - predictive parsing
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
1 Compiler Construction Syntax Analysis Top-down parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
111 Chapter 6 LR Parsing Techniques Prof Chung. 1.
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.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
CPSC46001 Bottom-up Parsing Reading Sections 4.5 and 4.7 from ASU.
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Unit-3 Bottom-Up-Parsing.
Parsing IV Bottom-up Parsing
CS 404 Introduction to Compiler Design
4d Bottom Up Parsing.
Lecture (From slides by G. Necula & R. Bodik)
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Bottom Up Parsing.
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Bottom-Up Parsing “Shift-Reduce” Parsing
Syntax Analysis - Parsing
Compiler Construction
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Parsing Bottom-Up Introduction.
Presentation transcript:

CS 536 Spring Introduction to Bottom-Up Parsing Lecture 11

CS 536 Spring Outline The strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms

CS 536 Spring Predictive Parsing Summary First and Follow sets are used to construct predictive tables –For non-terminal A and input t, use a production A  a where t  First( a ) –For non-terminal A and input t, if e  First(A) and t  Follow( a ), then use a production A  a where e  First( a ) We’ll see First and Follow sets again...

CS 536 Spring Bottom-Up Parsing Bottom-up parsing is more general than top- down parsing –And just as efficient –Builds on ideas in top-down parsing Bottom-up is the preferred method in practice Concepts today, algorithms next time

CS 536 Spring An Introductory Example Bottom-up parsers don’t need left-factored grammars Hence we can revert to the “natural” grammar for our example: E  T + E | T T  int * T | int | (E) Consider the string: int * int + int

CS 536 Spring The Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: int * int + intT  int int * T + intT  int * T T + intT  int T + TE  T T + EE  T + E E

CS 536 Spring TEST YOURSELF #1 Question: find the rightmost derivation of the string int * int + int

CS 536 Spring Observation Read the productions found by bottom-up parse in reverse (i.e., from bottom to top) This is a rightmost derivation! int * int + intT  int int * T + intT  int * T T + intT  int T + TE  T T + EE  T + E E

CS 536 Spring Important Fact #1 Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse

CS 536 Spring A Bottom-up Parse int * int + int int * T + int T + int T + T T + E E E TE + int * T T

CS 536 Spring A Bottom-up Parse in Detail (1) + int * int * int + int

CS 536 Spring A Bottom-up Parse in Detail (2) int * int + int int * T + int + int * T

CS 536 Spring A Bottom-up Parse in Detail (3) int * int + int int * T + int T + int T + int * T

CS 536 Spring A Bottom-up Parse in Detail (4) int * int + int int * T + int T + int T + T T + int * T T

CS 536 Spring A Bottom-up Parse in Detail (5) int * int + int int * T + int T + int T + T T + E TE + int * T T

CS 536 Spring A Bottom-up Parse in Detail (6) int * int + int int * T + int T + int T + T T + E E E TE + int * T T

CS 536 Spring A Trivial Bottom-Up Parsing Algorithm Let I = input string repeat pick a non-empty substring  of I where X   is a production if no such , backtrack replace one  by X in I until I = “S” (the start symbol) or all possibilities are exhausted

CS 536 Spring Questions Does this algorithm terminate? How fast is the algorithm? Does the algorithm handle all cases? How do we choose the substring to reduce at each step?

CS 536 Spring How to build the house of cards?

CS 536 Spring Where Do Reductions Happen Important Fact #1 has an interesting consequence: –Let  be a step of a bottom-up parse –Assume the next reduction is by X   –Then  is a string of terminals Why? Because  X    is a step in a right- most derivation

CS 536 Spring Notation Idea: Split string into two substrings –Right substring is as yet unexamined by parsing (a string of terminals) –Left substring has terminals and non-terminals The dividing point is marked by a | –The | is not part of the string Initially, all input is unexamined |x 1 x 2... x n

CS 536 Spring Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce

CS 536 Spring Shift Shift: Move | one place to the right –Shifts a terminal to the left string ABC|xyz  ABCx|yz

CS 536 Spring Reduce Apply an inverse production at the right end of the left string –If A  xy is a production, then Cbxy|ijk  CbA|ijk

CS 536 Spring The Example with Reductions Only int * int | + intreduce T  int int * T | + intreduce T  int * T T + int |reduce T  int T + T |reduce E  T T + E |reduce E  T + E E |

CS 536 Spring The Example with Shift-Reduce Parsing |int * int + intshift int | * int + intshift int * | int + intshift int * int | + intreduce T  int int * T | + intreduce T  int * T T | + intshift T + | intshift T + int |reduce T  int T + T |reduce E  T T + E |reduce E  T + E E |

CS 536 Spring A Shift-Reduce Parse in Detail (1) + int *  |int * int + int

CS 536 Spring A Shift-Reduce Parse in Detail (2) + int *  |int * int + int int | * int + int

CS 536 Spring A Shift-Reduce Parse in Detail (3) + int *  |int * int + int int | * int + int int * | int + int

CS 536 Spring A Shift-Reduce Parse in Detail (4) + int *  |int * int + int int | * int + int int * | int + int int * int | + int

CS 536 Spring A Shift-Reduce Parse in Detail (5) + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int 

CS 536 Spring A Shift-Reduce Parse in Detail (6) T + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int 

CS 536 Spring A Shift-Reduce Parse in Detail (7) T + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int 

CS 536 Spring A Shift-Reduce Parse in Detail (8) T + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | 

CS 536 Spring A Shift-Reduce Parse in Detail (9) T + int * T T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | 

CS 536 Spring A Shift-Reduce Parse in Detail (10) TE + int * T T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | 

CS 536 Spring A Shift-Reduce Parse in Detail (11) E TE + int * T T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | E | 

CS 536 Spring The Stack Left string can be implemented by a stack –Top of the stack is the | Shift pushes a terminal on the stack Reduce pops 0 or more symbols off of the stack (production rhs) and pushes a non- terminal on the stack (production lhs)

CS 536 Spring Key Issue (will be resolved by algorithms) How do we decide when to shift or reduce? –Consider step int | * int + int –We could reduce by T  int giving T | * int + int –A fatal mistake: No way to reduce to the start symbol E

CS 536 Spring Conflicts Generic shift-reduce strategy: –If there is a handle on top of the stack, reduce –Otherwise, shift But what if there is a choice? –If it is legal to shift or reduce, there is a shift-reduce conflict –If it is legal to reduce by two different productions, there is a reduce-reduce conflict

CS 536 Spring Source of Conflicts Ambiguous grammars always cause conflicts But beware, so do many non-ambiguous grammars

CS 536 Spring Conflict Example Consider our favorite ambiguous grammar: E  E + E |E * E |(E) |int

CS 536 Spring One Shift-Reduce Parse |int * int + intshift... E * E | + intreduce E  E * E E | + intshift E + | intshift E + int|reduce E  int E + E |reduce E  E + E E |

CS 536 Spring Another Shift-Reduce Parse |int * int + intshift... E * E | + intshift E * E + | intshift E * E + int |reduce E  int E * E + E|reduce E  E + E E * E |reduce E  E * E E |

CS 536 Spring Example Notes In the second step E * E | + int we can either shift or reduce by E  E * E Choice determines associativity of + and * As noted previously, grammar can be rewritten to enforce precedence Precedence declarations are an alternative

CS 536 Spring Precedence Declarations Revisited Precedence declarations cause shift-reduce parsers to resolve conflicts in certain ways Declaring “* has greater precedence than +” causes parser to reduce at E * E | + int More precisely, precedence declaration is used to resolve conflict between reducing a * and shifting a +

CS 536 Spring Precedence Declarations Revisited (Cont.) The term “precedence declaration” is misleading These declarations do not define precedence; they define conflict resolutions –Not quite the same thing!