0% found this document useful (0 votes)
0 views

CD-UNIT-3

The document outlines advanced concepts in compiler design, focusing on LR parsers, including LR1 and LALR, and their applications. It details the structure and functioning of LR parsers, including parsing tables, actions, and closure operations. Additionally, it provides examples of constructing SLR parsing tables and demonstrates parsing actions with specific input strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

CD-UNIT-3

The document outlines advanced concepts in compiler design, focusing on LR parsers, including LR1 and LALR, and their applications. It details the structure and functioning of LR parsers, including parsing tables, actions, and closure operations. Additionally, it provides examples of constructing SLR parsing tables and demonstrates parsing actions with specific input strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Compiler Design

UNIT-3
by
V VEERA RASAD
Asst. Prof., Department of CSE
Aditya College of Engineering

Approved by AICTE, Accredited by NAAC and


Permanently affiliated to JNTUK, Kakinada,
Topics

 More Powerful LR parser (LR1, LALR) Using


Armigers Grammars Equal Recovery in Lr
parser ​
 Syntax Directed Transactions Definition
 Evolution order of SDTS Application of

SDTS.
 Syntax ​Directed Translation Schemes. ​

Department of CSE, Aditya College of Engineering, Sur 2


Date : 09/06/2025
Why LR Parsers
 It is the most efficient method of Bottom-Up
Parsing which can be used to Parse the
large class of CFG
 This method is also called as LR(K) Parsing

L  Left to right scanning of input string


R  Constructing RMD in Reverse
K  Number of input symbols of the look
ahead used to make number of parsing
decision

Department of CSE, Aditya College of Engineering, Sur 3


Date : 09/06/2025
Model of LR Parser

Department of CSE, Aditya College of Engineering, Sur 4


Date : 09/06/2025
 LR parser consists of input buffer for storing
the input string
 A Stack for storing the grammar symbols
 Output and a Parsing table comprised of

two parts namely


(1) Action
(2) Goto
There is one Parsing program which is
actually a driven program and reads the
input symbol one at a time from the input
buffer

Department of CSE, Aditya College of Engineering, Sur 5


Date : 09/06/2025
The driven program works on the following
 It is initialize the stack with start symbol

and invokes Scanner(LA) to get the next


Token
 It determines Sj the state currently on the

top of the stack and ai the current input


symbol
 It consults the Parsing table for the

action[Sj, ai] which can have one of the four


values
Si means shift state I
rj means reduce by rule j
Accept means successful Parsing is
done
Department of CSE, Aditya College of Engineering, Sur 6
Date : 09/06/2025
Types of LR Parsers
LR – 1. SIMPLE LR(SLR)
2 CANONICAL LR(CLR)
3 LOOK AHEAD LR(LALR)
SLR<LALR<CLR

Department of CSE, Aditya College of Engineering, Sur 7


Date : 09/06/2025
LR(0) ITEM
A  XYZ is a given production
The items of the given production is
A  .XYZ
A  X.YZ
A  XY.Z
A  XYZ.
B  e is production
B.

Department of CSE, Aditya College of Engineering, Sur 8


Date : 09/06/2025
GOTO OPERATION
GOTO[I,X] I is set of items
X is grammar symbol
A  α.Xβ is in I
GOTO[I,X] = A  αX.β
E  E+.T
E  .T
GOTO[4,T] = E  E+T.
E  T.

Department of CSE, Aditya College of Engineering, Sur 9


Date : 09/06/2025
CLOSURE OPERATION
1 Initially every ITEM in I is added to
CLOSURE(I)
2 If A  α.Bβ is in CLOSURE(I) and Bλ is a
production then add the item B.λ to
closure(I), if it is not already there

Department of CSE, Aditya College of Engineering, Sur 10


Date : 09/06/2025
STEPS TO CONSTRUCT SLR
 Check the given grammar is CFG or not
 AUGMENTED GRAMMAR

S’S
S..
III) LR(0) ITEMS
IV) TABLE
V) PARSING

Department of CSE, Aditya College of Engineering, Sur 11


Date : 09/06/2025
 Construct SLR Parsing table for the following
grammar
EE+T|T
TT*F|F
F(E)|id

Department of CSE, Aditya College of Engineering, Sur 12


Date : 09/06/2025
Augmented grammar
E’E
EE+T
ET
TT*F
TF
F(E)
Fid

Department of CSE, Aditya College of Engineering, Sur 13


Date : 09/06/2025
LR(0) ITEMS CALCULATION
Io : Closure(E’.E) = E’.E
E.E+T
E.T
T.T*F
T.F
F.(E)
F.id
I1 :CLOSURE(GOTO[Io,E])=E’E.
EE.+T
I2 :CLOSURE(GOTO[Io,T])=ET.
TT.*F
I3 :CLOSURE(GOTO[Io,F])=TF.

Department of CSE, Aditya College of Engineering, Sur 14


Date : 09/06/2025
I4 :CLOSURE(GOTO[Io,(])=F(.E)
E.E+T
E.T
T.T*F
T.F
F.(E)
F.id
I5 :CLOSURE(GOTO[Io,id])=Fid.
I6 :CLOSURE(GOTO[I1, +])= EE+.T
T.T*F
T.F
F.(E)
F.id

Department of CSE, Aditya College of Engineering, Sur 15


Date : 09/06/2025
I7 :CLOSURE(GOTO[I2, *])=TT*.F
F.(E)
F.id
I8 :CLOSURE(GOTO[I4, E])=F(E.)
EE.+T
I2 :CLOSURE(GOTO[I4, T])=ET.
TT.*F
I3 :CLOSURE(GOTO[I4, F])=TF.
I4 :CLOSURE(GOTO[I4,(])=F(.E)
E.E+T
E.T
T.T*F
T.F
F.(E)
F.id
I5 :CLOSURE(GOTO[I4,id])=Fid.

Department of CSE, Aditya College of Engineering, Sur 16


Date : 09/06/2025
I9 :CLOSURE(GOTO[I6, T])=EE+T.
TT.*F
I3 :CLOSURE(GOTO[I6, F])=TF.
I4 :CLOSURE(GOTO[I6, (])=F(.E)
E.E+T
E.T
T.T*F
T.F
F.(E)
F.id
I5 :CLOSURE(GOTO[I6, id])=Fid.

Department of CSE, Aditya College of Engineering, Sur 17


Date : 09/06/2025
I10 :CLOSURE(GOTO[I7, F])=TT*F.
I4 :CLOSURE(GOTO[I7, (])=F(.E)
E.E+T
E.T
T.T*F
T.F
F.(E)
F.id
I5 :CLOSURE(GOTO[I7, id])=Fid.
I11 :CLOSURE(GOTO[I8, )])=F(E).
I6 : CLOSURE(GOTO[I8, +])=EE+.T
T.T*F
T.F
F.(E)
F.id
I7:CLOSURE(GOTO[I9,*])=TT*.F
F.(E)
F.id

Department of CSE, Aditya College of Engineering, Sur 18


Date : 09/06/2025
Construction of SLR parsing
table
Construct C = { I , I , ……. I }, the collection of sets of LR(0) items

0 1 n
for G’.
 State i is constructed from Ii. The parsing actions for state i are
determined as follow :
◦ If [ A -> α.aβ ] is in Ii and GOTO(Ii , a) = Ij , then set
ACTION[i, a] to “shift j”. Here a must be terminal.
◦ If [A -> α.] is in Ii, then set ACTION[i, a] to “reduce A -
> α” for all a in FOLLOW(A); here A may not be S’.
◦ Is [S’ -> S.] is in Ii, then set action[i, $] to “accept”. If
any conflicting actions are generated by the above
rules we say that the grammar is not SLR.
 The goto transitions for state i are constructed for all nonterminals
A using the rule:
if GOTO( Ii , A ) = Ij then GOTO [i, A] = j.
 All entries not defined by rules 2 and 3 are made error.

Department of CSE, Aditya College of Engineering, Sur 19


Date : 09/06/2025
(0)E’E. 1
(1)EE+T. 9
(2)ET. 2
(3)TT*F. 10
(4)TF. 3
(5)F(E). 11
(6)Fid. 5
FIRST(E)=FIRST(T)=FIRST(F)={(,id}
FOLLOW(E)={$,+,)}
FOLLOW(T)={$,+,)}U{*}={$,+,*,)}
FOLLOW(F)={$,+,*,)}

Department of CSE, Aditya College of Engineering, Sur 20


Date : 09/06/2025
STAT ACTION GOTO
E NO id + * ( ) $ E T F

0 S5 S4 1 2 3
1 S6 ACCEPT
2 R2 S7 R2 R2
3 R4 R4 R4 R4
4 S5 S4 8 2 3
5 R6 R6 R6 R6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 R1 S7 R1 R1
10 R3 R3 R3 R3
11 R5 R5 R5 R5

Department of CSE, Aditya College of Engineering, Sur 21


Date : 09/06/2025
Show the Parsing action for the input string:
id*(id+id)

Department of CSE, Aditya College of Engineering, Sur 22


Date : 09/06/2025
STACK INPUT BUFFER ACTION
0 Id*(id+id)$ S5
0id5 *(id+id)$ Reduce R6, Fid
0F3 *(id+id)$ Reduce R4, TF
0T2 *(id+id)$ S7
0T2*7 (id+id)$ S4
0T2*7(4 id+id)$ S5
0T2*7(4id5 +id)$ Reduce R6, Fid
0T2*7(4F3 +id)$ Reduce R4, TF
0T2*7(4T2 +id)$ Reduce R2, ET
0T2*7(4E8 +id)$ S6
0T2*7(4E8+6 id)$ S5
0T2*7(4E8+6id5 )$ Reduce R6, Fid
0T2*7(4E8+6F3 )$ Reduce R4, TF
0T2*7(4E8+6T9 )$ Reduce R1, EE+T
0T2*7(4E8 )$ S11

Department of CSE, Aditya College of Engineering, Sur 23


Date : 09/06/2025
0T2*7(4E8)11 $ Reduce R5,F(E)
0T2*7F10 $ REDUCE R3, TT*F
0T2 $ REDUCE R2, ET
0E1 $ ACCEPT

Department of CSE, Aditya College of Engineering, Sur 24


Date : 09/06/2025
(2) Construct SLR Parsing table for the
following grammar
SCC
CcC | d
Sol:
Augmented grammar:
S’S
SCC
CcC
Cd

Department of CSE, Aditya College of Engineering, Sur 25


Date : 09/06/2025
LR(0) ITEMS
I0: CLOSURE(S’.S)=S’.S
S.CC
C.cC
C.d
I1:CLOSURE(GOTO[I0,S])=S’S.
I2:CLOSURE(GOTO[I0,C])=SC.C
C.cC
C.d
I3:CLOSURE(GOTO[I0,c])=Cc.C
C.cC
C.d
I4:CLOSURE(GOTO[I0,d])=Cd.

Department of CSE, Aditya College of Engineering, Sur 26


Date : 09/06/2025
I5:CLOSURE(GOTO[I2,C])=SCC.
I3:CLOSURE(GOTO[I2,c])=Cc.C
C.cC
C.d
I4:CLOSURE(GOTO[I2,d])=Cd.
I6:CLOSURE(GOTO[I3,C])=CcC.
I3:CLOSURE(GOTO[I3,c])=Cc.C
C.cC
C.d
I4:CLOSURE(GOTO[I3,d])=Cd.

Department of CSE, Aditya College of Engineering, Sur 27


Date : 09/06/2025
SCC
CcC
Cd
FIRST(S)=FIRST(C)={c,d}
FOLLOW(S)={$}
FOLLOW(C)={c,d,$}
SCC SCC CcC

Department of CSE, Aditya College of Engineering, Sur 28


Date : 09/06/2025
(0)S’S. 1
(1)SCC. 5
(2)CcC. 6
(3)Cd. 4

Department of CSE, Aditya College of Engineering, Sur 29


Date : 09/06/2025
SLR Parsing Table
STATE ACTION GOTO
c d $ S C
0 S3 S4 1 2
1 ACCEPT
2 S3 S4 5
3 S3 S4 6
4 R3 R3 R3
5 R1
6 R2 R2 R2

Department of CSE, Aditya College of Engineering, Sur 30


Date : 09/06/2025
CLR Parser
 In the SLR method we were working with
LR(0) items.
 In CLR parsing we will be using LR(1) items.
 LR(k) item is defined to be an item using
Look-aheads of length k.
 So, the LR(1) item is comprised of two parts :
the LR(0) item and the Look-ahead associated
with the item.
 LR(1) parsers are more powerful parser.
 For LR(1) items we modify the Closure and
GOTO function.

Department of CSE, Aditya College of Engineering, Sur 31


Date : 09/06/2025
Closure Operation
Closure(I)
repeat
for (each item [ A -> α.Bβ, a ] in I )
for (each production B -> λ in G’)
for (each terminal b in FIRST(βa))
add [ B -> .λ , b ] to set I it
is not already added;
until no more items are added to I;
return I;

Department of CSE, Aditya College of Engineering, Sur 32


Date : 09/06/2025
Construction of CLR parsing
table
 Construct C = { I0, I1, ……. In}, the collection of sets of LR(1) items for G’.
 State i is constructed from Ii. The parsing actions for state i are determined
as follow :
◦ If [ A -> α.aβ,b ] is in Ii and GOTO(Ii , a) = Ij , then set
ACTION[i, a] to “shift j”. Here a must be terminal.
◦ If [A -> α.,a] is in Ii, then set ACTION[i, a] to “reduce A
-> α”(A‡$)
◦ If [S’ -> S.,$] is in Ii, then set action[i, $] to “accept”. If any
conflicting actions are generated by the above rules we say
that the grammar is not CLR.
 The goto transitions for state i are constructed for all nonterminals A using
the rule:
if GOTO( Ii , A ) = Ij then GOTO [i, A] = j.
 All entries not defined by rules 2 and 3 are made error.
 The initial state of the parser is the one constructed from the set containing
item [S’.S,$]

Department of CSE, Aditya College of Engineering, Sur 33


Date : 09/06/2025
(1) Construct CLR Parsing table for the
following grammar
SCC
CcC | d
Sol:
Augmented grammar:
S’S
SCC
CcC
Cd
FIRST(S)=FIRST(C)={c,d}

Department of CSE, Aditya College of Engineering, Sur 34


Date : 09/06/2025
LR(1) ITEMS
I0:CLOSURE(S’.S, $)=S’.S, $(Aα.Bβ,a)
S.CC, $
C.cC, c|d
C.d, c|d
I1:CLOSURE(GOTO[I0,S])=S’S., $
I2:CLOSURE(GOTO[I0,C])=SC.C, $
C.cC, $
C.d,$
I3:CLOSURE(GOTO[I0,c])=Cc.C, c|d
C.cC, c|d
C.d, c|d
I4:CLOSURE(GOTO[I0,d])=Cd., c|d

Department of CSE, Aditya College of Engineering, Sur 35


Date : 09/06/2025
I5: CLOSURE(GOTO[I2,C])=SCC. , $
I6: CLOSURE(GOTO[I2,c])=Cc.C ,$
C.cC, $
C.d, $
I7: CLOSURE(GOTO[I2,d])=Cd., $
I8: CLOSURE(GOTO[I3,C])=CcC., c|d
I3: CLOSURE(GOTO[I3,c])=Cc.C, c|d
C.cC, c|d
C.d, c|d
I4: CLOSURE(GOTO[I3,d])=Cd., c|d
I9: CLOSURE(GOTO[I6,C])=CcC. , $
I6: CLOSURE(GOTO[I6,c])=Cc.C , $
C.cC, $
C.d, $
I7: CLOSURE(GOTO[I6,d])=Cd. , $

Department of CSE, Aditya College of Engineering, Sur 36


Date : 09/06/2025
CLR PARSING TABLE
STATE ACION GOTO
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
7 R3
8 R2 R2
9 R2

Department of CSE, Aditya College of Engineering, Sur 37


Date : 09/06/2025
Parsing Action
STACK INPUT BUFFER ACTION
0 ccdd$ S3
0c3 cdd$ S3
0c3c3 dd$ S4
0c3c3d4 d$ Reduce R3, Cd
0c3c3C8 d$ Reduce R2, CcC
0c3C8 d$ Reduce R2, CcC
0C2 d$ S7
0C2d7 $ Reduce R3, Cd
0C2C5 $ Reduce R1, SCC
0S1 $ ACCEPT

Department of CSE, Aditya College of Engineering, Sur 38


Date : 09/06/2025
Construct CLR parsing table for the following grammar
EE+T | T
TT*F | F
F(E) | id
Sol:
AUGMENTED GRAMMAR:
E’E
EE+T
ET
TT*F
TF
F(E)
Fid

Department of CSE, Aditya College of Engineering, Sur 39


Date : 09/06/2025
LR(1) ITEMS
I0: CLOSURE(E’.E,$)=E’.E,$
E.E+T, $
E.T, $
T.T*F, $
T.F, $
F.(E), $
F.id, $
I1: CLOSURE(GOTO[I0,E])=E’E., $
EE.+T, $
I2: CLOSURE(GOTO[I0,T])=ET., $
TT.*F, $
I3: CLOSURE(GOTO[I0,F])=TF., $
I4: CLOSURE(GOTO[I0,(])=F(.E), $
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )

Department of CSE, Aditya College of Engineering, Sur 40


Date : 09/06/2025
I5: CLOSURE(GOTO[I0,id])=Fid., $
I6: CLOSURE(GOTO[I1,+])= EE+.T, $
T.T*F, $
T.F, $
F.(E), $
F.id, $
I7: CLOSURE(GOTO[I2,*])=TT*.F, $
F.(E), $
F.id, $
I8: CLOSURE(GOTO[I4,E])=F(E.),$
EE.+T, )
I9: CLOSURE(GOTO[I4,T])=ET., )
TT.*F,)
I10: CLOSURE(GOTO[I4,F])=TF., )
I11: CLOSURE(GOTO[I4,(])=F(.E), )
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I12: CLOSURE(GOTO[I4,id])=Fid., )

Department of CSE, Aditya College of Engineering, Sur 41


Date : 09/06/2025
I13: CLOSURE(GOTO[I6,T])=EE+T., $
TT.*F, $
I3: CLOSURE(GOTO[I6,F])=TF., $
I4: CLOSURE(GOTO[I6,(])=F(.E), $
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I5: CLOSURE(GOTO[I6,id])= Fid., $
I14: CLOSRE(GOTO[I7,F])=TT*F., $
I4: CLOSURE(GOTO[I7,(])=F(.E), $
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I3: CLOSURE(GOTO[I7,id])=Fid., $

Department of CSE, Aditya College of Engineering, Sur 42


Date : 09/06/2025
I15: CLOSURE(GOTO[I8,)]=F(E)., $
I16: CLOSURE(GOTO[I8,+])=EE+.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I17: CLOSURE(GOTO[I9,*])=TT*.F, )
F.(E), )
F.id, )
I18: CLOSURE(GOTO[I11,E])= F(E.), )
EE.+T, )
I9: CLOSURE(GOTO[I11,T])=ET., )
TT.*F, )
I10: CLOSURE(GOTO[I11,F])= TF., )
I11: CLOSURE(GOTO[I11,(])=F(.E), )
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I12: CLOSURE(GOTO[I11,id])=Fid., )

Department of CSE, Aditya College of Engineering, Sur 43


Date : 09/06/2025
I7: CLOSURE(GOTO[I13,*])=TT*.F, $
F.(E), $
F.id, $
I9: CLOSURE(GOTO[I16,T])=EE+T., )
TT.*F, )
I10: CLOSURE(GOTO[I16,F])=TF., )
I11: CLOSURE(GOTO[I16,(])=F(.E), )
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I12: CLOSURE(GOTO[I16,id])=Fid., )
I19: CLOSURE(GOTO[I17,F])=TT*F., )
I11: CLOSURE(GOTO[I17,(])=F(.E), )
E.E+T, )
E.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )
I12: CLOSURE(GOTO[I17,id])=Fid., )

Department of CSE, Aditya College of Engineering, Sur 44


Date : 09/06/2025
I20: CLOSURE(GOTO[I18,)])=F(E)., )
I16: CLOSURE(GOTO[I18,+])=EE+.T, )
T.T*F, )
T.F, )
F.(E), )
F.id, )

Department of CSE, Aditya College of Engineering, Sur 45


Date : 09/06/2025
LALR PARSER
 LALR parser are same as CLR parser with
one difference.
 In CLR parser if two states differ only in

Look-ahead then we combine those states


in LALR parser.
 After minimization if the parsing table has

no conflict that the grammar is LALR also.

Department of CSE, Aditya College of Engineering, Sur 46


Date : 09/06/2025
Construction of LALR Parsing
table
 Construct C= {I0, I1, I2,… , In}, the collection of sets
of LR(1) items.
 For each core present in among these sets, find all
sets having the core, and replace these sets by their
union.
 Parsing action table is constructed as for Canonical LR.
 The goto table is constructed by taking the union of all
sets of items having the same core. If J is the union of
one or more sets of LR (1) items, that is, J=I1 U I2 U …
U Ik, then the cores of goto(I1,X), goto(I2,X),…,
goto(Ik, X) are the same as all of them have same
core. Let K be the union of all sets of items having
same core as goto(I1, X). Then goto(J,X)=K.

Department of CSE, Aditya College of Engineering, Sur 47


Date : 09/06/2025
Construct LALR Parsing table for the following
grammar
SCC
CcC|d

Department of CSE, Aditya College of Engineering, Sur 48


Date : 09/06/2025
LALR(1) ITEMS
I0:CLOSURE(S’.S, $)=S’.S, $(Aα.Bβ,a)
S.CC, $
C.cC, c|d
C.d, c|d
I1:CLOSURE(GOTO[I0,S])=S’S., $
I2:CLOSURE(GOTO[I0,C])=SC.C, $
C.cC, $
C.d,$
I36:CLOSURE(GOTO[I0,c])=Cc.C, c|d|$
C.cC, c|d|$
C.d, c|d|$
I47:CLOSURE(GOTO[I0,d])=Cd., c|d|$
I5: CLOSURE(GOTO[I2,C])=SCC. , $
I89: CLOSURE(GOTO[I3,C])=CcC., c|d|$

Department of CSE, Aditya College of Engineering, Sur 49


Date : 09/06/2025
LALR Parsing Table
STATE
c d $ S C
0 S36 S47 1 2
1 ACCEPT
2 S36 S47 5
36 S36 S47 89
47 R3 R3 R3
5 R1
89 R2 R2 R2

Department of CSE, Aditya College of Engineering, Sur 50


Date : 09/06/2025
Parsing Action
STACK INPUT BUFFER ACTION
0 ccdd$ S36
0c36 cdd$ S36
0c36c36 dd$ S47
0c36c36d47 d$ Reduce R3, Cd
0c36c36C89 d$ Reduce R2, CcC
0c36C89 d$ Reduce R2, CcC
0C2 d$ S47
0C2d47 $ Reduce R3, Cd
0C2C5 $ Reduce R1, SCC
0S1 $ ACCEPT

Department of CSE, Aditya College of Engineering, Sur 51


Date : 09/06/2025
Construct SLR,CLR and LALR Parsing tables
for the following grammar
S(L) | a
LL,S | S
Show the parsing action for the input string
(a,a)

Department of CSE, Aditya College of Engineering, Sur 52


Date : 09/06/2025
OPERATOR PRECEDENCE
PARSER
 Operator precedence grammar is kinds of
shift reduce parsing method. It is applied to
a small class of operator grammars.
 A grammar is said to be operator

precedence grammar if it has two


properties:
 No R.H.S. of any production has a∈.
 No two non-terminals are adjacent.
 Operator precedence can only established

between the terminals of the grammar. It


ignores the non-terminal
Department of CSE, Aditya College of Engineering, Sur 53
Date : 09/06/2025
Parsing Operations
If a<b or a=b then
PUSH b on to the Stack
advance the input pointer to the next
input symbol
Else if a>b then
POP the stack
Else
ERROR;

Department of CSE, Aditya College of Engineering, Sur 54


Date : 09/06/2025
Consider the following grammar
E EAE | (E) | id
A + | - | * | /
Construct Operator Precedence Parsing table
Show the Parsing action for the input string
“id+(id*id)”

Department of CSE, Aditya College of Engineering, Sur 55


Date : 09/06/2025
Operator Precedence Table
+ - * / id ( ) $
+ > > < < < < > >
- > > < < < < > >
* > > > > < < > >
/ > > > > < < > >
Id > > > > error error > >
( < < < < < < = >
) > > > > error error > >
$ < < < < < < < ACCE
PT

Department of CSE, Aditya College of Engineering, Sur 56


Date : 09/06/2025
Parsing
STACK Action
INPUT BUFFER ACTION
$ Id+(id*id)$ $<id, SHIFT
$id +(id*id)$ Id>+, reduce Eid
$E +(id*id)$ $<+, shift
$E+ (id*id)$ +<(, shift
$E+( id*id)$ (<id, shift
$E+(id *id)$ Id>*, reduce, Eid
$E+(E *id)$ (<*, shift
$E+(E* id)$ *<id, shift
$E+(E*id )$ Id>), reduce, Eid
$E+(E*E )$ *>), Reduce, EE*E
$E+(E )$ (=), SHIFT
$E+(E) $ )>$, REDCE
$E+E $ +>$, REDUCE
$E $ ACEEPT

Department of CSE, Aditya College of Engineering, Sur 57


Date : 09/06/2025
Construct Operator Precedence Parser for the
following grammar
S(T) | a
TT,S | S
Show the parsing action for the input string
“(a,a)”

Department of CSE, Aditya College of Engineering, Sur 58


Date : 09/06/2025
Operator Precedence Parsing
Table
a ( ) , $
a Error Error > > >
( < < = < >
) Error Error > > >
, < < > > >
$ < < < < <

Department of CSE, Aditya College of Engineering, Sur 59


Date : 09/06/2025
SEMANTIC ANALYSIS
 Semantic Analysis makes sure that
declarations and statements of program are
semantically correct.
 It is a collection of procedures which is called
by parser as and when required by grammar.
 Both syntax tree of previous phase and symbol
table are used to check the consistency of the
given code.
 Type checking is an important part of
semantic analysis where compiler makes sure
that each operator has matching operands.

Department of CSE, Aditya College of Engineering, Sur 60


Date : 09/06/2025
Role of Semantic Analyzer
 It uses syntax tree and symbol table to
check whether the given program is
semantically consistent with language
definition.
 It gathers type information and stores it in

either syntax tree or symbol table.


 This type information is subsequently used

by compiler during intermediate-code


generation

Department of CSE, Aditya College of Engineering, Sur 61


Date : 09/06/2025
Semantic Errors
Errors recognized by semantic analyzer are
as follows:
 Type mismatch
 Undeclared variables
 Reserved identifier misuse

Department of CSE, Aditya College of Engineering, Sur 62


Date : 09/06/2025
Functions of Semantic
Analysis
 Type Checking –
Ensures that data types are used in a way
consistent with their definition.
 Label Checking –

A program should contain labels references.


 Flow Control Check –

Keeps a check that control structures are


used in a proper manner.(example: no break
statement outside a loop)

Department of CSE, Aditya College of Engineering, Sur 63


Date : 09/06/2025
Example
float x = 10.1;
float y = x*30;

In the above example integer 30 will be type-


casted to float 30.0 before multiplication, by
semantic analyzer.

Department of CSE, Aditya College of Engineering, Sur 64


Date : 09/06/2025
Static and Dynamic Semantics:
 Static Semantics –
It is named so because of the fact that
these are checked at compile time. The
static semantics and meaning of program
during execution, are indirectly related.
 Dynamic Semantic Analysis –

It defines the meaning of different units of


program like expressions and statements.
These are checked at runtime unlike static
semantics.

Department of CSE, Aditya College of Engineering, Sur 65


Date : 09/06/2025
S – attributed and L – attributed
SDTs in Syntax directed translation
 Types of attributes –
Attributes may be of two types – Synthesized or
Inherited.
 Synthesized attributes –
A Synthesized attribute is an attribute of the non-
terminal on the left-hand side of a production.
Synthesized attributes represent information that is
being passed up the parse tree. The attribute can
take value only from its children (Variables in the
RHS of the production).For eg. let’s say A -> BC is a
production of a grammar, and A’s attribute is
dependent on B’s attributes or C’s attributes then it
will be synthesized attribute

Department of CSE, Aditya College of Engineering, Sur 66


Date : 09/06/2025
Inherited attributes –
An attribute of a nonterminal on the right-
hand side of a production is called an
inherited attribute. The attribute can take
value either from its parent or from its
siblings (variables in the LHS or RHS of the
production).For example, let’s say A -> BC
is a production of a grammar and B’s
attribute is dependent on A’s attributes or
C’s attributes then it will be inherited
attribute.

Department of CSE, Aditya College of Engineering, Sur 67


Date : 09/06/2025

You might also like