LR(k) Grammar David Rodriguez-Velazquez CS6800-Summer I, 2009 Dr. Elise De Doncker.

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 Designs and Constructions
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.
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
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.
Bottom Up Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
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.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-up parsing Goal of parser : build a derivation
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
COMP3190: Principle of Programming Languages Formal Language Syntax.
LR Parsing Compiler Baojian Hua
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
1 Compiler Construction Syntax Analysis Top-down parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
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 
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Syntax and Semantics Structure of programming languages.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Compilation /15a Lecture 5 1 * Syntax Analysis: Bottom-Up parsing Noam Rinetzky.
Announcements/Reading
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
4. Bottom-up Parsing Chih-Hung Wang
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.
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.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
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.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Announcements/Reading
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Bottom-Up Syntax Analysis
Subject Name:COMPILER DESIGN Subject Code:10CS63
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
Chap. 3 BOTTOM-UP PARSING
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

LR(k) Grammar David Rodriguez-Velazquez CS6800-Summer I, 2009 Dr. Elise De Doncker

Bottom-Up Parsing Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state A powerful parsing technology LR grammars – Construct right-most derivation of program – Left-recursive grammar, virtually all programming language are left-recursive – Easier to express syntax

Bottom-Up Parsing Right-most derivation – Start with the tokens – End with the start symbol – Match substring on RHS of production, replace by LHS – Shift-reduce parsers Parsers for LR grammars Automatic parser generators (yacc, bison)

Bottom-Up Parsing Example Bottom-Up Parsing S  S + E | E E  num | (S) (1+2+(3+4))+5  (E+2+(3+4))+5  (S+2+(3+4))+5  (S+E+(3+4))+5  (S+(3+4))+5  (S+(E+4))+5  (S+(S+4))+5  (S+(S+E))+5  (S+(S))+5  (S+E)+5  (S)+5  E+5  S+5  S+E  S

Bottom-Up Parsing Advantage – Can postpone the selection of productions until more of the input is scanned SSE1+E2 SSE(SSE1+E2)+E Top-Down Parsing Bottom-Up Parsing S  S + E | E E  num | (S) More time to decide what rules to apply

Terminology LR(k) Left-to-right scan of input Right-most derivation k symbol lookahead [Bottom-up or shift-reduce] parsing or LR parser Perform post-order traversal of parse tree

Shift-Reduce Parsing Parsing actions: – A sequence of shift and reduce operations Parser state: – A stack of terminals and non-terminals (grows to the right) Current derivation step: = stack + input

Shift-Reduce Parsing Derivation Step (Stack + input) Stack (terminals & non-terminals) Unconsumed input (1+2+(3+4))+5  (1+2+(3+4))+5 shift (E+2+(3+4))+5  (E+2+(3+4))+5 reduce (S+2+(3+4))+5  (S+2+(3+4))+5 reduce (S+E+(3+4))+5  (S+E+(3+4))+5 reduce

Shift-Reduce Actions Parsing is a sequence of shift and reduces Shift: move look-ahead token to stack Reduce: Replace symbols from top of stack with non-terminal symbols X corresponding to the production: X  β (e.g., pop β, push X) StackInputAction (1+2+(3+4))+5Shift 1 (1(1+2+(3+4))+5 StackInputAction (S+E+(3+4)+5Reduce S  S+E (S+(3+4)+5

Shift-Reduce Parsing DerivationStackInput streamAction (1+2+(3+4))+5 shift (1+2+(3+4))+5(1+2+(3+4))+5shift (1+2+(3+4))+5(1+2+(3+4))+5reduce E  num (E+2+(3+4))+5(E+2+(3+4))+5reduce S  E (S+2+(3+4))+5(S+2+(3+4))+5Shift (S+2+(3+4))+5(S+2+(3+4))+5Shift (S+2+(3+4))+5(S+2+(3+4))+5reduce E  num (S+E+(3+4))+5(S+E+(3+4))+5reduce S  S + E (S+(3+4))+5(S+(3+4))+5Shift (S+(3+4))+5(S+(3+4))+5Shift (S+(3+4))+5(S+(3+4))+5Shift (S+(3+4))+5 … (S+(3+4))+5reduce E  num S  S + E | E E  num | (S)

Potential Problems How do we know which action to take: whether to shift or reduce, and which production to apply Issues – Sometimes can reduce but should not – Sometimes can reduce in different ways

Action Selection Problem Given stack β and loock-ahead symbol b, should parser: – Shift b onto the stack making it βb? – Reduce X  γ assuming that the stack has the form β = αγ making it αX ? If stack has the form αγ, should apply reduction X  γ (or shift) depending on stack prefix α ? – α is different for different possible reductions since γ’s have different lengths

LR Parsing Engine Basic mechanism – Use a set of parser states – Use stack with alternating symbols and states – Use parsing table to: Determine what action to apply (shift/reduce) Determine next state – The parser actions can be precisely determined from the table

LR Parsing Table Action TableGoto Table StateTerminalsNon-Terminals State number Next action and next state Next State

LR Parsing Table Example S  (L) | id L  S | L,S Input Terminal Non- Terminals STATE ()Id,$SL 1s3s2g4 2S  id 3S3s2g7g5 4accept 5s6s8 6S  (L) 7LSLSLSLSLSLSLSLSLSLS 8s3s2g9 9L  L,S

LR (k) Grammars LR(k) = Left-to-right scanning, right most derivation, k lookahead chars Main cases – LR(0) and LR(1) – Some variations SLR and LALR(1) Parsers for LR(0) Grammars: – Determine the action without any lookahead – Will help us understand shift-reduce parsing

Building LR(0) Parsing Table To build the parsing table – Define states of the parser – Build a DFA to describe transitions between states – Use the DFA to build the parsing table Each LR(0) state is a set of LR(0) items – An LR(0) item: X  α.β where X  αβ is a production in the grammar – The LR(0) items keep track of the progress on all of the possible upcoming productions – The item X  α.β abstracts the fact that the parser already matched the string α at the top of the stack

Example LR(0) State An LR(0) item is a production from the language with a separator “.” somewhere in the RHS of the production Sub-string before “.” is already on the stack (beginnings of possible γ‘s to be reduced) Sub-string after “.”: what we might see next E  num. E  (.S) state item

Start State and Closure Start state – Augment grammar with production: S’  S$ – Start state of DFA has empty stack: S’ .S$ Closure of a parser state: – Start with Closure(S) =S – Then for each item in S: X  α.γβ Add items for all the productions Y  γ to the closure of S: Y . γ

Closure Example Set of possible production to be reduced next Added items have the “.” located at the beginning no symbols for these items on the stack yet S  (L) | id L  S | L,S DFA start state S’ . S $ S . ( L ) S . id closure

The Goto Operation Goto operation : describes transitions between parser states, which are sets of items Algorithm: for state S and a symbol Y – If the item [X  α. Y β] is in I(state), then – Goto(I,Y) = Closure([X  α. Y β] ) S’ . S $ S . ( L ) S . id Goto(S,’(‘) Closure( { S  (. L ) } )

Shift: Terminal Symbols In new state, include all items that have appropriate input symbol just after dot, advance dot in those items and take closure S’ . S $ S . ( L ) S . id Grammar S  (L) | id L  S | L,S S  (. L ) L . S L . L, S S . ( L ) S . id S  id. ( id (

Goto: Non-terminal Symbols Same algorithm for transitions on non-terminals S’ . S $ S . ( L ) S . id Grammar S  (L) | id L  S | L,S S  (. L ) L . S L . L, S S . ( L ) S . id S  id. ( id ( L  S. S L S  ( L. ) L  L., S

Applying Reduce Actions Pop RHS off stack, replace with LHS X (X  β ), then rerun DFA S’ . S $ S . ( L ) S . id Grammar S  (L) | id L  S | L,S S  (. L ) L . S L . L, S S . ( L ) S . id S  id. ( id ( L  S. S L S  ( L. ) L  L., S States causing reduction (dot has reached the end)

Full DFA S’ . S $ S . ( L ) S . id Grammar S  (L) | id L  S | L,S S  (. L ) L . S L . L, S S . ( L ) S . id S  id. ( id ) L S  ( L. ) L  L., S S’  S. $ Final state S $ L  S. S L  L,. S S . ( L ) S . id, id S  ( L ). ( S  L, S. S (

LR Parsing Table Example S  (L) | id L  S | L,S Input Terminal Non- Terminals STATE ()Id,$SL 1s3s2g4 2S  id 3S3s2g7g5 4accept 5s6s8 6S  (L) 7LSLSLSLSLSLSLSLSLSLS 8s3s2g9 9L  L,S

Building the Parsing Table States in the table = states in the DFA For transitions S  S’ on terminal C: – Table [S,C] = Shift(S’) where S’ = next state For transitions S  S’ on non-terminal N – Table [S,N] = Goto(S’) If S is a reduction state X  β then: – Table [S, *] = Reduce(x  β)

Parsing Algorithm Algorithm: look at entry for current state s and input terminal a – If Table[s, a] = shift then shift: Push(t), let ‘a’ be the next input symbol – If Table[s, a] = X  α then reduce: Pop(| α |), t = top(), push(GOTO[t,X]), output X  α

Reductions On reducing X  β with stack α β – Pop β off stack, revealing prefix α and state – Take single step in DFA from top state – Push X onto stack with new DFA state Example: DerivationStackInputAction ((a),b  1(3(3a),b)shift, goto 2 ((a),b  1(3(3 a 2),b)reduce S  id ((S),b  a(3(3 S 7),b)reduce L  S

LR(0) Summary LR(0) parsing recipe: – Start with LR(0) grammar – Compute LR(0) states and build DFA Use the closure operation to compute states Use the goto operation to compute transitions – Build the LR(0) parsing table from the DFA This can be done automatically – Parser Generator Yacc

Question: Full DFA for this grammar S’ . S $ S . ( L ) S . id Grammar S  (L) | id L  S | L,S S  (. L ) L . S L . L, S S . ( L ) S . id S  id. ( id ) L S  ( L. ) L  L., S S’  S. $ Final state S $ L  S. S L  L,. S S . ( L ) S . id, id S  ( L ). ( S  L, S. S (

References Aho A.V., Ullman J. D., Lam M., Sethi R., “Compilers Principles, Techniques & Tools”, Second Edition,Addison Wesley Sudkamp A. T., An Introduction the Theory of Computer Science L&M, third edition, Addison Wesley

Question: Parsing ((a),b) DerivationStackInput streamAction ((a),b)  1((a),b)$shift, goto 3 ((a),b)  1(3(a),b)$shift, goto 3 ((a),b)  1(3(3a),b)$shift, goto 2 ((a),b)  1(3(3a2),b)$reduce S  id ((S),b)  1(3(3S7),b)$reduce L  S ((L),b)  1(3(3L5),b)$shift, goto 6 ((L),b)  1(3(3L5)6,b)$reduce S  (L) (S,b)  1(3S7,b)$reduce L  S (L,b)  1(3L5,b)$shift, goto 8 (L,b)  1(3L5,8b)$shift, goto 9 (L,b)  1(3L5,8b2)$reduce S  id (L,S)  1(3L5,8S9)$reduce L  L,S (L)  1(3L5)$shift, goto 6 (L)  1(3L5)6$reduce S  (L) SS 1S4$Done S  (L) | id L  S | L,S