Implementing LR Parsers
description
Transcript of Implementing LR Parsers
![Page 1: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/1.jpg)
Implementing LR Parsers
LR parsing tables and building LR(0), SLR(1) and LR(1)
Parsers
Adapted from Notes by Prof. Saman
Amarasinghe (MIT)
![Page 2: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/2.jpg)
Actions of a Shift-Reduce Parser• Shift to sn
– Push input token into the symbol stack– Push sn into state stack– Advance to next input symbol
• Reduce– Pop both stacks as many times as the number of symbols on
the RHS of rule n – Push LHS of rule n into symbol stack– Lookup [top of the state stack][top of symbol stack]– Push that state (in goto k) into state stack
• Accept– End of stream reached and stack only has the start symbol
• Reject– End of stream reached but stack has more than the start symbol
11
![Page 3: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/3.jpg)
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)ParserEngine
53
![Page 4: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/4.jpg)
Creating the LR(0) DFA
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3(
<X> ( ) •
)s5<X> ( <X> ) •
)s4
![Page 5: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/5.jpg)
Creating the LR(0) parse tables
• For each state
50
• 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 do a reduction with that production for all terminals (reduce k)
![Page 6: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/6.jpg)
Building LR(0) Parse Table Example
<S> • <X> $<X> • ( <X> )<X> • ( )
s0<S> <X> • $
s1X
<X> ( • <X> )<X> ( • )<X> • ( <X> )<X> • ( )
s2
(<X> ( <X> • )
X
s3(
<X> ( ) •
)s5<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s3s3 error shift to s4 error s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
![Page 7: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/7.jpg)
Changing the Language • String of one more more left parentheses followed
by the same number of right parentheses<S> <X> $<X> ( <X> )<X> ( )
• String of zero or more more left parentheses followed by the same number of right parentheses<S> <X> $<X> ( <X> )<X>
![Page 8: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/8.jpg)
Building the LR(0) Parse Table
<S> • <X> $<X> • ( <X> )<X> •
s0<S> <X> • $
s1X
<X> ( • <X> )<X> • ( <X> )<X> •
s2
(<X> ( <X> • )
X
s3(
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2/reduce (3) reduce (3) reduce(3) goto s1s1 error error accept s2 shift to s2/reduce (3) reduce (3) reduce(3) goto s3s3 error shift to s4 error s4 reduce(2) reduce(2) reduce(2)
Shift/reduce conflict Shift/reduce
conflict
16
![Page 9: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/9.jpg)
Idea behind SLR(1) grammars• 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• Do the reduction only when the input symbol
truly follows the reduction– Need to calculate the terminals that can follow a
non-terminal symbol
![Page 10: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/10.jpg)
Building the SLR(1) Parse Table
<S> • <X> $<X> • ( <X> )<X> •
s0<S> <X> • $
s1X
<X> ( • <X> )<X> • ( <X> )<X> •
s2
(<X> ( <X> • )
X
s3(
<X> ( <X> ) •
)s4
ACTION GotoState ( ) $ Xs0 shift to s2 reduce (2) reduce(2) goto s1s1 error error accept s2 shift to s2 reduce (3) reduce(3) goto s3s3 error shift to s4 error s4 error reduce (2) reduce (2)
follow(<X>) = { ), $ }follow(<S>) = { $ }
22
![Page 11: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/11.jpg)
Expanded Example Grammar
<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
![Page 12: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/12.jpg)
<S> • <X> $
<X> • <Y><X> • ( <Y> •
( <Y> )<Y> •
s0
Y<S> <X>
• $s5
( Y
s4<Y> ( <Y>
) •
)<Y> ( <Y> • )
s3
<X> ( •<Y> ( •
<Y> ) <Y> •
( <Y> )<Y> •
s1
(<Y> ( •
<Y> ) <Y> •
( <Y> )<Y> •
s2(
<X> <Y> •
s6X
Y
Expanded Example DFA<S> <X> $
<X> <Y>
<X> ( <Y> ( <Y> ) <Y>
26
![Page 13: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/13.jpg)
<S> • <X> $<X> • <Y><X> • ( <Y> • ( <Y> )<Y> •
s0
Y<S> <X> • $s5
( Y
s4<Y> ( <Y> ) •
)<Y> ( <Y> • )s3
ACTION GotoState ( ) $ X Ys0 shift to s1 reduce (5) reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce(3 or 5) goto s3s2 shift to s2 reduce (5) reduce (5) goto s3s3 error shift to s4 error s4 error reduce (4) reduce (4) s5 error error accept s6 error error reduce (2)
<X> ( •<Y> ( • <Y> ) <Y> • ( <Y> )<Y> •
s1
(<Y> ( • <Y> ) <Y> • ( <Y> )<Y> •
s2(
<X> <Y> •s6
XY
31
![Page 14: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/14.jpg)
LR(1) Items• Items will keep info 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
31
![Page 15: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/15.jpg)
[<S> • <X> $ ?][<X> • <Y> $][<X> • ( $][<Y> • (<Y>) $][<Y> • $]
s0
Y[<S> <X> •$ ?]s5
( Y
s4[<Y> (<Y>) • $]
)[<Y> (<Y> • ) $]s3
[<X> ( • $][<Y> ( • <Y> ) $][<Y> • ( <Y>) )][<Y> • )]
s1
([<Y> ( • <Y>) )][<Y> • ( <Y> ) )][<Y> • )]
s2(
[<X> <Y> • $]s6
X
Expanded Example LR(1) DFA
<S> <X> $<X> <Y>
<X> ( <Y> ( <Y> )
<Y>
46
Y[<Y> (<Y> • ) )]s7
)s8[<Y> (<Y>) • )]
![Page 16: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/16.jpg)
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
[<S> • <X> $ ?][<X> • <Y> $][<X> • ( $][<Y> • (<Y>) $][<Y> • $]
s0
Y[<S> <X> •$ ?]s5
( Y
s4[<Y> (<Y>) • $]
)[<Y> (<Y> • ) $]s3
[<X> ( • $][<Y> ( • <Y> ) $][<Y> • ( <Y>) )][<Y> • )]
s1
([<Y> ( • <Y>) )][<Y> • ( <Y> ) )][<Y> • )]
s2(
[<X> <Y> • $]s6
X
51
Y[<Y> (<Y> • ) )]s7
)s8[<Y> (<Y>) • )]
s7 … s8 =>
s7
![Page 17: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/17.jpg)
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
[<S> • <X> $ ?]
[<X> • <Y> $]
[<X> • ( $]
[<Y> • (<Y>) $]
[<Y> • $]
s0
Y[<S> <X>
•$ ?]s5
( Y
s4[<Y> (<Y>)
• $]
)[<Y> (<Y> • ) $]
s3
[<X> ( • $]
[<Y> ( • <Y> ) $]
[<Y> • ( <Y>) )]
[<Y> • )]
s1
([<Y> ( • <Y>)
)][<Y> •
( <Y> ) )][<Y> •
)]
s2(
[<X> <Y> • $]
s6X
51
Y[<Y> (<Y> • )
)]s7
)s8[<Y> (<Y>)
• )]
s3 … s4 =>
s7s
7s8reduce
(5)errors
8
![Page 18: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/18.jpg)
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
ACTION GotoState ( ) $ X Ys0 shift to s1 reduce (5) reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce(3 or 5) goto s3s2 shift to s2 reduce (5) reduce (5) goto s3s3 error shift to s4 error s4 error reduce (4) reduce (4) s5 error error accept s6 error error reduce (2)
SLR(
1)LR
(1)
52
s7 … s8 => similar to s3…s4 except for “reduce” column.
s7
![Page 19: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/19.jpg)
[<S> • <X> $ ?][<X> • <Y> $][<X> • ( $][<Y> • (<Y>) $][<Y> • $]
s0
Y[<S> <X> •$ ?]s5
( Y
s4[<Y> (<Y>) • {$,)}]
)[<Y> (<Y> • ) {$,)}]s3
[<X> ( • $][<Y> ( • <Y> ) $][<Y> • ( <Y>) )][<Y> • )]
s1
([<Y> ( • <Y>) )][<Y> • ( <Y> ) )][<Y> • )]
s2(
[<X> <Y> • $]s6
X
Example -- LALR(1) DFA
<S> <X> $<X> <Y>
<X> ( <Y> ( <Y> )
<Y>
46
Y
![Page 20: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/20.jpg)
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
[<S> • <X> $ ?][<X> • <Y> $][<X> • ( $][<Y> • (<Y>) $][<Y> • $]
s0
Y[<S> <X> •$ ?]s5
( Y
)
[<X> ( • $][<Y> ( • <Y> ) $][<Y> • ( <Y>) )][<Y> • )]
s1
([<Y> ( • <Y>) )][<Y> • ( <Y> ) )][<Y> • )]
s2(
[<X> <Y> • $]s6
X
51
Y
s4[<Y> (<Y>) • {$,)}]
[<Y> (<Y> • ) {$,)}]s3
reduce(4)
![Page 21: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/21.jpg)
LALR
(1)
52
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
reduce(4)
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
s7 … s8 =>
LR(1
)
s7
![Page 22: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/22.jpg)
ACTION GotoState ( ) $ X Ys0 shift to s1 reduce (5) reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce(3 or 5) goto s3s2 shift to s2 reduce (5) reduce (5) goto s3s3 error shift to s4 error s4 error reduce (4) reduce (4) s5 error error accept s6 error error reduce (2)
SLR(
1)LA
LR(1
)52
ACTION GotoState ( ) $ X Ys0 shift to s1 error reduce (5) goto s5 goto s6s1 shift to s2 reduce (5) reduce (3) goto s3s2 shift to s2 reduce (5) error goto s3s3 error shift to s4 error s4 error error reduce (4) s5 error error accept s6 error error reduce (2)
reduce(4)
![Page 23: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/23.jpg)
A Hierarchy of Grammar Classes
![Page 24: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/24.jpg)
LR Languages
• The set of LR languages are independent of the lookahead distance k.
• For all the languages with SLR(1), LALR(1) and LR(1) grammars we looked at, we could have found an LR(0) grammar!!!
• This grammar may not be natural however.
![Page 25: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/25.jpg)
LR Languages
• Given any LR(k) grammar Gk, if L(Gk) is prefix-free, then there exists an (equivalent) LR(0) grammar G0 such that L(Gk) = L(G0)– A language L is prefix-free iff [x in L and xy in L
implies y is . – A language can be made prefix-free by
concatenating a special end-marker to each string in the language.
![Page 26: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/26.jpg)
LR vs LL
In general, LL(k) grammars are LR(k) grammars, for any k.
![Page 27: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/27.jpg)
LR(0) vs LL(k)• The following LR(0) grammar (with left
recursion) is not LL(1).S S0 | 0
– The following LR(0) grammar (with left recursion) is not LL(k), for any k.
S S0 | S1 | 0 | 1• The following LL(1) grammar is not LR(0).
S Z$Z aZ |
![Page 28: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/28.jpg)
Not LR(k) for an k
• The following regular grammar (with left recursion) is not LR(k), for any k.
S Cc | BbC Ca | a B Ba | a
![Page 29: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/29.jpg)
SLR(1) vs LL(k)
• The following SLR(1) grammar (and hence, LR(1) grammar) requiring left factoring is not LL(k), for any k.
E T+E | TT x *T | x
![Page 30: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/30.jpg)
LL(1) vs SLR(1)
• The following grammar is LL(1), but not SLR(1).S 1X | 2AgX Af | BgA 3 | B 4 |
![Page 31: Implementing LR Parsers](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815b17550346895dc8c77a/html5/thumbnails/31.jpg)
LL(1) vs LALR(1)
• The following grammar is LL(1), but not LALR(1).S aF | bGF Xc | Yd G Xd | Yc X IA Y IB A I