Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.

Slides:



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

Compiler Designs and Constructions
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.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
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 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 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.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
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.
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
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
LR Parsing Techniques Bottom-Up Parsing
LESSON 24.
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.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
–Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L –The grammar can have shift/reduce conflict or reduce/reduce conflict.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
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.
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.
Bottom Up Parsing PITT CS PITT CS Bottom Up Parsing  Also known as Shift-Reduce parsing  More powerful than top down.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
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.
LR Parsing. LR Parsers The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. left to right right-mostk lookhead scanning derivation(k.
Lec04-bottomupparser 4/13/2018 LR Parsing.
Compiler design Bottom-up parsing Concepts
UNIT - 3 SYNTAX ANALYSIS - II
Compiler Lecture Note, LR Parsing
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
Implementing a LR parser engine
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Syntax Analysis - 3 Chapter 4.
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Presentation transcript:

Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University

Introduction  Deterministic bottom-up parsing  Left to right scanning + Right parse  LR parser (2012-1) Compiler 2

Introduction  Contents  How LR parser works  How to construct parsing table from a given grammar  How to construct parsing table from an ambiguous grammar  How to implement parser (2012-1) Compiler 3

1. LR parser (1)  LR parser (2012-1) Compiler 4 SmSm Parsing Table Driver Routine a1a1 aiai anan $ : input stack

1. LR parser (2)  Parsing stack  State number + grammar symbol  S 0 X 1 S 1 X 2 X m S m  S 0 X 1 S 1 X 2 X m S m, where S i : state and X i  V.  Input buffer  Input string  a i  a i a i+1 a n $ (2012-1) Compiler 5

1. LR parser (3)  LR parsing table  Row: grammar symbols  Column: states  ACTION: reduce, shift, accept, error  GOTO: indicate next state on reduce action (2012-1) Compiler 6 symbol states ACTION tableGOTO table

1. LR parser (4)  Example 1  Grammar: 1. LIST  LIST, ELEMENT 2. LIST  ELEMENT 3. ELEMENT  a  Parsing table (2012-1) Compiler 7

1. LR parser (5)  Definition 8.1: Configuration  4 actions of the LR parser  Shift: ACTION[S m, a i ] = shift S (2012-1) Compiler 8 Configuration: representation for the current parsing status : stack contents + un-read input symbols S m a i (S 0 X 1 S 1 X m S m, a i a i+1 a n $) stack contents unscanned input (S 0 X 1 S 1  X m S m, a i a i+1  a n $)  (S 0 X 1 S 1  X m S m a i S, a i+1  a n $)

1. LR parser (5)  Reduce: ACTION[S m, a i ] = reduce A  , |  | = r  Accept: ACTION[S m, a i ] = accept  Error: ACTION[S m, a i ] = error (2012-1) Compiler 9 (S 0 X 1 S 1  X m S m, a i a i+1  a n $)  (S 0 X 1 S 1  X m-r S m-r, a i a i+1  a n $), GOTO(S m-r, A) = S  (S 0 X 1 S 1  X m-r S m-r AS, a i a i+1  a n $)

1. LR parser (6)  Advantages of the LR parser  Recognize the most programming languages  More general than LL parser  Detect errors during parsing (2012-1) Compiler 10

1. LR parser (7)  Example 2: a, a (2012-1) Compiler 11 0a,a$ s3 0 a 3,a$ r3 GOTO 2 0 ELEMENT 2,a$ r2 GOTO 1 0 LIST 1,a$ s4 0 LIST 1, 4a$ s3 0 LIST 1, 4 a 3$ r3 GOTO 5 0 LIST 1, 4 ELEMENT 5$ r1 GOTO 1 0 LIST 1$ accept initial configuration STACK INPUT ACTION

1. LR parser (8)  Construction of the LR parsing table  SLR: LR(0) items + FOLLOW  CLR: LR(1) items  LALR: LR(1) items or LR(0) items + LOOKAHEAD (2012-1) Compiler 12 ` SLR CLR LALR

2. Set of LR(0) items (1)  Definition 8.2: LR(0) item  Example 3: A  XYZ  LR(0) items: [A .XYZ], [A  X.YZ], [A  XY.Z], [A  XYZ.]  Definition 8.3: Augmented production (2012-1) Compiler 13 LR(0) item: productions whose rhs has the dot symbol When G = (V N, V T, P, S), G’ = (V N  {S’}, V T, P  {S’  S}, S’) is augmented grammar S’: new start state S’  S: augmented production

2. Set of LR(0) items (2)  Definition 8.4: Mark symbol  Definition 8.5: kernel item, closure item, reduce item (2012-1) Compiler 14 A symbol next to the dot symbol in LR(0) item Kernel item: [A  .  ] when   , and [S’ .S] Closure item: [A .  ] Reduce item: [A  .]

2. Set of LR(0) items (3)  [A  .  ]  Until now, the input strings derivable from  are seen (processed)  If the strings derivable from  are seen (processed), we can reduce using A    Definition 8.6: viable prefix  Not handle  Symbols in the parsing stack (2012-1) Compiler 15  1 in S  *  A    1  2 

2. Set of LR(0) items (4)  Definition 8.7: valid  When parsing stack content is  1, choose shift or reduce   2   : shift  2 =  : reduce (2012-1) Compiler 16 If S  *  A    1  2  and   V T *, LR(0) item [A   1.  2] is valid for the viable prefix  1

2. Set of LR(0) items (5)  Set of valid LR(0) items for all viable prefix  LR parsing table  Add production S’  S  Get set of LR(0) items by marking the productions from the above production: same set  a state  Definition 8.8: CLOSURE, set of items (2012-1) Compiler 17 CLOSURE(I) = I  {[B .  ] | [A  .B  ]  CLOSURE(I), B    P}

2. Set of LR(0) items (6)  Algorithm for CLOSURE(I) (2012-1) Compiler 18 Algorithm CLOSURE(I); begin CLOSURE := I; repeat if [A  .B  ] and B    P then if [B .  ]  CLOSURE then CLOSURE := CLOSURE  {[B .  ]} fi fi until no change end

2. Set of LR(0) items (7)  Example 5:  CLOSURE: all LR(0) items to be seen on a state  GOTO: state transition (2012-1) Compiler 19 S’  G G  E = E | f E  E + T | T T  T * f | f CLOSURE({[S’ .G]}) = { [S’ .G], [G .E=E], [G .f] [E .E+T], [E .T], [T .T*f], [T .f] } CLOSURE({[E  E.+T]}) = { [E  E.+T] }

2. Set of LR(0) items (8)  Definition 8.9: GOTO function  Function to get the next state by parsing mark symbol (2012-1) Compiler 20 GOTO(I, X) = CLOSURE({[A   X.  ] | A  .X  ]  I})

2. Set of LR(0) items (9)  Example 6:  I = {[G  E.=E], [E  E. + T]} GOTO(I, +) = CLOSURE({[E  E+.T]}) = {[E  E+.T], [T .T*f], [T .f]}  I = {[E .T], [T .T*f], [T .f]} GOTO(I, T) = CLOSURE({[E  T.], [T  T.*f]}) = {[E  T.], [T  T.*f]} (2012-1) Compiler 21

2. Set of LR(0) items (10)  Canonical collection C 0  GOTO(I 0, X) = I 1  GOTO(I 1, X) = I 2  …  GOTO(I n-1, X) = I n  C 0 = {I 0, I 1, … I n } (2012-1) Compiler 22 C 0 = {CLOSURE({[S’ .S]})}  {GOTO(I,X) | I  C 0, X  V}

(2012-1) Compiler 23 Algorithm Canonical_Collection; begin C 0 := {CLOSURE({[S’ .S]})}; repeat for I  C0 do Closure := CLOSURE(I); for each X  MARK SYMBOL of Closure do J := GOTO(I, X); if  J i = J then GOTO(I, X) := J i else GOTO(I, X) := J; C 0 := C 0  {J}; fi end for until C 0 doesn’t change end.

2. Set of LR(0) items (12)  Example 7: C0 construction (2012-1) Compiler 24 LIST  LIST, ELEMENT LIST  ELEMENT ELEMENT  a ACCEPT  LIST LIST  LIST, ELEMENT LIST  ELEMENT ELEMENT  a I0: CLOSURE({[ACCEPT .LIST]}) = {[ACCEPT .LIST], [LIST .LIST,ELEMENT], [LIST .ELEMENT], [ELEMENT .a]} GOTO(I0, LIST) = I1 = {[ACCEPT  LIST.], [LIST  LIST., ELEMENT]} GOTO(I0, ELEMENT) = I2 = {[LIST  ELEMENT.]} GOTO(I0, a) = I3 = {[ELEMENT  a.]} GOTO(I1,,) = I4 = {[LIST  LIST,.ELEMENT], [ELEMENT .a]} GOTO(I4, ELEMENT) = I5 = {[LIST  LIST, ELEMENT.]} GOTO(I4, a) = I3

2. Set of LR(0) items (13)  GOTO graph (2012-1) Compiler 25 I1I1 I2I2 I3I3 I0I0 I4I4 I5I5 LIST ELEMENT a a,

2. Set of LR(0) items (14)  Example 8: (2012-1) Compiler 26 P  b D ; S e D  d ; D | d S  s ; S | s [P '  P.] I1I1 [P ' .P] [P .bD;Se] I0I0 [P  bD.;Se] I3I3 [P  bD;.Se] [S .s;S] [S .s] I5I5 [P  bD;S.e] I7I7 [P  bD;Se.] I8I8 [P  b.D;Se] [D .d;D] [D .d] I2I2 [S  s.;S] [S  s.] I8I8 [S .s;.S] [S .s;S] [S .s] I 11 [D  d.;D] [D  d.] I4I4 [D  d;.D] [D .d;D] [D .d] I6I6 [D  d;D.] I9I9 [S  s;S.] I 12 S e s S D ; ; P b D d d s ;

3. SLR parsing table construction (1)  SLR  simple LR  Parsing table construction by using C 0 and FOLLOW  Each item set in C 0 is a state: |C 0 |  (|V|+1) (2012-1) Compiler 27

3. SLR parsing table construction (2)  SLR parsing table construction  If [A  .a  ]  I i and GOTO(I i, a) = I j then M[i, a] := shift j  If [A  .]  I i then for each a  FOLLOW(A) do M[i, a] := reduce A    If [S’  S.]  I i then M[i, $] := accept  If [A  .B  ]  I i and GOTO(I i, B) = I j then M[i, B] := j;  other parsing table entries are error  A state including [S’  S] is a start state (2012-1) Compiler 28

3. SLR parsing table construction (3)  Feature  Reduce action on the FOLLOW symbol of the lhs of the reduce item production  Example 10: (2012-1) Compiler 29 E  E+T | T T  T*F | F F  (E) | id 0. E’  E 1.E  E+T 2.E  T 3.T  T*F 4.T  F 5.F  (E) 6.F  id FOLLOW(E) = {$, +, )} FOLLOW(T) = {*, +, ), $} FOLLOW(F) = {*, +, ), $}

(2012-1) Compiler 30

(2012-1) Compiler 31

3. SLR parsing table construction (4)  Shift-reduce conflict  [A  .], [B  .a  ]  If a  FOLLOW(A)?  Reduce-reduce conflict  [A  .], [B  .]  If a  FOLLOW(A) and a  FOLLOW(B)?  Reason of the conflict: ambiguous grammar (2012-1) Compiler 32

3. SLR parsing table construction (5)  Solution  Determine the reduce action using the exact context  CLR, LALR (2012-1) Compiler 33

4. CLR parsing table construction (1)  FOLLOW  Set of terminal symbols which can appear next to the nonterminal in all sentential forms  Lookahead  Set of terminal symbols which can appear next to the lhs of an item on the specific state (2012-1) Compiler 34 FOLLOW set Lookahead set

4. CLR parsing table construction (2)  LR(1) item: LR(0) item + lookahead information  Definition 8.10: LR(1) item (2012-1) Compiler 35 LR(1) item: [A  . , a], where A    P and a  V T  {$} 1.Core: A  .  2.a: lookahead, reduce action on this symbol

4. CLR parsing table construction (2)  Definition 8.11: CLOSURE in LR(1) item  Example 11: (2012-1) Compiler 36 CLOSURE(I) = I  {[B . , b] | [A  .B , a]  CLOSURE(I), B    P, b  FIRST(  a)} CLOSURE({[S’ .S, $]}) = {[S’ .S, $], [S .CC, $], [C .cC, c/d], [C .d, c/d]} S’  S S  CC C  cC C  d

4. CLR parsing table construction (3)  CLR parsing table construction  If [A  .a , u]  I i and GOTO(I i, a) = I j then M[i,a] := shift j  If [A  ., u]  I i then M[i, u] := reduce A    If [S’  S., $]  I i then M[i, $] := accept  If [A  .B , u]  I i and GOTO(I i, B) = I j then M[i, B] := j; (2012-1) Compiler 37

4. CLR parsing table construction (4)  Canonical LR(1) parsing table  LR(1) grammar: no multi-defined entry  Example 12: (2012-1) Compiler S’  S 1.S  CC 2.C  cC 3.C  d S  CC C  cC | d

(2012-1) Compiler 39 I0I0 [S .S,$] [S .CC,$] [C .cC,c/d] [C .d,c/d] [C  d.,c/d.] I4I4 [S'  S.,$] I1I1 [C  cC.,c/d.] I8I8 [C  c.C,c/d] [C .cC,c/d] [C .d,c/d] I3I3 [C  c.C,$] [C .cC,$] [C .d,$] I6I6 [C  d.,$] I7I7 [S'  CC.,$] I5I5 [C  cC.,$] I9I9 d c d C S C C c d c d c C [S  C.C,$] [C .cC,$] [C .d,$] I2I2

(2012-1) Compiler 40