0% found this document useful (0 votes)
23 views53 pages

Grammar RG CFG Afl

The document provides an overview of grammars in finite automata, focusing on regular grammars and context-free grammars. It defines formal grammar, explains its components, and illustrates language generation through examples. Additionally, it discusses the equivalence of grammars and different types of grammars based on production rules.

Uploaded by

Aryan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views53 pages

Grammar RG CFG Afl

The document provides an overview of grammars in finite automata, focusing on regular grammars and context-free grammars. It defines formal grammar, explains its components, and illustrates language generation through examples. Additionally, it discusses the equivalence of grammars and different types of grammars based on production rules.

Uploaded by

Aryan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

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

= Grammars in Finite Automata Module-3


Regular Grammars & CFG
Branch/Section: CSE20, CSE35, 3RD Faculty: Prof. Anil Kumar Swain

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.

1.1 Definition of Formal Grammar (or Simply Grammar) in Automata Theory


 A formal grammar (sometimes simply called a grammar) is a set of formation rules for strings
in a formal language.
 The rules describe how to form strings from the language’s alphabet that are valid according to
the language’s syntax.
 A grammar does not describe the meaning of the strings or what can be done with them in
whatever context—only their form.
 Defn: A grammar G can be formally written as a 4-tuple G = (V, T, S, P)
Where,
V (Sometimes written as N or VN) is a finite set of variables or non-terminal symbols.
Terminal symbols are those which are the constituents of the sentence generated using a
grammar. Terminal symbols are denoted by using small case letters such as a, b, c etc.

T (Sometimes written as ∑ ) is a finite set of Terminal symbols. Non-Terminal symbols ( or


variables) are those which take part in the generation of the sentence but are not part of it.
Non-Terminal symbols are denoted by using capital letters such as A, B, C etc.

P is Production rules for Terminals and Non-terminals.

S is a special symbol called start variable or start symbol, S ∈ V.

 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 ∈).

1.2 Language of a Grammar


 The language generated by G = (V, T, S, P) is

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

The string w is generated with the symbols of T and epsilon (ε).

It means to start with starting production and after applying some production rules, the
string w is generated.

1.3 Derivations from a Grammar


 Strings of a language may be derived using the production rules in a grammar.
 Example-1
Consider the grammarG = {{S}, {a}, {S → aS, S → ε} , S}. Find out the language generated
by this grammar.
Solution
Here, V = {S}, T = {a}, P ={S → aS, S → ε}, S=S
Take the production with start symbol
S → aS
S → a (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 → 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

Take the production with start symbol


S → aS
S → a (Replacing S in right by production rule S → ε)

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

S → aba (Replacing S in right by production rule S → ε)


….
….

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.

So, the language generated by this grammar −

L(G) = {ab, a2b, ab2, a2b2, ………} = {ambn | m ≥ 1 and n ≥ 1}

1.4 Equivalence of Grammars


 One grammar has one language, but one language has several grammars.
 Example-1
Given, Grammar G1 = ({S, A, B}, {a, b}, Given, Grammar G2 = ({S}, {a, b}, {S→ ab},
{S→ AB, A→a, B→b}, S) S)
Find out the language generated by this Find out the language generated by this
grammar. grammar.
Solution Solution
S→AB→aB→ab S→ab
So, L(G1) = {ab} So, L(G2) = {ab}

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}

Observation & Conclusion


The above example-2.a and 2.b the grammars are not equivalent as both do not generate the
same language. It looks similar, but differs with the sting ε.

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.

1.5 Types of grammar


 The grammars are of the following types.

 On different basis of Production Rules-

Types Grammars Automata Languages Productions


Recursively
Type-0 Unrestricted Grammar Turning Machine Enumerable α→β
Language

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

Context Sensitive Linear-bounded Context-Sensitive


Type-1 α → β, |β|>=|α|
Grammar Machine Language
Context Free Pushdown Context Free
Type-2 A→α
Grammar Automata Language
Type-3 Regular Grammar Finite Automata Regular Language A → αB, A -> a

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

2.2 Relationship of Regular Grammar and Finite Automata


 The relationship of regular grammar and finite automata is shown below:

 Theorem-1: If G is a regular grammar then L (G) is a regular language.


 We can convert FA to Regular Grammar and vice-versa.

(All have the same power and can be converted to other)

a) Conversion of Finite Automata to Regular Right Linear Regular (RLG)


 Method
a. Repeat the process for every state
b. Begin the process from start state
c. Write the production as the output followed by the state on which the transition is going
d. And at the last add ε because that's is required to end the derivation

 Examples
Ex-1: consider a Finite automaton (FA) as given below.

Find regular grammar for the above FA.

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 | ε

Step-4: Write down step-3 productions altogether in grammar style.


The grammar for the given FA is as follows:
G=({A, B}, {a, b}, {A → aB, B → aB | bB | ε}, A}
Where, V = {A, B}, T = {a, b}, P = {A → aB, B → aB | bB | ε}, S=A

Ex-2: consider a Finite automaton (FA) as given below.

Find regular grammar for the above FA.


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 → aA | bA |aB
2 B B → aB | bB | ε

Step-4: Write down step-3 productions altogether in grammar style.


The grammar for the given FA is as follows:
G=({A, B}, {a, b}, {A → aA | bA |aB, B → aB | bB | ε }, A}
Where, V = {A, B}, T = {a, b}, P = {A → aB, B → aB | bB | ε}, S=A

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

Ex-3: Consider a Finite automaton (FA) as given below.

Find regular grammar for the above FA.


Or
Questions will be asked as find regular grammar for the following language on {a, b}
L = {w | na(w) mod 3 = 0}
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 S S → bS | bA | ε
2 A A → bA | aB
3 B B → bB | aS

Step-4: Write down step-3 productions altogether in grammar style.


The grammar for the given FA is as follows:
G=({A, B}, {a, b}, {A → aA | bA |aB, B → aB | bB | ε }, A}
Where, V = {A, B}, T = {a, b}, P = {A → aB, B → aB | bB | ε}, S=A

Ex-4: Construct regular grammar for the following DFA.

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-2: Remove trap states and its associated transitions if any.

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.

Step-4: Write down step-3 productions altogether in grammar style.


The grammar for the given FA is as follows:
G=({S, A, B}, {a, b}, {S → aS | bA | ε, A → aA | aB, B → aB | bA | ε}, S}

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

The regular grammar for the above FA is as follows:


G=({S, A, B, C, D},{a, s, d, f), P, S}) with P as follows:
P: S → aA
A → sB | dC
B → sB | dC | fD
C → sB | dC | fD
D→ε

b) Conversion of Finite Automata to Regular Left Linear Regular (LLG)


 Method
FA -> Reverse of FA -> Right Linear Grammar -> Reverse of Right Linear Grammar = Left Linear Grammar
a. Take reverse of the finite automata
b. Then write right linear grammar following the previous lesson
c. Then take reverse of the right linear grammar that is the final left linear grammar

 Examples
Ex-1: consider a Finite automaton (FA) as given below.

Find regular grammar (LLG) for the above FA.


Solution:
Step-1: Take reverse of the finite automata

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

Removing the unreachable state C, the FA becomes

Step-2: Write the right linear grammar of the FA of step-1


B -> aB/bB/aA
A -> ε

Step-3: Reverse the right linear grammar of the FA of step-2


B -> Ba/Bb/Aa
A -> ε
This is the final Regular Left Linear Grammar.

Ex-2: Consider a Finite automaton (FA) as given below.

Find regular grammar (LLG) for the above FA.


Solution:
Step-1: Take reverse of the finite automata

Step-2: The corresponding RLG for this reversed FA is


B ⇢ aB/bB/bA
A ⇢ ∈
Step-3: Reverse the right linear grammar of the FA of step-2
B ⇢ Ba/Bb/Ab
A⇢ ∈
This is the final Regular Left Linear Grammar.

2.3 Conversion of Regular Right Linear Regular (RLG) to FA


 Method
a. Start from the first production (Production with the start symbol) and do for the every
production.
b. For each production, draw state diagram for FA.
c. Start State: It will be the first production's state and Final State: Take those states which
end up with input alphabets. eg. State A and C are below CFG

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-2: For the following grammar, draw a Finite automaton (FA).


G = ({A, B, C}, {a, b}, P, A)
Where,
P= {A -> aB/bA/b, B -> aC/bB, C -> aA/bC/a}
Find out the language for the FA.
Solution:

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

Finally, we need to add an edge labeled b between V1 and Vf.

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

Ex-7: For the following grammar, draw a Finite automaton (FA).


G = ({S, R, U},{a, b, c}, P, {S})
Where,
P= {S -> a | bU | cR, R -> abaU | U, U ->b | cS}
Find out the language for the FA.
Solution:
The productions R -> abaU | U are not validate for regular grammar, so that we convert it into
the following productions.
R -> aX | b | cS
X -> bY
Y -> aU
So that grammar has the following productions:
S -> a | bU | cR
R -> aX | b | cS
X -> bY
Y -> aU
U -> b | cS
Therefore the transitions diagram for the grammar is shown below.

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

2.4 Conversion of Left Linear Grammar (LLG) to Finite Automata


 Method
a. Take reverse of given LLG that gives the RLG
b. Create Finite automata (FA) for the RLG
c. Then again take reverse of the FA and that will be our final output for the given LLG.
d. Start State: It will be the first production's state and Final State: Take those states which
end up with input alphabets. eg. State A and C are below CFG

 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

3 Context Free Grammar (CFG)


 The productions in a regular grammar are restricted in two ways: The left side must be a single
variable, while the right side has a special form.
 To create grammars that are more powerful, we must relax some of these restrictions. By
retaining the restriction on the left side, but permitting anything on the right, we get context-free
grammars.

3.1 Definition of CFG


 A grammar G = (V, T, P, S) is said to be context-free if all productions in P have the form
α→β,
Where,
α∈ V and β ∈ (V ∪ T)*
In other words, in each production, in the lefthand side there will be one on-terminals
only, in the right hand side it may contains any combinations of symbols from set V
and T including ε.

All other notations V, T, S have their as usual meaning.

 Context Free language: A language L is said to be context-free if and only if there is a


context-free grammar G such that L = L (G).
 Examples
Example-1: Construct the CFG for the language having any number of a's over the set {a}.
Solution:
The language generated having any number of a's over the set {a}
L = { ε, a, aa, aaa, aaaa, aaaa, ….}
Production rule for the Regular expression is as follows:
S → aS
S→ ε

Example-2: Construct a CFG for the regular expression (a+b)*


Solution:
The string set of language for the regular expression (a+b)*
L = { ε, a, b, ab, ba, aaa, bbb,aab ….}
Production rule for the Regular expression is as follows:
S → aS | bS
S→ ε

Example-3: Construct a CFG for a language L = {anbn | n ≥ 0}.


Solution:
The string set of language L = {anbn | n ≥ 0 }
L = { ε, ab, aabb, aaabbb, aaaabbbb, ….}
Production rule for the Regular expression is as follows:
S → aSb
S→ ε
Task:
Construct a CFG for a language L = {anbn | n ≥ 0 }.
Ans:
S → aSb
S → ab

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

Example-4: Construct a CFG for a language L = {anbn+2 | n ≥ 0}.


Solution:
The string set of language L = {anbn+2 | n ≥ 0}
L = { bb, abbb, aabbbb, aaabbbbb, ….}
Production rule for the Regular expression is as follows:
S → aSb
S → bb
Task:
Construct a CFG for a language L = {anbn | n ≥ 1 }.
Ans:

Example-5: Construct a CFG for a language L = {a2nbn | n ≥ 0 }.


Solution:
The string set of language L = {a2nbn | n ≥ 0 }
L = { ε, aab, aaaabb, ….}
Production rule for the Regular expression is as follows:
S → aaSb
S→ ε
Task:
Construct a CFG for a language L = {a2nbn | n ≥ 1}
Ans:

Example-6: Construct a CFG for a language L = {a2n+3bn | n ≥ 0 }.


Solution:
The string set of language L = {a2n+3bn | n ≥ 0 }
L = {aaa , aaaaab, aaaaaaabb, ….}
Production rule for the Regular expression is as follows:
S → aaSb
S → aaa
Task:
Construct a CFG for a language L = {a2n+3bn | n ≥ 1 }
Ans:

Example-6: Construct a CFG for a language L = {ambn | m > n & m, n ≥ 0 }.


Solution:
The string set of language L = {ambn | m > n & m, n ≥ 0 }.
L = {a,aa, aaa, ……, aab, aaab, aaaab,…., aaabb ….}
Production rule for the Regular expression is as follows:
S1 → aS1b | ε (equal number of a’s followed by equal number of b’s)
A → aA | a (Any number of a’s)
S → AS1
Task:
Construct a CFG for a language L = {ambn | m > n & m, n ≥ 1}.
Ans:

Example-7: Construct a CFG for a language L = { w | na(w) = nb(w) }.


Solution:

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

The string set of language L = { w | na(w) = nb(w) }


L = { ε, ab, ba, aabb, bbaa, abab, abba, baab ….}
Production rule for the Regular expression with S as start symbol is as follows:
S → aSb ( This is for equal numbber of a’s followed by equal number of b’s)
S → bSa ( This is for equal numbber of b’s followed by equal number of a’s)
S → SS ( This for the mix of the above two)
S→ε (This is for terminating point of minimum string acceptance)
So, the required CFG G = ({S}, {a, b}, P, S}) for L = { w | na(w) = nb(w) } with the
productions are as follows:
S → aSb | bSa | SS | ε

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)

Combing the above two


The required CFG G = ({S}, {a, b}, P, S}) for L = { wwR | w∈ {a, b}*} with the
productions are as follows:
S → aSa | bSb | ε | a | b

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.

Strings of length 1: {no string} Strings of length 1: {0, 1}

Strings of length 2: {01,10, no morestring} Strings of length 2: {00,11, no morestring}

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. }

Strings of length 5: {00001, 00011, 00111,


01111, 10000, 11000, 11100, 11110. no more
strings }

Strings of length 8: {01001011, ….. and many


more similar strings}

Example-10: Construct a CFG for a language L = { ambmcn | m, n ≥ 0 )}.


Solution:
The string set of language L = { ambmcn | m, n ≥ 0 )}.
L = {, ….}
Production rule for the Regular expression with S as start symbol is as follows:
P → aPb | ε ( This is for ambm ,
equal numbber of a’s followed by equal number of b’s)
S → PC ( ambm generated by production P concatenates with any number of c’s)
C → cC | ε ( generates any number of c’s)

Example-10: Construct a CFG for a language L = { ambmcn | m, n ≥ 0 )}.


Solution:
The string set of language L = { ambmcn | m, n ≥ 0 )}.
L = {, ….}
Production rule for the Regular expression with S as start symbol is as follows:
P → aPb | ε ( This is for ambm ,
equal numbber of a’s followed by equal number of b’s)
S → PC ( ambm generated by production P concatenates with any number of c’s)
C → cC | ε ( generates any number of c’s)

Example-11: Construct a CFG for the following regular expressions.


Ref. https://t4tutorials.com/context-free-grammer-cfg-in-theory-of-automata/

Sl. Regular Expression Meaning CFG productions


No.
1 0*10* Accepts 1 or any S → 1 (Accepts 1)
number of 0’s S → T1T
followed by single 1 T → 0T |ε
again followed by No need to include S → 1 (Accepts
any number of 0’s. 1)
As this can be generated with the
production S → T1T with T → ε.
So, the final productions are,
S → T1T
T → 0T | ε

2 0 + 10* Union of 0 and 10* For 0

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 | ε

3 (0+1)*111(0+1)* Contiains a substring S → T111T


111 T → 0T | 1T | ε

4 0(11)* S → 0T
T → 11T | ε

5 0(11)*0 + 1(00)*1 S → 0T0 |1U1


T → 11T | ε
U → 00U | ε

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

10 (aa+ab+ba+bb)* S → aaS | abS | baS | bbS | ε

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

3.2 Simple grammar or s-grammar


 A context-free grammar G = (V, T, P, S ) is said to be a simple grammar or s-grammar if all its
productions are of the form
A → ax,
where A ∈ V, a ∈ T, x ∈ V*, and any pair (A, a) occurs at most once in P
 Examples
Example-1: Consider the following grammar.

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

3.3 Find Leftmost Derivation(LMD), Rightmost Derivation(RMD), Derivation Tree of String


from a given Grammar
 Derivation: The process of deriving a string by using production rules is called as derivation. I
other words, it is basically the sequence of steps followed to generate a word (string) fro a given
grammar.
 Parse Tree ( Or Derivation tree) : The geometrical representation of a derivation is called as a
parse tree or derivation tree.
 Types of Parse Tree

a) Leftmost Derivation (LMD)


 The process of deriving a string by expanding the leftmost non-terminal at each step is called as
leftmost derivation.
 The geometrical representation of leftmost derivation is called as a leftmost derivation tree.

b) Rightmost Derivation (RMD)


 The process of deriving a string by expanding the rightmost non-terminal at each step is called
as rightmost derivation.
 The geometrical representation of rightmost derivation is called as a rightmost derivation tree.

 Examples of Parse Tree


Example-1: Consider the following grammar-
S → ABC

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

RMD RMD Tree

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

Example-3: Consider the following grammar-


S->A1B Rule-1
A->0A| ε Rule-2
B->0B| 1B| ε Rule-3
Find LMD & RMD for string w=00101.
Solution:
Here w=00101
LMD RMD
S->A1B S->A1B
->0A1B rule2 ->A10B rule3
->00A1B rule2 ->A101B rule3
->001B rule2 ->A101 rule3
->0010B rule3 ->0A101 rule2
->00101B rule3 ->00A101 rule2
->00101 rule3 ->00101 rule2
Derived the string 00101 using LMD Derived the string 00101 using RMD

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

→ aaBB (Using B → aBB) → aaBB (Using B → aBB)

→ aaaBBB (Using B → aBB) → aaBaBB (Using B → aBB)

→ aaabBB (Using B → b) → aaBaBbS (Using B → bS)


→ aaabbB (Using B → b) → aaBaBbbA (Using S → bA)
→ aaabbaBB (Using B → aBB) → aaBaBbba (Using A → a)
→ aaabbabB (Using B → b) → aaBabbba (Using B → b)
→ aaabbabbS (Using B → bS)
→ aaaBBabbba (Using B → aBB)
→ aaabbabbbA (Using S → bA)
→ aaaBbabbba (Using B → b)
→ aaabbabbba (Using A → a)
→ aaabbabbba (Using B → b)

LMD Tree for w = aaabbabbba

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

RMD Tree for w = aaabbabbba

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

LMD LMD Tree

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

3.4 Ambiguity in Grammar


(Ambiguous Grammar and Check whether the grammar is ambiguous or not. Inherently
Ambiguous Grammar and Example)
 On the basis of number of derivation trees, CFG is further classified into ambiguous grammar
and unambiguous grammar.

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.

Theorem : Let G be a grammar and w ∈ L(G). w is unambiguous with respect to G if


and only if w has a unique parse tree with respect to G.

 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.

Example-2: Consider the following grammar-


S → aS | a | ∈
Check whether this is an ambiguous or unambiguous grammar
Solution:
This is an example of ambiguous grammar.
Here also, 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.

Since two different parse trees exist, so grammar is ambiguous.


Leftmost derivation and rightmost derivation for parse tree-01 are-
S→a

Leftmost derivation and rightmost derivation for parse tree-02 are-

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

Example-6: Consider the CFG, say G1,


S → 0S1 | ∈
Is this grammar G1 is ambiguous for 000111?
Solution:
No.
Derivation of 000111 with respect to G1.
S =⇒ 0S1 =⇒ 00S11 =⇒ 000S111 =⇒ 000111
Observe that the string 000111 has only one derivation with respect to G1, because the string
generated in each intermediate step has exactly one variable. In fact every string in L(G1) has
exactly one derivation. However this need not be the case with every grammar as we shall see.
The parse tree of the string 000111 with respect to the above grammar is

Example-7: Consider the CFG, say G2,


S → 1S0S | 0S1S | ∈
Is this grammar G2 is ambiguous for 011010?
Solution:
Yes as same string there exists more than two parse trees.
The parse tree of the string 011010 with respect to the above grammar is
Now we draw more than one parse trees to get string w = 011010
Derivation Tree-1 Derivation Tree-2

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

3.5 Inherently Ambiguous Grammar in CFG


 If L is a context-free language for which there exists an unambiguous grammar, then L is said to
be unambiguous.
 If every grammar that generates L is ambiguous, then the language L is called inherently
ambiguous language.
 Ambiguity is a property of grammar not languages.
 An inherently ambiguous language would be absolutely unsuitable as a programming language,
because we would not have any way of fixing a unique structure for all its programs.
 Important Points-
 If a grammar is ambiguous, it does not imply that its language will be ambiguous too.
 If a grammar is ambiguous, its language may be unambiguous.
 If a grammar is ambiguous, its language will be unambiguous when there exists at least one
unambiguous grammar which generates that language

 Examples on Inherently Ambiguous Grammar


Example-1: Consider the following language-
L = { an | n>=0 }
Is this language L inherently ambiguous language?
Solution:
Given Language, L = { an | n>=0 }
This language consists of strings having any number of a’s.
Possible grammars for this language are as follows
a) Grammar-1: S → aS / ∈ (Unambiguous Grammar)
b) Grammar-2: S → aS / a / ∈ (Ambiguous Grammar)
c) Grammar-3: S → aS / Sa / ∈ (Ambiguous Grammar)
d) Grammar-4: S → aS / a / Sa / ∈ (Ambiguous Grammar)

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.

Example-2: Consider the following language L with n and m nonnegative, .


L = { anbncm } ∪ { anbmcm }
Is this language L inherently ambiguous contest free language?
Solution:
Given Language, L = { anbncm } ∪ { anbmcm }
That L is context-free is easy to show. Notice that
L = L1 U L2
Where,
L1 = { anbncm }is generated by the following gramamar with satrt symbol as S1 and
productions are
S1 → S1c / A
A → aAb / ∈
And
L2 = { anbmcm } is given by an analogous grammar with start symbol S2 and productions are
S2 → aS2 / B
B → bBc / ∈

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

a) Removing Useless Symbols/Productions


 If a symbol does not exist on the right-hand side of the production rule and does not participate
in the derivation of any string, that symbol is considered a useless symbol. Similarly, if a
production rule does not participate in the derivation of any string, it is useless production.
 Rule:
 Examples
Sl. Given Grammar Observation Reduced Grammar
No.
1  Symbol C never occurs in the S → aaB | aaS
S → aaB | abA | aaS right side of any productions. This B → ab | b
A → aA symbol C can never be reached
B → ab | b from the initial variable S. This is
C → ae useless symbol. So remove all of
the productions in which variable
'C' appears. In this case we found
C→ae production only. Remove
this.
 Production A → aA is also useless
because there is no way to end it.
If it never ends, it can never
produce a string. As a result, this
production can never participate in
any derivation. As A here is the
useless symbol, so remove all of
the productions in which variable

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

'A' appears. In this case remove


productions S → abA and A →
aA
2 S→A Here variable B is useless and so is the S → A
A → aA | ε production B → bA. Although B can A → aA | ε
B → bA derive a terminal string, there is no
way we can achieve a string from start
symbol S.

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 -> cC | ε of their production. Variable 'A' A -> BC | B | C


is also nullable because both B -> bB | b
variables on the RHS are nullable C -> cC | c
in A -> BC. Variable 'S' is
similarly nullable. As a result,
variables 'S,' 'A,' 'B,' and 'C' are
all nullable.

Let's make a new grammar.

We begin with the initial


production. Include the initial
production as it is. Then we
generate all available
combinations by replacing the
nullable variables with ε.
As a result, line (1) is now:
'S -> ABCd | ABd | ACd | BCd |
Ad | Bd |Cd | d.'
We apply the same logic to the
following lines, but we do not
include 'A -> ε.' We eliminate all
productions of the type 'V -> ε.'

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,

all variables B such that


 Examples
Sl. Given Grammar Observation Reduced Grammar
No.
1 S → 0A | 1B | C S -> C denotes a unit production. As a result, we can
A → 0S | 000 However, before we remove S -> finally write CFG
B → 11 | A C, we must consider what C without unit production
C → 01 provides. As a result, we can add as
a rule to S. S → 0A | 1B | 01
Now S becomes A → 0S | 000
S → 0A | 1B | 01 B → 11 | 0S | 000
C → 01
Similarly, B -> A is a unit
production; we can change it as

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

2 Remove all unit- The dependency graph for the


productions from unit-productions

Note that the removal of


the unit-productions has
made B and the
associated productions
Now, we add to the original non- useless.
unit productions
3 S → aAb Removing C → D, it becomes So final reduced
A→B|a C → d, combing this with given grammar is
B→C|b C → d, now C production S → aAb
C→D|c becomes C → c | d. Now A→a|b|c|d
D→d removing B → C, replacing C
with its terminals, now B-
production becomes B → b | c | d.
Simlarly remoing unit production
A → B, A production becomes A
→ a | b | c | d.

 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.

2. Consider the grammar consisting of 6 productions.


S → Aa | B
A → a | bC
C→a|ε
After elimination of unit, useless & ε-productions, how many productions remain in the
resulting grammar?
A. 2
B. 3
C. 4
D. 5
E. NONE

3. 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

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

4.2 Normal Forms for CFG


 There are many kinds of normal forms, established for context-free grammars. Some of these,
because of their wide usefulness, have been studied extensively. We consider two of them
briefly.
a) Chomsky's Normal Form (CNF)
b) Greibach Normal Form (GNF)
 Definitions of CNF & GNF
Chomsky's Normal Form (CNF) Greibach Normal Form (GNF)
 A CFG is said to be in CNF if if the  A CFG is said to be in GNF if all its
following are true productions are of the following form,
a. Every rule is of the following forms. A→ax
A→BC Where,
A→a A∈V, x∈V* and a∈T
Where, (This means in all rules, a nonterminal
A, B, C ∈ V and a∈T generates a single terminal only or a single
(This means in all rules, either a terminal followed by string of non-terminals)
nonterminal generates two  Examples:
nonterminals or a single terminal) i. Consider the following CFG G3
b. The start variable is not present in the S → aA
right hand side of any rule. A → aA | b
c. The rule S→ε may be present Is this CFG G3 is in GNF? Justify your
(depending on whether the language has answer.
ε or not). Answer:
 Examples: G3 is in GNF as all of its productions satisfy
i. Consider the following CFG G1. the GNF conditions.
S → AB | a
A → BA | b ii. Consider the following GFG G4.
B→c S → AB | AAS
Is this CFG G1 is in CNF? Justify your A → aA | bB | a
answer. B→b
Answer: Is this CFG G4 is in GNF? Justify your
G1 is in CNF as all of its productions satisfy answer.
the CNF conditions. Answer:
G4 is not in CNF as all of its productions does
ii. Consider the following CFG G2. not satisfy the CNF conditions. The
S → AB | AAB productions S→AB violates the conditions of
A → BA | aa
GNF.
B→c
Is this CFG G2 is in CNF? Justify your  Precisely one nonterminal is generated
answer. for each rule application. This means that
Answer: we can put a bound on the number of rule
G2 is not in CNF as all of its productions does applications in any successful derivation.
not satisfy the CNF conditions. The
productions S→AAB and A→aa violate the
conditions of CNF.

 The branching factor is precisely 2. Tree


building algorithms can take advantage of
that.

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

Example-2: Convert the following CFG to CNF.


S → a | aA | B
A → aBB | ε
B → Aa | b
Solution:
Step-1: Not aaplicable as start symbol S does not occur in the right side of any production.
Step-2: As the CFG contains null and unit productions, so remove them first.
Remove null productions of the above grammar.
Nullable variables : A (Direct nullable)
As the grammar contains A → ε null production, its removal from the grammar yields:
S → a | aA | B
A → aBB
B → Aa | b | a
Remove the unit productions
As the above grammar contains Unit production S → B, its removal yield:
S → a | aA | Aa | b
A → aBB
B → Aa | b | a
This is the CFG in CNF.

Example-3: Convert the following CFG to CNF.


S → ASA | aB
A→B|S
B→b|ε
Solution:
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
S0 → S
S → ASA | aB
A→B|S
B→b|ε

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

After removing S0→ S, the production set becomes −


S0→ ASA | aB | a | AS | SA
S→ ASA | aB | a | AS | SA
A→B|S
B→b

After removing A→ B, the production set becomes −


S0 → ASA | aB | a | AS | SA
S→ ASA | aB | a | AS | SA
A→S|b
B→b

After removing A→ S, the production set becomes −


S0 → ASA | aB | a | AS | SA
S→ ASA | aB | a | AS | SA
A → b |ASA | aB | a | AS | SA
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.

Example-4: Convert the following CFG to CNF.


S → ASB
A → aASA | a | ε
B → SbS | A | bb

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 −

Now, After removing A → ε, the production set becomes −

Remove the unit productions


After removing S → S, the production set becomes −

After removing S0→ S, the production set becomes −

After removing A→ B, the production set becomes −

After removing A→ S, 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

This is theCFG in CNF.

 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.

iv. Consider the following GFG G4.


S → AB | AAS
A → aA | bB | a
B→b
Is this CFG G4 is in GNF? Justify your answer.
Answer:

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-2: Re-label the variables


S with A1
X with A2
A with A3
B with A4

After re-labeling the grammar looks like:


A1 → A2A3|A4A4
A4 → b|A1A4
A2 → b
A3 → a

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

For A4 → A1A4 | b production


To eliminate A1 we will use the substitution rule A1 → A2A3 | A4A4.

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

Therefore, we have A4 → A2A3A4 | A4A4A4 | b


Now,
Production iRj Remarks
A4 → A2A3A4 4>2 not ok. Eliminate A2 with its rule
A4 → A4A4A4 4=4 not ok. Remove left recursion.

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.

Removing left recusrion from A4, now


A4 → bA3A4 | b | bA3A4Z | bZ
Z → A4A4 | A4A4Z

At this stage our grammar now looks like


A1 → A2A3 | A4A4
A4 → bA3A4| b | bA3A4Z | bZ
Z → A4A4|A4A4Z
A2 → b
A3 → a
But the grammar is still not in Greibach Normal Form!

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

Finally the grammar in GNF is


A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4
A4 → bA3A4 | b | bA3A4Z | bZ
Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4Z | bA4Z | bA3A4ZA4Z | bZA4Z
A2 → b
A3 → a

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

4.3 Left Recursion


 A production of grammar is said to have left recursion if the leftmost variable of its RHS is
same as variable of its LHS.
 A grammar containing a production having left recursion is called as Left Recursive Grammar.
 Left recursion is considered to be a problematic situation for Top down parsers.
 Therefore, left recursion has to be eliminated from the grammar.
 Left recursion is eliminated by converting the grammar into a right recursive grammar.
 If we have the left-recursive pair of productions-
A → Aα / β
(Left Recursive Grammar)
where β does not begin with an A.
Then, we can eliminate left recursion by replacing the pair of productions with-
A → βA'
A' → αA' / ∈
(Right Recursive Grammar)
 This right recursive grammar functions same as left recursive grammar.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy