Unit 2
Unit 2
token
source Lexical parse Rest of front intermediate
program Parser tree representation
Analyzer end
get next
token
Symbol Table
• Phrase level
– Replacing a prefix of remaining input by some string that
allows the parser to continue.
• Error productions
– Augment the grammar with productions that generate the
erroneous constructs.
• Global correction
– Choosing minimal sequence of changes to obtain a globally
least-cost correction.
E → E A E | ( E ) | - E | id
A→+|-|*|/|^
Top-down parsers :
– Constructs the derivation tree from root to leaves.
– Leftmost derivation.
Bottom-up parsers :
– Constructs the derivation tree from leaves to root.
– Rightmost derivation in reverse.
Sentence
May 10, 2024 SCS1303-Compiler Design 30
Context Free Language
Given a grammar G with start symbol S,
define L(G) ,the language generated by G.
be replaced.
Rightmost Derivation(RMD):
• If we scan and replace the input with production rules, from
right to left, it is known as right-most derivation.
• The sentential form derived from the right-most derivation is
called the right-sentential form.
id + E
id + E * E E + E
id + id * E
id + id * id id E * E
id id
E+E*E
E + E * id E + E
E + id * id
id + id * id id E * E
id id
Leftmost Derivation:
E E*E E*E
-E*E E * id
-(E)*E - E * id
-(E+E)*E - ( E ) * id
- ( id + E ) * E - ( E + E ) * id
- ( id + id ) * E - ( E + id ) * id
- ( id + id ) * id - ( id + id ) * id
E -E
-(E)
-(E+E)
- ( id + E )
- ( id + id )
This grammar generates the same set of strings ,but allows only
one parsing for string.
Rule-1:
• The level at which the production is present defines the priority of the
operator contained in it.
– The higher the level of the production, the lower the priority of operator.
– The lower the level of the production, the higher the priority of operator.
Rule-2:
• If the operator is left associative, induce left recursion in its production.
• If the operator is right associative, induce right recursion in its production.
start a b b
0 1 2 3
b
Equivalent grammar,
A0 → a A0 | b A 0 | a A 1
A1 → b A2
A2 → b A3
A3 → ε
Types of Parser
E->TE’
E’->+TE’|ε E’ -> + T E’| ε A’ -> α A’ | ε
T->FT’
T’->*FT’|ε
F->(E)
F->id
May 10, 2024 SCS1303-Compiler Design 61
Example
Consider the Grammar:
S → Aa | b
A → Ac | Sd | ε
The non-terminal S is left recursive because S=> Aa => Sda, but it is not
immediate left recursive.
Eliminating the immediate left recursion among the A-production yields the
following grammar,
S → Aa | b
A → bdA’| A’
A’ → cA’ |ad A’ |ε
May 10, 2024 SCS1303-Compiler Design 62
May 10, 2024 SCS1303-Compiler Design 63
May 10, 2024 SCS1303-Compiler Design 64
May 10, 2024 SCS1303-Compiler Design 65
May 10, 2024 SCS1303-Compiler Design 66
May 10, 2024 SCS1303-Compiler Design 67
May 10, 2024 SCS1303-Compiler Design 68
May 10, 2024 SCS1303-Compiler Design 69
May 10, 2024 SCS1303-Compiler Design 70
May 10, 2024 SCS1303-Compiler Design 71
Model of Non-Recursive Predictive Parser
INPUT a + b $
STACK X
Y
Predictive Parsing OUTPUT
Z Program
$
Parsing Table
M
={ +,FOLLOW(E)} ={+,$,) }
E E →TE’ E →TE’
E’ E’ →+TE’ E’ →ε E’ →ε
T T →FT’ T →FT’
T’ T’ →ε T’ →*FT’ T’ →ε T’ →ε
F F →id F → (E)
Types of Parser
LR SLR LALR
Operator
Precedence Parser
e
a A B
A b c
d
Rule-01:
– If precedence of b is higher than precedence of a, then we define a <
b
– If precedence of b is same as precedence of a, then we define a = b
– If precedence of b is lower than precedence of a, then we define a > b
Rule-02:
– An identifier is always given the higher precedence than any other
symbol.
– $ symbol is always given the lowest precedence.
Rule-03:
– If two operators have the same precedence, then we go by checking
their associativity.
+ * id ( ) $
+ > < < < > >
* > > < < > >
id > > e e > >
LEADING(E)= {+ , * , ( , id}
LEADING(T)= {* , ( , id} ( < < < < = e
LEADING(F)= { ( , id}
) > > e e > >
T followed by NT $ < < < < e Accept
Rule-1. + T
+ < leading(T)
Rule-3. * F TRAILING(E)= {+ , * , ) , id} NT followed by T
* < leading(F) TRAILING(T)= {* , ) , id} Rule-1. E +
Rule-4. ( E TRAILING(F)= { ) , id} Trailing(E) > +
( < leading(E) Rule-3. T *
Trailing(T) > *
Rule-4. E )
May 10, 2024 SCS1303-Compiler Design Trailing(E) > ) 120
Parse the string
STACK REL. INPUT ACTION
+ * id ( ) $ $ $< ( (id+id)*id$ Shift (
+ > < < < > > $( ( < id id+id)*id$ Shift id
* > > < < > > ( < id $( id id > + +id)*id$ Pop id
id > > e e > > $( ( <+ +id)*id$ Shift +
( < < < < = e $(+ + < id id)*id$ Shift id
+< id $(+id id > ) )*id$ Pop id
) > > e e > >
( <+ $(+ +>) )*id$ Pop +
$ < < < < e Acce
pt $( (=) )*id$ Shift )
( =) $() ) >* *id $ Pop )
$ <( $( Pop (
$ $<* *id $ Shift *
$* *<id id$ Shift id
*<id $*id id >$ $ Pop id
$< * $* *>$ $ Pop *
$ $ Accept
g* f*
f+ g+
g$ f$
Advantages :
• It can easily be constructed by hand.
• It is simple to implement this type of parsing.
Disadvantages :
• It is hard to handle tokens like the minus sign (-), which has two
different precedence (depending on whether it is unary or
binary).
• It is applicable only to a small class of grammars.
May 10, 2024 SCS1303-Compiler Design 125
Error Recovery in Operator Precedence
Error cases:
Parsing
– No relation holds between the terminal on the top of the stack
and the next input symbol.
– A handle is found (reduction step),but there is no production
with this handle as a right side.
Error recovery:
– Each empty entry is filled with a pointer to an error routine.
– Decides the popped handle “looks like” which right-hand side,
and tries to recover from that situation.
3)
$= = < * * id $ Shift *
$=id id e * * id $ error
f= g=
g$ f$
The resulting Precedence functions are:
id = * $
f 3 1 1 0
g 2 2 2 0
.
E’→ E , $
Whenever we are about to reduce using
rule 0....
Accept! Parse is finished!
I0 : E'→•E
GOTO(I0 , E)
E→•E + T GOTO(I,X) → Moving • over
E→• T the symbols(both terminal
GOTO(I0 , T) and non-terminals.
T→•T * F
T→• F GOTO(I0 , F)
F→•(E)
F→• id GOTO(I0 , ()
GOTO(I0 , id)
E→• T GOTO(I0 , T)
I2 : E→T •
T→•T * F T→T • * F
GOTO(I0 , F)
T→• F I3 : T→F •
F→•(E) GOTO(I0 , ()
I4 : F→(•E)
E→•E + T
F→• id
E→• T
T→•T * F
T→• F
F→•(E)
F→• id
GOTO(I0 , id)
I5 : F→id •
May 10, 2024 SCS1303-Compiler Design 150
DFA
I1 : E'→E •
I0 : E E→E • + T
E'→•E
E→•E + T T I2 : E→T •
E→• T T→T • * F
T→•T * F
F
T→• F
I3 : T→F •
F→•(E)
F→• id (
I4 : F→(•E)
E→•E + T
id E→• T
T→•T * F
T→• F
F→•(E)
F→• id
I5 : F→id •
I4 : F→(•E)
GOTO(I4, E) F→(E•)
E→•E + T I8
E→E •+ T
E→• T
T→•T * F GOTO(I4, T) E→ T •
T→• F I2
T→T • * F
F→•(E)
F→• id GOTO(I4, F ) I3 F→(•E)
E→•E + T
GOTO
(I4 , ( E→• T I4
GOTO(I4, id) )
T→•T * F
I5 T→• F
F→•(E)
F→• id
Transition from item set I5
I5 : F→id •
Rule 6 Ready
for reduction
(
id
I5 : F→id •
May 10, 2024 SCS1303-Compiler Design 154
Transition from item set I6
Rule 1 Ready
I6 : E→E + • T E→E + T • for reduction
GOTO(I6, T) I9
T→T • * F
T→•T * F
T→• F GOTO(I6, F ) I3
F→•(E) F→(•E)
F→• id E→•E + T
GOTO
GOTO(I6, id) (I6 , ( E→• T I4
)
T→•T * F
I5 T→• F
F→•(E)
F→• id
Transition from item set I7
I7 : T→T * • F I10
GOTO(I7, F) Rule 3 Ready
F→•(E) T→T * F • for reduction
F→• id
GOTO
GOTO(I7, id) (I7 , (
)
I5
I4
May 10, 2024 SCS1303-Compiler Design 155
Transition from item set I8
Rule 5 Ready
I11 for reduction
I8 : F→(E•) GOTO(I8, ) )
F→ ( E )•
E→E •+ T
GO
TO E→E + • T
(I ,
8 +)
I6
T→•T * F
T→• F
F→•(E)
F→• id
I9 : E→E + T • GOTO(I9, * )
T→T • * F T→T * • F
F→•(E) I7
F→• id
I5 : F→id •
May 10, 2024 SCS1303-Compiler Design 157
Collection of set of items
I0 : E'→•E I4 : F→(•E)
I7 : T→T * • F
E→•E + T E→•E + T
F→•(E)
E→• T E→• T
F→• id
T→•T * F T→•T * F
T→• F T→• F
F→•(E) F→•(E) I8 : F→(E•)
F→• id F→• id E→E •+ T
Goal! Parser Rule 6 Rule 1
Announce Ready for Ready for
Accept reduction reduction
I1 : E'→E • I5 : F→id •
I9 : E→E+T •
E→E • + T T→T • * F
Rule 2
Ready for Rule 3
reduction Ready for
reduction
I2 : E→T • I6 : E→E + • T
Rule 4 I10 :T→T* F •
T→T • * F T→•T * F
Ready for
reduction T→• F Rule 5
F→•(E) Ready for
I3 : T→F • reduction
F→• id
I11 : F→ (E )•
ACTION GOTO
State id + * ( ) $ E T F
1.E→E + T
2.E→T 0 s5 s4 1 2 3
3.T→T * F
4.T→F 1 s6 acc
5.F→(E) 2 r2 s7 r2 r2
6.F→id
3 r4 r4 r4 r4
FOLLOW(E) 4 s5 s4 8 2 3
= {+,),$ }
FOLLOW(T) 5 r6 r6 r6 r6
={*,+,) ,$} 6 s5 s4 9 3
FOLLOW(F)
={*,+,) ,$} 7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
May 10, 2024 SCS1303-Compiler Design 160
May 10, 2024 SCS1303-Compiler Design 161
Moves of LR parser on id*id+id
A -> aA ---------(2)
A -> b ----------(3)
State Action Goto Reduce Action
I4 :A -> b.
a b $ S A Follow(A)=
FIRST(A)= {a,b,$}
0 S3 S4 1 2
1 Accept I5: S->AA.
2 S3 S4 5 Follow(S)= {$}
3 S3 S4 6
I6: A -> aA .
4 R3 R3 R3 Follow(A)=
5 R1 FIRST(A)= {a,b,$}
6 R2 R2 R2
Parsing
Actions : Shift, Reduce, Accept, Error Stat Action Goto
e
Stack Input Action a b $ S A
0 S3 S4 1 2
0 abab $ Shift (S3) 1 Acce
pt
0a3 bab $ Shift (S4) 2 S3 S4 5