Unit - 3 Context Free Grammar: Prof. Dixita B. Kagathara
Unit - 3 Context Free Grammar: Prof. Dixita B. Kagathara
GTU # 3160704
Unit – 3
Context Free
Grammar
Grammar
Unrestricted Restricted
grammar (type 0) grammar
Type 3
(Regular)
0 1
𝐴→ 0 𝐴
0 𝐴→ 1 𝐵
𝐵→0𝐶
𝐴 1
𝐵 𝐶 𝐵 →1 𝐵
1
0
𝐶 →0 𝐴
𝐶 →1 𝐵
𝐵→0
At last, all the incoming
transitions to the accepting
states are designated by the
a B a
b
b
A C
b b
a a
a
E D
S S
Parse tree
S-S represents the
structure of S - S
S*S-S derivation
a*S-S S S
* a
a*a-S
a*a-a a a
Leftmost
Parse tree
Derivation
#3160704 (TOC) Unit 3 – Context Free
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3130006 (PS) Unit 1 – Basic Probability 26
Rightmost derivation
A derivation of a string in a grammar is a right most derivation if at every
step the right most non terminal is replaced.
It is all called canonical derivation.
Grammar: SS+S | S-S | S*S | S/S | a Output string: a*a-a
S
S
S*S
S * S
S*S-S
S*S-a a S S
-
S*a-a
a*a-a a a
Rightmost Parse Tree
Derivation
#3160704 (TOC) Unit 3 – Context Free
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3130006 (PS) Unit 1 – Basic Probability 27
Exercise: Derivation
1. Perform leftmost derivation and draw parse tree.
SA1B
A0A | 𝜖
B0B | 1B | 𝜖
Output string: 1001
2. Perform leftmost derivation and draw parse tree.
S0S1 | 01 Output string: 000111
3. Perform rightmost derivation and draw parse tree.
EE+E | E*E | id | (E) | -E
Output string: id + id * id
Chip
S S S S
S*S S+S
S * S S + S
S+S*S a+S
a+S*S S + S a a+S*S a S * S
a+a*S a+a*S
a+a*a a a a+a*a a a
Here, Two leftmost derivation for string a+a*a is possible hence, above
grammar is ambiguous.
Sa X | SaX | Yb |
SaX|Yb|a
Yb a^
XS
X ˄ | S X^ | S
YbY|b
YbY|b YbY|b
Replacing X by ^ in
Nullable all productions Removing ^
variable={X} productions
containing X on
RHS and rewriting
the production
again
SABA|BA|AA|
|aA|a|bB|b Removing unit
AaA|aAB
BbB|b productions