Module 3
Module 3
Tech/S6
MODULE 3
UKFCET 1
Compiler Design/[Link]/S6
Along the way, a bottom-up parser searches for substrings of the working string that match the
right side of some production.
When it finds such a substring, it reduces it, i.e., substitutes the left side non-terminal for the
matching right side. The goal is to reduce all the way up to the start symbol and report a
successful parse.
This can be considered as the process of “reducing” a string w to the start symbol of a
grammar.
At each reduction step parser searches for substrings of the working string that match
the right side of some production.
When it finds such a substring, it reduces it, i.e., substitutes the left side non-terminal
for the matching right side. The goal is to reduce all the way up to the start symbol and
report a successful parse.
In Shift-reduce parsing a stack holds grammar symbols and an input buffer holds the
rest of the string to be parsed.
As we shall see, the handle always appears at the top of the stack just before it is
identified as the handle.
We use $ to mark the bottom of the stack and also the right end of the input. Initially,
the stack is empty, and the string w is on the input, as follows:
STACK INPUT
$ w$
During a left-to-right scan of the input string, the parser shifts zero or more input
symbols onto the stack, until it is ready to reduce a string β of grammar symbols on top
of the stack.
It then reduces β to the head of the appropriate production. The parser repeats this
cycle until it has detected an error or until the stack contains the start symbol and the
input is empty as follows:
STACK INPUT
$S $
UKFCET 2
Compiler Design/[Link]/S6
Upon entering this configuration, the parser halts and announces successful completion
of parsing.
There are actually four possible actions a shift-reduce parser can make:
🡪 Shift
🡪 Reduce
🡪 Accept
🡪 Error
1. Shift: The next input symbol is shifted onto the top of the stack.
2. Reduce: The parser knows the right end of the string to be reduced must be at the
top of the stack. It must then locate the left end of the string within the stack and
decide with what nonterminal to replace the string.
3. Accept: Announce successful completion of parsing.
4. Error: Discover a syntax error has occurred and calls an error recovery routine.
EXAMPLE
input
Following figure steps through the actions a shift-reduce parser might take in parsing the
string id *id according to the expression grammar.
1 2
E→ E + T | T
T→ T * F | F
F→ ( E ) | id
UKFCET 3
Compiler Design/[Link]/S6
EXAMPLE
Precedence Relations
In operator-precedence parsing, we define three precedence relations between certain
pairs of terminals as follows:
The intention of the precedence relations is to find the handle of a right sentential form,
4
Compiler Design/[Link]/S6
EXAMPLE
Consider the
grammar E 🡪 E+E
E 🡪 E*E
E 🡪 (E)
E🡪 id
And the input string is id + id * id
1 2 3
E🡪E+E
🡪E+E*E
🡪 E + E * id
3
🡪 E + id * id
2 3
🡪 Id id * id
1+ 2 3
5
Compiler Design/[Link]/S6
HANDLE PRUNING
The process of obtaining rightmost derivation in reverse order is called “handle pruning”.
(i.e.) if w is a sentence or string of the grammar at hand, then w = γ where γ is the n th right
n n
2. LR PARSING
LR parsing is most efficient method of bottom up parsing which can be used to parse large class
of context free grammar.
The technique is called LR(k) parsing; the “L” is for left to right scanning of input symbol, the “R”
for constructing right most derivation in reverse, and the k for the number of input symbols of
lookahead that are used in making parsing decision.
Requirements of LR parser:
🡪 Input Buffer
🡪 Stack
6
Compiler Design/[Link]/S6
🡪 Parsing Table
🡪 LR Parsing Program
Input Buffer
The parsing program reads characters from an input buffer one at a time.
Stack
Each state symbol summarizes the information contained in the stack below it
Combination of state symbol on stack top and current input symbol are used to index
the parsing table and determine the shift reduce parsing decision
Parsing Table
Consist of 2 parts
1. parsing action function action
2. goto function goto
Action
This function takes as arguments a state i and a terminal a (or $, the input end marker). The
value of ACTION [i, a] can have one of the four forms:
i. Shift j, where j is a state.
ii. Reduce by a grammar production A---> β.
iii. Accept.
iv. Error.
Goto
This function takes a state and grammar symbol as arguments and produces a state. If GOTO
[Ii ,A] = Ij, the GOTO also maps a state i and nonterminal A to state j.
consults parsing action table entry for action[s ,a ] which can have 4 values:
m i
The function goto takes a state and grammar symbol as arguments and produces a
state.
The function goto takes a state and grammar symbol as arguments and produces a
state.
( s X s X . . .X s , a a . . . a $)
0 1 1 2 m m i i+1 n
The next move of the parser is determined by reading ai, the current input symbol and
sm, state on stack top and then consulting parsing action table entry action[sm,ai].
The configuration resulting after each of four types of moves are as follows:
1. If action [sm, ai] = shift s, the parser executes a shift move, entering the
configuration
( s X s X . . .X s , a a ...a
0 1 1 2 m m i i+1 n
$)
Here the parser has shifted both the current input symbol a and the next state s,
i
which is given in action [s , a ], onto the slack; a becomes the current input
m i i+1
symbol.
2.
If action [s , a ] =reduce A 🡪β , then the parser executes a reduce move, entering
m i
the configuration
(s X s X s ...X s A s, a a . . . a $)
0 1 1 2 2 m-r m-r i i+1 n
where s = goto [s , A] and r is the length of β , the right side of the production.
m-r
Here the parser first popped 2r symbols off the stack (r state symbols and r
grammar symbols), exposing state s . The parser then pushed both A, the left side
m-r
of the production, and s, the entry for goto[s , A] , onto the stack. The
m-r current
3. input symbol is not changed in a reduce move.
If action [s , a ] = error, the parser has discovered an error and calls an error
LR Parsing Algorithm
m i
recovery routine.
INPUT : Input string w, LR-Parsing table with functions ACTION and GOTO for a grammar G
8
Compiler Design/[Link]/S6
METHOD : Initially, the parser has s on its stack, where s is the initial state, and w$ in the
0 0
input buffer. The parser then executes the following program until an accept or error action is
encountered.
end
else if action [s,a] = reduce A🡪β then begin
end
else if action [s,a] = accept then
return
else error();
end
SLR(l) - Simple LR
LR( 1) - LR parser
Drawbacks of LR parsers
It is too much work to construct LR parser by hand. It needs an automated parser
generator.
1. SLR
2. CLR
3. LALR
Simple LR (SLR)
Easy to implement
Least powerful
Canonical LR
Most powerful
Most expensive
LookAhead LR(LALR)
10
Compiler Design/[Link]/S6
This is actually a weak rule and can lead to erroneous results and/or shift/reduce conflicts.
EXAMPLE
Consider A -> α
State i: A -> α .
State j: A -> α .
String 1: γ α a
String 2: β α a
11
Compiler Design/[Link]/S6
A → .aBb
A → [Link]
A → aB.b
A → aBb.
A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for
constructing SLR parsers.
🡪 Augmented grammar
Augmented grammar
G’ is the augmented grammar of G with a new production rule S’→S where S’ is the new
starting symbol i.e,
Non-kernel items are the items which have the dot at leftmost end.
12
Compiler Design/[Link]/S6
Sets of items are formed by taking the closure of a set of kernel items.
We will apply this rule until no more new LR(0) items can be added to closure(I).
Computation of Closure
function closure ( I )
begin
J := I;
repeat
add B→.γ to J
return J
end
13
Compiler Design/[Link]/S6
Goto Operation
If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal), then
goto(I,X) is defined as follows:
EXAMPLE
I ={ E’ → E., E → E.+T}
goto(I,+) = { E → E+.T
T →.T*F
T →.F
F →.(E)
F →.id }
ALGORITHM
Procedure items( G’ )
begin
C := { closure({S’→.S}) }
repeat for each set of items I in C and each grammar symbol X
add goto(I,X) to C
\end
14
Compiler Design/[Link]/S6
F → (.E)
E’ → .E E → E+.T F → id.
E → .E+T T → .T*F
E → .E+T
E → .T T → .F
I :goto(I , T) E → .T
T → .T*F 9 6
F → .(E)
E → E+T. T → .T*F
T → .F
F → .id
T → T.*F T → .F
F → .(E)
I :goto(I , T) T → .F T → .F
I : goto(I , F) 2 4
3 0
E → T. F → .(E) F → .(E)
T → F.
T → T.*F F → .id F → .id
I : goto(I , ( )
4 0
I : goto(I , F) I : goto(I , id)
F → (.E) 3 4
5 6
I : goto(I , *)
7 9
T → F. T → T*.F
E → .E+T F → id.
F → .(E)
E → .T
I : goto(I , ( )
4 0 I : goto(I , F)
T → .T*F 10 7 F → .id
F → (.E)
T → .F E → .E+T T → T*F.
E → .T
F → .(E)
T → .T*F I : goto(I , id)
5 7
F → .id
T → .F
F → id.
15
I : goto(I , id) F → .(E)
5 0
F → .id
F → id.
Compiler Design/[Link]/S6
METHOD:
1. Construct C ={I , I ,…..I }, the collection of sets of LR(0) items for G'.
0 1 n
2. State i is constructed from I . The parsing actions for state i are determined as follows:
i
16
Compiler Design/[Link]/S6
If any conflicting actions are generated by the above rules, we say the grammar is not
SLR (1). The algorithm fails to produce a parser in this case.
3. The goto transitions for state i are constructed for all nonterminals A using the rule:
if GOTO(I , A)=I then GOTO[I, A]=j .
i j
4. All entries not defined by rules (2) and (3) are made “error”.
5. The initial state of the parser is the one constructed from the set of items containing [S’
🡪 S].
17
Compiler Design/[Link]/S6
I : F → (E).
11
Shift/Reduce and Reduce/Reduce conflicts
If a state does not know whether it will make a shift operation or reduction for a
terminal, we say that there is a shift/reduce conflict.
If a state does not know whether it will make a reduction operation using the
production rule i or j for a terminal, we say that there is a reduce/reduce conflict.
If the SLR parsing table of a grammar G has a conflict, we say that that grammar is not
SLR grammar.
Stack Implementation
Check whether the given input is valid or not?
18
Compiler Design/[Link]/S6
EXAMPLE
SLR ( 1 ) GRAMMAR
S→E
E→E+T|T
T→T*F|F
F → id
AUGMENTED GRAMMAR
S’ → S
S→E
E→E+T|T
T→T*F|F
F → id
I I = Go to (I , F) I = Go to (I , T)
0 3 0 7 5
S’→ S T → F• E → E + T•
S → •E
E → •E + T
I = Go to (I , id) I = Go to (I , F)
E → •T 4 0
3 5
T → •T * F F → id•
T → F•
T → •F
I = Go to (I , +)
F → •id 5 1 I = Go to (I , id)
4 5
E → E +•T
F → id•
T → •T * F
I = Go to (I , E)
1 0
T → •F
S` → E• I = Go to (I , F)
8 6
E → E• + T F → •id T → T * F•
I = Go to (I , T) I = Go to (I , *) I = Go to (I6, id)
2 0 6 2 4
E → T•T T → T * •F
F → id•
T• → * F F → •id
19
Compiler Design/[Link]/S6
1. S→E
2. E→E+T|T
3. T → T * F | F
4. F → id
SLR(1) TABLE
{I , $} = Accept
1
🡪
I contains the final item which drives E → T• and follow (E) = {+, $}, so action
2
🡪 {I , +} = R2, action {I , $} = R2
2 2
I contains the final item which drives T → F• and follow (T) = {+, *, $}, so action
3
🡪 I contains the final item which drives F → id• and follow (F) = {+, *, $}, so
4
EXAMPLE
Every SLR (1) grammar is unambiguous, but there are many unambiguous grammars that are
not SLR(1). Consider the grammar with productions
S🡪L=R
S🡪R L
🡪*R
L🡪 id R
🡪L
AUGMENTED GRAMMAR
S’ 🡪 S
S🡪L=R
S🡪R L
🡪*R
L🡪 id R
🡪L
21
Compiler Design/[Link]/S6
I I = Go to (I , id) I = Go to (I , R)
0 5 0 9 6
S’ 🡪• S L 🡪 id• S 🡪 L = R•
S 🡪 •L = R
S 🡪 •R
I = Go to (I , =) I = Go to (I , L)
L 🡪 •* R 6 2 8 6
S 🡪 L = •R
L 🡪 •id R R🡪L•
R🡪•L
🡪•L
I = Go to (I , L)
L 🡪 •* R 7 6
L 🡪 •id L 🡪 * R•
I = Go to (I , S)
1 0
S’ 🡪 S •
I = Go to (I , R) I = Go to (I , *)
7 4 4 6
L 🡪 *• R
I = Go to (I , L) L 🡪 * R•
2 0
R🡪•L L
S 🡪 L •= R
I = Go to (I , L) 🡪 •* R L
R 🡪 L• 8 4
R 🡪 L• 🡪 •id
I = Go to (I , R)
3 0
I = Go to (I , *)
4 4 I = Go to (I , id)
S 🡪 R• L 🡪 * •R
5 6
L 🡪 id•
R🡪•L L
I = Go to (I , *)
4 0
🡪 •* R L
L 🡪 * •R
🡪 •id
R🡪•L L
🡪 •* R
I = Go to (I , id)
5 4
L 🡪 •id
NUMBER THE PRODUCTIONS
L 🡪 id•
1. S 🡪 L = R
2. S 🡪 R
3. L 🡪 * R
4. L🡪 id
5. R 🡪 L
22
Compiler Design/[Link]/S6
PARSING TABLE
I = S’ 🡪 S •
1
FOLLOW (S)={$}
I = R 🡪 L•
2
FOLLOW (L)= {=,$}
I = S 🡪 R• FOLLOW (R)= {=,$}
3
I = L 🡪 id•
5
I = L 🡪 * R•
7
I = R 🡪 L• I =
8 9
(2, =), (2, $) =====> r5
S 🡪 L = R• (3, $) =====> r2
(9, $) =====> r1
id * = $ R L S
0 s5 s4 3 2 1
1 ACCEPT
2 s6/ r5 r5
3 r2
4 s5 s4 7 8
5 r4 r4
6 s5 s4 9 8/7
7 r3 r3
8 r5 r5
9 r1
23
Compiler Design/[Link]/S6
24
Compiler Design/[Link]/S6
METHOD:
1. Construct C’ = { I0, I1, …….., In}, the collection of sets of LR(1) items for G’.
state
i is determined as follows.
(a If [ A 🡪 α.aβ,b] is in Ii, and GOTO (Ii, a) = Ij, then set ACTION [i, a] To
)
“shift j”. Here a must be a terminal.
If [A 🡪 α., a] is in I A ≠ S’, then set ACTION[ i, a ] to “reduce A 🡪 α.”
(b) i,
If any conflicting actions result from the above rules, we say the grammar is
not LR(1). The algorithm falls to produce a parser in this case.
3. The goto transitions for state i are constructed for all non terminals
using the rule: if GOTO( I , A) = I , then GOTO[ i, A ]= j.
A i j
4. All entries not defined by rules (2) and (3) are made “error”.
5. The initial state of the parser is the one constructed from the set of items
containing [S’ 🡪 .S, $ ]. 25
Compiler Design/[Link]/S6
EXAMPLE
Construct CLR parsing table for the flowing grammar
S🡪 CC
C🡪 cC| d
AUGMENTED GRAMMAR
S’🡪 S
S🡪 CC
C🡪 cC
C🡪 d
I I = GOTO (I , c) I = GOTO (I , C)
0 6 2 8 3
S’ 🡪 •S , $ C 🡪 c•C, $ C 🡪 c C•, c | d
S🡪 •CC, $ C 🡪 •c C ,$ C
C🡪 •cC, c | d 🡪 •d , $ I = GOTO (I , c)
3 3
C🡪 •d, c | d
C 🡪 c• C , c | d
I = GOTO (I , d) C🡪 •cC, c | d
I = GOTO (I , S) 7 2
C🡪 •d, c | d
1 0
C 🡪 d• , $
S’ 🡪 S•, $
I = GOTO (I , d) I = GOTO (I , d)
4 3
I = GOTO (I , C) 4 0
2 0 C🡪 d •, c | d
C 🡪 d• , c | d
S 🡪 C•C, $
C 🡪 •cC, $ C I = GOTO (I , C)
I = GOTO (I , C) 9 6
🡪 •d, $ 5 2
C 🡪 c C• , $
S 🡪 CC•, $
I = GOTO (I , c) I = GOTO (I , c)
6 6
I = GOTO (I , c) 6 2
3 0 C 🡪 c• C ,$
C 🡪 c•C, $
C 🡪 c•C, c | d C🡪 •cC, $
C 🡪 •c C ,$ C
C 🡪 •c C , c | d C C🡪 •d, $
🡪 •d , $
🡪 •d, c | d
I = GOTO (I , d)
7 6
I = GOTO (I , d) I = GOTO (I , d)
4 0
7 2 C🡪 d•, $
C 🡪 d• , c | d C 🡪 d• , $
I = GOTO (I , C)
5 2
S 🡪 CC•, $
26
Compiler Design/[Link]/S6
1. S🡪 CC
2. C🡪 cC
3. C🡪 d
PARSING TABLE
I = S’ 🡪 S• , $
1
I = C 🡪 d• , c | d
4
I = S 🡪 CC•, $
5
I = C 🡪 d• , $
7
I = C 🡪 c C•, c | d
8
I = C 🡪 c C• , $
9
27
Compiler Design/[Link]/S6
28
Compiler Design/[Link]/S6
METHOD:
have the same core. Let K be the union of all sets of items having the same
*********
*
29