LR Parsing Dr Biswapati Jana Constructing Canonical LR1
LR Parsing Dr. Biswapati Jana
Constructing Canonical LR(1) Parsing Tables • In SLR method, the state i makes a reduction by A when the current token is a: – if the A . in the Ii and a is FOLLOW(A) • In some situations, A cannot be followed by the terminal a in a right-sentential form when and the state i are on the top stack. This means that making reduction in this case is not correct.
LR(1) Item • To avoid some of invalid reductions, the states need to carry more information. • Extra information is put into a state by including a terminal symbol as a second component in an item. • A LR(1) item is: . A , a where a is the look-head of the LR(1) item (a is a terminal or end-marker. ) • Such an object is called LR(1) item. – 1 refers to the length of the second component – The lookahead has no effect in an item of the form [A . , a], where is not . – But an item of the form [A . , a] calls for a reduction by A only if the next input symbol is a. – The set of such a’s will be a subset of FOLLOW(A), but it could be a proper subset.
LR(1) Item (cont. ) . • When ( in the LR(1) item A , a ) is not empty, the look-head does not have any affect. . • When is empty (A , a ), we do the reduction by A only if the next input symbol is a (not for any terminal in FOLLOW(A)). . • A state will contain A , a 1 where {a 1, . . . , an} FOLLOW(A). . A , an
Canonical Collection of Sets of LR(1) Items • The construction of the canonical collection of the sets of LR(1) items are similar to the construction of the canonical collection of the sets of LR(0) items, except that closure and goto operations work a little bit different. closure(I) is: ( where I is a set of LR(1) items) – every LR(1) item in I is in closure(I) . – if A B , a in closure(I) and B is a production rule of G; then B. , b will be in the closure(I) for each terminal b in FIRST( a).
goto operation • If I is a set of LR(1) items and X is a grammar symbol (terminal or non-terminal), then goto(I, X) is defined as follows: – If A . X , a in I then every item in closure({A X. , a}) will be in goto(I, X).
Construction of The Canonical LR(1) Collection • Algorithm: C is { closure({S’. S, $}) } repeat the followings until no more set of LR(1) items can be added to C. for each I in C and each grammar symbol X if goto(I, X) is not empty and not in C add goto(I, X) to C • goto function is a DFA on the sets in C.
A Short Notation for The Sets of LR(1) Items • A set of LR(1) items containing the following items . A , a 1. . A , an can be written as . A , a 1/a 2/. . . /an
Canonical LR(1) Collection -- Example S Aa. Ab S Bb. Ba A B I 0: S’ . S , $ S . Aa. Ab , $ S . Bb. Ba , $ A . , a B . , b I 1: S’ S. , $ S A B I 2: S A. a. Ab , $ a I 3: S B. b. Ba , $ b I 4: S Aa. Ab , $ A . , b A I 6: S Aa. A. b , $ a I 8: S Aa. Ab. , $ I 5: S Bb. Ba , $ B . , a B I 7: S Bb. B. a , $ b I 9: S Bb. Ba. , $ to I 4 to I 5
1. S’ S 2. S C C 3. C c C 4. C d An Example I 0: closure({(S’ S, $)}) = (S’ S, $) (S C C, $) (C c C, c/d) (C d, c/d) I 1: goto(I 1, S) = (S’ S , $) I 2: goto(I 1, C) = (S C C, $) (C c C, $) (C d, $) I 3: goto(I 1, c) = (C c C, c/d) (C c C, c/d) (C d, c/d) I 4: goto(I 1, d) = (C d , c/d) I 5: goto(I 3, C) = (S C C , $)
S’ S, $ S C C, $ C c C, c/d C d, c/d I 0 S I 1 (S’ S , $ C I 2 S C C, $ C c C, $ C d, $ C c C, c/d C c C, c/d C d, c/d I 4 d d C d , c/d I 6 C c C, $ C c C, $ C d, $ I 7 d c I 3 S C C , $ c c c I 5 C d C d , $ C I 8 C c C , c/d C I 9 C c. C , $
An Example I 6: goto(I 3, c) = (C c C, $) (C c C, $) (C d, $) I 7: goto(I 3, d) = (C d , $) I 8: goto(I 4, C) = (C c C , c/d) : goto(I 4, c) = I 4 : goto(I 4, d) = I 5 I 9: goto(I 7, c) = (C c C , $) : goto(I 7, c) = I 7 : goto(I 7, d) = I 8
An Example I 0 S C I 1 C I 2 c c d I 6 d I 7 C I 3 d I 4 I 5 I 8 C I 9
Construction of LR(1) Parsing Tables 1. Construct the canonical collection of sets of LR(1) items for G’. C {I 0, . . . , In} 2. Create the parsing action table as follows • • . If a is a terminal, A a , b in Ii and goto(Ii, a)=Ij then action[i, a] is shift j. If A , a is in Ii , then action[i, a] is reduce A where A S’. If S’ S , $ is in Ii , then action[i, $] is accept. If any conflicting actions generated by these rules, the grammar is not LR(1). . . 3. Create the parsing goto table • for all non-terminals A, if goto(Ii, A)=Ij then goto[i, A]=j 4. All entries not defined by (2) and (3) are errors. 5. Initial state of the parser contains S’. S, $
An Example 0 1 2 3 4 5 6 7 8 9 c s 3 d s 4 $ S g 1 C g 2 a s 6 s 3 r 3 s 7 s 4 r 3 g 5 g 8 r 1 s 6 s 7 g 9 r 3 r 2 r 2
The Core of LR(1) Items • The core of a set of LR(1) Items is the set of their first components (i. e. , LR(0) items) • The core of the set of LR(1) items { (C c C, c/d), (C c C, c/d), (C d, c/d) } is { C c C, C c C, C d}
The End
- Slides: 17