Principles of Programming Languages

Similar documents
Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Syn S t yn a t x a Ana x lysi y s si 1

S Y N T A X A N A L Y S I S LR

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

UNIT-III BOTTOM-UP PARSING

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

LALR Parsing. What Yacc and most compilers employ.

MODULE 14 SLR PARSER LR(0) ITEMS

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

LR Parsing Techniques

Context-free grammars

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Compiler Construction: Parsing

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

LR Parsers. Aditi Raste, CCOEW

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Compiler Construction 2016/2017 Syntax Analysis

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compilers. Bottom-up Parsing. (original slides by Sam

SLR parsers. LR(0) items

LR Parsing Techniques

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Bottom-Up Parsing LR Parsing

LR Parsing - The Items

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

CS308 Compiler Principles Syntax Analyzer Li Jiang

Bottom-Up Parsing. Lecture 11-12

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

Acknowledgements. The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal

Syntax Analyzer --- Parser

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Bottom-Up Parsing. Lecture 11-12

Lecture Bottom-Up Parsing

Lexical and Syntax Analysis. Bottom-Up Parsing

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

LR Parsing LALR Parser Generators

Concepts Introduced in Chapter 4

In One Slide. Outline. LR Parsing. Table Construction

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

UNIT III & IV. Bottom up parsing


Monday, September 13, Parsers

LR Parsing LALR Parser Generators

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Downloaded from Page 1. LR Parsing

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

shift-reduce parsing

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

CS 4120 Introduction to Compilers

Wednesday, August 31, Parsers

CSE302: Compiler Design

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Conflicts in LR Parsing and More LR Parsing Types

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Bottom-Up Parsing II. Lecture 8

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

4. Lexical and Syntax Analysis

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

4. Lexical and Syntax Analysis


COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

Syntax Analysis Part I

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

LR Parsing. Table Construction

Syntactic Analysis. Chapter 4. Compiler Construction Syntactic Analysis 1

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Bottom-up Parser. Jungsik Choi

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Exercises II. Exercise: Lexical Analysis

More Bottom-Up Parsing

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

3. Parsing. Oscar Nierstrasz

Chapter 4: LR Parsing

1 Recursive Descent (LL(1) grammars)

CS415 Compilers. LR Parsing & Error Recovery

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

The role of the parser

LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

Principles of Programming Languages

VIVA QUESTIONS WITH ANSWERS

How do LL(1) Parsers Build Syntax Trees?

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Transcription:

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 8! Bo;om- Up Parsing Shi?- Reduce LR(0) automata and SLR parsing LR(1), LALR Ambiguous grammars Error recovery

Shift-Reduce Parsing Grammar: S a A B e A A b c b B d Reducing a sentence: a b b c d e a A b c d e a A d e a A B e S Shi?- reduce corresponds to a rightmost derivalon: S rm a A B e rm a A d e rm a A b c d e rm a b b c d e S A A A A A A B A B a b b c d e a b b c d e a b b c d e a b b c d 2 e

Handles A handle is a substring of grammar symbols in a right-sentential form that matches a right-hand side of a production Grammar: S a A B e A A b c b B d a b b c d e a A b c d e a A d e a A B e S Handle a b b c d e a A b c d e a A A e? NOT a handle, because further reductions will fail (result is not a sentential form) 3

Stack Implementation of Shift-Reduce Parsing Grammar: E E + E E E * E E ( E ) E id Found handles to reduce Stack id E E+ E+id E+E E+E* E+E*id E+E*E E+E E Input id+id*id +id*id +id*id id*id *id *id id Action shift reduce E id shift shift reduce E id shift (or reduce?) shift reduce E id reduce E E * E reduce E E + E accept How to resolve conflicts? 4

Conflicts Shift-reduce and reduce-reduce conflicts are caused by The limitations of the LR parsing method (even when the grammar is unambiguous) Ambiguity of the grammar 5

Shift-Reduce Parsing: Shift-Reduce Conflicts Ambiguous grammar: S if E then S if E then S else S other Stack if E then S Input else Action shift or reduce? Resolve in favor of shift, so else matches closest if 6

Shift-Reduce Parsing: Reduce-Reduce Conflicts Grammar: C A B A a B a Stack a Input aa a Action shift reduce A a or B a? Resolve in favor of reducing A a, otherwise we re stuck! 7

LR(k) Parsers: Use a DFA for Shift/ Reduce Decisions 1 start 0 C a Grammar: S C C A B A a B a A 2 3 B a 4 5 goto(i 0,C) State I 0 : S C C A B A a State I 1 : S C goto(i 0,A) State I 2 : C A B B a State I 4 : C A B goto(i 2,B) goto(i 2,a) Can only reduce A a (not B a) goto(i 0,a) State I 3 : A a State I 5 : B a 8

DFA for Shift/Reduce Decisions Grammar: S C C A B A a B a State I 0 : S C C A B A a goto(i 0,a) The states of the DFA are used to determine if a handle is on top of the stack State I 3 : A a Stack 0 0 0 a 3 0 A 2 0 A 2 a 5 0 A 2 B 4 0 C 1 Input aa aa a a Action start in state 0 shift (and goto state 3) reduce A a (goto 2) shift (goto 5) reduce B a (goto 4) reduce C AB (goto 1) accept (S C) 9

DFA for Shift/Reduce Decisions Grammar: S C C A B A a B a State I 0 : S C C A B A a goto(i 0,A) The states of the DFA are used to determine if a handle is on top of the stack State I 2 : C A B B a Stack 0 0 0 a 3 0 A 2 0 A 2 a 5 0 A 2 B 4 0 C 1 Input aa aa a a Action start in state 0 shift (and goto state 3) reduce A a (goto 2) shift (goto 5) reduce B a (goto 4) reduce C AB (goto 1) accept (S C) 10

DFA for Shift/Reduce Decisions Grammar: S C C A B A a B a State I 2 : C A B B a goto(i 2,a) The states of the DFA are used to determine if a handle is on top of the stack State I 5 : B a Stack 0 0 0 a 3 0 A 2 0 A 2 a 5 0 A 2 B 4 0 C 1 Input aa aa a a Action start in state 0 shift (and goto state 3) reduce A a (goto 2) shift (goto 5) reduce B a (goto 4) reduce C AB (goto 1) accept (S C) 11

DFA for Shift/Reduce Decisions Grammar: S C C A B A a B a State I 2 : C A B B a goto(i 2,B) State I 4 : C A B The states of the DFA are used to determine if a handle is on top of the stack Stack 0 0 0 a 3 0 A 2 0 A 2 a 5 0 A 2 B 4 0 C 1 Input aa aa a a Action start in state 0 shift (and goto state 3) reduce A a (goto 2) shift (goto 5) reduce B a (goto 4) reduce C AB (goto 1) accept (S C) 12

DFA for Shift/Reduce Decisions Grammar: S C C A B A a B a State I 0 : S C C A B A a The states of the DFA are used to determine if a handle is on top of the stack goto(i 0,C) State I 1 : S C Stack 0 0 0 a 3 0 A 2 0 A 2 a 5 0 A 2 B 4 0 C 1 Input aa aa a a Action start in state 0 shift (and goto state 3) reduce A a (goto 2) shift (goto 5) reduce B a (goto 4) reduce C AB (goto 1) accept (S C) 13

DFA for Shift/Reduce Decisions Grammar: S C C A B A a B a State I 0 : S C C A B A a The states of the DFA are used to determine if a handle is on top of the stack goto(i 0,C) State I 1 : S C Stack 0 0 0 a 3 0 A 2 0 A 2 a 5 0 A 2 B 4 0 C 1 Input aa aa a a Action start in state 0 shift (and goto state 3) reduce A a (goto 2) shift (goto 5) reduce B a (goto 4) reduce C AB (goto 1) accept (S C) 14

Model of an LR Parser input a 1 a 2 a i a n stack s m X m s m-1 LR Parsing Program (driver) output X m-1 s 0 action shift reduce accept error goto DFA Constructed with LR(0) method, SLR method, LR(1) method, or LALR(1) method 15

LR Parsing (Driver) Configuration ( = LR parser state): (s 0 X 1 s 1 X 2 s 2 X m s m, a i a i+1 a n ) stack input If action[s m,a i ] = shift s then push a i, push s, and advance input: (s 0 X 1 s 1 X 2 s 2 X m s m a i s, a i+1 a n ) If action[s m,a i ] = reduce A β and goto[s m-r,a] = s with r= β then pop 2r symbols, push A, and push s: (s 0 X 1 s 1 X 2 s 2 X m-r s m-r A s, a i a i+1 a n ) If action[s m,a i ] = accept then stop If action[s m,a i ] = error then attempt recovery 16

Example LR(0) Parsing Table State I 0 : C C C A B A a State I 1 : C C State I 2 : C A B B a State I 3 : A a State I 4 : C A B State I 5 : B a Shift & goto 3 state action a goto C A B start 0 C a A 1 2 3 B a 4 5 Reduce by production #2 0 1 2 3 4 5 s3 s5 r3 r2 r4 acc r3 r2 r4 1 2 4 Grammar: 1. C C 2. C A B 3. A a 4. B a 17

Another Example LR Parse Table Grammar: 1. E E + T 2. E T 3. T T * F 4. T F 5. F ( E ) 6. F id state 0 1 2 3 action id + * ( ) s5 s4 s6 acc r2 s7 r2 r2 r4 r4 r4 r4 goto E T F 1 2 3 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 Shift & goto 5 7 s5 s4 10 8 s6 s11 Reduce by production #1 9 10 11 r1 s7 r1 r1 r3 r3 r3 r3 r5 r5 r5 r5 18

Example LR Shift-Reduce Parsing Grammar: 1. E E + T 2. E T 3. T T * F 4. T F 5. F ( E ) 6. F id Stack 0 0 id 5 0 F 3 0 T 2 0 T 2 * 7 0 T 2 * 7 id 5 0 T 2 * 7 F 10 0 T 2 0 E 1 0 E 1 + 6 0 E 1 + 6 id 5 0 E 1 + 6 F 3 0 E 1 + 6 T 9 0 E 1 Input id*id+id *id+id *id+id *id+id id+id +id +id +id +id id Action shift 5 reduce 6 goto 3 reduce 4 goto 2 shift 7 shift 5 reduce 6 goto 10 reduce 3 goto 2 reduce 2 goto 1 shift 6 shift 5 reduce 6 goto 3 reduce 4 goto 9 reduce 1 goto 1 accept 19

LR(0) Items of a Grammar An LR(0) item of a grammar G is a production of G with a at some position of the right-hand side Thus, a production A X Y Z has four items: [A X Y Z] [A X Y Z] [A X Y Z] [A X Y Z ] Note that production A ε has one item [A ] 20

The Closure Operation for LR(0) Items 1. Start with closure(i) = I 2. If [A α Bβ] closure(i) then for each production B γ in the grammar, add the item [B γ] to I if not already in I 3. Repeat 2 until no new items can be added 21

The Closure Operation (Example) closure({[e E]}) = { [E E] } Grammar: E E + T T T T * F F F ( E ) F id Add [E γ] { [E E] [E E + T] [E T] } Add [T γ] { [E E] [E E + T] [E T] [T T * F] [T F] } Add [F γ] { [E E] [E E + T] [E T] [T T * F] [T F] [F ( E )] [F id] } 22

The Goto Operation for LR(0) Items 1. For each item [A α Xβ] I, add the set of items closure({[a αx β]}) to goto(i,x) if not already there 2. Repeat step 1 until no more items can be added to goto(i,x) 3. Intuitively, goto(i,x) is the set of items that are valid for the viable prefix γx when I is the set of items that are valid for γ 23

The Goto Operation (Example 1) Suppose I = { [E E] [E E + T] [E T] [T T * F] [T F] [F ( E )] [F id] } Then goto(i,e) = closure({[e E, E E + T]}) = { [E E ] [E E + T] } Grammar: E E + T T T T * F F F ( E ) F id 24

The Goto Operation (Example 2) Suppose I = { [E E ], [E E + T] } Then goto(i,+) = closure({[e E + T]}) = { [E E + T] [T T * F] [T F] [F ( E )] [F id] } Grammar: E E + T T T T * F F F ( E ) F id 25

Constructing the set of LR(0) Items of a Grammar 1. The grammar is augmented with a new start symbol S and production S S 2. Initially, set C = closure({[s S]}) (this is the start state of the DFA) 3. For each set of items I C and each grammar symbol X (N T) such that goto(i,x) C and goto(i,x), add the set of items goto(i,x) to C 4. Repeat 3 until no more sets can be added to C 26

Example Grammar and LR(0) Items Augmented grammar: 1. C C 2. C A B 3. A a 4. B a start I 0 = closure({[c C]}) I 1 = goto(i 0,C) = closure({[c C ]}) goto(i 0,C) State I 0 : C C C A B A a State I 1 : C C goto(i 0,A) final State I 2 : C A B B a State I 4 : C A B goto(i 2,B) goto(i 2,a) goto(i 0,a) State I 3 : A a State I 5 : B a 27

SLR Grammars SLR (Simple LR): SLR is a simple extension of LR(0) shift-reduce parsing SLR eliminates some conflicts by populating the parsing table with reductions A α on symbols in FOLLOW(A) S E E id + E E id State I 0 : S E E id + E E id State I 2 : Shi? on + E id + E goto(i 0,id) E id goto(i 3,+) FOLLOW(E)={} thus reduce on 28

SLR Parsing Table Reductions do not fill entire rows Otherwise the same as LR(0) id + E 1. S E 2. E id + E 3. E id 0 1 2 s2 s3 acc r3 1 3 s2 4 Shi? on + 4 r2 FOLLOW(E)={} thus reduce on 29

SLR Parsing An LR(0) state is a set of LR(0) items An LR(0) item is a production with a (dot) in the right-hand side Build the LR(0) DFA by Closure operation to construct LR(0) items Goto operation to determine transitions Construct the SLR parsing table from the DFA LR parser program uses the SLR parsing table to determine shift/reduce operations 30

Constructing SLR Parsing Tables 1. Augment the grammar with S S 2. Construct the set C={I 0,I 1,,I n } of LR(0) items 3. If [A α aβ] I i and goto(i i,a)=i j then set action[i,a]=shift j 4. If [A α ] I i then set action[i,a]=reduce A α for all a FOLLOW(A) (apply only if A S ) 5. If [S S ] is in I i then set action[i,]=accept 6. If goto(i i,a)=i j then set goto[i,a]=j 7. Repeat 3-6 until no more entries added 8. The initial state i is the I i holding item [S S] 31

Example SLR Parsing Table State I 0 : C C C A B A a State I 1 : C C State I 2 : C A B B a State I 3 : A a State I 4 : C A B State I 5 : B a start 0 C a A 1 2 3 B a 4 5 state 0 1 2 3 4 5 action a s3 acc s5 r3 r2 r4 goto C A B 1 2 4 Grammar: 1. C C 2. C A B 3. A a 4. B a FOLLOW(A) = {a} FOLLOW(C) = {} FOLLOW(B) = {} 32

SLR, Ambiguity, and Conflicts SLR grammars are unambiguous But not every unambiguous grammar is SLR Consider for example the unambiguous grammar 1. S L = R 2. S R 3. L * R 4. L id 5. R L I 0 : S S S L=R S R L *R L id R L I 1 : S S I 2 : S L =R R L I 3 : S R I 4 : L * R R L L *R L id I 5 : L id action[2,=]=s6 Conflict: has no SLR action[2,=]=r5 no parsing table! I 6 : S L= R R L L *R L id I 7 : L *R I 8 : R L I 9 : S L=R 33

LR(1) Grammars SLR too simple LR(1) parsing uses lookahead to avoid unnecessary conflicts in parsing table LR(1) item = LR(0) item + lookahead LR(0) item: [A α β] LR(1) item: [A α β, a] 34

SLR Versus LR(1) Split the SLR states by adding LR(1) lookahead Unambiguous grammar 1. S L = R 2. S R 3. L * R 4. L id 5. R L S L =R action[2,=]=s6 I 2 : S L =R R L split R L lookahead= action[2,]=r5 Should not reduce on =, because no right-sentential form begins with R= 35

LR(1) Items An LR(1) item [A α β, a] contains a lookahead terminal a, meaning α already on top of the stack, expect to parse βa For items of the form [A α, a] the lookahead a is used to reduce A α only if the next symbol of the input is a. Clearly a is in FOLLOW(A), but not all of them must appear as lookahead. For items of the form [A α β, a] with β ε the lookahead has no effect 36

The Closure Operation for LR(1) Items 1. Start with closure(i) = I 2. If [A α Bβ, a] closure(i) then for each production B γ in the grammar and each terminal b FIRST(βa), add the item [B γ, b] to I if not already in I 3. Repeat 2 until no new items can be added 37

The Goto Operation for LR(1) Items 1. For each item [A α Xβ, a] I, add the set of items closure({[a αx β, a]}) to goto(i,x) if not already there 2. Repeat step 1 until no more items can be added to goto(i,x) 38

Constructing the set of LR(1) Items of a Grammar 1. Augment the grammar with a new start symbol S and production S S 2. Initially, set C = closure({[s S, ]}) (this is the start state of the DFA) 3. For each set of items I C and each grammar symbol X (N T) such that goto(i,x) C and goto(i,x), add the set of items goto(i,x) to C 4. Repeat 3 until no more sets can be added to C 39

Example Grammar and LR(1) Items Unambiguous LR(1) grammar: S L = R S R L * R L id R L Augment with S S LR(1) items (next slide) 40

I 0 : [S S, ] goto(i 0,S)=I 1 [S L=R, ] goto(i 0,L)=I 2 [S R, ] goto(i 0,R)=I 3 [L *R, =/] goto(i 0,*)=I 4 [L id, =/] goto(i 0,id)=I 5 [R L, ] goto(i 0,L)=I 2 I 6 : I 7 : [S L= R, ] goto(i 6,R)=I 9 [R L, ] goto(i 6,L)=I 10 [L *R, ] goto(i 6,*)=I 11 [L id, ] goto(i 6,id)=I 12 [L *R, =/] I 1 : [S S, ] I 8 : [R L, =/] I 2 : I 3 : I 4 : I 5 : [S L =R, ] goto(i 0,=)=I 6 [R L, ] [S R, ] [L * R, =/] goto(i 4,R)=I 7 [R L, =/] goto(i 4,L)=I 8 [L *R, =/] goto(i 4,*)=I 4 [L id, =/] goto(i 4,id)=I 5 [L id, =/] I 9 : I 10 : I 11 : I 12 : I 13 : [S L=R, ] [R L, ] [L * R, ] goto(i 11,R)=I 13 [R L, ] goto(i 11,L)=I 10 [L *R, ] goto(i 11,*)=I 11 [L id, ] goto(i 11,id)=I 12 [L id, ] [L *R, ] Shorthand for two items in the same set 41

Constructing Canonical LR(1) Parsing Tables 1. Augment the grammar with S S 2. Construct the set C={I 0,I 1,,I n } of LR(1) items 3. If [A α aβ, b] I i and goto(i i,a)=i j then set action[i,a]=shift j 4. If [A α, a] I i then set action[i,a]=reduce A α (apply only if A S ) 5. If [S S, ] is in I i then set action[i,]=accept 6. If goto(i i,a)=i j then set goto[i,a]=j 7. Repeat 3-6 until no more entries added 8. The initial state i is the I i holding item [S S,] 42

0 Example LR(1) Parsing Table id * = s5 s4 S L R 1 2 3 1 acc 2 s6 r6 Grammar: 1. S S 2. S L = R 3. S R 4. L * R 5. L id 6. R L 3 4 5 6 7 8 s5 s4 s12 s11 r5 r4 r6 r3 r5 r4 r6 8 7 10 9 9 r2 10 r6 11 s12 s11 10 13 12 r5 13 r4 43

LALR Parsing LR(1) parsing tables have many states LALR parsing (Look-Ahead LR) merges two or more LR(1) state into one state to reduce table size Less powerful than LR(1) Will not introduce shift-reduce conflicts, because shifts do not use lookaheads May introduce reduce-reduce conflicts, but seldom do so for grammars of programming languages 44

Constructing LALR Parsing Tables 1. Construct sets of LR(1) items 2. Combine LR(1) sets with sets of items that share the same first part I 4 : I 11 : [L * R, =/] [R L, =/] [L *R, =/] [L id, =/] [L * R, ] [R L, ] [L *R, ] [L id, ] [L * R, =/] [R L, =/] [L *R, =/] [L id, =/] 45

Example Grammar and LALR Parsing Table Unambiguous LR(1) grammar: S L = R R L * R id R L Augment with S S LALR items (next slide) 46

I 0 : [S S, ] goto(i 0,S)=I 1 [S L=R, ] goto(i 0,L)=I 2 [S R, ] goto(i 0,R)=I 3 [L *R, =/] goto(i 0,*)=I 4 [L id, =/] goto(i 0,id)=I 5 [R L, ] goto(i 0,L)=I 2 I 6 : I 7 : [S L= R, ] goto(i 6,R)=I 8 [R L, ] goto(i 6,L)=I 9 [L *R, ] goto(i 6,*)=I 4 [L id, ] goto(i 6,id)=I 5 [L *R, =/] I 1 : [S S, ] I 8 : [S L=R, ] I 2 : [S L =R, ] goto(i 0,=)=I 6 [R L, ] I 9 : [R L, =/] I 3 : [S R, ] Shorthand for two items I 4 : [L * R, =/] goto(i 4,R)=I 7 [R L, =/] goto(i 4,L)=I 9 [L *R, =/] goto(i 4,*)=I 4 [L id, =/] goto(i 4,id)=I 5 [R L, =] [R L, ] I 5 : [L id, =/] 47

Example LALR Parsing Table id * = S L R 0 s5 s4 1 2 3 Grammar: 1. S S 2. S L = R 3. S R 4. L * R 5. L id 6. R L 1 2 3 4 5 6 s5 s5 s4 s4 s6 r5 acc r6 r3 r5 9 7 9 8 7 r4 r4 8 r2 9 r6 r6 48

LL, SLR, LR, LALR Summary LL parse tables Nonterminals terminals productions Computed using FIRST/FOLLOW LR parsing tables computed using closure/goto LR states terminals shift/reduce actions LR states nonterminals goto state transitions A grammar is LL(1) if its LL(1) parse table has no conflicts SLR if its SLR parse table has no conflicts LALR if its LALR parse table has no conflicts LR(1) if its LR(1) parse table has no conflicts 49

LL, SLR, LR, LALR Grammars LR(1) LL(1) LALR SLR LR(0) 50