Professional Documents
Culture Documents
SEMANTIC ANALYSIS
– PART B
S L I D E S C O M P I L E D B Y: F A D Z L I N A H M A D O N
F A C U LT Y O F C O M P U T E R & M A T H E M A T I C A L
SCIENCES
UITM (MELAKA) JASIN CAMPUS
T E X T B O O K : C O M P I L E R D E S I G N : T H E O R Y, T O O L S &
E X A M P L E S ( J AVA E D I T I O N ) B Y B E R G M A N N , S E T H D . 1
(CHAPTER 4)
a S b
G8:
1. S → a S b b A c
2. S → b A c
3. A → b S b S
4. A → a
b A c
a
Derivation Tree
1 2 3 2 4
Fadzlin Ahmadon - UiTM Jasin
RELATIONS AND CLOSURE
• (a,b)
• (c,d)
• (b,a)
all pairs of R1
• (b,c)
• (c,c)
• (a,c)
• (b,d) transitive
• (a,d)
• (a,a)
• (b,b) reflexive
• (d,d)
2. Every pair of rules defining the same non terminal begin with
different terminals on the right side of the arrow
• Rule 1: { a }
• Rule 2: { b }
rule 2 b
abb a S d
b a S d
abbad rule 3 a b S d
b a S d
a a b a
S → S → S → b → → → ↵
▽ B B B B a Accep
▽ ▽ ▽ ▽ ▽ ▽ t
• Example: acbb
a A S a A S
c A S b
c A S
rule 2 rule 2
acb ⇒ acbb ⇒
ε b ε b
Fadzlin Ahmadon - UiTM Jasin 26
EXTENDED PUSHDOWN MACHINES
CONSTRUCTION FOR QUASI-SIMPLE
GRAMMARS
1. Build a table with:
1. Each column labeled by a terminal symbol (and endmarker ↵)
2. Each row labeled by a non terminal or terminal (and bottom marker ▽)
2. For each grammar rule of the form A → aα, fill in the cell in row A
and column a with: REP(αra), Retain where αr = α reversed
3. Fill in the cell in the row a and column a with pop, advance for
each terminal symbol a
4. Fill in the cell in row ▽ and column ↵ with Accept
1. For each ε rule in the grammar, fill in cells of the row corresponding to the
nonterminal on the left side of the arrow, but only in those columns
corresponding to elements of the follow set of the nonterminal. Fill in these cells
with Pop, Retain
5. Fill in all other cells with Reject
6. Initialize stack with ▽ and the starting non terminal
Fadzlin Ahmadon - UiTM Jasin 27
EXTENDED PUSHDOWN MACHINES
CONSTRUCTION FOR QUASI-SIMPLE
GRAMMARS
a b c ↵
S Rep (SAa) Rep
Retain (b) Reject Reject S
Retain ▽
A Pop Pop Rep
Retain Retain (SAc) Reject G14:
Retain
a Pop
1. S → a A S
Advance Reject Reject Reject 2. S → b
3. A → c A S
b Pop
Reject Advance Reject Reject 4. A → ε
Pop G14:
c Reject Reject Advance Sel(1) = {a}
Sel(2) = {b}
▽ Reject Reject Reject Accept
Sel(3) = {c}
Fadzlin Ahmadon - UiTM Jasin Sel(4) = {a,b} 28
RECURSIVE DESCENT PARSER FOR
QUASI-SIMPLE GRAMMARS
• Recursive descent parser for quasi-simple grammars are
similar to those for simple grammars
• The only difference is if any of the selection set for ε rule is
the current input symbol, we simply return to the calling
method without reading any input
void S ()
{
if (inp=='a') void A ()
// apply rule 1 {
{ if (inp=='c')
inp = getInp(); {
A (); inp = getInp();
S (); // apply rule 3
} // end rule 1 A();
else S();
if (inp=='b') }// end rule 3
inp = getInp(); else
// apply rule 2 if (inp=='a'||inp=='b')
else ; // apply rule 4
reject(); else
} reject();
}
G15:
1. S → ABc
2. A → bA
3. A → ε
4. B → c
Step 1.
Find all nullable rules and nullable nonterminals:
Step 1.
Find all nullable rules and nullable nonterminals:
d. All rules in the form A → B C D ... where B,C,D,... are all nullable
non-terminals are nullable rules, (nullable nonterminals).
e. A nonterminal is nullable if ε can be derived from it, and a rule is
nullable if ε can be derived from its right side.
Step 2.
Compute the relation “Begins Directly With” for each nonterminal:
Step 2.
Compute the relation “Begins Directly With” for each
nonterminal:
G15 :
S BDW A (from rule 1)
S BDW B (also from rule 1, because A is nullable)
A BDW b (from rule 2)
B BDW c (from rule 4
LL(1) GRAMMARS
Step 3.
Compute the relation “Begins With”:
S BW S
A BW A
B BW B (reflexive)
b BW b
c BW c
LL(1) GRAMMARS
Step 4.
Compute the set of terminals "First(x)" for each symbol x in the
grammar.
a. At this point, we can find the set of all terminals which can
begin a sentential form when starting with a given symbol of the
grammar.
b. First(A) = set of all terminals b, such that A BW b for each
nonterminal A.
c. First(t) = {t} for each terminal.
LL(1) GRAMMARS
Step 4.
Compute the set of terminals "First(x)" for each symbol x in the
grammar.
For G15 –
First(S) = {b,c}
First(A) = {b}
First(B) = {c}
First(b) = {b}
First(c) = {c}
LL(1) GRAMMARS
Step 5.
Compute "First" of right side of each rule:
a. Compute the set of terminals which can begin a sentential form
derivable from the right side of each rule.
First (XYZ...) = {First(X)}
U {First(Y)} if X is nullable
U {First(Z)} if Y is also nullable . . .
b. Find the union of the First(x) sets for each symbol on the right
side of a rule, but stop when reaching a non-nullable symbol.
LL(1) GRAMMARS
Step 5.
Compute "First" of right side of each rule:
For G15 –
1. First(ABc)=First(A) U First(B)={b,c}
(because A is nullable)
2. First(bA) = {b}
3. First(ε) = {}
4. First(c) = {c}
For G15 –
A FDB B (from rule 1)
B FDB c (from rule 1)
Step 7.
Compute the relation “Is Direct End Of”:
Step 7.
Compute the relation “Is Direct End Of”:
For G15 –
c DEO S (from rule 1)
A DEO A (from rule 2)
b DEO A (from rule 2, since A is nullable)
c DEO B (from rule 4)
LL(1) GRAMMARS
Step 8.
Compute the relation “Is End Of”:
Step 8.
Compute the relation “Is End Of”:
For G15 –
c EO S
A EO A (from DEO )
b EO A
c EO B
(no transitive entries)
c EO c
S EO S (reflexive)
b EO b
B EO B
LL(1) GRAMMARS
Step 9.
Compute the relation “Is Followed By”:
Step 9.
Compute the relation “Is Followed By”:
For G15 –
A EO A A FDB B B BW B A FB B
B BW c A FB c
b EO A B BW B b FB B
B BW c b FB c
B EO B B FDB c c BW c B FB c
c EO B c BW c c FB c
LL(1) GRAMMARS
Step 10.
Extend the FB relation to include endmarker:
For G15 –
S FB ↵ because S EO S
There are now seven pairs in the FB relation for grammar G15.
LL(1) GRAMMARS
Step 11.
Compute the Follow Set for each nullable nonterminal:
For G15 –
Fol(A) = {c} since A is the only nullable
nonterminal and A FB c.
LL(1) GRAMMARS
Step 12.
Compute the selection set for each rule:
i. A → α
if rule i is not a nullable rule, then
Sel(i) = First(α) (from Step 5)
if rule i is a nullable rule, then
Sel(i) = First(α) U Fol(A)
LL(1) GRAMMARS
Step 12.
Compute the selection set for each rule:
For G15 –
Sel(1) = First(ABc) = {b,c}
Sel(2) = First(bA) = {b}
Sel(3) = First(ε) U Fol(A) = {} U {c} = {c}
Sel(4) = First(c) = {c}
b c ↵
Pushdown Machines for
S Rep Rep LL(1) Grammars exactly as
(cBA) (cBA) Reject
Retain Retain
Pushdown Machines for
quasi-simple grammars
A Rep (Ab) Pop
Retain Retain Reject
B Reject Rep (c)
Retain Reject G15:
b Pop 1. S → ABc
Advance Reject Reject S
c Pop ▽ 2. A → bA
Reject Advance Reject 3. A → ε
▽ Reject Reject Accept Initial 4. B → c
Stack
Pushdown Machine for Grammar G5
b
b A A A c c Accept
→ B → B → B → B → c → → ↵
S c c c c c c →
▽ ▽ ▽ ▽ ▽ ▽ ▽ ▽
void B ()
{
if (inp=='c')
inp=getInp(); // apply rule 4
else
reject();
}
Dependency Graph for the Steps in the
Algorithm for Finding Selection Set 1
A→βR
R→αR
R→ε
Parsing Arithmetic Expressions Top Down
G16:
1. Expr → Term Elist
2. Elist → + Term Elist
3. Elist → ε
4. Term → Factor Tlist
5. Tlist → ∗ Factor Tlist
6. Tlist → ε
7. Factor → ( Expr )
8. Factor → var
Step 2.
Step 1. Expr BDW Term
Nullable rules: 3,6 Elist BDW +
Nullable nonterminals: Term BDW Factor
Elist, Tlist Tlist BDW ∗
Factor BDW (
Factor BDW var
Parsing Arithmetic Expressions Top Down
G16:
1. Expr → Term Elist
2. Elist → + Term Elist
Step 3. 3. Elist → ε
Expr BW Term 4. Term → Factor Tlist
Elist BW + 5. Tlist → ∗ Factor Tlist
Term BW Factor (from BDW) 6. Tlist → ε
Tlist BW ∗ 7. Factor → ( Expr )
Factor BW ( 8. Factor → var
Factor BW var
Expr BW Expr
Term BW Term
Expr BW Factor Factor BW Factor
Term BW ( Elist BW Elist
Term BW var (transitive) Tlist BW Tlist (reflexive)
Expr BW ( Factor BW Factor
Expr BW var + BW +
∗ BW ∗
( BW (
var BW var
) BW )
Parsing Arithmetic Expressions Top Down
G16:
1. Expr → Term Elist
Step 4. 2. Elist → + Term Elist
First (Expr) = {(,var} 3. Elist → ε
First (Elist) = {+} 4. Term → Factor Tlist
First (Term) = {(,var} 5. Tlist → ∗ Factor Tlist
First (Tlist) = {∗} 6. Tlist → ε
First (Factor) = {(,var} 7. Factor → ( Expr )
8. Factor → var
Step 5.
1. First(Term Elist) = {(,var}
2. First(+ Term Elist) = {+}
3. First(ε) = {}
4. First(Factor Tlist) = {(,var}
5. First(∗ Factor Tlist) = {∗}
6. First(ε) = {}
7. First(( Expr )) = {(}
8. First(var) = {var}
Parsing Arithmetic Expressions Top Down
Step 6. G16:
Term FDB Elist 1. Expr → Term Elist
Factor FDB Tlist 2. Elist → + Term Elist
Expr FDB ) 3. Elist → ε
4. Term → Factor Tlist
Step 7. 5. Tlist → ∗ Factor Tlist
Elist DEO Expr 6. Tlist → ε
Term DEO Expr 7. Factor → ( Expr )
Elist DEO Elist 8. Factor → var
Term DEO Elist
Tlist DEO Term
Factor DEO Term
Tlist DEO Tlist
Factor DEO Tlist
) DEO Factor
var DEO Factor
Parsing Arithmetic Expressions Top Down
Step 8. ) EO Term
Elist EO Expr ) EO Tlist
Term EO Expr ) EO Expr (transitive)
Elist EO Elist ) EO Elist
Term EO Elist var EO Term
Tlist EO Term var EO Tlist
Factor EO Term (from DEO) var EO Expr
Tlist EO Tlist var EO Elist
Factor EO Tlist
) EO Factor Expr EO Expr
Term EO Term
var EO Factor Factor EO Factor
Tlist EO Expr ) EO )
Tlist EO Elist (transitive) var EO var (reflexive)
Factor EO Expr + EO +
Factor EO Elist ∗ EO ∗
( EO (
Elist EO Elist
Tlist EO Tlist
Parsing Arithmetic Expressions Top Down
Step 9.
Tlist EO Term FDB Elist BW + Tlist FB +
BW Elist
Factor EO BW +
BW Elist
var EO BW +
BW Elist
Term EO BW +
BW Elist
) EO BW +
BW Elist
) EO Factor FDB Tlist BW ∗
BW Tlist
var EO BW ∗
BW Tlist
Factor EO BW ∗
BW Tlist
Elist EO Expr FDB ) BW ) Elist FB )
Tlist EO Expr Tlist FB )
Parsing Arithmetic Expressions Top Down
Step 10. G16:
Elist FB ↵ 1. Expr → Term Elist
Term FB ↵ 2. Elist → + Term Elist
Expr FB ↵ 3. Elist → ε
Tlist FB ↵ 4. Term → Factor Tlist
Factor FB ↵ 5. Tlist → ∗ Factor Tlist
6. Tlist → ε
Step 11. 7. Factor → ( Expr )
Fol (Elist) = {), ↵} 8. Factor → var
Fol (Tlist) = {+,),
↵}
Step 12.
Sel(1) = First(Term Elist) = {(,var}
Sel(2) = First(+ Term Elist) = {+}
Sel(3) = Fol(Elist) = {), ↵}
Sel(4) = First(Factor Tlist) = {(,var}
Sel(5) = First(∗ Factor Tlist) = {∗}
Sel(6) = Fol(Tlist) = {+,), ↵}
Sel(7) = First( ( Expr ) ) = {(}
Sel(8) = First(var) = {var}
PUSHDOWN MACHINES FOR LL(1)
GRAMMARS
G16:
1. Expr → Term Elist Sel(1) = {(,var}
2. Elist → + Term Elist Sel(2) = {+}
3. Elist → ε Sel(3) = {), ↵}
4. Term → Factor Tlist Sel(4) = {(,var}
5. Tlist → ∗ Factor Tlist Sel(5) = {∗}
6. Tlist → ε Sel(6) = {+,), ↵}
7. Factor → ( Expr ) Sel(7) = {(}
8. Factor → var Sel(8) = {var}
Since all rules defining the same non terminal (rules 2 and 3, rules 5 and 6
and rules 7 and 8 have disjoint selection sets, the grammar G16 is LL(1)
grammar.
+ * ( ) var ↵
Expr Reject Reject Rep(Elist, Reject Rep(Elist, Reject
Term) Term)
Retain Retain
Elist Rep(Elist, Term, Reject Reject Pop Reject Pop
+) Retain Retain
Retain