Lecture #8, Feb. 7, 2007 Shift-reduce parsing,

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

Compiler Designs and Constructions
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
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.
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.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
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 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.
Pertemuan 12, 13, 14 Bottom-Up Parsing
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
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.
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.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
LR(k) Grammar David Rodriguez-Velazquez CS6800-Summer I, 2009 Dr. Elise De Doncker.
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
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.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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 
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax and Semantics Structure of programming languages.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
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.
Announcements/Reading
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
COMPILER CONSTRUCTION
Announcements/Reading
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
Parsing #2 Leonidas Fegaras.
Compiler Design 7. Top-Down Table-Driven Parsing
Compiler SLR Parser.
Parsing #2 Leonidas Fegaras.
Kanat Bolazar February 16, 2010
Compiler Construction
Compiler Construction
Parsing Bottom-Up LR Table Construction.
Context Free Grammar – Quick Review
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Lecture #8, Feb. 7, 2007 Shift-reduce parsing, Shift-reduce and reduce-reduce conflicts, Precedence parsing, LR parsers, LR parsers and FSAs, LR parsing Tables, Sets of Items construction.

Invitation If you have not handed in a homework. And you would like another chance (a sort of amnesty) Please see me about making arrangements.

Assignments Reading Homework Project 1 Chapter 3, Sections 3.4 (Bottom up parsing) and 3.5 (LR(1) tables) Page 107-133 Possible Quiz on Monday Homework Homework 7, will be assigned Monday, but it is posted already. Please take a look if you want to. Project 1 Recall Project #1, the scanner is due Feb. 14th Midterm (sort of) Exam – Monday Feb 19

Bottom Up Parsing Shift-Reduce parsing data structures: input-string and stack. look at symbols on top of stack, and the input-string and decide: shift (move first input to stack) reduce (replace top n symbols on stack by a non-terminal) accept (declare victory) error (be gracious in defeat)

Example Bottom up Parse Consider the grammar: (note: left recursion is NOT a problem, but the grammar is still layered to prevent ambiguity) E ::= E + T1 | T2 T ::= T * F3 | F4 F ::= ( E )5 | id6 stack Input Action x + y shift x + y reduce 6 F + y reduce 4 T + y reduce 2 E + y shift E + y shift E + y reduce 6 E + F reduce 4 E + T reduce 1 E accept

Problems (ambiguous grammars) 1) shift reduce conflicts: stack Input Action x + y + z ? stack Input Action if x t if y t s2 e s3 ? 2) reduce reduce conflicts: suppose both procedure call and array reference have similar syntax: x(2) := 6 f(x) stack Input Action id ( id ) id ? Should id reduce to a parameter or an expression. Depends on whether the bottom most id is an array or a procedure.

Using ambiguity to your advantage Shift-Reduce and Reduce-Reduce errors are caused by ambiguous grammars. We can use resolution mechanisms to our advantage. Use an ambiguous grammar (smaller more concise, more natural parse trees) but resolve ambiguity using rules. Operator Precedence Every operator is given a precedence Precedence of the operator closest to the top of the stack and the precedence of operator next on the input decide shift or reduce. Sometimes the precedence is the same. Need more information: Associativity information.

Example Precedence Parser + * ( ) id $ < : : > = accept input : x * x + y stack Input Action $ E * E + y $ reduce! topmost terminal next input

Precedence parsers Precedence parsers have limitations No production can have two consecutive non-terminals Parse only a small subset of the Context Free Grammars Need a more robust version of shift- reduce parsing. LR - parsers State based - finite state automatons (w / stack) Accept the widest range of grammars Easily constructed (by a machine) Can be modified to accept ambiguous grammars by using precedence and associativity information.

LR Parsers ACTION GOTO Table Driven Parsers Table is indexed by state and symbols (both term and non-term) Table has two components. ACTION part GOTO part terminals non-terminals state id + * ( ) $ E T F shift (state = 5) 1 2 goto(state = 2) reduce(prod = 12) ACTION GOTO

LR Table encodes FSA * transition on terminal is a E -> E + T | T shift in action table, on nonterminal is a goto entry E -> E + T | T T -> T * F | F F -> ( E ) | id 1 2 3 4 5 6 7 8 9 10 11 ( T E ) F * id +

Table vs FSA The Table encodes the FSA The action part encodes Transitions on terminal symbols (shift) Finding the end of a production (reduce) The goto part encodes Tracing backwards the symbols on the RHS Transition on non-terminal, the LHS Tables can be quite compact

LR Table terminals non-terminals state id + * ( ) $ E T F s5 s4 1 2 3 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5

Reduce Action If the top of the stack is the rhs for some production n And the current action is “reduce n” We pop the rhs, then look at the state on the top of the stack, and index the goto-table with this state and the LHS non-terminal. Then push the lhs onto the stack in the new s found in the goto-table. (?,0)(id,5) * id + id $ Where: Action(5,*) = reduce 6 Production 6 is: F ::= id And: GOTO(0,F) = 3 (?,0)(F,3) * id + id $

Example Parse E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id Stack Input (?,0) id * id + id $ (?,0)(id,5) * id + id $ (?,0)(F,3) * id + id $ (?,0)(T,2) * id + id $ (?,0)(T,2)(*,7) id + id $ (?,0)(T,2)(*,7)(id,5) + id $ (?,0)(T,2)(*,7)(F,10) + id $ (?,0)(T,2) + id $ (?,0)(E,1) + id $ (?,0)(E,1)(+,6) id $ (?,0)(E,1)(+,6)(id,5) $ (?,0)(E,1)(+,6)(F,3) $ (?,0)(E,1)(+,6)(T,9) $ (?,0)(E,1) $

Constructing LR Tables A state represents some local state of the parse. It encodes “how far” we have progressed in recognizing some RHS of a production. We will represent this by an “item” An item is a production with a dot A -> . X Y Z A -> X . Y Z A -> X Y . Z A -> X Y Z. An item with a dot at the end is called a completed item An item with a dot at the beginning is called an initial item A state in the FSA is represented by a set of items.

Constructing Sets of items To construct the set of items that a state represents we need to know if we’re in some state how could we get there? This is done by a procedure called taking the closure of an item. Algorithm for computing the closure of an item. let I be a set of items then every item in I is in closure(I) if (A -> a . B b ) is in closure(I) and (B -> c ) is a production, then add the item (B -> . c ) if it is not already there.

Example closure construction Start of with the single item derived from the start symbol. I = { E’ -> . E } Then the closure of I contains: { E’ -> . E, E -> . E + T, E -> .T T -> . T * F T -> . F F -> . ( E ) F -> . id } 0) E’ -> E 1) E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id

The GOTO Construction Example: I = { E’-> . E, E-> E . + T} Goto(I,X) I is a set of items X is a grammar symbol This discovers new sets of items in the FSA given an old set. GOTO(I,X) = closure(J) such that [A -> a X . b ] in J when [A -> a . X b ] is in I GOTO(I,X) will be {} if there is no item in I with a dot before an X Example: I = { E’-> . E, E-> E . + T} GOTO(I,+) = { E-> E + . T, (initial item) T-> . T * F, (plus the closure of it) T-> . F, F-> . ( E ), F-> . id }

The Sets of items algorithm Let G’ be an augmented grammar G’ = G U S’ -> S The production where S is the start symbol of G procedure items(G’) = begin C := closure( { S’ -> . S } ); repeat for each set of items I in C for each symbol X in G’ Do if GOTO(I,X) is not empty and is not already in C then add it to C until no more sets of items can be added to C.