0% found this document useful (0 votes)
12 views49 pages

07 - SLR Parsers - LR

The document discusses SLR parsers and LR(0) items, focusing on the mechanics of bottom-up parsing and the use of parsing tables. It explains the construction of SLR parsing tables, the role of look-ahead symbols, and the handling of shift/reduce conflicts. Additionally, it highlights the differences between SLR, canonical LR, and LALR parsers, as well as the importance of closure and goto functions in the parsing process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views49 pages

07 - SLR Parsers - LR

The document discusses SLR parsers and LR(0) items, focusing on the mechanics of bottom-up parsing and the use of parsing tables. It explains the construction of SLR parsing tables, the role of look-ahead symbols, and the handling of shift/reduce conflicts. Additionally, it highlights the differences between SLR, canonical LR, and LALR parsers, as well as the importance of closure and goto functions in the parsing process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

SLR Parsers, LR (0) items

Bottom-up Parsers
• Simple Shift-reduce parsers has lot of Shift/Reduce conflicts
• Operator precedence parsers is for a small class of grammars
• Go for LR parsers
LR Parsers
• LR(1) parsers recognize the languages in which one symbol of look-
ahead is sufficient to decide whether to shift or reduce
• L : for left-to-right scan of the input
• R : for reverse rightmost derivation
• 1: for one symbol of look-ahead
LR Parsers
• Read input, one token at a time
• Use stack to keep track of current state
• The state at the top of the stack summarizes the information below.
• The stack contains information about what has been parsed so far.
LR Parsers
• Use parsing table to determine action based on current
state and look-ahead symbol.
• Parsing table construction takes into account the shift,
reduce, accept or error action
LR Parsers
• SLR
• Simple LR parsing
• Easy to implement, but not powerful
• Uses LR(0) items
• Canonical LR
• Larger parser but powerful
• Uses LR(1) items
• LALR
• Condensed version of canonical LR
• May introduce conflicts
• Uses LR(1) items
SLR Parsers - Handle
• As a SLR parser processes the input, it must identify all possible
handles.
• For example, consider the usual expression grammar and the input
string
a + b.
SLR Parsers
• If the parser has processed ‘a’ and reduced it to E. Then, the current
state can be represented by E • +E where • means
• E has already been parsed and
• +E is a potential suffix, which, if determined, yields to a successful parse.
SLR parsers
• Our ultimate aim is to finally reach state E+E•,
which corresponds to an actual handle yielding to
the reduction E→E+E
SLR Parsers
• LR parsing works by building an automata where each state
represents what has been parsed so far and what we intend to
parse after looking at the current input symbol. This is indicated
by productions having a “.” These productions are referred to as
items.
• Items that has the “.” at the end leads to the reduction by that
production
input a1 a2 … ai … an $

stack
LR Parsing Program
sm (driver)
output

Xm
sm-1
Xm-1 action goto

DFA
s0
SLR (1) Parser
• Form the augmented grammar
• Construction of LR(0) items
• Construct the follow() for all the non-terminals which
requires construction of first() for all the terminals and non-
terminals
SLR(1) parser
• Using this and the follow( ) of the grammar, construct the parsing
table
• Using the parsing table, a stack and an input parse the input
LR (0) items
• An LR(0) item of a grammar G is a production of G with a • at some
position of the right-hand side
• Thus, a production
A→XYZ
has four items:
[A → • X Y Z]
[A → X • Y Z]
[A → X Y • Z]
[A → X Y Z •]
• that production A →  has one item [A → •]
LR (0) items
• The grammar is augmented with a new start symbol S’ and
production S’→S
• Initially, set C = closure({[S’→•S]})
• For each set of items I  C and each grammar symbol X  (NT)
such that goto(I,X)  C and goto(I,X)  ,
• add the set of items goto(I,X) to C
• Repeat until no more sets can be added to C
Closure (I)
• Start with closure(I) = I
• If [A→•B]  closure(I) then for each production B→ in the
grammar, add the item [B→•] to closure(I) if it is not already
there
• Repeat 2 until no new items can be added to closure(I)
Goto (I, X)
• For each item [A→•X]  I, add the set of items
closure({[A→X•]}) to goto(I,X) if not already there
• Repeat until no more items can be added to goto(I,X)
• Intuitively, goto(I,X) is the set of items that are valid for the viable
prefix X when I is the set of items that are valid for 
Augmented Grammar
E’ → E
1. E→E+T
2. E→T
3. T→T*F
4. T→F
5. F → (E)
6. F → id
Augmented Grammar
E’ → E
1. E→E+T
2. E→T
3. T→T*F
4. T→F
5. F → (E)
6. F → id
I0
• E’ → .E
• E → . E +T
• E → .T
• T → .T * F
• T → .F
• F → . (E)
• F → . id
Items
• I0 • I1 = Goto (I0 , E)
• E’ → .E • E’ → E.
• E → . E +T •E→E.+T
• E → .T
• T → .T * F • I2 = Goto (I0 , T), Goto (I4 , T),
• T → .F E → T.
• F → . (E) T → T.*F
• F → . id
• I3 = Goto (I0 , F), Goto (I4 , F), Goto • I4 = Goto (I0, ( ), Goto (I4, (),
(I6 , F) Goto (I6, (), Goto (I7,()
• T → F. • F → (.E)
• E → .E + T
• I5= Goto (I0 , id), Goto (I4 , id), Goto • E → .T
(I6, id), Goto (I7, id) • T → .T * F
F → id . • T → .F
• F → .(E)
• F → .id
Items
I8= Goto (I4 , E)
I6= Goto (I1 , +), Goto (I8 , +), F → (E.)
E→E+.T E → E . +T
T→ .T * F
T → .F I9 = Goto(I6 , T)
F→ .(E) E → E + T.
F → .id T→ T. * F

I7: Goto (I2, * ), Goto (I9, * ) I10 : Goto (I7, F )


T→ T * . F T → T * F.
F → .(E)
I11 : Goto (I8, ) )
F → .id
F → (E).
SLR Parsing Table
• Input: Augmented Grammar G’
• Output: SLR parsing table with functions, shift, reduce and
accept
• Parsing table is between items and Terminals and non-
terminals
• The non-terminals correspond to the goto() of the items set
• The terminals have the parsing table corresponding to the
action – shift / reduce/accept
SLR Parsing Table
• Augment the grammar with S’→S
• Construct the set C={I0,I1,…,In} of LR(0) items
• If [A→•a]  Ii and goto(Ii,a)=Ij then set action[i,a]=shift j, where
a is a terminal
• If [A→•]  Ii then set action[i,a]=reduce A→ for all a 
FOLLOW(A) where AS’)
SLR parsing table
• If [S’→S•] is in Ii then set action[i,$]=accept
• If goto(Ii,A)=Ij then set goto[i,A]=j
• Repeat for all the items until no more entries added
• The initial state i is the Ii holding item [S’→•S]
• All other entries are error
Grammar
• E’ →E
• E → E +T
•E→T
• T→T*F
• T→F
• F → (E)
• F → id
Follow
• Follow (E) = {$, +, ) }
• Follow (T) = {$, +, *, ) }
• Follow (F) = {$, +, *, ) }
• si means shift state i
• rj means reduce by production numbered j
• Blank means error
Shift, Accept, Reduce
State Action Goto
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 accept
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
Shift, Accept and Reduce
State Action Goto
id + * ( ) $ E T F
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3 11
11 r5 r5 r5 r5
SLR Parsing

(s0 X1 s1 X2 s2 … Xm sm, ai ai+1 … an $)

stack input
Parsing action
• If action[sm,ai] = shift s, then push ai, push s, and advance input:
(s0 X1 s1 X2 s2 … Xm sm ai s, ai+1 … an $)
• If action[sm,ai] = reduce A →  and goto[sm-r,A] = s with r=|| then
pop 2r symbols, push A, and push s:
(s0 X1 s1 X2 s2 … Xm-r sm-r A s, ai ai+1 … an $)
• If action[sm,ai] = accept, then stop
• If action[sm,ai] = error, then attempt recovery
Parsing algorithm
• Set input to point to the first symbol of w$
• Repeat
• Let s be the state on the top of the stack
• Let a be the symbol pointed to by ip
• If action [s, a] = shift s’ then
• Push a then s’ on top of the stack
• Move input to the next input symbol
• Else if action [s, a] = reduce A → β then
• Pop 2 * | β | symbols off the stack
• Let s’ be the state now on the top of the stack
• Push A then goto [s’, A] on top of the stack
• Output the production A → β
• Else if action[s, a] = accept then return;
• Else error()
Parsing action
Stack Input Action
0 id * id + id $ [0, id] → s5 , shift
0 id 5 * id + id $ [5, *] → r6, pop 2 symbols,
Goto[0, F] → 4
0F3 * id + id $ [3, *] → r4, pop 2 symbols,
Goto[0, T] → 2
0T2 * id + id $ [2, *] → s7, shift

0T2*7 id + id $ [7, id] → s5, shift


0 T 2 * 7 id 5 + id $ [5, +] → r6, pop 2 symbols,
Goto[7, F] → 10
0T2 + id $ [2, +] → r2, pop 2 symbols
and goto [0 , E] → 1
Parsing action
Stack Input Action
0E1 + id $ [1, +] → s6, shift
0E1+6 id $ [6, id] → s5, shift
0 E 1 + 6 id 5 $ [5, $] → r6, pop 2 symbols,
goto [6, F] → 3
0E1+6F3 $ [3 , $] → r4, pop 2 symbols,
goto [ 6, T] → 9
0E1+6T9 $ [9, $] → r1, pop 6 symbols,
goto [0, E] → 1
0E1 $ [1, $] → accept, hence
successful parsing
Problems with SLR grammar
• Every SLR grammar is unambiguous, but not every
unambiguous grammar is SLR
• Consider for example the unambiguous grammar
Example
• S→L=R
• S→ R
•L→*R
• L → id
• R→L
Items set
• I0: • I1: (I0 , S)
S’ → •S S’ → S•
1. S → •L=R • I2: (I0 , L)
2. S → •R S → L•=R
R → L•
3. L → •*R
• I3: (I0 ,R)
4. L → •id S → R•
5. R → •L
Items set
• I4: (I0 , *) (I4 , *) (I6 , *) • I6: (I2 , =)
L → *•R S → L=•R
R → •L R → •L
L → •*R
L → •*R L → •id
L → •id
• I7: (I4 , R)
• I5: (I0 , id) (I4 , id) (I6 , id) L → *R•
L → id•
• I8: (I4 , L) (I6 , L)
• I9: (I6 , R) R → L•
S → L=R•
• Follow (S) = { $}
• Follow (L) = { =, $ }
• Follow (R) = { $, =}
State Action Goto
id = * $ S L R
0 s5 s4 1 2 3
1 accept
2 S6 /r5 r5
3 r2
4 s5 s4 8 7
5 r4 r4
6 s5 s4 8 9
State Action Goto
id = * $ S L R
7 r3 r3
8 r5 r5
9 r1
Conflict
• Shift / reduce conflict arises
• Because the grammar is not SLR(1)
• Follow information alone is not sufficient
• Hence, powerful parser is required
Summary
• Learnt to parse the SLR(1) grammar using the
SLR(1) parsing algorithm
• Some grammar results in Shift / Reduce conflict

You might also like