 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.

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 Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
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.
Mooly Sagiv and Roman Manevich School of Computer Science
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)
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
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
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.
LR Parsing Compiler Baojian Hua
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
lec04-bottomupparser April 23, 2017 Bottom-Up Parsing
CS 321 Programming Languages and Compilers Bottom Up Parsing.
1 Compiler Construction Syntax Analysis Top-down parsing.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Bottom-Up Parsing A bottom-up parser creates the parse tree of the given input starting from leaves.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
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.
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. 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.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
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.
S YNTAX A NALYSIS O R P ARSING Lecture 08. B OTTOM -U P P ARSING A bottom-up parser creates the parse tree of the given input starting from leaves towards.
Lec04-bottomupparser 4/13/2018 LR Parsing.
Compiler design Bottom-up parsing Concepts
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
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
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
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
Presentation transcript:

 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 ” for constructing a Rightmost derivation in reverse.  The attractive reasons of LR parsers (1) LR parsers can be constructed for most programming languages. (2) LR parsing method is more general than LL parsing method. (3) LR parsers can detect syntactic errors as soon as possible. But,  it is too much work to implement an LR parser by hand for a typical programming-language grammar. =====>  Parser Generator

 The techniques for producing LR parsing tables  Simple LR(SLR) - LR(0) items, FOLLOW  Canonical LR(CLR) - LR(1) items  Lookahead LR(LALR) - ① LR(1) items ② LR(0), Lookahead LR Parsing [2/60]

 LR parser S 0 X 1 S 1 X 2 X m S m  Stack : S 0 X 1 S 1 X 2 X m S m, where S i : state and X i  V.  Configuration of an LR parser : 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

 LR Parsing Table (ACTION table + GOTO table)  The LR parsing algorithm ::= same as the shift-reduce parsing algorithm.  Four Actions :  shift  reduce  accept  error

1. ACTION[S m,a i ] = shift S ::= (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 $) 2. ACTION[S m,a i ] = reduce A  α and |α| = r ::= (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 $) 3. ACTION [S m,a i ] = accept, parsing is completed. 4. ACTION [S m,a i ] = error, the parser has discovered an error and calls an error recovery routine.

 G:1. LIST  LIST, ELEMENT 2. LIST  ELEMENT 3. ELEMENT  a  Parsing Table : where,sj means shift and stack state j, ri means reduce by production numbered i, acc means accept, and blank means error.

 Input :  = a, a  Parsing Configuration : initial configuration

 The method for constructing an LR parsing table from a grammar ① SLR ② LALR ③ CLR  Definition : an LR(0) item  a production with a dot at some position of the right side. ex) A  XYZ  P, [A .XYZ] [A  X.YZ] [A  XY.Z] [A  XYZ.]  mark symbol ::= the symbol after the dot if it exists.  kernel item ::= [A  α.  ] if α , A = S'.  closure item ::= [A . α ] : the result of performing the CLOSURE operation.  reduce item ::= [A  α.]

 [A  α.β] means that  an input string derivable from α has just been seen,  if next seeing an input string derivable from β, we may be able to reduce by the production A  αβ.  Definition : Augmented Grammar G = (V N, V T, P, S)  G' = (V N  {S'},V T, P  {S'  S}, S') where, S' is a new start symbol not in V N.  The purpose of this new starting production is to indicate to the parser when it should stop parsing and announce acceptance of the input. That is, acceptance occurs when and only when the parser is about to reduce by S'  S.

 If S  αAω  αβ 1 β 2 ω, then αβ 1 : viable prefix. ” viable prefix is a prefix of a right sentential form that does not continue past the right end of the handle of that sentential form. ”  We say item [A  β 1.β 2 ] is valid for a viable prefix if there is a derivation S  αAω  αβ 1 β 2 ω, “ In general, an item will be valid for many viable prefixes. ”  Canonical collection of LR(0) items ::= the set of valid items for each viable prefix that can appear on the stack of an LR parser.  Computation : CLOSURE & GOTO function rm *

 Definition :CLOSURE(I) = I ∪ {[B .  ] | [A  .B  ]  CLOSURE(I), B    P}  Meaning :  [A  .B  ] in CLOSURE(I) indicates that, at some point in the parsing process, we next expect to see a substring derivable from B as input. If B   is a production, we would also expect to see a substring from  at this point. For this reason, we also include [B .  ] in CLOSURE(I).

 Computing Algorithm: Algorithm CLOUSURE(I) ; begin CLOUSURE := I ; repeat if [A  .B  ]  CLOSURE and B    P then if [B .  ]  CLOSURE then CLOSURE := CLOSURE ∪ {[B .  ]} fi until no change end.

 예 1)  E'  E E  E + T | T T  T  F | F F  (E) | id  CLOSURE ({[E' .E]}) = {[E' .E], [E .E+T], [E .T], [T .T  F], [T .F], [F .(E)], [F .id]}.  CLOSURE({[E  E.+T]}) = { [E  E.+T] }.  예 2)  S  AS | b A  SA | a  CLOSURE({[S  A.S]}) = {[S  A.S], [S .AS], [S .b], [A .SA], [A .a]}.

GOTO(I,X)  Definition : GOTO(I,X) = CLOSURE({[A   X.  ] | [A  .X  ]  I}).  Meaning : If I is the set of items that are valid for some viable prefix , then GOTO(I,X) is the set of items that are valid for the viable prefix  X.  ex) I = {[E'  E.], [E  E.+T]} GOTO(I,+) = CLOSURE({[E  E+.T]}) = {[E  E+.T], [T .T  F], [T .F], [F .(E)], [F .id]}

C 0  C 0 = {CLOSURE ({[S' .S]})} ∪ {GOTO(I,X) | I ∈ C 0, X ∈ V}  We are now ready to give the algorithm to construct C 0, the canonical collection of sets of LR(0) items for an augmented grammar; the algorithm is the following:

 Construction algorithm of C 0. Algorithm Canonical_Collection; begin C 0 := { CLOSURE({[S' . S]}) }; repeat for I ∈ C 0 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 no change end.

 G : LIST  LIST, ELEMENT LIST  ELEMENT ELEMENT  a   Augmented Grammar  G' : ACCEPT  LIST LIST  LIST, ELEMENT LIST  ELEMENT ELEMENT  a

 Co :  I 0 : CLOSURE({[ACCEPT .LIST]}) = {[ACCEPT .LIST], [LIST .LIST,ELEMEMT], [LIST .ELEMENT], [ELEMENT .a]}.  GOTO(I 0,LIST) = I 1 = {[ACCEPT  LIST.], [LIST  LIST.,ELEMEMT]}.  GOTO(I 0,ELEMENT) = I 2 = {[LIST  ELEMENT.]}.  GOTO(I 0,a) = I 3 = {[ELEMENT  a.]}.  GOTO(I 1,,) = I 4 = {[LIST  LIST,.ELEMEMT], [ELEMENT .a]}.  GOTO(I 4,ELEMENT) = I 5 = {[LIST  LIST,ELEMEMT.]}.  GOTO(I 4,a) = I 3.

 Definition ::= a directed graph in which the nodes are labeled by the sets of items and the edges by grammar symbols. Ex)

 예 1) G : PR  b DL ; SL e (PR  P ) DL  d ; DL | d (DL  D ) SL  s ; SL | s (SL  S )  예 2) G : S  S + A | A A  (S) | a(S) | a  - 생성 규칙에 대 한 LR(0) 아이템 [A->.] 은 closure 아이템인 동시에 reduce 아이템이 된다. renaming

 Three methods  SLR(simple LR) - C 0, Follow  CLR(Canonical LR) - C 1  LALR(Lookahead LR) -  C 1  C 0. Lookahead  Parsing Table

 State i is constructed from I i, where I i ∈ C 0.  The size of parsing table depends on the number of states of C 0. But, |C 0 | << |C 1 |.  The size of parsing table : SLR: |V| x |C 0 | CLR: |V| x |C 1 | LALR: |V| x |C 0 |

::= The method constructing the SLR parsing table from the C 0.  Constructing Algorithm: C 0 = {I 0,I 1,I 2,...,I n } 1. ACTION[i,a] := "shift j" if [A  .a  ] ∈ I i and GOTO(I i,a) = I j. 2. ACTION[i,a] := "reduce A  α", for all a ∈ FOLLOW(A) if [A  .] ∈ I i. 3. ACTION[i,$] := "accept" if [S'  S.] ∈ I i. 4. GOTO[i,A] := j if GOTO(I i, A) = I j. 5. "error" for all undefined entries and initial state is i if [S' .S] ∈ I i.  reduce item 에 대해 FOLLOW 를 사용하여 resolve.

 G : 0. A  L (A : ACCEPT, L : LIST, E : ELEMENT) 1. L  L, E 2. L  E 3. E  a FOLLOW(A) = {$} FOLLOW(L) = {,,$} FOLLOW(E) = {,,$}

 Parsing Table :

 G: 1. S  L = R 2. S  R 4. L  id 3. L   R 5. R  L  C 0 :

 Consider I 2 :  ACTION[2,=] := “shift 6 ”  ACTION[2,=] := “reduce R  L ” ( ∵ = ∈ FOLLOW(R))  shift-reduce conflict  Not SLR(1)  Shift-reduce conflict vs. Reduce-reduce conflict