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.
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.
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.
LR Parsing Table Costruction
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.
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
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.
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
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.
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
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(k) Parsing CPSC 388 Ellen Walker Hiram College.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
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.
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.
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.
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.
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.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
LR(1) Parsers Comp 412 COMP 412 FALL 2010
UNIT - 3 SYNTAX ANALYSIS - II
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
Simple, efficient;limitated
Canonical LR Parsing Tables
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.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Lecture 9 SLR Parse Table Construction
Parsing VII The Last Parsing Lecture
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
5. Bottom-Up Parsing Chih-Hung Wang
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
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 -- LR(1) closure(I)

LR(1) -- GOTO(I, X)

Algorithm 4.53 -- LR(1) Sets of Items Input Augmented Grammar G’

Fig 4.41

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 } Engineering: A Compiler 2nd Edition by Cooper & Torczon

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 ) Engineering: A Compiler 2nd Edition by Cooper & Torczon

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] } Engineering: A Compiler 2nd Edition by Cooper & Torczon

Engineering: A Compiler 2nd Edition by Cooper & Torczon S6 : { [Term  Factor * • Term , EOF], [Term  Factor * • Term , –],  [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 •, –] } Engineering: A Compiler 2nd Edition by Cooper & Torczon

Engineering: A Compiler 2nd Edition by Cooper & Torczon Example The Goto Relationship (from the construction) Engineering: A Compiler 2nd Edition by Cooper & Torczon

Engineering: A Compiler 2nd Edition by Cooper & Torczon 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 Engineering: A Compiler 2nd Edition by Cooper & Torczon

Expr Grammar: Filling in the tables The algorithm produces the following table Engineering: A Compiler 2nd Edition by Cooper & Torczon

Example 4.54 GLR(1) sets of items Consider the following augmented grammar. S’ S S  C C C  c C | d S d C c d C c C c d C d

Algorithm 4.56 : Construction of canonical-LR parsing tables. INPUT: An augmented grammar G’ . OUTPUT: The canonical-LR parsing table ACTION and GOTO for G’. Construct C = {I0, I1, … Ii, … In }, the sets of LR(1) items If state i = Ii the parsing action for state i is determined as follows If [A a ; b] is in Ii and GOTO(Ii ; a) = Ij , then set ACTION[i; a] = “shift j” If [A α; a] is in Ii and A != S’ , then set ACTION[i; a] to “reduce A  α" If [S’S. ; $] is in Ii , then set ACTION[i; $] to “accept.” If any conflicting actions result from the above rules, we say the grammar is not LR(1). The algorithm fails to produce a parser in this case. goto transitions for state i are read from the diagram All other entries are “error.” The initial state of the parser is closure([S’.S; $])

Example 4.54 and Figure 4.42 Sets of Items LR(1) parse 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” Engineering: A Compiler 2nd Edition by Cooper & Torczon

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)