Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
28 views28 pages

04 - CALR Parsing

The document discusses CALR parsing and its advantages over SLR parsers, particularly in resolving shift/reduce conflicts by incorporating lookahead symbols. It explains the construction of LR(1) items and the parsing algorithm, detailing how to build a parsing table and execute parsing actions. The examples illustrate the parsing process with CALR, demonstrating its effectiveness in handling complex grammars without conflicts.
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)
28 views28 pages

04 - CALR Parsing

The document discusses CALR parsing and its advantages over SLR parsers, particularly in resolving shift/reduce conflicts by incorporating lookahead symbols. It explains the construction of LR(1) items and the parsing algorithm, detailing how to build a parsing table and execute parsing actions. The examples illustrate the parsing process with CALR, demonstrating its effectiveness in handling complex grammars without conflicts.
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/ 28

CALR Parsing

Conflict in SLR parsers


• Shift / reduce conflict arises
• Follow information alone is not sufficient to decide when
to reduce.
• Hence, powerful parser is required
Conflicts in SLR parsers
• In SLR, if there is a production of the form A → α▪ , then a reduce
action takes place based on follow(A)
• There would be situations, where when state i appears on the TOS,
the viable prefix βα on the stack is such that βA cannot be followed
by terminal ‘a’ in a right sentential form
• Hence, the reduction A → α would be invalid on input ‘a’
CALR parsers motivation
• If it is possible to do more in the states that allow us to rule out some
of the invalid reduction, introduce more states
• Introduce exactly which input symbols to follow a particular non-
terminal
CALR parsers
• Construct LR(1) items
• Use these items to construct the CALR parsing table involving action
and goto
• Use this table, along with input string and stack to parse the string
CALR motivation
• Extra symbol is incorporated in the items to include a terminal
symbol as a second component
• A →[α .β, a] where A →αβ is a production and ‘a’ is a terminal
or the right end marker $ - LR(1) item
LR(1) item
• 1 – refers to the length of the second component – lookahead of the item
• Lookahead has no effect in A →[α .β , a] where β is not ε, but A →[α . , a] calls
for a reduction A → α if the next input symbol is ‘a’, ‘a’ will be subset of
follow(A)
LR(1) item
• A → [α .β , a] is a valid item for a viable prefix γ if there is a
derivation S => δAw => δαβw where γ = δα and either ‘a’ is the
first symbol of ‘w’ or ‘w’ is ε and ‘a’ is $
LR(1) item algorithm
• Closure (I)
{repeat for each item [A → α▪Bβ, a] in I,
for each production B → γ in G’ and
each terminal b in First(βa) such that [B → .γ , b] is not in I do
add [B → .γ , b] to set I
until no more items can be added to I
end }
Goto(I, X)
Begin
Initialize J to be the empty set
For each item [A→α.Xβ , a] in I such that
add item [A→αX.β , a] to set J;
Return closure(J)
end
Items(G’)
Begin C:= closure ( {S’ → .S, $});
repeat
for each set of items I in C
for each grammar symbol X
if goto(I,X) is not empty and not in C
add goto(I, X) to C;
until no more set of items can be added to C
end
Example
• S → CC • Augmented
• C → cC • S’ → S
•C→d • S → CC
• C → cC
•C→d
LR(1) items
• I0 : • I1 : goto(I0 , S)
S’ → .S, $ S’ → S., $
S → .CC, $ • I2 : goto(I0 , C)
C → .cC, c/d (first(C$) S → C.C, $
C → .d, c/d C → .cC, $
C → .d, $
• I3 : goto(I0 , c), goto(I3 , c), • I5 : goto(I2 , C)
C → c.C, c/d S → CC., $
C → .cC, c/d • I6 : goto(I2 , c) goto(I6 ,c)
C → .d, c/d C → c.C, $
• I4 : goto(I0 , d) goto(I3, d) C → .cC, $
C → d., c/d C → .d, $
• I7 : goto(I2 ,d) goto(I6 ,d)
C → d., $
• I8 : goto(I3 ,C)
C → cC., c/d
• I9 : goto(I6 ,C)
C → cC., $
Parsing Table
• Construct C = {I0 ,I1 ,I2 … In } the collection of LR(1) items for G’
• State I of the parser is from Ii
• if [A → α.aβ, b] is in Ii and goto(Ii, a) = Ij set action [i, a] = shift j, where a is a
terminal
• if [ A → α . , a] is in Ii and A ≠ S’, then set action[i, a] = reduce by A → α
// a conflict here implies the grammar is not CALR grammar
• If goto(Ii , A) = Ij then goto (i, A) = j
• [S’ → .S, $] implies an accept action
• All other entries are error
Parsing table - CALR
Stat Action goto
e c d $ S C
0 s3 s4 1 2
1 accept
2 s6 s7 5
3 s3 s4 8
4 r3 r3
5 r1
6 s6 s7 9
State Action goto
c d $ S C
7 r3
8 r2 r2
9 r2
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 with CALR parser
Stack Input Action
0 ccdd$ [0, c] – shift 3
0c3 cdd$ [3, c] – shift 3
0c3c3 dd$ [3, d] – shift 4
0c3c3d4 d$ [4, d] – reduce 3, pop 2 symbols from stack, push
C, goto(3, C) = 8
0c3c3C8 d$ [8, d] – reduce 2, pop 4 symbols from the stack,
push C, goto(3, C) = 8
0c3C8 d$ [8, d] – reduce 2, pop 4 symbols from the stack,
push C, goto(0, C) = 2
Stack Input Action
0C2 d$ [2, d] – shift 7
0C2d7 $ [7, $] – reduce 3, pop 2 symbols from the
stack, goto(2, C) = 5
0C2C5 $ [5, $] – reduce 1, pop 4 symbols off the
stack, goto(0, S) = 1
0S1 $ [1, $] – accept – successful parsing
Example
• S’→ S
• S→L=R
• S→ R
•L→*R
• L → id
•R→L
Another Example
• I0
[S’ → •S, $] goto(I0,S)=I1 • I3 : goto(I0,R)
[S → R•, $]
[S → •L=R, $] goto(I0,L)=I2
[S → •R, $] goto(I0,R)=I3
[L → •*R, =/$] goto(I0,*)=I4 • I4 : goto(I0,*) goto(I4,*)
[L → *•R, =/$] goto(I4,R)=I7
[L → •id, =/$] goto(I0,id)=I5 [R → •L, =/$] goto(I4,L)=I8
[R → •L, $] goto(I0,L)=I2 [L → •*R, =/$] goto(I4,*)=I4
• I1: goto(I0,S) [L → •id, =/$] goto(I4,id)=I5
[S’ → S•, $]
• I5 : goto(I0,id) goto(I4,id)
• I2 : goto(I0,L)
[L → id•, =/$]
[S → L•=R, $] goto(I2,=)=I6
[R → L•, $]
• I6 : goto(I2,=) • I9 : goto(I6,R)
[S → L=•R, $] goto(I6,R)=I9 [S → L=R•,$]
[R → •L, $] goto(I6,L)=I10
• I10 : goto(I6,L) goto(I11,L)
[L → •*R, $] goto(I6,*)=I11
[L → •id, $] goto(I6,id)=I12 [R → L•, $]
• I7 : goto(I4,R) • I11 : goto(I6,*) goto(I11,*)
[L → *R•, =/$] [L → *•R, $] goto(I11,R)=I13
• I8 : goto(I4,L) [R → •L, $] goto(I11,L)=I10
[R → L•, =/$] [L → •*R, $] goto(I11,*)=I11
[L → •id, $] goto(I11,id)=I12
• I12 : goto(I6,id) goto(I11,id)
[L → id•, $]
• I13 : goto(I11,R)
[L → *R•, $]
Parsing Table
State Action goto
id * = $ S L R
0 s5 s4 1 2 3
1 accept
2 s6 r5
3 r2
4 s5 s4 8 7
5 r4 r4
6 s12 s11 10 9
State Action Goto
id * = $ S L R
7 r3 r3
8 r5 r5
9 r1
10 r5
11 s12 s11 10 13
12 r4
13 r3
Summary
• CALR – most powerful parser
• Have so many items and states
• No conflicts

You might also like