This is about a topic of compiler design, LR and SLR parsing algorithm and LR grammar, Canonical collection and Item, Conflict in LR parsing shift reduce. Classification of Bottom up parsing.
3. Shift-Reduce (SR) parsing
âą A shift-reduce parser is a class of efficient, table-driven bottom-
up parsing methods for computer languages and other
notations formally defined by a grammar.
3
4. 4
âą The parsing methods most commonly used for parsing
programming languages, LR parsing and its variations, are
shift-reduce methods.
âą All shift-reduce parsers have similar outward effects, in the
incremental order in which they build a parse tree or call
specific output actions
Shift-Reduce (SR) parsing
5. Basic steps
A shift-reduce parser works by doing some combination of Shift steps
and Reduce steps, hence the name.
ïŒA Shift step advances in the input stream by one symbol. That shifted symbol
becomes a new single-node parse tree.
ïŒA Reduce step applies a completed grammar rule to some of the recent parse
trees, joining them together as one tree with a new root symbol.
5
6. Actions of SR parsing
A shift-reduce parser has just four canonical actions:
ï§ Shift ï This involves moving of symbols from input buffer onto the stack.
ï§ Reduce ï If the handle appears on top of the stack then, its reduction by
using appropriate production rule is done i.e. RHS of production rule is
popped out of stack and LHS of production rule is pushed onto the stack.
6
7. Cont âŠ
ï§ Accept ï If only start symbol is present in the stack and the input buffer is
empty then, the parsing action is called accept. When accept action is
obtained, it is means successful parsing is done.
ï§ Error ï This is the situation in which the parser can neither perform shift
action nor reduce action and not even accept action.
7
8. Conflicts
There are two kinds of conflicts that can occur in an SR parsing table.
ï§ A shift-reduce conflict occurs in a state that requests both a shift action and a
reduce action.
ï§ A reduce-reduce conflict occurs in a state that requests two or more different
reduce actions.
8
9. Example
Consider the grammar
S â> S + S
S â> S * S
S â> id
Perform Shift Reduce parsing for input string âid + id + idâ.
9
12. LR Parser
The LR parser is a
âą non-recursive
âą shift-reduce
âą bottom-up parser
It uses a wide class of context-free grammar which makes it the most
efficient syntax analysis technique.
12
13. 13
LR parsers are also known as LR(k) parsers Where
o L = left-to-right scanning of the input stream;
o R = the construction of right-most derivation in reverse
o and k denotes the number of look-ahead symbols to make decisions.
14. 14
Types of LR parser
2.LR(1) â Simple LR Parser(SLR)
3.LALR(1) â Look-Ahead LR Parser
1.LR(0) parser
4.Canonical LR (1) PARSER
15. SLR(1) â Simple LR Parser:
15
â Works on complete set of LR(1) Grammar
â Generates large table and large number of states
â Slow construction
16. SLR(1) parser
âą SLR (1) refers to simple LR Parsing. It is same as LR(0) parsing.
âą The only difference is in the parsing table.
âą To construct SLR (1) parsing table, we use canonical collection of LR
(0) item.
âą In the SLR (1) parsing, we place the reduce move only in the follow of
left hand side.
16
17. Item of LR(0)
âą An LR(0) item of a grammar G is a production of G, a dot at the some
position of the right side.
Example:
A â aBb
Possible LR(0) Items:
A â.aBb
A â a.Bb
A â aB.b
A â aBb.
(four different possibility)
17
18. Cont.âŠ
âą Sets of LR(0) items will be the states of âactionâ and âgotoâ table of the
SLR parser.
âą A collection of sets of LR(0) items (the canonical LR(0) collection) is
the basis for constructing SLR parsers.
18
19. 19
Augmented Grammar:
Gâ is G with a new production rule EââE where Eâ is the new starting symbol
EXAMPLE
Eâ â E
E ï E+T
E ï T
T ï T*F
T ï F
F ï ( E )
F ï id
20. Closure Operation
âą If I is a set of LR(0) items for a grammar G, then closure(I) is the set of
LR(0) items constructed from I by the two rules:
1. Initially, every LR(0) item in I is added to closure(I).
2. If A â α.BÎČ is in closure(I) and BâÎł is a production rule of G;
then Bâ.Îł will be in the closure(I).
We will apply this rule until no more new LR(0) items can be
add d t l (I) ed to closure(I).
20
21. Example
Grammar : I= {Eâ â.E}
Eâ â E
E ï E+T
E ï T
T ï T*F
T ï F
F ï ( E )
F ï id
closure({Eâ â.E}) =
Eâ â.E
E â.E+T
E â.T
T â.T*F
T â.F
F â.(E)
F â.id
21
22. Goto Function
If I is a set of items and X â (VnU VT ), then, goto(I, X) is the closure of
the set of all items A â αX. ÎČ such that A â α. XÎČ is in I.
If I is a set of items valid for a prefix α of a right-sentential form,
then, Goto(I, X) is valid for the prefix αX
22
23. 23
Goto(I, X) represents the transition of the automaton
from state I and input X.
GOTO(I=state ,X=input)
24. 24
Grammar :
EâČ â E
E â E + T
E â T
T â T â F
T â F
F â (E)
F â id
I2
E â E+.T
T â .T â F
T â . F
F â .(E)
F â . id
I0
E â.E+T
E â.T
T â.T*F
T â.F
F â.(E)
F â.id
I1
EâČ â E. ,
E â E. +T
Example
GOTO(I0,E) GOTO(I1,+)
25. Example
If I1 = {EâČ â E. , E â E. +T}, then:
goto(I1,+) = closure({E â E+.T}),
is the set:
E â E+.T
T â .T â F
T â . F
F â .(E)
F â . id
Grammar :
EâČ â E
E â E + T
E â T
T â T â F
T â F
F â (E)
F â id
25
26. Canonical Collection
Algorithm. Canonical Collection for an Augmented Grammar GâČ
1. Initially, C = {closure({SâČ â. S})};
2. For each set of items I in C and each Grammar symbol X
If goto(I, X) â â and Goto(I, X) /â C, then add Goto(I, X) to C;
3. Go to step 2 if new items have been added, otherwise stop
26
28. Sâ ï .E
E ï .E+T
E ï .T
T ï .T*F
T ï .F
F ï .id
Sâ ï E.
E ï E.+T
E ï E+.T
T ï .T*F
T ï .F
F ï .id
E ï T.
T ï T.*F
T ï T*.F
F ï .id
E ï E+T.
T ï T.*f
T ï T*F.
T ï F.
F ï id.
Canonical Collection
28
E
T
F
id
+
*
T
*
F
id
id
F
I0
I1
I5
I7
I8
I6
I4
I2
I3
29. Table Construction Rules
α, ÎČ = any string of terminals and/or nonâterminals
X, Sâ, S = nonâterminals
(When dot is in middle)
1. if [A ââ> α.aÎČ] Δ Ii and read on âaâ produces Ij then ACTION [i , a] = SHIFT j.
2. if [A ââ> α.XÎČ] Δ Ii and read on âXâ produces Ij then GOTO [i , X] = j.
29
30. Cont..
(When dot is at end)
3. if [A ââ> α.] Δ Ii then ACTION [i , a] = REDUCE on A â> α for all a Δ FOLLOW(A).
4. if [Sâ ââ> S.] Δ Ii then ACTION [i , $] = ACCEPT.
30