Grammar RG CFG Afl
Grammar RG CFG Afl
Lecture Summary
1. Introduction to grammar
2. Regular grammar
3. Context Free Grammar
1 Introduction to Grammar
It is a finite set of formal rules for generating syntactically correct sentences.
Every language is generated by the rules of grammar.
In English grammar, if a sentence is correct grammatically then that sentence will be the part of
language, otherwise not. Example- “I am going to school”. It is a valid example of English
language. But, “I going am to school”. It is not a valid example of English language.
A production rule has the form α → β, where α and β are strings on V∪T and atleast one
symbol of α belongs to V. In otherwords, P is a finite set of productions of form
(V∪T)+ → (V∪T)*
The left-hand side of the production can contain variables (non-terminals) and terminals, but
non-empty symbols (ε or ∈). The right-hand side of the production can contain variables (non-
terminals) and terminals, including empty symbols (ε or ∈).
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 1
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
It means to start with starting production and after applying some production rules, the
string w is generated.
Similarly,
S → aS
S → aaS (Replacing S in right by production rule S → aS)
S → aa (Replacing S in right by production rule S → ε)
Similarly,
S → aS
S → aaS (Replacing S in right by production rule S → aS)
S → aaaS (Replacing S in right by production rule S → aS)
S → aaa (Replacing S in right by production rule S → ε)
S → aS
S → aaS (Replacing S in right by production rule S → aS)
S → aaaS (Replacing S in right by production rule S → aS)
S → aaaaS (Replacing S in right by production rule S → aS)
S → aaaa (Replacing S in right by production rule S → ε)
Observing the above derivations, we found that S derives strings as ε, a, aa, aaa, aaaa,…… this
is nothing but the language having RE=a*
L(G) = {w | w ∈ T*}
Example-2
Consider the grammar G = ({S}, {a, b}, {S → aS, S → bS, S → ε} , S). Find out the language
generated by this grammar.
Solution
Here, V = {S}, T = {a, b}, P ={S → aS, S → bS, S → ε}, S=S
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 2
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Similarly,
S → bS
S → b (Replacing S in right by production rule S → ε)
Similarly,
S → aS
S → aaS (Replacing S in right by production rule S → aS)
S → aa (Replacing S in right by production rule S → ε)
Similarly,
S → bS
S → bbS (Replacing S in right by production rule S → bS)
S → bb (Replacing S in right by production rule S → ε)
Similarly,
S → aS
S → abS (Replacing S in right by production rule S → bS)
S → ab (Replacing S in right by production rule S → ε)
Similarly,
S → bS
S → baS (Replacing S in right by production rule S → aS)
S → ba (Replacing S in right by production rule S → ε)
Similarly,
S → aS
S → aaS (Replacing S in right by production rule S → aS)
S → aaaS (Replacing S in right by production rule S → aS)
S → aaa (Replacing S in right by production rule S → ε)
Similarly,
S → bS
S → bbS (Replacing S in right by production rule S → bS)
S → bbbS (Replacing S in right by production rule S → bS)
S → aaa (Replacing S in right by production rule S → ε)
Similarly,
S → aS
S → aaS (Replacing S in right by production rule S → aS)
S → aabS (Replacing S in right by production rule S → bS)
S → aab (Replacing S in right by production rule S → ε)
Similarly,
S → aS
S → abS (Replacing S in right by production rule S → bS)
S → abaS (Replacing S in right by production rule S → aS)
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 3
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Observing the above derivations, we found that S derives strings as ε, a, b, aa, bb, aaa, bbb, aab,
aba,…… this is nothing but the language having RE=(a+b)*
L(G) = {w | w ∈ T*}
Example-3
Consider the grammar G = ({S, A, B}, {a, b}, {S → AB, A → aA|a, B → bB|b}, S)
Solution
Here, V = {S, A, B}, T = {a, b}, P = {S → AB, A → aA|a, B → bB|b}, Statrt Symbol=S
Taking the production with start symbol,
S → AB S → AB S → AB
→ aB (as A → a) → aAB (as A → aA) → aAB (as A → aA)
→ ab (as B → b) → aaB (as A → a) → aaB (as A → a)
This generates string ab → aab (as B → b) → aabB (as B → bB)
→ a2b (as B → b) → aabb (as B → b)
This generates string a b
2
→ a2b2 (as B → b)
This generates string a2b2
From the productions A → aA|a, if A will be replaced repeatedly, then A will generate any
number of a’s. Similarly, B → bB|b will derive the strings with any number of b’s.
Now, S → AB will geneate the string with the type any number of a’s followed by any number
of b’s.
Given, Grammar G3 = ({S, A, B}, {a, b}, Given, Grammar G1 = ({S, A, B}, {a, b}, {S→
{S→ AB, A→ε, B→ab}, S) AB, A→ab, B→ε}, S)
Find out the language generated by this Find out the language generated by this
grammar. grammar.
Solution Solution
S→AB→B→ab S→AB→abB→ab
So, L(G3) = {ab} So, L(G4) = {ab}
We, observe from the above that for a given language, we can write different grammars.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 4
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Example-2
a) Construct the language generated from the given grammar G1=({S}, {a, b}, {S→aSb,
S→ab}, S)
Solution
This grammar consists of two production rules P: {S→aSb, S→ab}
We can derive strings by taking the production with start symbol and using production rules.
For, S→ab ( We can get the string only ab)
But with S→aSb, the following strings can be generated.
Everytime we use the production S→ab, we will get the strings like ab, aabb, aaabbb,
aaaabbbb,….
So, L(G1) = {ab, aabb, aaabbb, aaaabbbb,….} = Same number of a’s and b’s, where everytime
a’s followed by b’s with atleast one ab = {anbn| n≥1}
b) Construct the language generated from the given grammar G2=({S}, {a, b}, {S→aSb,
S→ε}, S)
Solution
This grammar consists of two production rules P: {S→aSb, S→ε}
We can derive strings by taking the production with start symbol and using production rules.
With S→ε (only ε string is generated)
With S→aSb, the following strings can be generated.
Everytime we use the production S→ε, we will get the strings like ab, aabb, aaabbb,
aaaabbbb,….
So, L(G1) = {ε, ab, aabb, aaabbb, aaaabbbb,….} = Same number of a’s and b’s, where
everytime a’s followed by b’s with atleast one ab = {anbn| n≥0}
c) Construct the language generated from the given grammar G3=({S, A}, {a, b},
{S→aAb|ε, A→aAb|ε}, S)
Solution
This grammar consists of four production rules P: {S→aAb|ε, A→aAb|ε}
G3 will also generate the same language as G2. So both are equivalent.
d) Construct the language generated from the given grammar G4=({S, A, B}, {a, b}, {S→A,
A→aB|ε}, B→Ab, S)
Solution
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 5
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
G4 will also generate the same language as G2 & G3. So G2, G3 & G4 are equivalent.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 6
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
2 Regular Grammar
2.1 Introduction to regular grammar
Grammars are often an alternative way of specifying languages.
The grammar that generates the regular languages is called regular grammar.
A regular grammar is one that is either right-linear or left-linear grammar.
Right-Linear Grammar (RLG)
A grammar G =(V, T, P, S) is said to be right-linear if all productions are of the form
A → xB,
A → x,
where A, B ∈ V, and x ∈ T*.
In other words, the left-hand side of production rule in right linear grammar consists of only one
symbol from set of variables, and right hand side contains either strings of terminals or only one
variable present at rightmost position.
Left-Linear Grammar (LLG)
A grammar G =(V, T, P, S) is said to be left-linear if all productions are of the form
A → Bx,
A → x,
where A, B ∈ V, and x ∈ T*.
In other words, the left-hand side of production rule in left linear grammar consists of only one
symbol from set of variables, and right hand side contains either strings of terminals or only one
variable present at leftmost position.
A linear grammar is a grammar in which at most one variable can occur on the right side of
any production, without restriction on the position of this variable. Clearly, a regular grammar is
always linear, but not all linear grammars are regular
Examples
Sl. Examples Type of Regular Grammar or Not
No. Grammar
1 Grammar with production rules Right-Linear Regular Grammr
A ⇢ bB
B ⇢ aB/bB/∈
2 Grammar with production rules Left-Linear Regular Grammr
A ⇢ Bb
B ⇢ ∈/Ba/Bb
3 The grammar G =({S, A, B}, {a, Linear Grammar Not a Regular Grammar
b}, P, S) with productions S→A Because each production
A→ aB|λ, B→Ab, does not follow either left-
linear style or right-linear
style. Productions are
mixed style.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 7
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Examples
Ex-1: consider a Finite automaton (FA) as given below.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 8
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Solution:
Step-1: Initially, assign state levels of the FA as grammar variables like S, A, B, C etc. Or use
state levels as grammar variables directly. Here we use original state levels as grammar
variables.
Step-2: Remove trap states and its associated transitions if any. Not applicable here as the FA
does not contain any trap state.
Step-3 Write the production as the output followed by the state on which the transition is going
for each state starting from initial state.
Sl. State RLG associated with the state
No.
1 A A → aB
2 B B → aB | bB | ε
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 9
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 10
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Solution:
Step-1: Initially, assign state levels of the FA as grammar variables like S, A, B, C etc. Or use
state levels as grammar variables directly.
Step-3: Write down RLG productions for each state deriving ( with transitions) to its neighbour
states. For every final state there is an ε-move. Write the production as the output followed
by the state on which the transition is going.
Using new state levels as S, A, B,… as Using original state levels as grammar
grammar variables variables
Sl. State RLG associated with the state Sl. State RLG associated with
No. No. the state
1 S S → aS | bA | ε 1 q0 q0 → aq0 | bA | ε
2 A A → aA | aB 2 A q1 → aq1 | aq2
3 B B → aB | bA | ε 3 B q2 → aq2 | bq1 | ε
Note: Better follow left-hand style.
Ex-5: Find the regular grammar for the regular expression: a(s|d)+f.
Solution:
The FA for the re=a(s|d)+f is as follows:
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 11
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Examples
Ex-1: consider a Finite automaton (FA) as given below.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 12
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 13
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Examples
Ex-1: For the following grammar, draw a Finite automaton (FA).
G = ({S, T}, {0, 1}, P, S)
Where,
P= {S → 0, S → 1T, T → ε, T → 0T, T → 1T}
Find out the language for the FA.
Solution:
S → 0 means 0 is accepted. So draw a state level as S with transition 0 to a new state, Let it be
Sf, So Sf is a final state.
S → 1T means draw a state level as S of not yet drawn with the transition 1 to a state leveled as
T, if not yet drawn, draw it. Now put T as ε in S → 1T, so S → 1T becomes S → 1. Now 1 is
accepted with T as the final state.
Similarly draw state transition diagram for T → 0T, T → 1T.
The final FA becomes,
Ex-3: Construct a finite automaton that accepts the language generated by the grammar
V0 →aV1, V1→ abV0|b
Solution:
The productions R -> abaU | U are not validate for regular grammar, so that we convert it into
The first production rule creates an edge labeled a between V0 and V1.
For the second rule, we need to introduce an additional vertex so that there is a path labeled ab
between V1 and V0
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 14
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
The language generated by the grammar and accepted by the automaton is the regular language
L ((aab) * ab)
Ex-4: Construct a finite automaton that accepts the language generated by the grammar
S → aA
S → bA
A→a
A→b
Solution:
Lets convert the grammar to an equivalent NFA step-by-step.
The language generated by the grammar and accepted by the automaton is the regular language
L ( (a+b)(a+b) )
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 15
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Ex-5: Construct a finite automaton that accepts the language generated by the grammar
S → aS
S → bS
S → aB
B → bC
C→b
Solution:
Lets convert the grammar to an equivalent NFA step-by-step.
The language generated by the grammar and accepted by the automaton is the regular language
L ( (a+b)*abb)
Ex-6: Construct a finite automaton that accepts the language generated by the grammar
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 16
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
S → aS
S → bB
S → cC
S→ε
B → bB
B → cC
B→ε
C → cC
C→ε
Solution:
Lets convert the grammar to an equivalent NFA step-by-step.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 17
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 18
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 19
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Examples
Ex-2: For the following grammar, draw a Finite automaton (FA).
G = ({A, B, C}, {a, b}, P, A)
Where,
P= {A -> Ba/Ab/b, B -> Ca/Bb, C -> Aa/Cb/a}
Find out the language for the FA.
Solution:
Given LLG is
G = ({A, B, C}, {a, b}, P, A)
Where,
P= {A -> Ba/Ab/b, B -> Ca/Bb, C -> Aa/Cb/a}
The reverse of G (G1=GR)
G1 = ({S, R, U},{a, b, c}, P, {S})
Where,
P= {S -> a | bU | cR, R -> abaU | U, C ->b | cS}
The FA for this G1 (it is a RLG) is as follows:
Now, take the reverse of this FA that will give the final FA for the given LLG grammar.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 20
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 21
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 22
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Example-8: Construct a CFG for a language L = { wwR | w∈ {a, b}*}. In other words find
a CFG for the language of all palindromes over {a, b}
Solution:
The string set of language L = { wwR | w∈ {a, b}*
L={}
Production rule for the Regular expression with S as start symbol is as follows:
S → aSa | bSb | ε ( This is for even palindrome)
S → aSa | bSb | a | b ( This is for odd palindrome)
Example-9: Construct a CFG for the language of all non-palindromes over {a, b}
Solution:
The string set of language for the language of all non-palindromes over {a, b}
L={}
Production rule for the Regular expression with S as start symbol is as follows:
S → aSa | bSb
This is for palindromes if S terminates with either ε, a or b, if it terminates minimum
with ab or ba then the strings generated will be non-palindromic. We can write ab as aPb
or bPa with P as ε. if we extend P to generate any number of a’s or any number of b’s
then we can write P → aP | bP | ε . If minimum strings ab or ba violates the principles of
palindromes, then ab or ba includes in between any strings also violates palindromics
principles, thus generates non-palindromes sequence.
So, the required CFG G = ({S, P, Q}, {a, b}, P, S}) for for the language of all non-
palindromes over {a, b} with the productions are as follows:
S → aSa | bSb | aPb | bPa
P → aP | bP | ε
Explanation
Strings that must be accepted Strings that must be rejected
We are testing with some of the strings, you can We are testing with some of the strings, you
try it on more strings. can try it on more strings.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 23
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Strings of length 3: {001, 011, 100, no more Strings of length 5: {10001, 11011, ….. and
strings } many more similar strings. }
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 24
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
S→0
For 10*
S → 1T
T → 0T | ε
Combining,
S → 0 | 1T
T → 0T | ε
4 0(11)* S → 0T
T → 11T | ε
6 {(0+1)(0+1)(0+1)(0+1)}* S → TTTTS
T → 0T | 1T ε
7 (00)*(11)*1 S → TU1
T → 00T | ε
T → 11T | ε
8 0(0+1)*1 S → 0T1
T → 0T |1T | ε
9 (0+1)*(00+11)(0+1)* S → XYX
X → 0X |1X | ε
Y → 00 | 11
11 a+a(aa+b)*+(aa)b S → a | aX | Y
X → aaX | bX | ε
Y → aab
Example-4: Construct the CFG for the language having any number of a's over the set {a}.
Solution:
Example-5: Construct the CFG for the language having any number of a's over the set {a}.
Solution:
https://www.geeksforgeeks.org/ugc-net-ugc-net-cs-2014-dec-ii-question-35/
https://t4tutorials.com/cfg-for-the-language-of-all-non-palindromes/
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 25
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Is this a s-grammar?
Solution:
Yes
Example-2: Consider the following grammar.
Is this a s-grammar?
Solution:
No because the pair (S, a) occurs in the two productions S → aS and S → aSS
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 26
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
A→a
B→b
C→c
Find all possible derivations for string abc.
Solution:
Consider a string w = abc. Total 6 derivations exist for string w.
Derivation-1
S → ABC
→ aBC (Using A → a)
→ aBc (Using C → c)
→ abc (Using B → b)
Derivation-2
S → ABC
→ AbC (Using B → b)
→ abC (Using A → a)
→ abc (Using C → c)
Derivation-3
S → ABC
→ AbC (Using B → b)
→ Abc (Using C → c)
→ abc (Using A → a)
Derivation-4
Example-2: Draw a derivation tree for the string "bbabb" from the CFG given by
S → bSb | a | b
Solution:
Here w=bbabb
LMD LMD Tree
Or
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 27
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Or
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 28
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Classwork: Derive the string "00101" for leftmost derivation and rightmost derivation using a
the above CFG.
LMD RMD
S S
A1B A1B
0A1B A10B
00A1B A101B
001B A101
0010B 0A101
00101B 00A101
00101 00101
Example-4: Derive the string "abb" for leftmost derivation and rightmost derivation using a
CFG given by,
S → AB | ε
A → aB
B → Sb
Solution:
Here w=abb
LMD RMD
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 29
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Classwork: Show the derivation tree for string "aabbbb" with the above grammar.
Solution
Here w=aabbbb
LMD LMD Tree
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 30
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Example-5: Construct a derivation tree for the string aaabbabbba for the CFG given by
S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
Solution:
Here w = aaabbabbba
LMD RMD
S → aB S → aB
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 31
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Classwork: Show the derivation tree for string "aabbabb" with the above grammar.
Solution
Here w=aabbabb
LMD LMD Tree
Example-6: Construct a derivation tree for the string aabaa for the CFG given by
S-> aAS|aSS
A->SbA|ba
Solution:
Here w = aabaa
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 32
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Here w = aabaa
RMD RMD Tree
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 33
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
a) Ambiguous grammar: A grammar is said to be ambiguous if there exists more than one
leftmost derivation or more than one rightmost derivation or more than one parse tree
for at least one string. For ambiguous grammar leftmost derivation and rightmost
derivation represents different parse trees.
A grammar is said to ambiguous grammar if for any string generated by it produces
more than one Parse tree Or syntax tree Or leftmost derivation Or rightmost
derivation.
b) Unambiguous grammar: If the grammar is not ambiguous, then it is called unambiguous.
A unique leftmost derivation and a unique rightmost derivation exist for all the strings. For
unambiguous grammar leftmost derivation and rightmost derivation represents the same
parse trees.
A grammar is said to unambiguous grammar if for every string generated by it
produces exactly the one Parse tree Or syntax tree Or leftmost derivation Or
rightmost derivation.
Note: If for all the strings of a grammar, leftmost derivation is exactly same as rightmost
derivation, then that grammar may be ambiguous or unambiguous.
Examples on Ambiguity in Grammar
Example-1: Consider the following grammar-
S → aS |∈
Check whether this is an ambiguous or unambiguous grammar
Solution:
This is an example of an unambiguous grammar.
Here, each string have its leftmost derivation and rightmost derivation exactly same.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 34
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Consider a string w = a.
S → aS
S → a∈
S→a
Example-3: Check whether the following grammar is ambiguous or not for string w = ab
S→A/B
A → aAb / ab
B → abB / ∈
Solution:
As original string (w =ab) can derived through two different parse trees. So, the given
grammar is ambiguous.
Now we draw more than one parse trees to get string w = ab.
Parse Tree-1 Parse Tree-2
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 35
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Example-4: Check whether the following grammar is ambiguous or not for string
w=aabbccdd
S → AB / C
A → aAb / ab
B → cBd / cd
C → aCd / aDd
D → bDc / bc
Solution:
As original string (w =aabbccdd) can derived through two different parse trees. So, the given
grammar is ambiguous.
Now we draw more than one parse trees to get string w = aabbccdd
Parse Tree-1 Parse Tree-2
Example-5: Check whether the following grammar is ambiguous or not for string w=aab.
S → AB
A → Aa | a
B→b
Solution:
Now we draw more than one parse trees to get string w = aab.
LMD Tree RMD Tree
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 36
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Because all parse trees, syntax tress, left or right derivations will be similar for above grammar
of string “aab”. So, above grammar is unambiguous.
Example-5: Check whether the following grammar is ambiguous or not for string
w=d+id*id.
E→E+T
E→T
T→T*F
T→F
F → id
Solution:
Because all parse trees, syntax tress, left or right derivations will be similar for above grammar
of string “id+id*id”. As given below
Now we draw more than one parse trees to get string w = aab.
LMD Tree RMD Tree
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 37
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Also,
Consider the string 0101 in the language of the CFG G2. Below we show 3 derivations of
the string with respect to G2.
a. Derivation 1: S =⇒ 0S1S =⇒ 01S =⇒ 010S1S =⇒ 0101S =⇒ 0101
b. Derivation 2: S =⇒ 0S1S =⇒ 0S10S1S =⇒ 0S101S =⇒ 0S101 =⇒ 0101
c. Derivation 3: S =⇒ 0S1S =⇒ 01S0S1S =⇒ 010S1S =⇒ 0101S =⇒ 0101
The variable that gets replaced at each step of the derivation is underlined.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 38
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
All the above grammars generate the same language L = { an | n>=0 }. Grammar-1 is
unambiguous. Since there exists at least one unambiguous grammar which generates language L.
Therefore, L is not an inherently ambiguous language.
Then L is generated by the combination of these two grammars with the additional production
S → S1 / S2
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 39
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
This grammar is ambiguous in nature because following two parse trees exist for string w = abc
If L is a context-free language for which there exists an unambiguous grammar, then L is said to
be unambiguous.
https://www.tutorialspoint.com/what-is-ambiguous-grammar
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 40
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
L
4 Simplification of Context-Free Grammars and Normal Forms
4.1 Simplification of CFG
All CFG are not always optimized that means the CFG may consist of some production rules
and symbols that are not needed for the derivation of strings.
Having extra symbols/useless productions, unnecessary increase the length of grammar.
Simplification of CFG means reduction of CFG by removing useless symbols & useless
productions.
Simplification essentially comprises of the following steps:
a) Removing Useless Productions
b) Removing ε-Productions
c) Removing Unit-Productions
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 41
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
b) Removing ε-Productions
Productions of type A → ε (also known as null productions) is called ε-productions.
These productions can only be eliminated from grammars that do not generate ε (an empty
string). It is possible for a grammar to contain null productions and yet not produce an empty
string.
Steps to remove ε Productions
Step 1:- First, find all nullable non-terminal variables that derive ε. A variable 'A' is nullable if ε
can be derived from 'A.' For any productions of type 'B -> A1A3A3...An', where all 'Ai's are
nullable, 'B' is also nullable.
Step 2:- Construct all productions A -> x for each production A -> a, where x is obtained from
a by eliminating one or more non-terminals from step 1.
In otherwords, For all the productions in the original grammar , we add the original production
as well as all the combinations of the production that can be formed by replacing the nullable
variables in the production by ε. If all the variables on the RHS of the production are nullable ,
then we do not add ‘A -> ε’ to the new grammar.
Step 3:- Now, join the result of step 2 with the original production and remove ε productions.
Examples
Sl. Given Grammar Observation Reduced Grammar
No.
1 S → aTb This grammar generates the ε- S → aTb | ab
T → aTb | ε free language {anbn : n ≥ 1}. The T → aTb | ab
ε-production T → ε can be
removed after adding new This new grammar
productions obtained by generates the same
substituting ε for T where it language as the original
occurs on the right. one.
2 Find a context-free We find that the nullable S → ABaC | BaC | AaC
grammar without variables are A, B, C | ABa | aC | Aa | Ba | a
ε-productions A → BC | B | C
equivalent to the B→b
grammar defined by C→D
D→d
S → ABaC
A → BC
B→b|ε
C→D|ε
D→d
3 S -> ABCd Find all of the nullable variables. S -> ABCd | ABd |
A -> BC Variables 'B' and 'C' are nullable ACd | BCd | Ad | Bd
B -> bB | ε because they have ε on the RHS |Cd | d
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 42
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
c) Removing Unit-Productions
Unit productions are those in which one non-terminal produces another non-terminal. Any
production of a context-free grammar of the form A → B, where A, B ∈ V, is called a unit-
production.
To remove unit-productions, we use the substitution rule.
To eliminate unit production, take the following steps:
Step 1: To remove X → Y, add production X → a to the grammar rule whenever Y → a occurs
in the grammar.
Step 2: Now delete X → Y from the grammar.
Step 3: Repeat step 1 and step 2 until all unit productions are removed.
But, A → B, B → A <= For this type of unit productions, To get around this, we first find, for each A,
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 43
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
B → 11 | 0S | 000
MCQ Questions
1. Consider the grammar consisting of 7 productions.
S → aA | aBB
A → aaA | ε
B → bB | bbC
C→B
After elimination of unit, useless & ε-productions, how many productions remain in the
resulting grammar?
A. 2
B. 3
C. 4
D. 5
E. NONE
Solution
4
Explanation
Removing useless symbols/productions, the grammar becomes
Given Grammar After removal of After removal of ε- After removal of unit
useless productions productions
symbols/productions
S → aA | aBB S → aA S → aA | a No unit production left
A → aaA | ε A → aaA | ε A → aaA | aa
B → bB | bbC
C→B
Productions S → aBB,
B → bB | bbC, C → B
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 44
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
are useless.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 45
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 46
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Steps for Converting CFG to CNF: Follow the below steps for converting context-free
grammar to Chomsky Normal Form.
Step 1: Removing start symbol S from RHS of rules
If S appears on the RHS of some rule, add a new start variable S0 and the rule S0 → S.
Step 2: Removing ε rule and unit productions respectively.
In the grammar, remove the null, unit and useless productions (if any) wrt the
Simplification of CFG.
Step 3: Replacing certain terminals
Eliminate terminals from the RHS of the production if they exist with other non-terminals
or terminals.
For example,
Given production Can be decomposed as
S → aA S → BaA, Ba → a
S → aAb S → BaABb, Ba → a, Bb → b
A → aba A → BaBbBa, Ba→a, Bb→b
Step 4: Shortening the RHS
Eliminate RHS with more than two non-terminals. For example, S → ASB can be
decomposed as:
Given production Can be decomposed as
S → PQR S→AD1, D1→QR
S → ABCDE S→AD1, D1→BD2, D2→CD3, D3→DE
Examples
Example-1: Convert the following CFG to CNF.
S→ABa
A→aab
B→Ac
Solution:
Step-1: Not aaplicable as start symbol S does not occur in the right side of any production.
Step-2: Not aaplicable as the given CFG does not contain any .null, unit and useless
productions.
Step-3: Eliminating terminals from the RHS of the productions, the grammar becomes
Step-4: Eliminating RHS with more than two non-terminals, the grammar becomes
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 47
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Step-2: As the CFG contains null and unit productions, so remove them first.
Remove null productions of the above grammar.
Nullable variables : B (Direct nullable)
A (indirect nullable)
Now, After removing B → ε, the production set becomes −
S0 → S
S → ASA | aB | a
A→B|S|ε
B→b
Now, After removing A → ε, the production set becomes −
S0 → S
S → ASA | aB | a | SA | AS | S
A→B|S
B→b
Remove the unit productions
After removing S → S, the production set becomes −
S0→S,
S→ ASA | aB | a | AS | SA
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 48
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
A→B|S
B→b
Step-3: Eliminating terminals from the RHS of the productions, the grammar becomes
S0 → ASA | BaB | a | AS | SA
S→ ASA | BaB | a | AS | SA
A → b | ASA | BaB | a | AS | SA
B→b
Ba → a
Step-4: Eliminating RHS with more than two non-terminals, the grammar becomes
S0 → AD1 | BaB | a | AS | SA
S→ AD1 | BaB | a | AS | SA
A → b | AD1 | BaB | a | AS | SA
B→b
Ba → a
D1 → SA
This is theCFG in CNF.
Solution:
Refer https://ict.iitk.ac.in/wp-content/uploads/CS340-Theory-of-Computation-09-
toc.pdf
Step-1: As start symbol S occurs in the right side of the above grammar rules, so add a new
production S0 → S to the given grammar. The CFG becomes
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 49
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Step-2: As the CFG contains null and unit productions, so remove them first.
Remove null productions of the above grammar.
Nullable variables : B (Direct nullable)
A (indirect nullable)
Now, After removing B → ε, the production set becomes −
Step-3: Eliminating terminals from the RHS of the productions, the grammar becomes
Step-4: Eliminating RHS with more than two non-terminals, the grammar becomes
GNF
A CFG is said to be in GNF if all its productions are of the following form,
A→ax
Where,
A∈V, x∈V* and a∈T
(This means in all rules, a nonterminal generates a single terminal only or a single terminal
followed by string of non-terminals)
Examples:
iii. Consider the following CFG G3
S → aA
A → aA | b
Is this CFG G3 is in GNF? Justify your answer.
Answer:
G3 is in GNF as all of its productions satisfy the GNF conditions.
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 50
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
G4 is not in CNF as all of its productions does not satisfy the CNF conditions. The productions
S→AB violates the conditions of GNF.
Precisely one nonterminal is generated for each rule application. This means that we can put
a bound on the number of rule applications in any successful derivation.
Steps for Converting CFG to GNF: Follow the below steps for converting Context-Free
Grammar (CFG) to Greibach Normal Form (GNF)
Step 1: Conversion of the given CFG into CNF.
If the given grammar is not in CNF, convert it into CNF.
Step 2: . Rename the variables like A1, A2, . . . An starting with S = A1.
Step 3: . Modify the rules in R' so that if Ai → Ajγ ∈ R' then i < j
Check for every production rule if RHS has first symbol as non terminal say Aj for the
production of Ai, it is mandatory that i should be less than j.
Starting with A1 and proceeding to An this is done as follows:
i. If i > j then replace the production rule of Aj at its place in Ai.
ii. If i = j, it is the left recursion. Remove left recursion.
Step-4: If any production rule is not present in GNF, convert the production rule given in
the grammar into GNF form by substitution rule.
Examples
Example-1: Convert the following grammar G into Greibach Normal Form (GNF).
S → XA|BB
B → b|SB
X→b
A→a
Solution:
Step-1: Rewrite G in Chomsky Normal Form (CNF)
It is already in CNF.
Step-3: Identify all productions which do not conform Ai → Ajγ with i < j
Production iRj Remarks
A1 → A2A3 1<2 ok
A1 → A4A 1<4 ok
A4 → A1A4 4>1 not ok. Now replace it with A1‘s production rule
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 51
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
From rule A4 → A2A3A4 , remove A2 with replacing its RHS of rule, so the new
production becomes
A4 → bA3A4 as A2 → b
So A4 becomes A4 → bA3A4 | A4A4A4 | b
Left Recursion
Here A4 → A4A4A4 in production rule of A4 is the example of left recursion.
To replace the left most recursion take a new Non terminal symbol say Z.
Now write α part or the trailing part of the left most recursive production once
followed by Z and once without Z. Here in A4A4A4, the part after the first A4 is
A4A4, therefore
Z → A4A4 | A4A4Z
Now change the above production rule by putting Z after every previous production of
that Ai, and remove the left recursive production.
Step-3: If any production rule is not present in GNF, convert the production rule given
in the grammar into GNF form.
Identify the productions that are already in GNF: Here productions for A2, A3 and A4 are in
GNF
Now, for A1 → A2A3 |A4A4
Substitute for A2 and A4 to convert it to GNF
A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4
for Z → A4A4 | A4A4Z
Substitute for A4 to convert it to GNF
Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4Z | bA4Z | bA3A4ZA4Z | bZA4Z
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 52
S ch o o l o f C o m p u ter E n g in eerin g , C S -2100 3 : A u to m ata T h eo ry 2024
K IIT U n iversity, B h u b an esw ar an d F o rm al L an g u ag e AUTUM N
SERIES
Ex-2: https://www.tutorialandexample.com/greibach-normal-form
Lecture Notes of Compiler Design by Prof. Anil Kumar Swain, CSE, KIIT University, Bhubaneswar Page 53