Parsing Bottom-Up LR Table Construction.

Slides:



Advertisements
Similar presentations
Bottom-up Parser Table Construction David Walker COS 320.
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.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Compiler Designs and Constructions
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.
 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
LR Parsing Table Costruction
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)
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
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.
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.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
Chapter 4 Syntactic Analysis II. Chapter 4 -- Syntactic Analysis II2 1. Introduction to Bottom-Up parsing  Grammar: E--> E+E | E*E | i  Expression:
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CS 153 A little bit about LR Parsing. Background We’ve seen three ways to write parsers:  By hand, typically recursive descent  Using parsing combinators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
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.
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.
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.
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.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Announcements/Reading
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
CS 404 Introduction to Compiler Design
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
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
Bottom Up Parsing.
Ambiguity in Grammar, Error Recovery
Compiler Construction
Compiler SLR Parser.
Parsing #2 Leonidas Fegaras.
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.
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Lecture 11 LR Parse Table Construction
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow.
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Parsing Bottom-Up LR Table Construction

Parsing Bottom-Up

Construction of LR Parsing Tables It is customary to cover the generation of LR parsing tables in a series of stages, showing three levels of LR parsers of increasing complexity. (1) Simple LR (SLR) (2) the Canonical LR parser, and (3) the lookahead LR (LALR) parser. Parsing Bottom-Up

This approach leads us into the subject by gradual stages, each building on the previous one, until we reach the LALR parser, the most practical one, which is impossibly complicated if presented without the background provided by the other 2. Let’s begin by introducing items that will be common for all three parsers. Parsing Bottom-Up

Parser States In the LR parsers, each current state corresponds to a particular sequence of symbols at the top of the stack States in a FSA do two things. They reflect what has happened in the recent past, and they control how the FSA will respond to the next input. Hence, in the design of an LR parser, we must relate the state transition to what goes onto the stack. Parsing Bottom-Up

Items An item is a production with a placeholder (.) telling how far we have gotten. The production E-> E+T gives rise to the following items. [E->.E+T] [E->E.+T] [E->E+.T] [E->E+T.] symbols to the left of the . are already on the stack; the rest is yet to come. Parsing Bottom-Up

So, putting it all together: An item is a summary of the recent history of the parse. An LR parser is controlled by a finite-state machine. The recent history of a finite-state machine is contained in its state...So an item must correspond to a state in a LR parser Parsing Bottom-Up

We need to tell the parser when to accept. Almost, If we have a state for each item we basically have an NDFA. Getting the LR states parallels getting the DFA from the NDFA. We need to tell the parser when to accept. For this we add a new "dummy" Non-Terminal Z-> E instead of reducing this production, we accept. Parsing Bottom-Up

State Transitions Transitions are determined by the grammar and the item sets obtained from it. If we have 2 items P=[F->.(E)] and Q=[F->(.E)], then the structure of the items dictates that we have a transition on the symbol ( from P to Q. Parsing Bottom-Up

Constructing the State Table 1. put Z -> .E into the set 2. for all items in the set, if there is a . before a Non-Terminal include all their initial items. (initial items are where the N-T --> .stuff (note the . is first) 3. Apply 2 until nothing can be added. for every item in a state C->a.Xb move the . past X perform closure Parsing Bottom-Up

Our Language Example. -- by hand (0) Z -> E (1) E -> E + T (2) E -> E - T (3) E -> T (4) T -> T * F (5) T -> T / F (6) T -> F (7) F -> ( E ) (8) F -> I Parsing Bottom-Up

Our Language Example. -- by hand 0: Z -> .E E -> .E+T E -> .E-T E -> .T T -> .T*F T -> .T/F T -> .F F -> .(E) F -> .i Parsing Bottom-Up

4: (move over '(' from 0) 1: (move over E from 0) F -> (.E) Z -> E. E -> E.+T E -> E.-T 2: (move over T from 0) E -> T. T -> T.*F T -> T./F 3: (move over F from 0) T -> F. 4: (move over '(' from 0) F -> (.E) E -> .E+T E -> .E-T E -> .T T -> .T*F T -> .T/F T -> .F F -> .(E) F -> .i Parsing Bottom-Up

7: (move over '-' from 1) 5: (move over i from 0) E -> E-.T T -> .T*F T -> .T/F T -> .F F -> .(E) F -> .i 8: (move over '*' from 2) T -> T*.F 5: (move over i from 0) F -> i. 6: (move over '+' from 1) E -> E+.T T -> .T*F T -> .T/F T -> .F F -> .(E) F -> .i Parsing Bottom-Up

9: (move over '/' from 2) 10: (move over E from 4) T -> T/.F F -> .(E) F -> .i 10: (move over E from 4) F -> (E.) E -> E.+T E -> E.-T (over T from 4) -- same as 2 (over F from 4) -- same as 3 11: (over T from 6) E -> E+T. T -> T.*F T -> T./F 12: (over T from 7) E -> E-T. 13: (over F from 8) T -> T*F. 14: (over F from 9) T -> T/F. 15: (over ')' from 10) F -> (E). Parsing Bottom-Up

Parsing Bottom-Up

Our Language Example: Yacc grammar : E PlusTok T | E MinusTok T | T ; T : T TimesTok F | T DivideTok F | F F : LParenTok E RParenTok | IDTok Parsing Bottom-Up

Yacc output with states $accept : _E $end IDTok shift 5 LParenTok shift 4 . error E goto 1 T goto 2 F goto 3 state 1 $accept : E_$end E : E_PlusTok T E : E_MinusTok T $end accept PlusTok shift 6 MinusTok shift 7 . error Parsing Bottom-Up

state 2 state 4 state 3 state 5 state ... E : T_ (3) T : T_TimesTok F T : T_DivideTok F TimesTok shift 8 DivideTok shift 9 . reduce 3 state 3 T : F_ (6) . reduce 6 state 4 F : LParenTok_E RParenTok IDTok shift 5 LParenTok shift 4 . error E goto 10 T goto 2 F goto 3 state 5 F : IDTok_ (8) . reduce 8 state ... Parsing Bottom-Up

Filling the Rows of the State Table Parsing Bottom-Up

Creating Action Table Entries The shift entries are taken from the state table entries we just created. (terminals we moved across to get the next state). If a state, q, contains a completed item n:[C->b.] then for all inputs, x, in the Follow(C) reduce n is in [q,x] If State q contains [Z -> E.] then the action for [q,$] is "accept" Parsing Bottom-Up

Parsing Bottom-Up

Error Handling For each empty slot in your table, you can have a unique error message. You could also try to guess what they left out. Panic Mode -- ignore everything until a ; Parsing Bottom-Up

Conflicts If a grammar is not LR it will show up in the creation of the table. No ambiguous grammar is LR. Parsing Bottom-Up

Parsing Bottom-Up

Canonical LR Parsers The SLR parser breaks down with a conflict. LR(1) item sets standard item sets plus a lookahead symbol this creates a lot more states. It is possible to have |LR(0) item set| * |terminals| Parsing Bottom-Up

Lookahead LR (LALR) Parsers Why not just use the LR(0) item sets and only add a look ahead when we need one? They are as powerful as Canonical LR parsers. They are slower to detect errors (but will detect one before the next token is shifted onto the stack) Parsing Bottom-Up

Two ways to construct these: 1. Brute Force LALR Parser Construction Start with the LR(1) item sets and merge states. 2. Efficient LALR Parser Construction Start with the LR(0) item sets and add lookaheads as needed. Parsing Bottom-Up

Compiler-Compilers YACC generates LALR(1) parser code When it runs into conflicts it notifies the user shift/reduce conflicts are resolved in favor of the shift. operators are right associative by default Parsing Bottom-Up

Summary: Which Parser Should I Use? We have seen several different parsing techniques, of which the most realistic are probably the table driven parsers. Predictive parser, operator precedence parser, and LR parser Parsing Bottom-Up

Summary: Which Parser Should I Use? Which is best? it seems to be personal taste. Now that Yacc-like parser generators are available, the LR parser seems to be the inevitable choice, but, a lot of people still write predictive, recursive-descent parsers. Parsing Bottom-Up

Parsing Bottom-Up