Formal Language
Formal Language
In this chapter \ve introduce the concepts of grammars and formal languages
and discuss the Chomsky classification of languages. We also study the
inclusion relation between the four classes of languages. Finally. we discuss the
closure properties of these classes under the variuus operations.
107
108 ~ Theory of Compurer Science
past tense, and 'quickly' by 'slowly', l.e. by any adverb. we get other
grammatically correct sentences. So the structure of 'Ram ate quickly' can be
given as (noun) (verb) (adverb). For (noun) we can substitute 'Ram'. 'Sam',
'Tom'. 'Gita', etc. . Similarly. we can substirure "ate'. 'walked', 'ran', etc. for
(verb). and 'quickly". 'slowly' for (adverb). Similarly, the structure of 'Sam ran'
can be given in the form (noun)
We have to note that (noun) (vdb) is not a sentence but only the
description of a particular type of sentence. If we replace (noun), (verb) and
(adverb) by suitable \vords, we get actual grammatically correct sentences. Let
us call (noun), (adverb) as variables. Words like 'Ram', 'Sam', 'ate',
'ran'. 'quickly", 'slowly' which form sentences can be caHed terminals. So our
sentences tum out to be strings of terminals. Let S be a variable denoting a
sentence. Now. we can form the following rules to generate two types of
sentences:
S -+ (noun) (verb) <adverb>,
\
G = (VI' L P, S) is a grammar
where
Vy = {(sentence). (noun). (verb). (adverb)}
L = [Ram. Sam. ate, sang. well]
5 = (sentence)
P consists of the follmving productions:
(sentence) -7 (noun) (verb)
(semence) -7 (noun) (verb) (adverb)
(noun) --7 Ram
(noun) ----7 Sam
(verb) -7 ate
(\ erb) -7 sang
(adverb) ----7 well
NOTATION: (i) If A is any set. then A'" denotes the set of all strings over A.
A + denotes A. ':' - {;\}. where ;\ is the empty string.
(ii) A, B. C, A 1, A 2• ... denote the variables.
(i ll) a, b, c. ' .. denote the terminals.
(iv) x. y. ;. H • . . . denote the strings of terminals.
lY, {3, Y. ... denote the elements of (t', u D*.
(vi) ":'{J = <\ for any symbol X in V\ u "
11 0 ~ Theory of Computer Science
----------------
Productions are used to derive one stling over \/N U L from another string.
We give a formal definition of derivation as follows:
Definition 4.2 If a ~ f3 is a production in a grammar G and y, 8 are any
two strings on U 2:, then we say that ya8 directly derives yf38 in G (we
Defmition 4.3 If a and f3 are strings on \/v U :E, then we say that a derives
~, '"
f3 if a ~ f3. Here ~ denotes the reflexive-transitive closure of the relation ~
G G G
in (Fy U :E)* (refer to Section 2.1.5).
Note: We can note in particular that a 7 a. Also, if a 7 f3. [X '1'= f3, then
there exist strings [Xl- a2, .. " (tll' where II ;::: 2 such that
(X = (X] ~ a2 ~ a 3 . .. ~ all = f3
G G G
NOTATION: (i) We \vrite CY. ~ {3 simply as CY. :b {3 if G is clear from the context.
e
(ji) If A ~ CY. is a production where A E Vv, then it is called an
A-production.
(iii) If A ~ ai' A. ~ a:. .. nA. ~ CY.!/! are A-productions. these
productions are written as A ~ ai i CY.: j ... I am'
We give several examples of grammars and languages generated by them.
EXAMPLE 4.2
If G = ({5}. {a. I}. {5 ~ 051, s~ A}. S). find L(G).
Solution
As 5 ~ A is a production. S =? A. So A is in L(G). Also. for all n :::: 1.
G
=? 0"51" =? 0"1"
G G
Therefore.
0"1" E L(G) for n :::: a
(Note that in the above derivation, S ~ 051 is applied at every step except
in the last step. In the last step, we apply 5 ~ A). Hence, {O"I" In:::: O} ~ UG).
To show that L( G) ~ {O''1'' i 17 :::: A}. we start with ].V in L(G). The
derivation of It' starts with 5. If S ~ A is applied first. we get A. In this case
].V = A. Othenvise the first production to be applied is 5 ~ 051. At any stage
l.e.
112 ~ Theory of Computer Science
Therefore.
LeG) = {Qlll l1 /n 2: Q}
EXAMPLE 4.3
If G = ({ 5}, {a}, {5 ----;; 55}, 5), find the language generated by G.
Solution
L(G) = 0. since the only production 5 -> 55 in G has no terminal on the
right-hand side.
EXAMPLE 4.4
Let G = ({ S. C}, {a, b}, P, 5), where P consists of 5 ----;; aCa. C ----;; aCa I b.
Find L(G).
Solution
S:=;. aea :=;. aba. So aba E L(G)
5 :=;. aCa (by application of 5 ----;; aCa)
b d'Cd' (by application of C ----;; aCa (n - 1) times)
fl
:=;. d'ba (by application of C ----;; b)
Hence. a"ba" E LeG), where n :2: 1. Therefore.
{d'ba"ln 2: I} s:: L(G)
As the only S-production is 5 ----;; aCa, this is the first production we have
to apply in the derivation of any terminal string. If we apply C ----;; b. we get aba.
Otherwise we have to apply only C ----;; aCa. either once or several times. So
we get d'Ca" with a single variable C. To get a terminal string we have to
replace C by b. by applying C ----;; b. So any delivation is of the fonn
S b a"bu n with n 2: 1
Therefore.
L( G) s:: {a" bail I n 2: ]}
Thus.
L(G) = {(/ba ll i Jl 2: I}
EXERCISE Construct a grammar G so that UG) = {a"bc/ 1I
1 n. m 2: l}.
Remark By applying the com'ention regarding the notation of variables.
terminals and the start symbol. it \vill be clear from the context whether a
symbol denotes a variable or terminal. We can specify a grammar by its
productions alone.
Chapter 4: Formal Languages ~ 113
EXAMPLE 4.5
If Gis S ~ as i ItS [ a [ h, find L(G).
Solution
We sho\v that U C) = {a. b} As V·le have only two terminals a, h,
7.
UG) :;;;;; {a. b} *. All productions are S-productions. and so A can be in L(G)
on1\ when S ~ A is a production in the grammar G. Thus.
UG) :;;;;; {a. h} ':' - {A} = {a, b} +
To show {Cl, b r :; ; ;
ICG). consider any string al a: ... ali' where each ai
is either a or h. The first production in the delivation of ClI{l2 ... all is S ~
as or 5 ~ bS according as a] = a or (lj = b. The subsequent productions are
obtained in a similar way. The last production is S ~ a or S ~ b according
as = a or a" = b. So aja2 ... ali E UG). Thus. we have L(G) = {a, h ]+.
EXAMPLE 4.6
Let L be the set of all pahndromes over {a. h}. Construct a grammar G
generating L.
Solution
For constructing a grammar G generating the set of all palindromes. \ve use
the recursive definition (given in Section 2.4) to observe the following:
ii) A is a palindrome.
Iii) a. b are palindromes.
(Jii) If x is a palindrome axo. then bxb are palindromes.
So \\e define P as the set consisting of:
S ~.\
S ~ (fand S ~ b
Oii) S ~ aSa and S ~ hSb
Let G = ({5} {a. b}, P. S). Then
5 => A, S => (I, S=>b
The. fore.
A. a. h E L(G)