Bottom-Up Parsing. Canonical Collection of LR(0) items. Part II

Similar documents
Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

CS 573 Automata Theory and Formal Languages

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER MACHINES AND THEIR LANGUAGES ANSWERS

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Nondeterministic Automata vs Deterministic Automata

CS241 Week 6 Tutorial Solutions

Closure Properties of Regular Languages

CS 314 Principles of Programming Languages

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis

Convert the NFA into DFA

Parsing and Pattern Recognition

NON-DETERMINISTIC FSA

FABER Formal Languages, Automata and Models of Computation

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/28

Semantic Analysis. CSCI 3136 Principles of Programming Languages. Faculty of Computer Science Dalhousie University. Winter Reading: Chapter 4

Chapter 4 State-Space Planning

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

Finite State Automata and Determinisation

CSCI565 - Compiler Design

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

Minimal DFA. minimal DFA for L starting from any other

Regular languages refresher

Formal languages, automata, and theory of computation

Technische Universität München Winter term 2009/10 I7 Prof. J. Esparza / J. Křetínský / M. Luttenberger 11. Februar Solution

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Prefix-Free Regular-Expression Matching

Review for the Midterm

Chapter 2 Finite Automata

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Designing finite automata II

@#? Text Search ] { "!" Nondeterministic Finite Automata. Transformation NFA to DFA and Simulation of NFA. Text Search Using Automata

CS 275 Automata and Formal Language Theory

Nondeterministic Finite Automata

Petri Nets. Rebecca Albrecht. Seminar: Automata Theory Chair of Software Engeneering

CMSC 330: Organization of Programming Languages

Homework Solution - Set 5 Due: Friday 10/03/08

Algorithm Design and Analysis

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Parse trees, ambiguity, and Chomsky normal form

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Thoery of Automata CS402

Normal Forms for Context-free Grammars

1 PYTHAGORAS THEOREM 1. Given a right angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Formal Languages Simplifications of CFGs

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Algorithm Design and Analysis

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

= state, a = reading and q j

AP Calculus BC Chapter 8: Integration Techniques, L Hopital s Rule and Improper Integrals

Regular expressions, Finite Automata, transition graphs are all the same!!

CS 330 Formal Methods and Models

Data Structures and Algorithm. Xiaoqing Zheng

Lecture 6: Coding theory

Counting Paths Between Vertices. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

First Midterm Examination

2.4 Theoretical Foundations

Tutorial Worksheet. 1. Find all solutions to the linear system by following the given steps. x + 2y + 3z = 2 2x + 3y + z = 4.

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Chapter 3. Vector Spaces. 3.1 Images and Image Arithmetic

1 From NFA to regular expression

1.4 Nonregular Languages

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Running an NFA & the subset algorithm (NFA->DFA) CS 350 Fall 2018 gilray.org/classes/fall2018/cs350/

CS 275 Automata and Formal Language Theory

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

First Midterm Examination

Harvard University Computer Science 121 Midterm October 23, 2012

1 Nondeterministic Finite Automata

Introduction to Olympiad Inequalities

Formal Languages and Automata

System Validation (IN4387) November 2, 2012, 14:00-17:00

Discrete Structures, Test 2 Monday, March 28, 2016 SOLUTIONS, VERSION α

Unit 4. Combinational Circuits

Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem 2/16/15

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Arrow s Impossibility Theorem

22: Union Find. CS 473u - Algorithms - Spring April 14, We want to maintain a collection of sets, under the operations of:

Arrow s Impossibility Theorem

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Homework 3 Solutions

Name Ima Sample ASU ID

Alpha Algorithm: Limitations

CS 275 Automata and Formal Language Theory

Coalgebra, Lecture 15: Equations for Deterministic Automata

Lecture 08: Feb. 08, 2019

CSE 401 Compilers. Today s Agenda

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

Exercise 3 Logic Control

Transcription:

2 ottom-up Prsing Prt II 1 Cnonil Colletion of LR(0) items CC_LR(0)_I items(g :ugmented_grmmr){ C = {CLOURE({ })} ; repet{ foreh(i C) foreh(grmmr symol X) if(goto(i,x) && GOTO(I,X) C) C = C {GOTO(I,X)}; }until(no new sets of items re dded to C) return C; }

4 LR(0) utomton G : ugmented grmmr LR(0) utomton for G Q, q0, GOTO: Q (T G N G ) Q, F where: Q = F = items(g ), q0 = CLOURE({ }) 3 Constrution of the LR(0) utomton for the expression grmmr: E E E E + T T T T* F F F (E) id

E E E E + T T T T* F F F (E) id 5 E E E E + T T T T* F F F (E) id id I 0 E >.E E >. E+T E >.T T >.T*F T >.F F >.(E) F >.id ( F E I + I 6 T 1 E > E+. T E > E. T >. T*F E > E. + T T >.F F F >.(E) F >.id ( T I 2 E > T. T > T.*F * I 7 T > T*.F F >.(E) F >. id id I 5 I 4 F ( I 9 E > E+T. T > T.*F I 3 I 10 T > T*F. * I 7 I 3 T > F. id I 5 I 4 I 5 F > id. id I 4 F > (. E ) E >. E + T E >.T T >. T * F T >. F F >. ( E ) F >. id E F T I 8 F > ( E. ) E > E. + T I 2 ) + I 6 I 11 F > ( E ). ( I 3 6

8 E E E E + T T T T* F F F (E) id F E + T I 0 I 1 I 6 T id F $ ( id ept ( I 9 * I 2 * id I 7 ( F I 10 I 5 + I 8 ) I 11 T I 4 id E ( F I 3 7 hift-redue prsing using LR(0) utomton Push(I0); repet{ //egin to sn the input from left to right Ii = top() nd next input symol ; if (Ij = GOTO(Ii,)) then shift nd Ij; // Push() // push(ij). else if ( β Ii) then{ perform redue y β ; go to the stte Ij = GOTO(I,) nd push nd Ij into the stk //where I is the stte on the top_of stk //fter removing β nd the orresponding sttes; } _exit(rejet if none of the ove n e done); _exit(report onflits if more thn one n e done); } until EOF is seen

MODEL OF N LR PRER input 1... i... n $ s m s m-1 : : $ LR Prsing Progrm output stk CTION GOTO 9 I0,...,In enumertion of items(g') I0= CLOURE({ʼ }) Q = {0,...,n} s Is GOTO(s,X)=s' GOTO(Is,X)= Is' Proposition: if GOTO(s',X)=s nd GOTO(s",Y)=s then X=Y s Q we ssoite unique symol X s T G N G to s 0 if GOTO(s',X)=s then X s = X. Moreover, X 0 represents the symol $ (ottom of the stk) Q, 0, GOTO: Q (TGʼ NGʼ) Q, F Fix for G' finite ijetive enumertion p: {1,...,n} G'-produtions, s.t. p(1)= ' 10

12 Construt the LR(0) utomton for the grmmr C Ce C df 11 C Ce C df ept

C Ce C df!"#$%&'()*+,--,./01!"#$%&'&()*+&,-..-/012 3%4%#*&5)*("&)+*6()7&$6#)7%89 (:&;& &α<β&*6%)&#==&,>31?0β2@ε&*+&,-..-/0<2 (:&;& &α<&*6%)&#==&,-..-/0;2&*+&,-..-/0<2 (:&;& &α<β&#)=&ε&(8&()&,>31?0β2&*6%)&#==&,-..-/0;2& *+&,-..-/0<2 Follow 1 ()={$} Follow 1 ()={f,$} Follow 1 ()={f,$} Follow 1 (C)={e,f,$} 13 C Ce C df!"#$%&'()*+,--,./01!"#$%&'&()*+&,-..-/012 3%4%#*&5)*("&)+*6()7&$6#)7%89 (:&;& &α<β&*6%)&#==&,>31?0β2@ε&*+&,-..-/0<2 (:&;& &α<&*6%)&#==&,-..-/0;2&*+&,-..-/0<2 (:&;& &α<β&#)=&ε&(8&()&,>31?0β2&*6%)&#==&,-..-/0;2& *+&,-..-/0<2 Follow 1 ()={$} Follow 1 ()={f,$} Follow 1 ()={f,$} Follow 1 (C)={e,f,$} 14

15 terminls nd $ PRING TLE non terminls tte CTION GOTO i = CTION[i,] G = GOTO[j,] j G CTION[i,]= s j (shift symol j (nmely Xj)) CTION[i,]= r k (redue prodution p(k)) CTION[i,]= ept CTION[i,]= error 16

LR-PRER CONFIGURTION onfigurtion stk remining input ( 0 s 1... s m, i i+1... n $ ) top right-sententil form X0 Xs 1... Xs m i i+1... n 17 EHVIOR OF THE LR PRER ( s 0 s 1... s m, i i+1... n $ ) CTION[sm,i] = shift s symol Xs ( s 0 s 1... s m s, i+1... n $ ) ( s 0 s 1... s m, i i+1... n $ ) CTION[sm,i] = redue k ( s 0 s 1... s m-r s, i i+1... n $ ) p(k)= β β = r s=goto[s m-r, ] symol Xs 18

20 EHVIOR OF THE LR PRER ( s 0 s 1... s m, i i+1... n $ ) CTION[sm,i] = ept end of prsing ( s 0 s 1... s m, i i+1... n $ ) CTION[sm,i] = error error reovery routine 19 LR-prsing lgorithm

F E + T 0 1 6 T id F $ ( id ept ( * 2 7 F id ( + ) 5 8 id T E 4 ( F 3 9 * 10 11 X 1 = E X 2 = T X 3 = F X 4 = ( X 5 = id X 6 = + X 7 = * X 8 = E X 9 = T X 10 = F X 11 = ) 21 X 1 = E X 2 = T X 3 = F X 4 = ( X 5 = id X 6 = + X 7 = * X 8 = E X 9 = T X 10 = F X 11 = ) id id + id 22

24 X 1 = E X 2 = T X 3 = F X 4 = ( X 5 = id X 6 = + X 7 = * X 8 = E X 9 = T X 10 = F X 11 = ) 23 1.G : ugmented grmmr, with enumertion p of produtions; 2.onstrut the LR(0) utomton (nonil item set C={I0,...,In}, I0=CLOURE[ ] GOTO funtion); 3.foreh stte i: () if ( α β Ii && GOTO[Ii,]=Ij) dd shift j to CTION[i,]; () if ( α Ii && ) foreh ( FOLLOW()) dd redue p -1 ( α) to CTION[i,]; () if ( Ii) dd ept to CTION[i,$] 4. foreh (stte i,k && symol ) if (GOTO[Ij,]=Ik) dd k to GOTO[s,] 5. if (CTION[i,] is undefined) dd error to CTION[i,] 6. if (GOTO[i,] is undefined) dd error to GOTO[i,]

26 25 LR-prsing lgorithm

' C C Ce df Exerise: Construt the PRING TLE 27 ' C q2 '!" q8 C!d"f!"!" C Ce df Prse tle omplete. Press "prse" to use it. q2 '!" q0 '!"!"!"!" q8 C!d"f!"!" q0 '!"!"!"!" q4 d!"ce!"!"c q10 FIRT C!d"f q1!" q4 d!"ce!"!"c q10 C!d"f q1!" f q5!" f q5!" q11 C!df" q11 C!df" q3!" FOLLOW {, } { f, $ } { } { f, $ } C { d } { f, e, $ } {, } { $ } q3!" C q6!"ce C!"df!"C q7!c"!c"e C q6 e q7!c"!c"e!"ce C!"df!"C q9!ce" e q9!ce" 28

30 ' C C Ce df q2 '!" q8 C!d"f!"!" q0 '!"!"!"!" d q1!" q4!"ce!"!"c q5!" f q11 q10 C!df" C!d"f q3!" C q6 q7!c"!c"e!"ce C!"df!"C e q9!ce" Prse tle omplete. Press "prse" to use it. FIRT FOLLOW {, } { f, $ } { } { f, $ } C { d } { f, e, $ } {, } { $ } q1 29 q2 q0!" q3!" '!" q8 C!d"f!"!" '!"!"!"!" q4 d!"ce!"!"c q5!" Construting n LR-prsing Tle I0=CLOURE[ ] GOTO funtion); q11 q10 C!df" C!d"f C q6 q7!c"!c"e!"ce C!"df!"C e q9!ce" 1.G : ugmented grmmr, with enumertion p of produtions; 2.onstrut the LR(0) utomton (nonil item set C={I0,...,In}, 3.foreh stte i: () if ( α β Ii && GOTO[Ii,]=Ij) dd shift j to CTION[i,]; () if ( α Ii && ) foreh ( FOLLOW()) dd redue p -1 ( α) to CTION[i,]; () if ( Ii) dd ept to CTION[i,$] 4. foreh (stte i,k && symol ) if (GOTO[Ij,]=Ik) dd k to GOTO[s,] 5. if (CTION[i,] is undefined) dd error to CTION[i,] 6. if (GOTO[i,] is undefined) dd error to GOTO[i,]

' Prse tle omplete. Press "prse" to use it. FIRT FOLLOW { } {, } {, } { } { } { $ } q0!" '!" q1 '!" q9!" q4 q0!" '!"!"!"!"!" q1 '!" q9!" q0 q2!" '!"!"!"!" q1 q7 '!"!" q8 q4!"!"!"!"!"!"!"!" q7!" q3!"!"!" q4 q9!"!"!"!" q2!" q12!" q8!" q8 q3!"!"!"!" q12!" q2!"!"!" q7!" q6!"!"!" q6!"!"!" q3!"!"!" q12!" q5!" q10!" q11!" q5!" q6 q10!"!"!"!" q11!" q5!" q10!" q11!" 31 Do eleted Do tep Do ll Next Prse ' Prse tle omplete. Press "prse" to use it. FIRT $ FOLLOW { } {, } 0{, } s2 { } 1 { } 1 { $ } 2 s4 3 3 s6 s7 5 4 s4 s9 8 q0 q2 q3 q5 5 s10!"!"!"!" rse tle omplete. Press "prse" to use it. 6 s6 s7 11 '!"!"!" 7 r5 FIRT FOLLOW!"!" Next Prse tle omplete. to use it. } 8 s12 rse Press { }"prse" {, } {, { } 9 r3 r3 q10 { } {, } { } {, } { $ } { } q7 10 r1 { } { $ } q4!" q9 11 r4!" q6 12!" r2 r2!" q1!"!" q8 q12 q11!"!" q0 q2 q3 q5 '!"!"!"!"!"!"!"!" '!" q0!" '!" q1 '!" q9!" $ 0 s2 1 1 2 s4 3 3 s6 s7 5 q4!"!"!" q7!" 4 s4 s9 8!"!"!" q6 q10!" 32

it is not miguous Item set? 33 Item set : Prsing tle? 34

36 Item set : = if ( α Ii && ) foreh ( FOLLOW()) dd redue p -1 ( α) to CTION[i,]; prsing tle shift 6 CTION[2,=] symol = FOLLOW(R) nd therefore: redue R L CTION[2,=] onflit! 35 Vile prefix prefix of right-sententil forms tht n pper on the top of the stk Not ll prefixes of right-sententil forms n pper on the stk, however, sine the prser must not shift pst the hndle Exmple: TCK: (, (E, nd (E), ut not (E)* (E) is hndle (the prser must redue it to F efore shifting *)

38 Vile prefix: prefix of right-sententil forms tht n pper on the top of the stk nd tht does not ontinue pst the right end of the rightmost hndle of tht sententil form. prefix γ of αβ, where ' * αw αβw rm rm β 1 β 2 is vlid for vile prefix αβ 1 if ' * αw αβ 1 β 2 w rm rm 37 β 1 β 2 is vlid for vile prefix αβ 1 if ' * αw αβ 1 β 2 w rm rm if β 2 ε then shift if β 2 =ε then redue β 1 possile onflits Two vlid items my tell us to do different things for the sme vile prefix. ome of these onflits n e resolved y looking t the next input symol THEOREM: the set of vlid items for vile prefix γ is extly the set of items rehed from the initil stte long the pth leled γ in the LR(0) utomton for the grmmr

Item set : = Prsing tle shift 6 CTION[2,=] symol = FOLLOW(R) nd therefore: redue R L CTION[2,=] Conflit! 39 LR(1) PRING Grmmr: L = R R L R id R L shift 6 CTION[2,=] symol = FOLLOW(R) nd therefore: redue R L CTION[2,=] there is no right-sententil form of the grmmr tht egins R =... Thus stte 2, whih is the stte orresponding to vile prefix L only, should not relly ll for redution of tht L to R $L 0 2 0 3 $R 40

In LR(1) prsing, if α i, nd FOLLOW(), then we perform the redution α However, it is possile tht when stte i is on the top of the stk, we hve the vile prefix βα on the top of the stk, nd β nnot e followed y. In this se, we nnot perform the redution α. 41 LR(1) items n LR(1) item is in the form of [ α β,] 1.the first field α β is n LR(0) item 2.the seond field is terminl elonging to suset (possily proper) of FOLLOW[]. Intuition: perform redution sed on n LR(1) item [ α, ] only when the next symol is. Insted of mintining FOLLOW sets of vile prefixes, we mintin FIRT sets of possile future extensions of the urrent vile prefix. [ α β, ] is vlid for vile prefix γ if there exists derivtion where γ = δα w = w' or w=ε nd =$. ' * δw δαβw rm rm γ 42

Exmples of LR(1) items Grmmr: = = rm rm vile prefix n reh [, ] = = rm rm vile prefix n reh [,$] [ α β, ] is vlid for vile prefix γ if there exists derivtion ' * δw δαβw rm where γ = δα w = w' or w=ε nd =$. rm 43 Finding ll LR(1) items Ides: redefine the losure funtion. uppose [ α β,] is vlid for vile prefix γ δα. In other words, = δ ω = δ αβ ω. rm rm γ ω is ɛ or sequene of terminls. Then for eh prodution η, ssume βω derives the sequene of terminls eω. γ γ γ = rm δα βω = rm δα eω = rm δαη eω Thus [ η,] is lso vlid for γ for eh FIRT(β). Note is terminl. o FIRT(β) =FIRT(βω). [ α β, ] is vlid for vile prefix γ if there exists derivtion ' * δw δαβw rm rm γ where γ = δα w = w' or w=ε nd =$. 44

46 set_of_items CLOURE(I: set_of_items){ J=I; repet{ foreh ( [ α β,] J) foreh ( γ G) foreh ( FIRT[β]) J = J {[ γ,]}; }until (no more new items re dded to J) return J; } 45 set_of_items GOTO(I set_of_items, X:symol){ J= ; foreh([ α Xβ,] I) J = J {[ αx β,]}; return CLOURE(J); }

48 CC_LR(1)_I items(g :ugmented_grmmr){ C = {CLOURE({[,$]})} ; repet{ foreh(i C) foreh(grmmr symol X) if(goto(i,x) && GOTO(I,X) C) C = C {GOTO(I,X)}; }until(no new sets of items re dded to C) return C; } 47 1.G : ugmented grmmr, with enumertion p of produtions; 2.onstrut the LR(1) utomton (nonil item set C={I0,...,In}, I0=CLOURE[[,$]] GOTO funtion); 3.foreh stte i: () if ([ α β,] Ii && GOTO[Ii,]=Ij) dd shift j to CTION[i,]; // is terminl () if ([ α,] Ii && ) dd redue p -1 ( α) to CTION[i,]; () if ([,$] Ii) dd ept to CTION[i,$] 4. foreh (stte i,k && symol ) if (GOTO[Ij,]=Ik) dd k to GOTO[s,] 5. if (CTION[i,] is undefined) dd error to CTION[i,] 6. if (GOTO[i,] is undefined) dd error to GOTO[i,]

50 Exmple for onstruting LR(1) losures Grmmr: CC C C d losure 1 ({[, $]}) = {[, $], [ CC, $], [C C, /d], J=I; [C d, /d]} Note: repet{ FIRT(ɛ$) = {$} FIRT(C$) = {, d} [C foreh C, /d] ( mens γ G) set_of_items CLOURE(I: set_of_items){ } foreh ( [ α β,] J) [Cforeh C, ] ( nd FIRT[β]) J = J {[ γ,]}; [C C, d]. }until (no more new items re dded to J) return J; tion 1 GOTO 1 stte d $ C 0 s3 s4 1 2 1 ept 2 s6 s7 5 3 s3 s4 8 4 r3 r3 5 r1 6 s6 s7 9 7 r3 8 r2 r2 9 r2 49 Exmple for onstruting LR(1) losures Grmmr: CC C C d losure 1 ({[, $]}) = {[, $], [ CC, $], [C C, /d], [C d, /d]} Note: FIRT(ɛ$) = {$} FIRT(C$) = {, d} [C C, /d] mens [C C, ] nd [C C, d]. set_of_items CLOURE(I: set_of_items){ J=I; repet{ foreh ( [ α β,] J) foreh ( γ G) foreh ( FIRT[β]) J = J {[ γ,]}; }until (no more new items re dded to J) return J; }

52 Exmple for onstruting LR(1) losures Grmmr: CC C C d losure 1 ({[, $]}) = {[, $], [ CC, $], [C C, /d], [C d, /d]} Note: FIRT(ɛ$) = {$} FIRT(C$) = {, d} [C C, /d] mens [C C, ] nd [C C, d]. set_of_items CLOURE(I: set_of_items){ J=I; repet{ foreh ( [ α β,] J) foreh ( γ G) foreh ( FIRT[β]) J = J {[ γ,]}; }until (no more new items re dded to J) return J; } 51 set_of_items CLOURE(I: set_of_items){ J=I; repet{ foreh ( [ α β,] J) foreh ( γ G) foreh ( FIRT[β]) J = J {[ γ,]}; }until (no more new items re dded to J) return J; }

54 Exmple for onstruting LR(1) losures Grmmr: CC C C d losure 1 ({[, $]}) = {[, $], [ CC, $], [C C, /d], [C d, /d]} Note: FIRT(ɛ$) = {$} FIRT(C$) = {, d} [C C, /d] mens [C C, ] nd [C C, d]. tion 1 GOTO 1 stte d $ C 0 s3 s4 1 2 1 ept 2 s6 s7 5 3 s3 s4 8 4 r3 r3 5 r1 6 s6 s7 9 7 r3 8 r2 r2 9 r2 53 LLR(1) prser Lookhed LR The method tht is often used in prtie. Most ommon syntti onstruts of progrmming lnguges n e expressed onveniently y n LLR(1) grmmr [DeRemer 1969]. LR(1) nd LLR(1) lwys hve the sme numer of sttes. Numer of sttes is out 1/10 of tht of LR(1). imple oservtion: n LR(1) item is of the form [ α β,] We ll α β the first omponent. Definition: in n LR(1) stte, set of first omponents is lled its ore.

56 1.G : ugmented grmmr, with enumertion p of produtions; 2.onstrut the LR(1) utomton (nonil item set C={I0,...,In}, I0=CLOURE[[,$]] GOTO funtion); 3.foreh stte i: () if ([ α β,] Ii && GOTO[Ii,]=Ij) dd shift j to CTION[i,]; // is terminl () if ([ α,] Ii && ) dd redue p -1 ( α) to CTION[i,]; () if ([,$] Ii) dd ept to CTION[i,$] 4. foreh (stte i,k && symol ) if (GOTO[Ij,]=Ik) dd k to GOTO[s,] 5. if (CTION[i,] is undefined) dd error to CTION[i,] 6. if (GOTO[i,] is undefined) dd error to GOTO[i,] 55 Intuition for LLR(1) grmmrs In n LR(1) prser, it is ommon thing tht severl sttes only differ in lookhed symols, ut hve the sme ore. To redue the numer of sttes, we might wnt to merge sttes with the sme ore. If I 4 nd I 7 re merged, then the new stte is lled I 4,7. fter merging the sttes, revise the GOT O 1 tle ordingly. Merging of sttes n never produe shift-redue onflit tht ws not present in one of the originl sttes. I 1 = {[ α,],...} For I 1, one of the tions is to perform redue when the lookhed symol is. I 2 = {[ β γ,],...} For I 2, one of the tions is to perform shift on input. Merging I 1 nd I 2, the new stte I 1,2 hs shift-redue onflits. However, we merge I 1 nd I 2 euse they hve the sme ore. Tht is, [ α,] I 2 nd [ β γ,d] I 1. The shift-redue onflit lredy ours in I 1 nd I 2. Merging of sttes n produe new redue-redue onflit.

LLR(1) trnsition digrm d I 0!>., $!>. CC, $ C!>. C, /d C!>.d, /d C I 3 C!>.C, /d C!>.C, /d C!>.d, /d I 2!> C.C, $ C!>.C, $ C!>.d, $ I 1!>., $ C d I 5!> CC., $ I 6 C!>.C, $ C!>.C, $ C!>.d, $ C d C d I 8 C!> C., /d I 9 C!> C., $ I 4 C!> d., /d I 7 C!> d., $ 57 Possile new onflits from LLR(1) My produe new redue-redue onflit. For exmple (textook pge 267, Exmple 4.58), grmmr: d f e e The lnguge reognized y this grmmr is {d, e, d, e}. You my hek tht this grmmr is LR(1) y onstruting the sets of items. You will find the set of items {[,d], [,e]} is vlid for the vile prefix, nd {[,e], [,d]} is vlid for the vile prefix. Neither of these sets genertes onflit, nd their ores re the sme. However, their union, whih is {[, d/e], [, d/e]}, genertes redue-redue onflit, sine redutions y oth nd re lled for on inputs d nd e. 58

60 How to onstrut LLR(1) prsing tle Nive pproh: Construt LR(1) prsing tle, whih tkes lots of intermedite spes. Merging sttes. pe nd/or time effiient methods to onstrut n LLR(1) prsing tle re known. Construting nd merging on the fly. 59 ummry LLR(1) LLR(1) LL(1) LR(1) LR(1) LR(1) LR(1) LR(0) LR(1) nd LLR(1) n lmost express ll importnt progrmming lnguges issues, ut LLR(1) is esier to write nd uses muh less spe. LL(1) is esier to understnd nd uses muh less spe, ut nnot express some importnt ommon-lnguge fetures. My try to use it first for your own pplitions.

62 miguous grmmrs re not too d... miguous grmmrs unmiguous grmmrs LR(1) miguous grmmrs often provide shorter, more nturl speifition thn their equivlent unmiguous grmmrs. 61 miguity from preedene nd ssoitivity Preedene nd ssoitivity re importnt lnguge onstruts. Exmple: G 1 : E E + E E E (E) id miguous, ut esy to understnd nd mintin! G 2 : E E + T T T T F F F (E) id Unmiguous, ut diffiult to understnd nd mintin!

64 LR(0) sttes input: id+id id the prser enters stte 7 fter proessing id+id 63 miguity from dngling-else Grmmr: ttement Other ttement if Condition then ttement if Condition then ttement else ttement When seeing if C then else there is shift/redue onflit, we lwys fvor shift. Intuition: fvor longer mth. Need mehnism to let user speify the defult onflithndling rule when there is shift/redue onflit.