Upload
brenna
View
33
Download
0
Embed Size (px)
DESCRIPTION
Implementing LR Parsers. Adapted from Notes by Prof. Saman Amarasinghe (MIT). LR parsing tables and building LR(0), SLR(1) and LR(1) Parsers. 11. Actions of a Shift-Reduce Parser. Shift to s n Push input token into the symbol stack Push s n into state stack - PowerPoint PPT Presentation
Citation preview
Implementing LR Parsers
LR parsing tables and building LR(0), SLR(1) and LR(1)
Parsers
Adapted from Notes by Prof. Saman
Amarasinghe (MIT)
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
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
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
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)
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)
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>
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
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
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
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
<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
<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
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
[<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>) • )]
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
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
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
[<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
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)
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
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)
A Hierarchy of Grammar Classes
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.
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.
LR vs LL
In general, LL(k) grammars are LR(k) grammars, for any k.
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 |
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
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
LL(1) vs SLR(1)
• The following grammar is LL(1), but not SLR(1).S 1X | 2AgX Af | BgA 3 | B 4 |
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