Syntax Analysis - LR(1) and LALR(1) Parsing

Slides:



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

Compiler Designs and Constructions
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
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)
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction LR Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
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.
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.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
LESSON 24.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
11 Outline  6.0 Introduction  6.1 Shift-Reduce Parsers  6.2 LR Parsers  6.3 LR(1) Parsing  6.4 SLR(1)Parsing  6.5 LALR(1)  6.6 Calling Semantic.
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.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
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.
Announcements/Reading
Lexical Analysis - Scanner- Contd Computer Science Rensselaer Polytechnic Compiler Design Lecture 3(01/21/98)
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
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.
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.
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.
Announcements/Reading
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
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
Canonical LR Parsing Tables
Syntax Analysis Part II
Implementing a LR parser engine
Lecture (From slides by G. Necula & R. Bodik)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Syntax Analysis - Parsing
Syntax Analysis - 3 Chapter 4.
Kanat Bolazar February 16, 2010
Compiler Construction
Chapter 4. Syntax Analysis (2)
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
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:

Syntax Analysis - LR(1) and LALR(1) Parsing 66.648 Compiler Design Lecture (02/9/98) Computer Science Rensselaer Polytechnic

Lecture Outline LR(1)Parsing Algorithm Examples LALR(1) Parsing Algorithm Administration

Example 1) S --> P 2) P --> a P a 3) P --> b P b 4) P --> epsilon As we have seen (did we see) in the last class, this grammar leads to shift/reduce conflicts in LR(0) grammar.

Yet Another Example Consider a grammar to generate all nested parentheses 1) S--> P 2) P --> ( P ) 3) P --> epsilon In canonical state consisting of items { [S-->.P],[P--> .( P )],[P-->,]} there will be shift reduce conflict. can you find other canonical states in which shift/reduce conflict occurs.

LR(1) Item An LR(1) item has the form [A--> alpha . beta, a], where a is the lookahead of the item and it is a terminal symbol (including a $). LR(1) parser uses the lookahead to improve the precision in invoking the reduce operation. An LR(1) item [A--> alpha.,a] invokes a reduce action only when the next input symbol is a. How do we define closure of an item?

Closure of LR(1) item Let I be the set of LR(1) items. Then, closure(I) is the set of items that can be constructed from I as follows: 1. Every Item in I is also an item in closure(I) 2. If [A--> alpha . B beta,a] is in I and B--> gamma is a production, then add item [B--> .gamma,b] to closure(I),if it is not already a member. What is b? b is the first(beta a).

FIRST - revisited In the example grammar 1, first(P) = { a,b,epsilon}, First(S)={a,b,epsilon,$) In the example grammar 2, first(X) = { (,epsilon} = first(S) first of terminal symbols can be defined easily. e.g., first( ( ) = ( first(X1…Xk) can also be defined easily

Items and Closure Contd Example 1: closure of item [S--> .P,$] [S --> .P, $], [P-->.a P a,$], [P-->.b P b, $], [P-->.,$] Example 2: closure of item [X--> (. X ), $] [X --> (. X ), $], [X--> .(X),) ], [X --> .( ), ()]

GOTO operation Let I be a set of items, and X be a grammar symbol (terminal/nonterminal). Then GOTO(I,X) = closure ( { [ A--> alpha X . beta, a] | [ A --> alpha . X beta, a] is in I) Canonical set of LR(1) items This is similar to LR(0) case. Enumerate possible states of LR(1) parser. Each state is a canonical set of LR(1) items.

Canonical States 1) Start with the canonical set by performing a closure ( [S’--> .S, $] ). 2) If I is a canonical set and X is a grammar symbol such that I’ = goto(I,X) is nonempty, then make I’ a new canonical set. Repeat until no more canonical sets can be added.

Example 1 state 0: { [S--> .P, $], [ P--> . a P a, $], [P--> .b P b , $], [P--> .,$] } state 1: { [ S --> P.,$] } state 2: { [P--> a . P a,$], [P --> . a P a, a], [P --> .b P b, a], [P --> ., a] } state 3: { [ P --> b. P b,$], [P --> .a P a , b], [P--> .b P b, b], [ P--> .,b] } state 4:{ [P --> a P .a, $]} state 5: { [ P --> a . P a, a], [P --> .a P a, a], [P--> .b P b, a], [P--> ., a] }

Example 1 - Contd Enumerate the rest of the states …

Example 2 S0: { [S--> . X, $], [ X --> . ( X ), $], [ X--> ., $]} S1: { [ S--> X.,$]} S2: { [ X --> ( . X ),$], [X--> . ( X ), ) ], [ X--> ., )] S3: { [ X --> ( X . ), $] } S4: { [ X --> ( . X ), )], [ X--> . ( X ) , )], [X--> ., )] } S5:{ [ X --> ( X ). $] } S6: { [ X --> ( X .), ) ] } S7: { [ X --> ( X ). , ) ] }

Finite State Machine Draw the FSA. The major difference is that transitions can be both terminal and nonterminal symbols.

Actions Associated with LR(1) States If a state contains an item of the form [A--> beta . a], then state prompts a reduce action provided the next input symbol is a. If a state contains A--> [alpha . a delta, b] then the state prompts the parser to perform a shift action when the input symbol is a. If a state contains [S’--> S.,$] and there are no more input symbols left, then the parser is prompted to accept. Else an error message is prompted.

Parsing Table state Input symbol goto ( ) $ X

Parsing Table Contd si means shift the input symbol and goto state I. rj means reduce by jth production. Note that we are not storing all the items in the state in our table. example: ( (( ) ) ) $

LR(1) Grammars A Grammar is said to be LR(1) if there is no conflict present in any of its LR(1) canonical sets I.e. if no state prompts the parser to perform more than one action on some input symbol. Most programming languages can be described by LR(1), but involves a large number of states. The number of states can be reduced by appropriately merging certain states. This is what is done in LALR grammar (in YACC)

LALR Grammar LA LR - Look Ahead LR grammar, Core of a LR(1) Canonical set Th core of an LR(1) canonical set is the set of first part of all the items in that canonical set. e.g. in S2 = {[X--> (.X),$],[X-->.(X),)],[X-->.,)] has cores ={ X--> (.X),X-->.(X), X-->.} in S4 = {[X-->(.X),)], [X--> .(X),)],[X-->.,)] have the same cores as above

LALR(1) PARSING TABLE Basic LALR(1) Parsing: Merge LR(1) states with the same core:

Comments and Feedback Project 2 is out. Project 1 is due to-night. Please keep reading chapter 4 and understand the material. Work out as many exercises as you can.