CS 536 Spring 20011 Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
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.
Pertemuan 12, 13, 14 Bottom-Up Parsing
CS 536 Spring Introduction to Bottom-Up Parsing Lecture 11.
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.
Bottom Up Parsing.
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.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
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.
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-up parsing Goal of parser : build a derivation
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Syntax and Semantics Structure of programming languages.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
LR Parsing Compiler Baojian Hua
Top-Down Parsing - recursive descent - predictive parsing
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
LR Parser: LR parsing is a bottom up syntax analysis technique that can be applied to a large class of context free grammars. L is for left –to –right.
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.
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.
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.
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.
CPSC46001 Bottom-up Parsing Reading Sections 4.5 and 4.7 from ASU.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Parsing #2 Leonidas Fegaras.
Lecture (From slides by G. Necula & R. Bodik)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 4. Syntax Analysis (2)
LR Parsing. Parser Generators.
Parsing #2 Leonidas Fegaras.
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
Compiler Construction
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12

CS 536 Spring Reading Assignment –Bottom-Up Parsing: Sections 6.1 – 6.4 –Also, you may want to read about Top-down parsing in Chapter 5 Sections 5.8 and 5.9 are optional

CS 536 Spring Overview LR(k) parsing –L: scan input Left to right –R: produce rightmost derivation –k tokens of lookahead LR(0) –zero tokens of look-ahead –“What? No lookahead?” See clarification in Section SLR –Simple LR: like LR(0) but uses FOLLOW sets to build more “precise” parsing tables –LR(0) is a toy, so we are going to use SLR in this lecture

CS 536 Spring Problem: when to shift, when to reduce? Recall our favorite grammar: E  T + E | T T  int * T | int | (E) The step T * int + int  int * int + int is not part of any rightmost derivation Hence, reducing first int to T was a mistake how to know when to reduce and when to shift?

CS 536 Spring What we need to know to do LR parsing LR(0) states –describe states in which the parser can be –Note: LR(0) states are used by both LR(0) and SLR parsers Parsing tables –transitions between LR(0) states, –actions to take when transiting: shift, reduce, accept, error How to construct LR(0) states How to construct parsing tables How to drive the parser

CS 536 Spring An LR(0) state = a set of LR(0) items An LR(0) item [X  .  ] says that –the parser is looking for an X –it has an  on top of the stack –expects to find in the input a string derived from . Notes: –[X .a  ] means that if a is on the input, it can be shifted. That is: a is a correct token to see on the input, and shifting a would not “over-shift” (still a viable prefix). –[X .] means that we could reduce X

CS 536 Spring LR(0) states S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Naïve SLR Parsing Algorithm 1.Let M be LR(0) state machine for G each state contains a set I of LR(0) items 2.Let |x 1 …x n $ be initial configuration 3.Repeat until configuration is S|$ Let  |  be current configuration Run M on current stack  If M rejects , report parsing error If M accepts  with items I, let a be next input Shift if X  . a   I Reduce if X    I and a  Follow(  )   a    X  a  Report parsing error if neither applies

CS 536 Spring Notes If there is a conflict in the last step, grammar is not SLR(k) k is the amount of lookahead –In practice k = 1

int 1 * int 2

CS 536 Spring LR(0) states S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift

CS 536 Spring Configuration |int * int$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift int | * int$3 * not in Follow(T) shift

CS 536 Spring Configuration int| * int$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int | * int$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift int | * int$3 * not in Follow(T) shift int * | int$11 shift

CS 536 Spring Configuration int * | int$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * | int$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * | int$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift int | * int$3 * not in Follow(T) shift int * | int$11 shift int * int |$3 $  Follow(T) red. T  int

CS 536 Spring Configuration int * int|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * int|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * int|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * int|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift int | * int$3 * not in Follow(T) shift int * | int$11 shift int * int |$3 $  Follow(T) red. T  int int * | T $11 shift int * T |$4 $  Follow(T) red. T  int*T

CS 536 Spring Configuration int * T|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * T|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * T|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration int * T|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift int | * int$3 * not in Follow(T) shift int * | int$11 shift int * int |$3 $  Follow(T) red. T  int int * | T $11 shift int * T |$4 $  Follow(T) red. T  int*T |T$ 1 shift T |$5 $  Follow(E) red. E  T

CS 536 Spring Configuration T|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring Configuration T|$ S’ . E E . T E .T + E T .(E) T .int * T T .int S’  E. E  T. E  T. + E T  int. * T T  int. T  (. E) E .T E .T + E T .(E) T .int * T T .int E  T + E. E  T +. E E .T E .T + E T .(E) T .int * T T .int T  int *.T T .(E) T .int * T T .int T  int * T. T  (E.) T  (E). E T ( int * ) E E T ( ( T  (

CS 536 Spring SLR Example ConfigurationDFA Halt StateAction |int * int$1shift int | * int$3 * not in Follow(T) shift int * | int$11 shift int * int |$3 $  Follow(T) red. T  int int * | T $11 shift int * T |$4 $  Follow(T) red. T  int*T |T$ 1 shift T |$5 $  Follow(E) red. E  T |E$1shift E |$2accept

CS 536 Spring Notes Textbook uses one more state: –it accepts in state “S’  E $. ” –I.e., it accepts in configuration E$|, not in E|$. Rerunning the automaton at each step is wasteful –Most of the work is repeated

CS 536 Spring An Improvement Remember the state of the automaton on each prefix of the stack Change stack to contain pairs  Symbol, DFA State 

CS 536 Spring An Improvement (Cont.) For a stack  sym 1, state 1 ...  sym n, state n  state n is the final state of the DFA on sym 1 … sym n Detail: The bottom of the stack is  any,start  where –any is any dummy state (textbook uses blank, Fig 6.6) –start is the start state of the DFA

CS 536 Spring Goto Table Define Goto[i,A] = j if state i  A state j Goto is just the transition function of the DFA –One of two parsing tables

CS 536 Spring Refined Parser Moves Shift x –Push  a, x  on the stack –a is current input –x is a DFA state Reduce X   –As before Accept Error

CS 536 Spring Action Table For each state s i and terminal a –If s i has item X  .a  and Goto[i,a] = j then Action[i,a] = shift j –If s i has item X  . and a  Follow(X) and X  S’ then Action[i,a] = reduce X   –If s i has item S’  S. then action[i,$] = accept –Otherwise, action[i,a] = error

CS 536 Spring SLR Parsing Algorithm (Fig 6.3) Let I = w$ be initial input Let j = 0 Let DFA state 1 have item S’ .S Let stack =  dummy, 1  repeat case action[top_state(stack),I[j]] of shift k: push  I[j++], k  reduce X  A: pop |A| pairs, I[--j] = X // prepend X to input accept: halt normally error: halt and report error

CS 536 Spring Notes on SLR Parsing Algorithm Note that the algorithm uses only the DFA states and the input –The stack symbols are never used! However, we still need the symbols for semantic actions

CS 536 Spring Constructing LR states Read Section 6.3 –we’ll review it on Friday –today: an intuitive construction LR(0) state machine –encodes all strings that are valid on the stack. –each valid string is a configuration, and hence corresponds to a state of the LR(0) state machine –each state tells us what to do (shift or reduce?)

CS 536 Spring LR(0) states for the example grammar