Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing a LR parser engine

Similar presentations


Presentation on theme: "Implementing a LR parser engine"— Presentation transcript:

1 Implementing a LR parser engine
Building SLR(1) and LR(1) parser engines Lecture Notes by Prof. Saman Amarasinghe (MIT)

2 Outline Overview of building a LR(0) parser
5 Outline Overview of building a LR(0) parser Limitations in LR(0) languages Building a SLR(1) parser engine Limitations in SLR(1) languages Building a LR(1) parser engine

3 Building a LR(0) parser engine
Add the special production S  S’ $ Find the items of the CFG Create the DFA using closure and goto functions Build the parse table LR(0) Parser Engine S’ is the start symbol of the given grammar.

4 Building Parse Table Example
<S>  <X> • $ s0 s2 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( ) ( <X>  ( • <X> ) <X>  ( • ) <X>  • ( <X> ) <X>  • ( ) ( s3 <X>  ( <X> • ) X ) ) s5 s4 <X>  ( ) • <X>  ( <X> ) •

5 Outline Overview of building a LR(0) parser
8 Outline Overview of building a LR(0) parser Limitations in LR(0) languages Building a SLR(1) parser engine Limitations in SLR(1) languages Building a LR(1) parser engine Building a LALR(1) parse engine

6 Example String of one or more left parentheses followed by the same number of right parentheses <S>  <X> $ <X>  ( <X> ) <X>  ( ) String of zero or more left parentheses followed by the same number of right parentheses <X>   Eg1: Reduce on () and (X) and X$. Shift o/w Moral: If a string and its prefix are in the language, then it is not LR(0) because there will be a shift-reduce conflict at the end of the prefix. Eg2: Reduce on e is trouble if there is (… on input Source of non-LR(0). Moral: If the symbol on the input is $ reduce, else shift. Reducing e to X marks the end of sequence of “(“ and now we pair the stacked “(“ with corresponding “)”. LR(0) guesses middle of the string everywhere, while we need to avoid that when the input is “(“.

7 Example The grammar Items <S>  <X> $
10 Example The grammar <S>  <X> $ <X>  ( <X> ) <X>   Items <S>  • <X> $ <S>  <X> • $ <X>  • ( <X> ) <X>  ( • <X> ) <X>  ( <X> • ) <X>  ( <X> ) • <X>  • X marks the middle of the string.

8 Building the DFA for the Example
s0 <S>  • <X> $ <X>  • ( <X> ) <X>  •

9 Building the DFA for the Example
s1 X <S>  <X> • $ s0 <S>  • <X> $ <X>  • ( <X> ) <X>  •

10 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 <S>  • <X> $ <X>  • ( <X> ) <X>  • <X>  ( • <X> ) <X>  • ( <X> ) <X>  • (

11 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 <S>  • <X> $ <X>  • ( <X> ) <X>  • <X>  ( • <X> ) <X>  • ( <X> ) <X>  • (

12 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 <S>  • <X> $ <X>  • ( <X> ) <X>  • <X>  ( • <X> ) <X>  • ( <X> ) <X>  • (

13 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • (

14 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( X

15 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( X

16 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

17 Building the DFA for the Example
s1 X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

18 Building the DFA for the Example
13 s1 X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

19 Building the Parse Table
X <S>  <X> • $ s0 Observe that “reduce”-item all by itself causes no problem in LR(0), but with ano other item containing “.Terminal” it will cause shift-reduce conflict. The null-production will always conflict. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

20 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

21 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

22 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

23 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

24 Building the Parse Table
X <S>  <X> • $ s0 The only valid application of the null production in s0 is for the null string input. If the input contains “(“, it requires a “shift”. If the input is “)”, it is an illegal string. SLR(1) strategy will prohibit reduction on “(“ on the basis of Follow(X), though it will be continue to reduce on “)”, postponing error detection. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 <X>  ( <X> ) •

25 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

26 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

27 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

28 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

29 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

30 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

31 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

32 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 <X>  ( <X> ) •

33 Building the Parse Table
X <S>  <X> • $ s0 Again LR(0) table has shift reduce conflict, even though for the given language and grammar, “(“ on the input implies that it should continue SHIFT as the middle of the string is not reached. “$” on the input heralds premature end to input with umatched “(“ on the stack, hence an ERROR. “)“ is legal only if it is the first one marking the center. Otherwise, one does not get to s2. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

34 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

35 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

36 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

37 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

38 Building the Parse Table
X <S>  <X> • $ s0 Conflict in s0 results from reducing e to X in spite of non-null input. Conflict in s2 results when (( is on the stack and reduction from e to X is made with the remaining input of ())). This null-reduction is correct only when all the “(“ have been pushed on stack, and are now ready to be paired with corresponding “)” on the input. Distinguished on the basis of the follow symbol of X $ means reduce in s0, but not when ( on the input. ) means reduce in s2 but not when ( is on the input. Also observe that we get into state s2 by consuming ( and hence if $ is on the input it is an error. However, the table says reduce. Error is caught however after a few reduces, but before any additional shift is done. LR(1) scheme exploits this additional contextual information. Also notice that entries for s4 can be strengthened to : ERROR-REDUCE-REDUCE. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

39 Building the LR(0) Parse Table
16 s1 X <S>  <X> • $ s0 A grammar with e-production cannot be LR(0) because the trivial reduce always conflicts with a shift. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) Shift/reduce conflict X s4 Shift/reduce conflict <X>  ( <X> ) •

40 Building the Parse Table
17 How do we get rid of these shift/reduce conflicts?

41 Limitations of LR(0) grammars
17 Limitations of LR(0) grammars Many shift/reduce conflicts Reason an item X   • in the current state identifies a reduction But does not select when to reduce Thus, have to perform the reduction on all input symbols

42 Outline Overview of building a LR(0) parser
17 Outline Overview of building a LR(0) parser Limitations in LR(0) languages Building a SLR(1) parser engine Limitations in SLR(1) languages Building a LR(1) parser engine Building a LALR(1) parse engine

43 Idea behind SLR(1) grammars
Problem: Many shift/reduce conflicts in LR(0) an item X   • in the current state identifies a reduction But does not select when to reduce Thus, have to perform the reduction on all input symbols Solution: Do the reduction only when the input symbol truly follows the reduction Need to calculate the terminals that can follow a non-terminal symbol

44 follow() For each non-terminal A, follow(A) is the set of terminals that can come after A If there is a derivation sequence of the form S   A b b then follow(A) will include b If there is a derivation sequence of the form S   A $ then follow(A) will include $ Find follow(<X>) grammar <S>  <X> $ <X>  ( <X> ) <X>   ), $

45 Building a SLR(1) parser engine
Add the special production S  S’ $ Calculate the follow sets for all the nonterminals Find the items of the CFG Create the DFA using closure and goto functions Build the parse table using the DFA and follow set information SLR Parser Engine

46 Creating the SLR(1) parse tables
19 Creating the SLR(1) parse tables For each state Transition to another state using a terminal symbol is a shift to that state (shift to sn) Transition to another state using a non-terminal is a goto that state (goto sn) If there is an item A   • in the state, for all terminals c  follow(A) do a reduction with the production (reduce k)

47 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

48 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

49 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

50 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

51 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

52 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

53 Building the Parse Table
X <S>  <X> • $ s0 Entry for s0 and “)” can be strengthened to “ERROR”. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

54 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

55 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

56 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

57 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

58 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

59 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

60 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

61 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

62 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

63 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

64 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

65 Building the Parse Table
X <S>  <X> • $ s0 Entry for s2 and “$” can be strengthened to “ERROR” because there are unmatched “(“ on the stack. The reduce however leads to a state such as “s3” when unmatched “(“ exist, and hence yield error from s3. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

66 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

67 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

68 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

69 Building the Parse Table
X <S>  <X> • $ s0 Entry for s2 and “$” can be strengthened to “ERROR” because there are unmatched “(“ on the stack. The reduce however leads to a state such as “s3” when unmatched “(“ exist, and hence yield error from s3. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

70 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

71 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $} <X>  ( <X> ) •

72 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

73 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

74 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

75 Building the Parse Table
X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

76 Building the Parse Table
X <S>  <X> • $ s0 Entry for s4 and “$” will be triggered when there are no remaining unmatched “(“ for acceptance. s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

77 Building the SLR(1) Parse Table
22 s1 X <S>  <X> • $ s0 s2 s3 <S>  • <X> $ <X>  • ( <X> ) <X>  • ( <X>  ( • <X> ) <X>  • ( <X> ) <X>  • <X>  ( <X> • ) ( ) X s4 follow(<X>) = { ), $ } <X>  ( <X> ) •

78 LR(0) and SLR(1) parse tables
23 SLR(1) Adding the rule : X -> X X to SL(1) grammar causes ambiguity. What about the grammar : S -> X$ X -> e | (X)X which has FOLLOW(X) = { (, ), $ }. Can this cause shift reduce conflicts??? Notice how the error in “()(“ is detected before reduction in SLR(1) (that is in s4), while it is detected after reduction in LR(0) (that is in state s1). Note also that in SLR(1) table, entry(s2,$) should be ERROR, not REDUCE(3), because there are unmatched “(“ when “$” seen. Similarly, entry(s4,$) is triggered when there are no remaining unmatched “(“ for acceptance. (see previous overhead for justification). LR(0)

79 Outline Overview of building a LR(0) parser
23 Outline Overview of building a LR(0) parser Limitations in LR(0) languages Building a SLR(1) parser engine Limitations in SLR(1) languages Building a LR(1) parser engine Building a LALR(1) parse engine

80 SLR(1) vs LR(1) Grammars SLR(1) keeps the current position as well as some information on the next input character LR(1) keeps full information on the next input character Lets examine a grammar that is LR(1) but not SLR(1)

81 Expanded Example The language: Change language to:
One or more open parentheses followed by matching # of close parentheses The grammar <S>  <X> $ <X>  ( <X> ) <X>  ( ) Change language to: Zero or more open parentheses followed by matching # of close parentheses or a single open parenthesis

82 Expanded Example Grammar
Example strings: “((((()))))” “()” “(” “” <S>  <X> $ (1) <X>  <Y> (2) <X>  ( (3) <Y>  ( <Y> ) (4) <Y>   (5) Change language to: Zero or more open parentheses followed by matching # of close parentheses or a single open parenthesis

83 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   Problem: After consuming one “(“ one cannot terminate when “(“ is on input due to follow symbol. However, one can still reduce e to Y, which causes trouble.

84 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0

85 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 (

86 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 (

87 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 (

88 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( s2 <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 Transition from s1 -> s2 prunes the possibility of a single “(“. (

89 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( s2 <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y

90 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( s2 <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y <Y>  ( <Y> • ) s3

91 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( s2 <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y <Y>  ( <Y> • ) s3

92 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y <Y>  ( <Y> • ) s3

93 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   Y ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( s2 <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y <Y>  ( <Y> • ) s3

94 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3

95 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3 )

96 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3 ) s4 <Y>  ( <Y> ) •

97 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 X <Y>  ( <Y> • ) ) s4 <Y>  ( <Y> ) •

98 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 X <Y>  ( <Y> • ) ) s5 s4 <S>  <X> • $ <Y>  ( <Y> ) •

99 Expanded Example DFA <S>  <X> $ <X>  <Y>
26 Expanded Example DFA <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3 Y X ) <S>  <X> • $ s5 s4 <Y>  ( <Y> ) •

100 Expanded Example DFA <S>  <X> $ <X>  <Y>
26 Expanded Example DFA <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   ( <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3 Y X ) <X>  <Y> • s6 <S>  <X> • $ s5 s4 <Y>  ( <Y> ) •

101 Extended Example follow sets
follow(<X>) = follow(<Y>) = grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   $ ), $

102 ( ( ( Y Y Y X ) 27 s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

103 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

104 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

105 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

106 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

107 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

108 Follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

109 Follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 No problem with reduction in s0 (SLR(1)). This entry should be an ERROR because “)” is not a legal prefix. But SLR(1) does not see that, while LR(1) captures it. ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

110 Follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

111 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

112 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

113 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

114 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

115 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

116 Follow(<X>) = { $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

117 Follow(<X>) = { $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 This case arises when the input tape is “($”. In all other cases, you will either see the second “(“ or first “)”. If the string length > 2, we will never be in s1 for this example. s2 collects “(“ on stack. s3 marks the middle. s4 matches “()” and deletes the pair, transiting to s3 via s2. Eventually go from s3 on last “()” reduction to s5 via s6. ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

118 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

119 Follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 This corresponds to the case where input tape is: “()$”. Observe also that “$” is not really an appropriate follow symbol here. Only “)” is correct in this context. The reasoning is that the null production is used only to derive empty string in the language, or to mark the middle. Because we have already seen a “(“, the input is not empty. So, it better be the case that the input is “()”. Later we will see that “$” is the cause for reduce-reduce conflict. ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

120 Follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

121 Follow(<Y>) = { ), $ }
Reduce/reduce conflict! ( s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 Problem Source: Prefix of a string is also in the language All strings (, X, (Y , (Y), and Y are sentential forms. Reduce 3 is correct for entry(s1,$) because of input “(“ of the language, Reduce 5 is correct for entry(s1,$) because of e and “()” (where “(“ is the middle of another string in the language). In the current situation, we also know that “e” is not a possibility given that “(“ has been seen. Unfortunately $ in FOLLOW causes ambiguity even though for the second case the FOLLOW must be “)” for correctness. That is reduce-reduce conflict is resolvable in the favor of REDUCE-3. See how the state itself contains all the information about the legitimate FOLLOW symbol of Y as “)”. ===================================== To understand the details, consider the derivation of e, (, and (). S=>X$=>Y$=>$ S=>X$=>($ S=>X$=>Y$=>(Y)$=>()$ ^^ $ in follow ^^ Real situation in s1: only ) can follow. MORAL FOR LR(1): exploit the LOCAL information in the state to restrict the potential follow symbols, rather than use the GLOBAL information in FOLLOW-set. ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

122 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

123 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

124 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

125 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

126 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

127 follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

128 Follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

129 follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 Note that reduce(5) for state s2 and input $ is actually an error because “(($” is illegal input. Later we will observe that LR(1) parser will quit immediately while SLR(1) continues for one more step. Consider input string: | (($ leading to ((Y|$ Legitimate continuation: ((Y|))$ ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

130 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

131 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

132 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

133 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

134 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

135 follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

136 follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

137 follow(<Y>) = { ), $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

138 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

139 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

140 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

141 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

142 ( ( ( Y Y Y X ) s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

143 follow(<X>) = { $ }
<Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3 Y X ) s6 <S>  <X> • $ s5 s4 <Y>  ( <Y> ) • <X>  <Y> •

144 follow(<X>) = { $ }
s0 ( s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • Y Y s3 Y X <Y>  ( <Y> • ) ) s6 s5 s4 <X>  <Y> • <S>  <X> • $ <Y>  ( <Y> ) •

145 follow(<X>) = { $}
<Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s2 <S>  • <X> $ <X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 ( Y Y <Y>  ( <Y> • ) s3 Y X ) s6 <S>  <X> • $ s5 s4 <Y>  ( <Y> ) • <X>  <Y> •

146 ( ( ( Y Y Y X ) 31 s0 s2 s1 s3 s6 s5 s4 <S>  • <X> $
<X>  • <Y> <X>  • ( <Y>  • ( <Y> ) <Y>  • s0 ( <X>  ( • <Y>  ( • <Y> ) <Y>  • ( <Y> ) <Y>  • s1 Summary of operation: ============== If the string length > 2, we will never be in s1 for this example. s2 collects “(“ on stack. s3 marks the middle. s4 matches “()” and deletes the pair, transiting to s3 via s2. Eventually go from s3 on last “()” reduction to s5 via s6. ( Y Y <Y>  ( <Y> • ) s3 Y X ) <X>  <Y> • s6 <S>  <X> • $ s5 s4 <Y>  ( <Y> ) •

147 Can we eliminate the reduce/reduce conflicts?
31 Can we eliminate the reduce/reduce conflicts?

148 Outline Overview of building a LR(0) parser
31 Outline Overview of building a LR(0) parser Limitations in LR(0) languages Building a SLR(1) parser engine Limitations in SLR(1) languages Building a LR(1) parser engine Building a LALR(1) parse engine

149 LR(1) Items Items will keep information on
31 LR(1) Items Items will keep information on production right-hand-side position (the dot) look ahead symbol LR(1) item is of the form [A   •  a] A    is a production The dot in A   •  denotes the position a is a terminal or the end marker ($) For the item [A   • a] a is the next symbol after A in the string, i.e., there exist a derivation S   A a  Expected follow symbol in the context of the prefix of the production being used. Overall goal of LR(1) items is to potentially resolve conflicts due to an erroneous REDUCE by passing down the follow tokens that can arise when a reduce generates the lhs non-terminal. (FOLLOW sets contain global information corresponding to all RHS occurrences of a non-terminal, the LR(1) approach is to track local follow contexts for each RHS occurrence.) Whenever a non-terminal is rewritten, it keeps track of the expected next symbol in that context.

150 The grammar Terminal symbols End of input symbol LR(1) Items
<S>  <X> $ <X>  ( <X> ) <X>   Terminal symbols ‘(’ ‘)’ End of input symbol ‘$’ [ <S>  • <X> $ ) ] [ <S>  <X> • $ ) ] [ <X>  • ( <X> ) ) ] [ <X>  ( • <X> ) ) ] [ <X>  ( <X> • ) ] [ <X>  ( <X> ) • ) ] [ <X>  • ) ] LR(1) Items [ <S>  • <X> $ ) ] [ <S>  • <X> $ ( ] [ <S>  • <X> $ $ ] [ <S>  <X> • $ ) ] [ <S>  <X> • $ ( ] [ <S>  <X> • $ $] [ <X>  • ( <X> ) ) ] [ <X>  • ( <X> ) ( ] [ <X>  • ( <X> ) $ ] [ <X>  ( • <X> ) ) ] [ <X>  ( • <X> ) ( ] [ <X>  ( • <X> ) $ ] [ <X>  ( <X> • ) ] [ <X>  ( <X> • ) ( ] [ <X>  ( <X> • ) $ ] [ <X>  ( <X> ) • ) ] [ <X>  ( <X> ) • ( ] [ <X>  ( <X> ) • $ ] [ <X>  • ) ] [ <X>  • ( ] [ <X>  • $ ] LR(1) Items [ <S>  • <X> $ ) ] [ <S>  • <X> $ ( ] [ <S>  <X> • $ ) ] [ <S>  <X> • $ ( ] [ <X>  • ( <X> ) ) ] [ <X>  • ( <X> ) ( ] [ <X>  ( • <X> ) ) ] [ <X>  ( • <X> ) ( ] [ <X>  ( <X> • ) ] [ <X>  ( <X> • ) ( ] [ <X>  ( <X> ) • ) ] [ <X>  ( <X> ) • ( ] [ <X>  • ) ] [ <X>  • ( ] LR(1) Items Number of LR(1) items = Number of productions * (Number of terminals + 1) /* +1 for end marker */

151 Calculating the LR(1) Items first() and follow() sets
For any string , first() is the set of terminals that begin the string derived from  For each non-terminal A, follow(A) is the set of terminals that can come after A

152 Algorithm for first() If a is a terminal then first(a) = { a }
32 Algorithm for first() If a is a terminal then first(a) = { a } If A   is a production or A  X1 X2... Xk is a production and   first(X1 ), …, first(Xk) then first(A) = first(A)  {  } If A  X1 X2... Xi Xi Xk is a production and   first(X1 ), …, first(Xi) and the terminal a  first(Xi+1 ) then first(A) = first(A)  { a }

153 Example of first() Grammar first( ) ) = { ) } first( ( ) = { ( }
<S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   first( ) ) = { ) } first( ( ) = { ( } first(Y) = { , ( } first(X) = { , ( } first(S) = { , ( , $ } If a is a terminals then first(a) = { a } If A   is a production or A  X1 X2... Xk is a production and   first(X1 ), …, first(Xk) then first(A) = first(A)  {  } If A  X1 X2... Xi Xi Xk is a production and   first(X1 ), …, first(Xi) and the terminal a  first(Xi+1 ) then first(A) = first(A)  { a }

154 Algorithm for follow()
34 Algorithm for follow() follow(S) = { $ } where S is the start symbol If A  B is a production then follow(B) (first() - {}) If A  B is a production or A  B is a production and   first() follow(B) follow(A)

155 Example of follow() follow(S) = {} follow(X) = { $ }
first( ) ) = { ) } first( ( ) = { ( } first(S) = { (, , $ } first(X) = { (,  } first(Y) = { (,  } Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   follow(S) = {} follow(X) = { $ } follow(Y) = { ), $ } follow(S) = { $ } where S is the start symbol If A  B is a production then follow(B) = follow(B)  (first() - {}) If A  B is a production or A  B is a production and   first() follow(B) = follow(B)  follow(A)

156 Creating a LR(1) Parser Engine
36 Creating a LR(1) Parser Engine Need to define closure() and goto() functions for LR(1) items Need to provide an algorithm to create the DFA Need to provide an algorithm to create the parse table For nodes and edge labels For getting the “connected” graph For filling the parse table with parser actions

157 Closure algorithm closure(I) repeat for all items [A   • X  c] in I
until I does not change return I

158 Closure algorithm closure(I) repeat for all items [A   • X  c] in I
for any production X   until I does not change return I

159 Closure algorithm closure(I) repeat for all items [A   • X  c] in I
38 Closure algorithm closure(I) repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change return I Closure algorithm is computing local follow symbols used in the event of a reduction, not right now. Specifically, for a state containing an item with period followed by the non-terminal, the closure operation adds items for the non-terminal rules with period at the beginning and the following terminal as the lookahead. The computation is “local”.

160 Closure Example I = What is closure([ Y  ( • Y ) $ ]) ? Grammar
<S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

161 Closure Example I = What is closure([ Y  ( • Y ) $ ]) Grammar
repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

162 Closure Example I = What is closure([ Y  ( • Y ) $ ]) Grammar
repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

163 Closure Example I = What is closure([ Y  ( • Y ) $ ])
First( )$ ) = ??? repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

164 Closure Example I = What is closure([ Y  ( • Y ) $ ])
First( )$ ) = { ) } I = repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

165 Closure Example I = What is closure([ Y  ( • Y ) $ ])
First( )$ ) = { ) } I = repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

166 Closure Example I = What is closure([ Y  ( • Y ) $ ]) Grammar
repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

167 Closure Example I = What is closure([ Y  ( • Y ) $ ]) Grammar
repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

168 Closure Example I = What is closure([ Y  ( • Y ) $ ])
First( )$ ) = { ) } I = repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

169 Closure Example I = What is closure([ Y  ( • Y ) $ ])
First( )$ ) = { ) } I = repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

170 Closure Example I = What is closure([ Y  ( • Y ) $ ]) Grammar
repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

171 Closure Example I = What is closure([ Y  ( • Y ) $ ]) Grammar
repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

172 Question: Find the Closure
What is closure([ X  • Y $ ]) ???? I = repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

173 Question: Find the Closure
40 Question: Find the Closure What is closure([ X  • Y $ ]) [ X  • Y $ ] [ Y  • ( Y ) $ ] [ Y  • $ ] I = repeat for all items [A   • X  c] in I for any production X   for any d first(c) I = I  { [X  •  d] } until I does not change Grammar <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>  

174 Goto algorithm return J goto(I, X) J = { }
41 Goto algorithm goto(I, X) J = { } for any item [A   • X  c] in I J = J  {[A   X •  c]} return J

175 Goto Example What is goto([ Y  ( • Y ) $ ], Y) goto(I, X) J = { }
for any item [A   • X  c] in I J = J  {[A   X •  c]}

176 Goto Example What is goto([ Y  ( • Y ) $ ], Y) { } goto(I, X) J = { }
{ } goto(I, X) J = { } for any item [A   • X  c] in I J = J  {[A   X •  c]}

177 Goto Example What is goto([ Y  ( • Y ) $ ], Y) { } goto(I, X) J = { }
{ } goto(I, X) J = { } for any item [A   • X  c] in I J = J  {[A   X •  c]}

178 Goto Example What is goto([ Y  ( • Y ) $ ], Y) {[ Y  ( Y •) $ ]}
goto(I, X) J = { } for any item [A   • X  c] in I J = J  {[A   X •  c]}

179 Goto Example What is goto([ Y  ( • Y ) $ ], Y) {[ Y  ( Y •) $ ]}
goto(I, X) J = { } for any item [A   • X  c] in I J = J  {[A   X •  c]}

180 Building the DFA Repeat until no more additions possible
43 Building the DFA Start with the item [<S’>  • <S> $ ?] where ? represents any terminal Find the closure of the item and make a state Pick a state I for each item [A  • X  c] in I find goto(I, X) if goto(I, X) is not already a state, make one Add an edge X from state I to goto(I, X) state Repeat until no more additions possible

181 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>  

182 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<S>  • <X> $ ?]

183 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   closure([<S>  • <X> $ ?]) = ??? first( $? ) = { $ } [<S>  • <X> $ ?]

184 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   closure([<S>  • <X> $ ?]) = ??? first( $? ) = { $ } closure([<S>  • <X> $ ?]) = ??? first( $? ) = { $ } [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $]

185 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0

186 Expanded Example DFA goto( , ( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   goto( , ( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 (

187 Expanded Example DFA goto( , ( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   goto( , ( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 [<X>  ( • $] [<Y>  ( • <Y> ) $] (

188 Expanded Example DFA closure( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   closure( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 [<X>  ( • $] [<Y>  ( • <Y> ) $] (

189 Expanded Example DFA closure( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   closure( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] (

190 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

191 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

192 Expanded Example DFA goto( , ( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   goto( , ( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

193 Expanded Example DFA goto( , ( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   goto( , ( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<Y>  ( • <Y>) )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

194 Expanded Example DFA closure( ) = ??? <S>  <X> $
<X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   closure( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<Y>  ( • <Y>) )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

195 Expanded Example DFA first( )) ) = ??? closure( ) = ???
<S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   first( )) ) = ??? closure( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<Y>  ( • <Y>) )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

196 Expanded Example DFA first( )) ) = { ) } closure( ) = ???
<S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   first( )) ) = { ) } closure( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<Y>  ( • <Y>) )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

197 Expanded Example DFA first( )) ) = { ) } closure( ) = ???
<S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   first( )) ) = { ) } closure( ) = ??? [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

198 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( s2 [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

199 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( s2 [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

200 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<Y>  ( • <Y> ) )] ( [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( s2 [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

201 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<Y>  ( • <Y> ) )] [<Y>  • ( <Y> ) )] [<Y>  • )] ( [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( s2 [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

202 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 (

203 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( X [<S>  <X> •$ ?]

204 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( X [<S>  <X> •$ ?] s5

205 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y X [<X>  <Y> • $] [<S>  <X> •$ ?] s5

206 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y X [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5

207 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] Y X [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5

208 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5

209 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 [<Y>  (<Y>) • $]

210 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

211 Expanded Example DFA <S>  <X> $ <X>  <Y>
<Y>  ( <Y> ) <Y>   [<Y>  (<Y> • ) )] Y ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

212 Expanded Example DFA <S>  <X> $ <X>  <Y>
46 Expanded Example DFA <S>  <X> $ <X>  <Y> <X>  ( <Y>  ( <Y> ) <Y>   [<Y>  (<Y> • ) )] s7 Y ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 For a state containing an item with period followed by the non-terminal, the closure operation adds items for the non-terminal rules with period at the beginning and the following terminal as the lookahead. The computation is “local”. ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

213 <S>  <X> $ <X>  <Y> <X>  (
<Y>  ( <Y> ) <Y>   Y [<Y>  (<Y> • ) )] s7 ) s8 [<Y>  (<Y>) • )] ( [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

214 Creating the LR(1) parse tables
47 Creating the LR(1) parse tables For each state Transition to another state using a terminal symbol is a shift to that state (shift to sn) Transition to another state using a non-terminal is a goto that state (goto sn) If there is an item [A   • a] in the state, do a reduction for input symbol a with the production (reduce k)

215 s7 … s8 => Y ) ( ( ( Y Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] s0 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

216 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

217 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

218 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

219 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] s1 ( Y [<Y>  (<Y> • ) $] s3 Y X ) [<X>  <Y> • $] s6 [<S>  <X> •$ ?] s5 s4 [<Y>  (<Y>) • $]

220 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

221 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

222 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

223 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

224 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

225 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

226 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

227 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

228 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

229 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

230 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

231 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

232 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

233 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

234 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

235 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

236 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 Reached only after matched parenthsis consumed. ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

237 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 Observe the reduction in s4 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

238 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

239 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

240 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

241 s7 … s8 => Y ) ( ( ( Y Y X ) s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

242 s7 … s8 => Y ) ( ( ( Y Y X ) 51 s7 s8 s0 s2 s1 s3 s6 s5 s4 s7
[<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 For input (|$ , that is lookahead $, reduce in s using X -> ( do not reduce using Y -> e For input (|)$, that is lookahead ), reduce in s1 using Y -> e. do not reduce using X -> ( For input (|())$, that is lookahead (, shift ) in s1. do not reduce ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

243 s3 … s4 => Y ) ( ( ( Y Y X ) 51 s7 s8 s0 s2 s1 s3 s6 s5 s4 s7 s7 s8
reduce (4) error s3 … s4 => Y [<Y>  (<Y> • ) )] s7 ) s8 [<Y>  (<Y>) • )] ( s0 ( s2 [<S>  • <X> $ ?] [<X>  • <Y> $] [<X>  • ( $] [<Y>  • (<Y>) $] [<Y>  • $] [<Y>  ( • <Y>) )] [<Y>  • ( <Y> ) )] [<Y>  • )] s1 s7 and s8 transitions are similar to s3 and s4 transitions except for the column of reduction. The DFA can be imagined as gluing four sets of computations: e, (, (), (^n)^n. ( [<X>  ( • $] [<Y>  ( • <Y> ) $] [<Y>  • ( <Y>) )] [<Y>  • )] Y s3 Y X [<Y>  (<Y> • ) $] ) s6 s5 s4 [<X>  <Y> • $] [<S>  <X> •$ ?] [<Y>  (<Y>) • $]

244 52 LR(1) s7 s7 … s8 => SLR(1) Note also that the entry corresponding to (row s2, column $). SLR(1) table has a reduce(5) action based on follow symbol $ which is incorrect because what is allowed there is only ). LR(1) table makes that entry error – this catches mistakes earlier. MORAL: SLR(1) claims large set of symbols can follow a nonterminal than is in fact the case. LR(1) captures reality better by focusing on admissible follow symbols. As a result, the allowable reductions in an LR(1) table are smaller than those in SLR(1) table. If SLR(1) table had multiple REDUCE entries, LR(1) table can have single REDUCE entry, thereby getting rid of the REDUCE-REDUCE conflict. If SLR(1) table had one REDUCE entry, LR(1) table can have an ERROR entry there, as shown in the above diagram. =================================================== s2 – result of consuming two left parens – after getting into s2 if we see $, it is an error. There is no point in reducing on follow symbol $. (Legal strings can be empty (s0) or single left paren (s1) for which reduction on $ makes sense.) so –with right paren lookahead not possible. (No point reducing even though in some other context X is generated when right paren detected.)


Download ppt "Implementing a LR parser engine"

Similar presentations


Ads by Google