Lecture 11 LR Parse Table Construction

Slides:



Advertisements
Similar presentations
Compiler Designs and Constructions
Advertisements

Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Parsing Wrap Up Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
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.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #10 Parsing.
Parsing VI The LR(1) Table Construction. LR(k) items The LR(1) table construction algorithm uses LR(1) items to represent valid configurations of an LR(1)
Parsing VII The Last Parsing Lecture Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
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.
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Parsing Wrap-up. from Cooper and Torczon2 Filling in the A CTION and G OTO Tables The algorithm Many items generate no table entry  Closure( ) instantiates.
Bottom-up parsing Goal of parser : build a derivation
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.
LR(1) Parsers Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
LALR Parsing Canonical sets of LR(1) items
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
11 Outline  6.0 Introduction  6.1 Shift-Reduce Parsers  6.2 LR Parsers  6.3 LR(1) Parsing  6.4 SLR(1)Parsing  6.5 LALR(1)  6.6 Calling Semantic.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Parsing V LR(1) Parsers C OMP 412 Rice University Houston, Texas Fall 2001 Copyright 2000, Keith D. Cooper, Ken Kennedy, & Linda Torczon, all rights reserved.
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.
Parsing VII The Last Parsing Lecture. High-level overview  Build the canonical collection of sets of LR(1) Items, I aBegin in an appropriate state, s.
Parsing V LR(1) Parsers N.B.: This lecture uses a left-recursive version of the SheepNoise grammar. The book uses a right-recursive version. The derivations.
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.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
COMPILER CONSTRUCTION
Announcements/Reading
Programming Languages Translator
50/50 rule You need to get 50% from tests, AND
Lexical and Syntax Analysis
LR(1) Parsers Comp 412 COMP 412 FALL 2010
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing VI The LR(1) Table Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
N.B.: This lecture uses a left-recursive version of the SheepNoise grammar. The book uses a right-recursive version. The derivations (& the tables) are.
Michael Santacroce EECE 6083 Compiler Theory University of Cincinnati
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing VII The Last Parsing Lecture
Lecture 9: Bottom-Up Parsing
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Ambiguity in Grammar, Error Recovery
Syntax Analysis - 3 Chapter 4.
Kanat Bolazar February 16, 2010
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Lecture 11 LR Parse Table Construction CSCE 531 Compiler Construction Lecture 11 LR Parse Table Construction Topics Review of Shift-Reduce Parsing LR parsing; handles Sample Test Overview Readings: June 23, 2004

Building the Canonical Collection Start from s0 = closure( [S’S,EOF ] ) Repeatedly construct new states, until all are found The algorithm s0  closure ( [S’S,EOF] ) S  { s0 } k  1 while ( S is still changing )  sj  S and  x  ( T  NT ) sk  goto(sj,x) record sj  sk on x if sk  S then S  S  sk k  k + 1 Fixed-point computation Loop adds to S S  2ITEMS, so S is finite Worklist version is faster

Example (grammar & sets) Simplified, right recursive expression grammar Goal  Expr Expr  Term – Expr Expr  Term Term  Factor * Term Term  Factor Factor  ident

Valid LR(1) items

Figure 4.40

Example (building the collection) Initialization Step s0  closure( { [Goal  •Expr , EOF] } ) { [Goal  • Expr , EOF], [Expr  • Term – Expr , EOF], [Expr  • Term , EOF], [Term  • Factor * Term , EOF], [Term  • Factor * Term , –], [Term  • Factor , EOF], [Term  • Factor , –], [Factor  • ident , EOF], [Factor  • ident , –], [Factor  • ident , *] } S  {s0 }

Example (building the collection) Iteration 1 s1  goto(s0 , Expr) s2  goto(s0 , Term) s3  goto(s0 , Factor) s4  goto(s0 , ident ) Iteration 2 s5  goto(s2 , – ) s6  goto(s3 , * ) Iteration 3 s7  goto(s5 , Expr ) s8  goto(s6 , Term )

Example: LR(1) Sets of Items S0 : { [Goal  • Expr , EOF], [Expr  • Term – Expr , EOF], [Expr  • Term , EOF], [Term  • Factor * Term , EOF], [Term  • Factor * Term , –], [Term  • Factor , EOF], [Term  • Factor , –], [Factor  • ident , EOF], [Factor  • ident , –], [Factor • ident, *] } S1 : { [Goal  Expr •, EOF] } S2 : { [Expr  Term • – Expr , EOF], [Expr  Term •, EOF] } S3 : { [Term  Factor • * Term , EOF],[Term  Factor • * Term , –], [Term  Factor •, EOF], [Term  Factor •, –] } S4 : { [Factor  ident •, EOF],[Factor  ident •, –], [Factor  ident •, *] } S5 : { [Expr  Term – • Expr , EOF], [Expr  • Term – Expr , EOF], [Expr  • Term , EOF], [Term  • Factor * Term , –], [Term  • Factor , –], [Term  • Factor * Term , EOF], [Term  • Factor , EOF], [Factor  • ident , *], [Factor  • ident , –], [Factor  • ident , EOF] }

S6 : { [Term  Factor * • Term , EOF], [Term  Factor * • Term , –], [Term  • Factor , EOF], [Term  • Factor , –], [Factor  • ident , EOF], [Factor  • ident , –], [Factor  • ident , *] } S7: { [Expr  Term – Expr •, EOF] } S8 : { [Term  Factor * Term •, EOF], [Term  Factor * Term •, –] }

Example The Goto Relationship (from the construction)

ACTION and GOTO Tables  set sx  S  item i  sx if i is [A •ad,b] and goto(sx,a) = sk , a  T then ACTION[x,a]  “shift k” else if i is [S’S •,EOF] then ACTION[x ,a]  “accept” else if i is [A •,a] then ACTION[x,a]  “reduce A”  n  NT if goto(sx ,n) = sk then GOTO[x,n]  k x is the state number

Expr Grammar: Filling in the tables The algorithm produces the following table

Shift / Reduce Conflicts What if set s contains [A•a,b] and [B•,a] ? First item generates “shift”, second generates “reduce” Both define ACTION[s,a]  cannot do both actions This is a shift/reduce error or conflict Example if-then-else, expression grammar Solution Modify the grammar to eliminate it Let Shifting have priority will often resolve it correctly Yacc defaults to “shift”

Reduce / Reduce Conflicts What is set s contains [A•, a] and [B•, a] ? [A•, a]  reduce by [B•, a]  reduce by Each generates “reduce”, but with a different production Both define ACTION[s,a] — cannot do both reductions This is called a reduce/reduce conflict Solutions Modify the grammar to eliminate it In either case shift/reduce or reduce/reduce, the grammar is not LR(1)

Shrinking the Tables Three options: Combine terminals such as number & identifier, + & -, * & / Directly removes a column, may remove a row For expression grammar, 198 (vs. 384) table entries Combine rows or columns Implement identical rows once & remap states Requires extra indirection on each lookup Use separate mapping for ACTION & for GOTO Use another construction algorithm Both LALR(1) and SLR(1) produce smaller tables Implementations are readily available

LR(k) versus LL(k)(Top-down Rec. Desc.) Finding Reductions LR(k)  Each reduction in the parse is detectable with the complete left context, the reducible phrase, itself, and the k terminal symbols to its right LL(k)  Parser must select the reduction based on The complete left context The next k terminals Thus, LR(k) examines more context “… in practice, programming languages do not actually seem to fall in the gap between LL(1) languages and deterministic languages” J.J. Horning, “LR Grammars and Analysers”, in Compiler Construction, An Advanced Course, Springer-Verlag, 1976

Summary Advantages Fast Good locality Simplicity Good error detection Deterministic langs. Automatable Left associativity Disadvantages Hand-coded High maintenance Right associativity Large working sets Poor error messages Large table sizes Top-down recursive descent LR(1)