Professional Documents
Culture Documents
Syntax Analysis (Parsers)
Syntax Analysis (Parsers)
Syntax Analysis (Parsers)
Unit: 2
Subject:
Compiler Design (RCS-602)
Nishant Kumar Hind
Course Details Assistant Professor
B Tech CSE 6th Sem CSE Department
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10PO11PO12
RCS-602.1 3 3 3 3 3 1 1 1 3 1 2 2
RCS-602.2 3 3 3 3 3 1 1 1 3 1 2 2
RCS-602.3 3 3 3 3 3 1 1 1 3 1 2 2
RCS-602.4 3 2 3 3 3 1 1 1 3 1 2 2
RCS-602.5 3 2 3 3 3 1 1 1 3 1 2 2
AVG 3 2.6 3 3 3 1 1 1 3 1 2 2
• Algorithms
• Languages and machines
• Operating systems
• Computer architectures
• Theory of Automata
• getNext aly
• Token zer
• Symbol
• table
• Algorithm
– For each non-terminal A, find the longest prefix α common to
two or more of its alternatives. If α<> ɛ, then replace all of A-
productions A->αβ1 |αβ2 | … | αβn | γ by
• A -> αA’ | γ
• A’ -> β1 |β2 | … | βn
• Example:
– S -> I E t S | i E t S e S | a
– E -> b
E -> TE’ E E E E E E
lm lm lm lm lm
E’ -> +TE’ | Ɛ
T -> FT’ T E’ T E’ T E’ T E’ T E’
T’ -> *FT’ | Ɛ
F -> (E) | id F T’ F T’ F T’ F T’ + T E’
id id Ɛ id Ɛ
• Example
S->cAd
A->ab | a Input: cad
S S S
c A d c A d c A d
a b a
• Example
First Follow
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’ {(,id} {+, *, ), $}
F
T’ -> *FT’ | Ɛ {(,id} {+, ), $}
T
F -> (E) | id
E {(,id} {), $}
E’ {+,ɛ}
T’ {), $}
{*,ɛ} {+, ), $}
Input Symbol
Non -terminal id + * ( ) $
E E -> TE’ E -> TE’
E’ E’ -> Ɛ E’ -> Ɛ
E’ -> +TE’
T
T -> FT’ T -> FT’
T’
T’ -> Ɛ T’ -> *FT’ T’ -> Ɛ T’ -> Ɛ
F
a +
• Predictive
• X • parsing • output
• st
• Y • program
a
c • Z
k • $ • Parsing
• Table
• M
Nishant Kumar Hind RCS-602 CD Unit -2
08/11/2021 28
Construction of Predictive Parsing Table
• The general idea is to shift some symbols of input to the stack until
a reduction can be applied
• At each reduction step, a specific substring matching the body of a
production is replaced by the nonterminal at the head of the
production
• The key decisions during bottom-up parsing are about when to
reduce and about what production to apply
• A reduction is a reverse of a step in a derivation
• The goal of a bottom-up parser is to construct a derivation in
reverse:
– E=>T=>T*F=>T*id=>F*id=>id*id
id*id id F->id
F*id F T->F
T*id id F->id
•
• For this we can construct two functions f and g where the following
hold:
1. If a. >b then f(a)>g(b)
2. If a< . b then f(a)<g(b)
3. If a. =b then f(a)=g(b)
• Augmented grammar:
– G with addition of a production: S’->S
• Closure of item sets:
– If I is a set of items, closure(I) is a set of items constructed from I
by the following rules:
• Add every item in I to closure(I)
• If A->α.Bβ is in closure(I) and B->γ is a production then add
the item B->.γ to clsoure(I). I0=closure({[E’->.E]}
• Example E’->.E
E->.E+T
E’->E E->.T
E -> E + T | T
T->.T*F
T -> T * F | F
F -> (E) | id T->.F
F->.(E)
F->.id
Nishant Kumar Hind RCS-602 CD Unit -2
08/11/2021 48
Constructing canonical LR(0) item sets
• Closure algorithm
SetofItems CLOSURE(I) {
J=I;
repeat
for (each item A-> α.Bβ in J)
for (each prodcution B->γ of G)
if (B->.γ is not in J)
add B->.γ to J;
until no more items are added to J on one round;
return J;
• GOTO algorithm
SetofItems GOTO(I,X) {
J=empty;
if (A-> α.X β is in I)
add CLOSURE(A-> αX. β ) to J;
return J;
}
• •$ I2 • • I9
E->E+.T
>E. •
•
T
T->.T*F
• +
• • T->.F
E->E+T.
• E •• E-
E’-
• I0=closure({[E’- • F->.(E)
• T->T.*F
>.E]} • F->.id
•
•
E’->.E
E->.E+T
>T.
• T >E. • *
•
• F • T->T*F.
•
• I10I7
T->T*.F
• E->.T
•• I5 • id
T-
+T
• F->.(E)
• T->.T*F
• id • F->.id
• T->.F
• F->T.* • +
•
•
F->.(E)
• ( • I4
F->.id
• F
>id. F->(.E)
• I8 • I11
• E •• E->E.+T •
• E->.E+T
F •
•
E->.T
T->.T*F F->(E.)
) • F->(E).
• T->.F
• F->.(E)
• I3
• F->.id
• T>F.
Nishant Kumar Hind RCS-602 CD Unit -2
08/11/2021 53
LR parsing Algorithm
let a be the first symbol of w$;
while(1) { /*repeat forever */
let s be the state on top of the stack;
if (ACTION[s,a] = shift t) {
push t onto the stack;
let a be the next input symbol;
} else if (ACTION[s,a] = reduce A->β) {
pop |β| symbols of the stack;
let state t now be on top of the stack;
push GOTO[t,A] onto the stack;
output the production A->β;
} else if (ACTION[s,a]=accept) break; /* parsing is done */
else call error-recovery routine;
}
I4
Cd. , c/d
I47
Cd. , c/d/$
I7
Cd. , $
• Which of the following methods is used by the bottom-up parser to generate a parse
tree?
(a) Leftmost derivation
(b) Rightmost derivation
(c) Leftmost derivation in reverse
(d) Rightmost derivation in reverse
• Handle pruning forms the basis of ____________ .
(a) Bottom-up parsing
(b) Top-down parsing
(c) Both (a) and (b)
(d) None of these
• In shift-reduce parsing, accept action occurs __________ .
(a) When we have the right end of the handle at the top of the stack
(b) When we have the left end of the handle at the top of the stack
(c) When parser declares the successful completion of parsing
(d) When the parser finds a syntax error in the input and calls an error recovery routine
• If A → αβ is a production and a is terminal symbol or right end marker $, then LR(1) items
will be defined by the production ________.
(a) [A → α.β, a]
(b) [A → .αβ, a]
(c) [A → α.βa]
(d) [A → a.β, α]
• Given a grammar G:
T → BCTd | Bcd
CB → BC
Cc → cc
Bc → bc
Bb → b
Which of the following sentences can be derived by G?
(a) bcd
(b) bbc
(c) bcdd
(d) bccd
• Construct LL (1) parsing table for the following grammar and parse
string (a, (a, a)) by using the table.
S (L) | a
L L, S | S
• Explain LR (k) parsers and write an algorithm for implementing it.
• Construct LR(1) parsing table for grammar. Where start symbol is S.
S xAy / xBy / xAz
A aS / q
B q.
Thank You