handles and lr(1) parsing - compiler design - dr. d. p. sharma - nit surathkal by wahid311
DESCRIPTION
Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311TRANSCRIPT
![Page 1: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/1.jpg)
1
Expression GrammarExpression GrammarExpression GrammarExpression Grammar1 Goal → Expr2 Expr → Expr + Term3 | Expr – Term4 | Term5 Term → Term Factor6 | Term ∕ Factor7 | Factor8 Factor → num9 | id10
| ( Expr )
![Page 2: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/2.jpg)
2
The states of the bottom-up parser on input x – 2 y(tokenized as id – num * id) are
![Page 3: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/3.jpg)
3
word Stack Handle Action
1 id ► - none - shift
2 – id ► Factor → id,1 reduce
3 – Factor ► Term → Factor,1 reduce
4 – Term ► Expr → Term,1 reduce
5 – Expr ► - none - shift
6 num Expr – ► - none - shift
7 Expr – num ► Factor → num,3 reduce
8 Expr – Factor ► Term → Factor,3 reduce
![Page 4: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/4.jpg)
4
word Stack Handle Action
9 Expr – Term ► - none - shift
10
id Expr – Term ► - none - shift
11
$ Expr – Term id► Factor → id,5 reduce
12
$ Expr–Term Factor► Term→TermFactor,5
reduce
13
$ Expr – Term ► Expr → Expr – Term,3 reduce
14
$ Expr ► Goal → Expr,1 reduce
15
$ Goal - none - accept
![Page 5: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/5.jpg)
5
HandlesHandlesHandlesHandles The handle-finding
mechanism is the key to effiecient bottom-up parsing
As it process an input string, the parser must find and track all potential handles
![Page 6: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/6.jpg)
6
HandlesHandlesHandlesHandles The handle-finding
mechanism is the key to effiecient bottom-up parsing
As it process an input string, the parser must find and track all potential handles
![Page 7: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/7.jpg)
7
HandlesHandlesHandlesHandles For example, every legal
input eventually reduces the entire frontier to grammar’s goal symbol
Thus, Goal → Expr,1 is a potential handle at the start of every parse
![Page 8: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/8.jpg)
8
HandlesHandlesHandlesHandles For example, every legal
input eventually reduces the entire frontier to grammar’s goal symbol
Thus, Goal → Expr,1 is a potential handle at the start of every parse
![Page 9: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/9.jpg)
9
HandlesHandlesHandlesHandles As the parser builds a
derivation, it discovers other handles
At each step, the set of potential handles represent different suffixes that lead to a reduction.
![Page 10: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/10.jpg)
10
HandlesHandlesHandlesHandles As the parser builds a
derivation, it discovers other handles
At each step, the set of potential handles represent different suffixes that lead to a reduction.
![Page 11: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/11.jpg)
11
HandlesHandlesHandlesHandles Each potential handle
represent a string of grammar symbols that, if seen, would complete the right-hand side of some production.
![Page 12: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/12.jpg)
12
HandlesHandlesHandlesHandles
For the bottom-up parse of the expression grammar string, we can represent the potential handles that the shift-reduce parser should track
![Page 13: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/13.jpg)
13
HandlesHandlesHandlesHandlesUsing the placeholder to represent top of the stack, there are nine handles:
![Page 14: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/14.jpg)
14
Same
Handles
1 Factor → id
2 Term → Factor
3 Expr → Term
4 Factor → num
5 Term → Factor
6 Factor → id
7 Term → Term Factor
8 Expr → Expr – Term
9 Goal → Expr
Same
![Page 15: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/15.jpg)
15
HandlesHandlesHandlesHandles This notation shows that the
second and fifth handles are identical, as are first and sixth
It also create a way to represent the potential of discovering a handle in future.
![Page 16: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/16.jpg)
16
HandlesHandlesHandlesHandles This notation shows that the
second and fifth handles are identical, as are first and sixth
It also create a way to represent the potential of discovering a handle in future.
![Page 17: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/17.jpg)
17
HandlesHandlesHandlesHandlesConsider the parser’s state in step 6:
6 num Expr – ► - none - shift
7 Expr – num ► Factor → num,3 reduce
8 Expr – Factor ► Term → Factor,3 shift
9 Expr – Term ► - none - shift
10 id Expr – Term ► - none - shift
11 $ Expr – Term id► Factor → id,5 reduce
12 $ Expr–TermFactor►
Term→TermFactor,5
reduce
13 $ Expr – Term ► Expr→Expr–Term,3 reduce
![Page 18: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/18.jpg)
18
HandlesHandlesHandlesHandlesThe parser has recognized Expr –
6 num Expr – ► - none - shift
7 Expr – num ► Factor → num,3 reduce
8 Expr – Factor ► Term → Factor,3 shift
9 Expr – Term ► - none - shift
10 id Expr – Term ► - none - shift
11 $ Expr – Term id► Factor → id,5 reduce
12 $ Expr–TermFactor►
Term→TermFactor,5
reduce
13 $ Expr – Term ► Expr→Expr–Term,3 reduce
It needs to recognize, eventually, a Term before it can reduce this part of the frontier
![Page 19: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/19.jpg)
19
HandlesHandlesHandlesHandles Using the stack-relative
notation, we can represent the parser’s state as Expr → Expr – Term
The parser has already recognized an Expr and a –
![Page 20: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/20.jpg)
20
HandlesHandlesHandlesHandles Using the stack-relative
notation, we can represent the parser’s state as Expr → Expr – Term
The parser has already recognized an Expr and a –
![Page 21: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/21.jpg)
21
HandlesHandlesHandlesHandles If the parser reaches a state
where it shifts a Term on top of Expr and –, it will complete the handleExpr → Expr – Term
![Page 22: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/22.jpg)
22
HandlesHandlesHandlesHandles How many potential handles
must the parser recognize?
The right-hand side of each production can have a placeholder at its start, at its end and between any two consecutive symbols
![Page 23: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/23.jpg)
23
HandlesHandlesHandlesHandles How many potential handles
must the parser recognize?
The right-hand side of each production can have a placeholder at its start, at its end and between any two consecutive symbols
![Page 24: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/24.jpg)
24
HandlesHandlesHandlesHandlesExpr → Expr –TermExpr → Expr – TermExpr → Expr – TermExpr → Expr – Term
if the right-hand side has k symbols, it has k +1 placeholder positions
![Page 25: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/25.jpg)
25
HandlesHandlesHandlesHandles The number of potential handles
for the grammar is simply the sum of the lengths of the right-hand side of all the productions
The number of complete handles is simply the number of productions
![Page 26: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/26.jpg)
26
HandlesHandlesHandlesHandles The number of potential handles
for the grammar is simply the sum of the lengths of the right-hand side of all the productions
The number of complete handles is simply the number of productions
![Page 27: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/27.jpg)
27
HandlesHandlesHandlesHandles These two facts lead to the
critical insight behind LR parsers:
A given grammar generates a finite set of handles (and potential handles) that the parser must recognize
![Page 28: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/28.jpg)
28
HandlesHandlesHandlesHandles These two facts lead to the
critical insight behind LR parsers:
A given grammar generates a finite set of handles (and potential handles) that the parser must recognize
![Page 29: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/29.jpg)
29
LookaheadLookaheadLookaheadLookahead However, it is not a simple
matter of putting the placeholder in right-hand side to generate handles
The parser needs to recognize the correct handle by different right contexts
![Page 30: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/30.jpg)
30
LookaheadLookaheadLookaheadLookahead However, it is not a simple
matter of putting the placeholder in right-hand side to generate handles
The parser needs to recognize the correct handle by different right contexts
![Page 31: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/31.jpg)
31
word Stack Handle Action
9 Expr – Term ► - none - shift
10
id Expr – Term ► - none - shift
11
$ Expr – Term id► Factor → id,5 reduce
12
$ Expr–Term Factor►
Term→TermFactor,5 reduce
13
$ Expr – Term ► Expr → Expr – Term,3 reduce
14
$ Expr ► Goal → Expr,1 reduce
15
$ Goal - none - accept
Consider the parser’s action at step 9.The frontier is Expr – Term, suggesting a handle Expr → Expr – Term However, the parser decides to extend the frontier by shifting on to the stack rather than reducing frontier to Expr
![Page 32: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/32.jpg)
32
LookaheadLookaheadLookaheadLookahead Clearly, this the correct move
for the parser
No potential handle contains Expr followed by
![Page 33: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/33.jpg)
33
LookaheadLookaheadLookaheadLookahead Clearly, this the correct move
for the parser
No potential handle contains Expr followed by
![Page 34: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/34.jpg)
34
LookaheadLookaheadLookaheadLookahead At step 9, the set of potential
handles is
Expr → Expr – Term
Term →Term Factor
Term →Term / Factor
![Page 35: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/35.jpg)
35
LookaheadLookaheadLookaheadLookahead The next input symbol clearly
matches the second choice
Expr → Expr – Term
Term →Term Factor
Term →Term / Factor
![Page 36: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/36.jpg)
36
LookaheadLookaheadLookaheadLookahead The parser needs a basis for
deciding between first (reduce) and second (shift) choices:
Expr → Expr – Term
Term →Term Factor
![Page 37: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/37.jpg)
37
LookaheadLookaheadLookaheadLookahead This requires more context
than the parser has in the frontier (stack)
![Page 38: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/38.jpg)
38
LookaheadLookaheadLookaheadLookahead To choose between reducing
and shifting, the parser must recognize which symbols can occur to the right of Expr and Term in valid phrases
![Page 39: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/39.jpg)
39
LR(1) ParsersLR(1) ParsersLR(1) ParsersLR(1) Parsers The LR(1) parsers can
recognize precisely those languages in which one-symbol lookahead suffices to determine whether to shift or reduce.
![Page 40: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/40.jpg)
40
LR(1) ParsersLR(1) ParsersLR(1) ParsersLR(1) Parsers The LR(1) construction
algorithm builds a handle-recognizing DFA
The parsing algorithm uses this DFA to recognize handles and potential handles on the parse stack
![Page 41: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/41.jpg)
41
LR(1) ParsersLR(1) ParsersLR(1) ParsersLR(1) Parsers The LR(1) construction
algorithm builds a handle-recognizing DFA
The parsing algorithm uses this DFA to recognize handles and potential handles on the parse stack
![Page 42: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/42.jpg)
42
0
10
1
2 3 4
76 5
11
8 9
Reduce:E → int on ),+
int
E
E
+ (
E
)
+
int
int(
+
)
Reduce:E → int on $,+
accept on $
Reduce:E → E+(E) on ),+
Reduce:E → E+(E) on ),+
![Page 43: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/43.jpg)
43
Parsing DFAParsing DFAParsing DFAParsing DFA►int + (int) + (int) $
shift 1
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ red E → E+(E)
![Page 44: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/44.jpg)
44
Parsing DFAParsing DFAParsing DFAParsing DFA
How can the parser remember the state the DFA goes into on a symbol?
![Page 45: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/45.jpg)
45
Parsing DFAParsing DFAParsing DFAParsing DFA
Store the DFA state in the stack along with the symbol
Initial entry in the stack will be ‘<dummy,0>’.
![Page 46: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/46.jpg)
46
◘ is dummy symbol
◘ 0 ►int + (int)+(int) $
shift 1
◘ 0 int 1 ►+(int)+(int)$
reduce E → int
◘ 0 E 2 ►+(int)+(int)$
shift 3 times
.. ... ... ... ....int
◘
►
stack
0
1
![Page 47: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/47.jpg)
47
◘ 0 ►int + (int)+(int) $
shift 1
◘ 0 int 1 ►+(int)+(int)$
reduce E → int
◘ 0 E 2 ►+(int)+(int)$
shift 3 times
.. ... ... ... ....int
◘
►
stack
0
1
E 2
![Page 48: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/48.jpg)
48
Parsing DFAParsing DFAParsing DFAParsing DFA
How is next state decided upon a reduce?
◘ 0 ►int + (int)+(int) $
shift 1
◘ 0 int 1 ►+(int)+(int)$
reduce E → int
◘ 0 E 2 ►+(int)+(int)$
shift 3 times
.. ... ... ... ....
![Page 49: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/49.jpg)
49
Representing DFARepresenting DFARepresenting DFARepresenting DFA Parsers represent DFA as a
2D table
Rows correspond to DFA states
Columns correspond to terminals and non-terminals
![Page 50: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/50.jpg)
50
Representing DFARepresenting DFARepresenting DFARepresenting DFA Parsers represent DFA as a
2D table
Rows correspond to DFA states
Columns correspond to terminals and non-terminals
![Page 51: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/51.jpg)
51
Representing DFARepresenting DFARepresenting DFARepresenting DFA Parsers represent DFA as a
2D table
Rows correspond to DFA states
Columns correspond to terminals and non-terminals
![Page 52: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/52.jpg)
52
Representing DFARepresenting DFARepresenting DFARepresenting DFA
columns are split into:
those for terminals: action table
those for non-terminals: goto table
![Page 53: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/53.jpg)
53
3
7
6E → int on ),+
(
E
)
int
E → E+(E) on ),+
4
5
state int + ( ) $ E...
3 s4
4 s5 g6
5 r2 r2
6 s8 s7
7 r1 r1
...
action goto
![Page 54: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/54.jpg)
54
Parsing DFAParsing DFAParsing DFAParsing DFA
The state comes from the goto table
◘ 0 ►int + (int)+(int) $
shift 1
◘ 0 int 1 ►+(int)+(int)$
reduce E → int
◘ 0 E 2 ►+(int)+(int)$ shift 3 times
.. ... ... ... ....
![Page 55: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/55.jpg)
55
Building LR(1) TablesBuilding LR(1) TablesBuilding LR(1) TablesBuilding LR(1) Tables To construct Action and Goto
tables, the LR(1) parser generator builds a model of handle-recognizing DFA
The model is used to fill in the tables
![Page 56: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/56.jpg)
56
Building LR(1) TablesBuilding LR(1) TablesBuilding LR(1) TablesBuilding LR(1) Tables To construct Action and Goto
tables, the LR(1) parser generator builds a model of handle-recognizing DFA
The model is used to fill in the tables
![Page 57: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/57.jpg)
57
LR(1) ItemsLR(1) ItemsLR(1) ItemsLR(1) Items The LR(1)-Table construction
needs a concrete representation for the handles and their associated lookahead symbols.
We call this representation an LR(1) item.
![Page 58: Handles and LR(1) Parsing - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311](https://reader036.vdocuments.us/reader036/viewer/2022081419/5517569b497959a0308b4825/html5/thumbnails/58.jpg)
58
LR(1) ItemsLR(1) ItemsLR(1) ItemsLR(1) Items The LR(1)-Table construction
needs a concrete representation for the handles and their associated lookahead symbols.
We call this representation an LR(1) item.