Announcements/Reading

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Compiler Designs and Constructions
 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.
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.
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)
1 Bottom Up Parsing. 2 Bottom-Up Parsing l Bottom-up parsing is more general than top-down parsing »And just as efficient »Builds on ideas in top-down.
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.
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
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.
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.
LR(k) Grammar David Rodriguez-Velazquez CS6800-Summer I, 2009 Dr. Elise De Doncker.
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.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
LESSON 24.
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.
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.
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 
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Spring 2014Jim Hogg - UW - CSE - P501E-1 LR ~ Bottom-Up ~ Shift-Reduce - concluded Dotted Items Building the Handles DFA Building Action & Goto Tables.
Syntax and Semantics Structure of programming languages.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing LR Parsing Muhammed Al-Mulhem March 1, 2009.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
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.
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.
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.
Syntax and Semantics Structure of programming languages.
Announcements/Reading
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
Syntax Analysis Part II
Implementing a LR parser engine
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing #2 Leonidas Fegaras.
Lecture (From slides by G. Necula & R. Bodik)
Parsing #2 Leonidas Fegaras.
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

Syntax Analysis – Part V Finish LR(0) Parsing Start on LR(1) Parsing EECS 483 – Lecture 8 University of Michigan Monday, October 2, 2006

Announcements/Reading Today 4.5, 4.7 Project 1 signup sheet available in class Grading Wed 1:30 – 3:00, Thu 1:30 – 3:30 If you have conflicts with both days, let me know and we’ll work something out Sign up for 10 min slot Project 2 – not ready yet, hopefully later this week

From Last Time: Shift-Reduce Parsing S  S + E | E E  num | (S) derivation stack input stream action (1+2+(3+4))+5 (1+2+(3+4))+5 shift (1+2+(3+4))+5 ( 1+2+(3+4))+5 shift (1+2+(3+4))+5 (1 +2+(3+4))+5 reduce E num (E+2+(3+4))+5 (E +2+(3+4))+5 reduce S E (S+2+(3+4))+5 (S +2+(3+4))+5 shift (S+2+(3+4))+5 (S+ 2+(3+4))+5 shift (S+2+(3+4))+5 (S+2 +(3+4))+5 reduce E num (S+E+(3+4))+5 (S+E +(3+4))+5 reduce S  S+E (S+(3+4))+5 (S +(3+4))+5 shift (S+(3+4))+5 (S+ (3+4))+5 shift (S+(3+4))+5 (S+( 3+4))+5 shift (S+(3+4))+5 (S+(3 +4))+5 reduce E num ...

From Last Time: LR Parsing Table Example We want to derive this in an algorithmic fashion Input terminal Non-terminals ( ) id , $ S L 1 s3 s2 g4 2 Sid Sid Sid Sid Sid 3 s3 s2 g7 g5 4 accept 5 s6 s8 6 S(L) S(L) S(L) S(L) S(L) 7 LS LS LS LS LS 8 s3 s2 g9 9 LL,S LL,S LL,S LL,S LL,S State

From Last Time: Start State and Closure 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   . Y  Add items for all the productions Y   to the closure of S: Y  . 

Closure S  (L) | id L  S | L,S S’  . S $ DFA start state S  . (L) S  . id DFA start state closure S’  . S $ Set of possible productions to be reduced next - Closure of a parser state, S: - Start with Closure(S) = S - Then for each item in S: - X   . Y  - Add items for all the productions Y   to the closure of S: Y  . 

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, then Goto(I, Y) = Closure( [X   Y .  ] ) S’  . S $ S  . (L) S  . id Goto(S, ‘(‘) Closure( { S  ( . L) } )

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

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

Class Problem E’  E E  E + T | T T  T * F | F F  (E) | id If I = { [E’  . E]}, then Closure(I) = ?? If I = { [E’  E . ], [E  E . + T] }, then Goto(I,+) = ??

Applying Reduce Actions S  ( . L) L  . S L  . L, S S  . (L) S  . id S  (L . ) L  L . , S L S’  . S $ S  . (L) S  . id ( S L  S . id id ( Grammar S  (L) | id L  S | L,S S  id . states causing reductions (dot has reached the end!) Pop RHS off stack, replace with LHS X (X  ), then rerun DFA (e.g., (x))

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

Full DFA 8 9 1 2 L  L , . S S  . (L) S  . id id id L  L,S . 3 ( S  ( . L) L  . S L  . L, S S  . (L) S  . id , 5 L S  (L . )L L  L . , S S ) 6 ( 4 S S  (L) . 7 S’  S . $ L  S . Grammar S  (L) | id L  S | L,S $ final state

Parsing Example ((a),b) S  (L) | id L  S | L,S Parsing Example ((a),b) derivation stack input action ((a),b)  1 ((a),b) shift, goto 3 ((a),b)  1(3 (a),b) shift, goto 3 ((a),b)  1(3(3 a),b) shift, goto 2 ((a),b)  1(3(3a2 ),b) reduce Sid ((S),b)  1(3(3(S7 ),b) reduce LS ((L),b)  1(3(3(L5 ),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,8 b) shift, goto 9 (L,b)  1(3L5,8b2 ) reduce Sid (L,S)  1(3L8,S9 ) reduce LL,S (L)  1(3L5 ) shift, goto 6 (L)  1(3L5)6 reduce S(L) S  1S4 $ done

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

Computed LR Parsing Table Input terminal Non-terminals ( ) id , $ S L 1 s3 s2 g4 2 Sid Sid Sid Sid Sid 3 s3 s2 g7 g5 4 accept 5 s6 s8 6 S(L) S(L) S(L) S(L) S(L) 7 LS LS LS LS LS 8 s3 s2 g9 9 LL,S LL,S LL,S LL,S LL,S State blue = shift red = reduce

LR(0) Summary LR(0) parsing recipe: This can be done automatically 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

Class Problem Generate the DFA for the following grammar S  E + S | E E  num

LR(0) Limitations An LR(0) machine only works if states with reduce actions have a single reduce action Always reduce regardless of lookahead With a more complex grammar, construction gives states with shift/reduce or reduce/reduce conflicts Need to use lookahead to choose shift/reduce reduce/reduce OK L  L , S . S  S . , L L  S , L . L  S . L  L , S .

A Non-LR(0) Grammar Grammar for addition of numbers S  S + E | E E  num Left-associative version is LR(0) Right-associative is not LR(0) as you saw with the previous class problem S  E + S | E

LR(0) Parsing Table 1 2 3 S  E + . S S  . E + S S  . E E  . num Grammar S  E + S | E E  num + E S’  . S $ S  .E + S S  . E E  .num S  E . +S S  E . E 4 num E  num . S num 5 S 7 S  E + S . S’  S . $ S’  S $ . $ Shift or reduce in state 2? num + $ E S s4 g2 g6 SE s3/SE SE

Solve Conflict With Lookahead 3 popular techniques for employing lookahead of 1 symbol with bottom-up parsing SLR – Simple LR LALR – LookAhead LR LR(1) Each as a different means of utilizing the lookahead Results in different processing capabilities

SLR Parsing SLR Parsing = Easy extension of LR(0) For each reduction X  , look at next symbol C Apply reduction only if C is in FOLLOW(X) SLR parsing table eliminates some conflicts Same as LR(0) table except reduction rows Adds reductions X   only in the columns of symbols in FOLLOW(X) Example: FOLLOW(S) = {$} num + $ E S s4 g2 g6 s3 SE Grammar S  E + S | E E  num

SLR Parsing Table Reductions do not fill entire rows as before Otherwise, same as LR(0) Grammar S  E + S | E E  num num + $ E S s4 g2 g6 s3 SE s4 g2 g5 Enum Enum SE+S s7 accept

Class Problem Consider: S  L = R S  R L  *R L  ident R  L Think of L as l-value, R as r-value, and * as a pointer dereference When you create the states in the SLR(1) DFA, 2 of the states are the following: S  L . = R R  L . S  R . Do you have any shift/reduce conflicts? (Not as easy as it looks)

LR(1) Parsing Get as much as possible out of 1 lookahead symbol parsing table LR(1) grammar = recognizable by a shift/reduce parser with 1 lookahead LR(1) parsing uses similar concepts as LR(0) Parser states = set of items LR(1) item = LR(0) item + lookahead symbol possibly following production LR(0) item: S  . S + E LR(1) item: S  . S + E , + Lookahead only has impact upon REDUCE operations, apply when lookahead = next input

LR(1) States LR(1) state = set of LR(1) items LR(1) item = (X   .  , y) Meaning:  already matched at top of the stack, next expect to see  y Shorthand notation (X   .  , {x1, ..., xn}) means: (X   .  , x1) . . . (X   .  , xn) Need to extend closure and goto operations S  S . + E +,$ S  S + . E num

LR(1) Closure LR(1) closure operation: Start with Closure(S) = S For each item in S: X   . Y  , z and for each production Y   , add the following item to the closure of S: Y  .  , FIRST(z) Repeat until nothing changes Similar to LR(0) closure, but also keeps track of lookahead symbol

LR(1) Start State Initial state: start with (S’  . S , $), then apply closure operation Example: sum grammar S’  S $ S  E + S | E E  num S’  . S , $ S  . E + S , $ S  . E , $ E  . num , +,$ closure S’  . S , $

LR(1) Goto Operation LR(1) goto operation = describes transitions between LR(1) states Algorithm: for a state S and a symbol Y (as before) If the item [X   . Y ] is in I, then Goto(I, Y) = Closure( [X   Y .  ] ) S1 Goto(S1, ‘+’) S2 S  E . + S , $ S  E . , $ Closure({S  E + . S , $}) Grammar: S’  S$ S  E + S | E E  num

Class Problem 1. Compute: Closure(I = {S  E + . S , $}) S’  S $ S  E + S | E E  num 2. Compute: Goto(I, num) 3. Compute: Goto(I, E)