General Framework: X X X X: LR Parser
General Framework: X X X X: LR Parser
General Framework: X X X X: LR Parser
General Framework:
A configuration of an LR parser is a pair whose first component is the stack and whose second
component is the unexpended part of the input string:
A B
part which is already part of the input string.
considered for parsing.
STACK
Shift:
s0……… |Yjsi+k xpsi+k*|xp+1… … xk
Reduce:
[s0………..si+gA] xpfrom
State … …thexktable
Reduce A α, | α | = N
Pop 2*| N | elements including states
Push A
Put a state s
[s0………..si+gAs] xp… … xk
Example:
Consider the following grammar
1. S’ S
2. S aABe
3. A Abc
4. A b
5. B d
Table for parsing action and goto function for the given grammar is:-
Where
1. rj means reduce by production numbered j,
2. acc means accept
3. si means shift and stack state i.
4. blank means error.
Moves of LR parser
1. Item:
Item of a grammar G is a production of G with a dot at some position of the right side.
The production A XYZ has the following four items
A .XYZ
A X.YZ
Z XY.Z
A XYZ.
2. Item closure:
If A X.YZ belongs to Is
Yβ
Then add Y .β in Is
Constructing the GOTO graph from the LR(0) Items ABOVE derived.
1. Enlist all the collection of Items: C (I0, I1,…..)
2. Put all the transitions in the between the Items in he GOTO graph.
Rules for putting the label on the transition
If there is a transition from,
A->α . Xβ to A-> αX . β
then, the transition in the GOTO Graph is labeled X
If there is a transition
A-> α . Bβ to B-> .γ
then, the transition is labeled ε, and since we take ε-closure of all Items, these productions lie in
the same item as that of A->α . Bβ to A-> αB . β.
So, the GOTO Graph of the given grammar is produced follows.
S’-> .S a
S->.aABe S-> a.Abe
A->.Abc
A->.b b
I0
I2
A -> b.
$
I4
A
S’ -> S.
S->aA.Be S->aABe.
e
A->A.bc
I1 B->.d I8
B
b I3
S->aAB.e
A->Ab.c d I5
B->d.
I6
I7
A->Abc.
I9
…………………………………………………………………………………….
From the GOTO Graph we now start filling the Parsing table for the SLR (simple LR) parser, Which
contains State i as the row index and action & goto as the vertical column index.
The Rules for filling the action entries in the table are:
1.If [ A-> α .a β ] is in Ii and goto ( Ii , a ) = Ij, then set action [i ,a] to “shift j “.Here a must be a
terminal.
2. If [A-> α .] is in Ii, then set action[i , a ] to “reduce A -> α “ for all a in FOLLOW (A); here A
may not be in S’.
3. If [ S’-> S.] is in Ii , then set action [I, $] to accept.
If any conflicting actions are generated by the given rules, then we say the grammar is not in SLR or
LR (0) and we fail to produce a parser.
…. Note : goto (Ii, A) is an entry in graph, whereas goto[i, A] is the corresponding entry in parsing
table .
Also the initial state of the parser is one constructed from the set of items containing [S’-> .S]
So, the Parsing table for the grammar comes out to be.
We have a noteworthy point in the grammar we just now used. We have the starting production as
S’-> S and then S had its own set of productions. Intuitively, we see that, there arises no need for the
extra production rule S’->S and this rule might be treated as a dummy rule. We could always do away
with S’ and have S as the start state. However, closer inspection brings to light on the fact that, while
staring the parsing, we may not have any problem, but on making reduction to start state (remember we
are parsing bottom up!), we might just end up having reduce conflicts. So, a need to have a ‘one-level’
higher hierarchy is necessitated and is provided by the rule S’-> S, whenever the start state S, has
multiple productions.
We now look into the problems underlying the SLR grammar, and move onto greener pastures where
we could possibly cope up with these problems.
Lectrure 2: Joy Deep Nath 03CS3021 dt:06.09.05
Lectrure 3: Bhaben Deori 03CS3020 dt:06.09.05
Follow(R) = {$, =}
So in the Parsing table we have the following conflict when the follow ‘=’ appears.
|.…..……………………………………
| …. ….. ‘=’ ….. ……
I1 …. ….. s2/r6 ….. ……. -> shift reduce conflict
In such a case we break up the item into separate parts. Here the two items will be:
{E->L. =R, $} and {R->L., =}
In general terms, we have-
A1->alpha1 A1->alpha1, Follow (A1) Product of
A2->alpha2 A2->alpha2, Follow (A2) Follow (Ai)
A3->alpha3 A3->alpha3, Follow (A3) elements.
Suppose there are ‘k’ items in the SLR.
Each item has ‘p’ rules.
Each follow has N elements.
Then the total number of items after separation is
K*POW (N, P). Even if we have one step look ahead the
Complexity will be exponential!!!!!!