Formal Languages And Automata Theory 1
UNIT-IV
Objective:
To understand regular grammars and context free grammars.
Syllabus:
Chomsky hierarchy of languages, Regular grammars- right linear and left
linear grammars, Equivalence between regular linear grammar and FA
and its inter conversion, Context free grammar, derivation trees,
Sentential forms, right most and left most derivation of strings
Learning Outcomes:
Students will be able to:
understand Chomsky hierarchy of languages.
understand and construct the regular grammar for the given
regular language or regular expression.
convert Regular Grammar into equivalent DFA and viceversa.
construct Context free grammar for the given language.
construct right most, left most derivation and derivation trees for
the given string and grammar.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 2
Learning Material
4.1 Chomsky hierarchy of languages:
The four classes of languages are often called the Chomsky hierarchy,
after Noam Chomsky, who defined these classes as potential models of
natural languages.
Fig:4.1 Chomsky hierarchy
Chomsky classifies the grammar into four types:
Table:4.1Types of Grammar
Production
Grammar Languages Automaton
rules
Recursively
enumerable/ Turing
Type 0 αβ
Phrase machines
Structured
Context- Linear-bound αβ
Type 1
sensitive automata |α|<=|β|
Push-down
Type 2 Context-free Aα
automata
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 3
A w
Finite-state
Type 3 Regular automata A wB
A Bw
4.1.1Regular Grammar:
A right- or left-linear grammar is called a regular grammar.
Right-Linear Grammar:
If all productions of a grammar are of the form A wB or A w, where A
and B are variables and w is a (possibly empty) string of terminals, then
we say the grammar is right-linear.
Example:
Represent the language 0(10)* by the right-linear grammar.
The language generated by the given Regular Expression is
L = {0, 010, 01010, 0101010, .......}
Right-Linear Grammar:
S0A
A10A | ε
Left-Linear Grammar:
If all productions are of the form A Bw or A w, we call it left-linear.
Example:
Represent the language 0(10)* by the left-linear grammar.
The language generated by the given Regular Expression is
L = {0, 010, 01010, 0101010, .......}
Left-Linear Grammar:
SS10 | 0
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 4
Equivalence of regular grammars and finite automata:
A language is regular if and only if it has a left-linear grammar and if and
only if it has a right-linear grammar.
Construction of a Regular Grammar for a given DFA:
Let M = ({q0, q1... qn} , ∑, δ, qo, F). We construct G as G = ({A0, A1, ....,
An},∑, P, A0)
where P is defined by the following rules:
(i) Ai aAj is included in P if δ(qi, a) = qj ∉ F.
(ii) Ai aAj and Ai a are included in P if δ(qi, a) = qj ∈ F.
Note: We can construct only right linear grammar for the given DFA.
If we want to construct left linear grammar for the given DFA, reverse the
edges of the given DFA and interchange initial and final states.
Example:
1. Construct regular grammar (right linear grammar) for the
given DFA.
Given M= ({q0,q1}, {a,b}, δ, qo, {q1})
Construct G= ({A0,A1}, {a,b} ,P, A0) where P is given by
(i) Ai aAj is included in P if δ(qi, a) = qj ∉ F.
δ(q0, a) = q0 ∉ F ⇒ A0aA0
(ii) Ai aAj and Ai a are included in P if δ(qi, a) = qj ∈ F.
δ(q0, b) = q1∈ F ⇒ A0bA1 and A0b
δ(q1, a) = q1∈ F ⇒ A1aA1 and A1a
δ(q1, b) = q1∈ F ⇒ A1bA1 and A1b
∴ P is given by
A0aA0, A0bA1, A0b
A1aA1, A1a, A1bA1, A1b
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 5
Steps to convert Finite Automata to Left Linear Grammar:
Step 1: Reverse all the edges of the given automata and interchange
initial state and final states.
Step 2: Represent the productions using Left Linear Grammar.
Example:
2. Construct left linear grammar for the given DFA.
Step 1: Reverse all the edges of the given automata and interchange
initial state and final states.
Step 2: Represent the productions using Left Linear Grammar.
BBa BAa
BBb Ba
4.1.2 Construction of a DFA for a given Regular Grammar:
Let G = ({A0, A1, ...., An},∑, P, A0). We construct a DFA M whose
(i) states correspond to variables.
(ii) initial state corresponds to A0.
(iii) transitions in M correspond to productions in P. As the last
production applied in any derivation is of the form Ai a, the
corresponding transition terminates at a new state, and this is the
unique final state.
We define M as ({q0, q1... qn, qf} , ∑, δ, qo, {qf}) where δ is defined as
follows:
(i) Each production AiaAj induces a transition from qi to qj with
label a,
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 6
(ii) Each production Ak a induces a transition from qk to qf with
label a.
Example:
1. G= ({A0, A1}, {a,b} ,P, A0) where P consists of A0aA1, A1bA1,
A1a, A1bA0. Construct a DFA M accepting L(G).
A0aA1 induces a transition from q0 to q1 with label a.
A1 bA1 induces a transition from q1 to q1 with label b.
A1bA0 induces a transition from q1 to q0 with label b.
A1a induces a transition from q1 to qf with label a.
M = ({q0, q1, qf} , ∑, δ, q o, {qf}), where q0 and qf correspond to A0 and A1
respectively and qf is the new final state introduced.
2. Construct Finite Automata for the grammar which consists of the
productions
A aB | bA | b
B aC | bB
C aA | bC | a
4.2 Context-Free Grammar:
A context-free grammar (CFG or just grammar) is denoted G = (V, T, P, S),
where
V and T are finite sets of variables and terminals, respectively.
P is a finite set of productions; each production is of the form A α,
where A is a variable and α is a string of symbols from (V ∪ T)*.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 7
S is a special variable called the start symbol.
The language generated by G [denoted L(G)] is {w | w is in T* and }.
That is, a string is in L(G) if:
1) The string consists solely of terminals.
2) The string can be derived from S.
We call L a context-free language (CFL) if it is L(G) for some CFG G.
Note: C language is an example for Context Free Language.
Examples:
1. Write CFG for the language L= {an bn | n>=1}.
L= {ab, aabb, aaabbb, aaaabbbb, aaaaabbbbb, ………….}
G = ({S}, {a, b}, P, S)
P: S -> aSb | ab
(Or)
S -> aSB
S -> aB
B -> b
2. Write CFG for the language L= {an bm | n , m >=1}.
L= {a, b, ab, aab, abb,aabb, aaabbb, aaaabbbb,
aaaaabbbbb,………………………}
G = ({S, A, B}, {a, b}, P, S)
P: S -> AB
A -> aA | a
B -> bB | b
3. Write CFG for the language L={aa,ab,ba,bb}
G = ({S, A}, {a, b}, P, S)
P: S -> AA
A -> a | b
4. Write CFG for the language L= { an | n>=0}.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 8
L= { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,………………………}
G = ({A}, {a}, P, A)
P: A -> aA | ε
5. Write CFG for the regular expression (a+b)*.
L= { ε, a, b, aa, ab , ba, bb, aaa,abb,aba,………………………}
G = ({S}, {a, b}, P, S)
P: S -> aS | bS | ε
6. Write CFG to generate all strings of {a, b} whose length is atleast 2.
L= { aa, ab , ba, bb, aaa,abb,aba,………………………}
G = ({S, A, B}, {a, b}, P, S)
P: S -> AAB
A -> a | b
B -> aB | bB | ε
7. Write CFG to generate all strings of {a, b} whose length is atmost 2.
L= { ε , a,b, aa, ab , ba, bb}
G = ({S, A}, {a, b}, P, S)
P: S -> AA
A -> a | b | ε
8. Write CFG to generate palindromes over {a, b}.
L= { ε , a,b, aa,bb,aba,bab,aaaa,abba,………………}
G = ({S}, {a, b}, P, S)
P: S -> aSa | bSb
S -> a | b | ε
9. Write CFG to generate equal number of a’s and b’s.
L= { ab, ba,aabb, abab, bbaa,baba,………………………}
G = (V, T, P, S), where V = {S, A, B}, T = {a, b},S and P .
P: S -> aB A -> bAA
S -> bA B ->b
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 9
A ->a B ->bS
A ->aS B -> aBB
Sentential Form:
A string of terminals and variables α is called a sentential form if
Derivation:
Derivation is the process of applying productions repeatedly to expand
non-terminals in terms of terminals or non-terminals, until there are no
more non-terminals.
A derivation can be either Leftmost derivation or Right most
derivation.
Leftmost derivation:
If at each step in a derivation a production is applied to the leftmost
variable, then the derivation is said to be leftmost.
Example:
Consider the grammar G = ({S, A}, {a, b}, P, S), where P consists of
S aAS | a
A SbA|SS|ba
The corresponding leftmost derivation is
S => aAS => aSbAS => aabAS => aabbaS => aabbaa.
Rightmost derivation:
A derivation in which the rightmost variable is replaced at each step is
said to be rightmost.
Example:
Consider the grammar G = ({S, A}, {a, b}, P, S), where P consists of
S aAS | a
A SbA|SS|ba
The corresponding rightmost derivation is
S => aAS => aAa => aSbAa => aSbbaa => aabbaa.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 10
Note:“If w is in L(G) for CFG G, then w has at least one parse tree, and
corresponding to a particular parse tree, w has a unique leftmost and a
unique rightmost derivation.”
4.3 Derivation Trees (or) Parse tree:
The derivations in a CFG can be represented using trees. Such trees
representing derivations are called derivation trees.
Let G = (V, T, P, S) be a CFG. A tree is a derivation (or parse) tree for G if:
1) Every vertex has a label, which is a symbol of V ∪ T ∪ {ε}.
2) The label of the root is S.
3) If a vertex is interior and has label A, then A must be in V.
4) If n has label A and vertices n1, n2, n3, ..., nk are the sons of
vertex n, in order from the left, with labels X1, X2, ......., Xk,
respectively, then AX1X2 .......Xk must be a production in P.
5) If vertex n has label ε, then n is a leaf and is the only son of its
father.
Example:
Consider the grammar G = ({S, A}, {a, b], P, S), where P consists of
S aAS | a
A SbA|SS|ba
Construct a derivation tree for the string “aabbaa”
A derivation tree is a natural description of the derivation of a particular
sentential form of the grammar G. If we read the labels of the leaves from
left to right, we have a sentential form. We call this string the yield of the
derivation tree.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 11
S => aAS => aSbAS => aabAS => aabbaS => aabbaa.
Note: Some leaves could be labelled by ε.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 12
UNIT - IV
Assignment-Cum-Tutorial Questions
SECTION-A
Objective Questions
1. The C language is [ ]
a) A context free language b) A context sensitive language
c) A regular language d) None
2. Every regular grammar is context free grammar. (True |
False)
3. The finite automata accepts the following language: [ ]
a) Context free language b) regular language
c) Context sensitive language d) all of the above
4. Context-free grammar can be recognized by [ ]
a) Finite Automata b) Linear bounded Automata
c) Push down Automata d) both (b) and (c)
5. The language accepted by a Turing Machine: [ ]
a) Type 0 b) Type 1 c) Type 2 d) Type 3
6. Match the following
1. Context Free Language a. Turing Machine [ ]
2. Recursively Enumerable b. Finite Automata [ ]
3. Regular Language c. Linear Bounded Automata [ ]
4. Context Sensitive Language d. Push Down Automata [ ]
7. For every right linear grammar, there will be an equivalent FA.
[True/ False]
8. Recursively Enumerable language is also called as _________________.
9. A context free grammar is [ ]
a) Type 0 b) Type 1 c) Type 2 d) Type 3
10. Which word can be generated by S->d| bA ,A->d |ccA [ ]
a) bccccd b) aabccd c) ababccd d) abbbd
11. Which of the following strings is in the language defined by grammar
S 0A , A 1A | 0A | 1 [ ]
a) 01100 b) 00101 c) 10011 d) 11111
12. Recognize the CFL for the given CFG. [ ]
S-> aB| bA,
A-> a|aS|bAA,
B-> b|bS|aBB
a) strings contain equal number of a's and equal number of b's.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 13
b) strings contain odd number of a's and odd number of b's.
c) strings contain odd number of a's and even number of b's.
d) strings contain even number of a's and even number of b's
13. Given the following productions of a grammar: [ ]
SaA| aBB AaaA | ε B bB| bbC C B
Which of the following is true?
a) The language corresponding to the given grammar is a set of
even number of a’s.
b) The language corresponding to the given grammar is a set of odd
number of a’s.
c) The language corresponding to the given grammar is a set of
even number of a’s followed by odd number of b’s.
d) The language corresponding to the given grammar is a set of odd
number of a’s followed by even number of b’s.
14. A regular grammar for the language L={ anbm | n is even and m is
even} is [ ]
a) SaSb | X; XbXa | ε
b) SaaS | X; X bSb| ε
c) SaSb | X; X Xab| ε
d) SaaS | X; X bbX|ε
15. Which of the regular expressions corresponds to this grammar?
S AB | AS A a | aA Bb [ ]
a) (aa)*b b) aa*b c) (ab)*
d)a(ab)*
16. Identify the language generated by the following grammar [ ]
SaS | bS | abA
AaA | bA | ε
a) L= x | ab is a substring of x, x ∈{a,b}*
b) L= x | a is a substring of x, x ∈{a,b}*
c) L= x | b is a substring of x, x ∈{a,b}*
d) L= x | ba is a substring of x, x ∈{a,b}*
17. The CFG SaS | bS | a | b is equivalent to the regular expression
a) (a*+b)* b) (a+b)* c) (a+b) (a+b)* d) (a+b)(a+b) [ ]
18. The regular grammar for the given FA is [ ]
a) AaA | bB | a c) AaA | bB | b
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 14
BbA | aB | b BbA | aB | a
b) AaA | bB | ε d) AbA | aB | a
BbA | aB | ε BaA | bB | b
SECTION-B
Descriptive questions
1. Sketch the Venn diagram of Chomsky hierarchy language and their
counterpart automata.
2. Define Regular grammar with an example.
3. What is sentential form? Explain with an example.
4. Explain derivation tree with an example.
5. Explain left most derivation and right most derivation with examples.
6. Define Context Free Grammar and obtain CFG for:
a) the set of all strings with odd palindromes over alphabet {0,1}
b) the set of all strings over alphabet {a, b} with exactly twice as many
a’s as b’s.
c) balanced parenthesis
7. Show that id+id*id can be generated by two distinct derivation trees for
the grammar:
E E+E | E*E | (E) | id
8. Let G be the grammar
S aB | bA
A a | aS | bAA
B b | bS | aBB.
For the string aaabbabbba find a
i. Left most derivation
ii. Right most derivation
iii. Parse Tree
9. Obtain the right linear grammar for the following FA.
10. Obtain a Right Linear Grammar for the language L = {an bm | n>=2 , m>=3}
11. Convert the following DFA to Regular grammar
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 15
12. Is the following grammar ambiguous?
S AB|aaB
A> a|Aa
Bb
13. Find the language generated by the following grammar.
SSS Saa Sε
14. Draw a derivation tree for the string abaaba for the CFG given by G where P =
{SaSa SbSb Sa ⃒ b ⃒Є }
15. Obtain a right linear grammar and left linear grammar for the following FA.
SECTION-C
Gate Questions
1. G1: S aS |B, Bb | bB [GATE 2016]
G2: SaA | bB; A aA |B | , B bB |
Which one of the following pairs of languages is generated by G1
and G2, respectively? [ ]
a) {ambn | m > 0 or n > 0 } and { ambn | m > 0 and n > 0}
b) {ambn | m > 0 and n > 0 } and { amb n | m > 0 or n > =0}
c) {ambn | m >= 0 or n > 0} and { ambn | m > 0 and n > 0 }
d) {ambn | m >= 0 and n > 0 } and { ambn | m > 0 or n > 0}
2. S aSa | bSb | a | b [ ] [GATE 2009]
The language generated by the above grammar over the alphabet {a,
b} is the of
a) all palindromes
b) all odd length palindromes
c) strings that begin and
d) all even length palindromes end with the same symbol
3. Consider the CFG with {S,A,B} as the non-terminal alphabet {a,b} as
the terminal alphabet,S as the start symbol and the following set of
production rules: [GATE 2007]
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 16
S aB S bA
Bb Aa
B bS A aS
B aBB S bAA
Which of the following strings is generated by the grammar? [ ]
a) aaaabb b) aabbbb c) aabbab d) abbbba
4. How many derivation trees are there for the grammar in Question 3?
a) 1 b) 2 c) 3 d) 4 [ ]
5. Consider the regular grammar: [GATE 2005]
S→Xa|Ya
X→Za
Z→Sa|
Y→Wa
W→Sa
where S is the starting symbol, the set of terminals is {a} and the set of
non-terminals is {S, W, X, Y, Z}. We wish to construct a deterministic
finite automaton (DFA) to recognize the same language. What is the
minimum number of states required for the DFA?
a) 2 b) 3 c)4 d)5
6. [GATE 2005]
II.B.Tech-II-Semester 2019-20 CSE