Language of Grammars
Language of Grammars
1
Language of Grammar:
2
Example: Consider a grammar G = (V , T , P , S) where-
V={S}
T={a,b}
P = { S → aSbS , S → bSaS , S → ∈ }
S={S}
3
Example: Consider a grammar G = (V , T , P , S) where-
V={S,A,B,C}
T={a,b,c}
P = { S → ABC , A → a , B → b , C → c }
S={S}
This grammar generates only one string “abc”.
So, language of this grammar is - L(G) = { abc }
4
Important Concept:
For any given grammar, the language generated by it is
always unique.
However for any given language, we may have more than
5
Grammar G1-
S → AB
A → a
B → b
The language generated by this grammar is- L(G1) = { ab }
Grammar G2-
S → AB
A → ∈
B → ab
The language generated by this grammar is- L(G2) = { ab }
Note:
Here, both the grammars generate a unique language and given a
language L(G) = { ab }, we have two different grammars generating the
same language.
This justifies that for any given language, we may have more than
grammar generating the given language.
6
7
Regular Expressions
Regular Expressions are used to denote regular
languages.
8
Regular Sets:
A set that represents the value of the regular expression is
called a regular set.
11
Regular Languages :
A language is regular if it can be expressed in terms of
regular expressions.
( or )
A regular language can be generated from regular grammar
Ex:
12
Definition of Non-Regular Languages
A language that cannot be defined by a regular expression is
called a non-regular language.
Notes:
– By Kleene’s theorem, a non-regular language can also not
be accepted by any FA.
– All languages are either regular or non-regular; none are
both.
13
Properties of Regular Sets
14
Property 1. The union of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(aa)* L1 = {a, aaa, aaaaa,.....}
RE2 = (aa)* L2 ={ ε, aa, aaaa, aaaaaa,.......}
Therefore
Where
L1 Strings of odd length excluding Null
L2 Strings of even length including Null
L1 ∪ L2 Strings of all possible lengths including Null
15
Property 2. The intersection of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(a*) L1 = { a,aa, aaa, aaaa, ....}
RE2 = (aa)* L2 = { ε, aa, aaaa, aaaaaa,.......}
So,
L1 ∩ L2 = { aa, aaaa, aaaaaa,.......}
Therefore
Where
L1 Strings of all possible lengths excluding Null
L2 Strings of even length including Null
L1 ∩ L2 Strings of even length excluding Null
.
16
Property 3. The complement of a regular set is regular.
Proof −
Let us take a regular expression −
RE = (aa)* L = {ε, aa, aaaa, aaaaaa, .......}
Therefore
Where
17
Property 4. The difference of two regular set is regular.
Proof −
Let us take two regular expressions −
RE1 = a (a*) L1 = {a, aa, aaa, aaaa, ....}
RE2 = (aa)* L2 = { ε, aa, aaaa, aaaaaa,.......}
Therefore
Where
L1 = Strings of all possible lengths excluding Null
L2 = Strings of even length including Null
L1 – L2 (Strings of all odd lengths excluding Null)
18
Property 5. The reversal of a regular set is regular.
Proof −
We have to prove LR is also regular if L is a regular set.
Let,
L = {01, 10, 11, 10}
RE (L) = 01 + 10 + 11 + 10
RE (LR) = 01 + 10 + 11 + 10
which is regular
19
Property 6: The closure of a regular set is regular.
Proof −
If L = {a, aaa, aaaaa, .......}
(Strings of odd length excluding Null)
RE (L*) = a (a)*
20
Property 7. The concatenation of two regular sets is regular.
Proof −
Let RE1 = (0+1)*0
RE2 = 01(0+1)*
L1 L2 = { 001,0010,0011,0001,00010,00011,1001,10010,.....}
Set of strings containing 001 as a substring which can be
represented by
RE = (0 + 1)*001(0 + 1)*
21
Identities Rules of Regular Expressions :
Given R, P, L, Q as regular expressions, the following identities hold −
1. ∅* = ε
2. ε* = ε
3. RR* = R*R
4. R*R* = R*
5. (R*)* = R*
6. RR* = R*R
7. (PQ)*P =P(QP)*
8. (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
9. R + ∅ = ∅ + R = R (The identity for union)
10. R ε = ε R = R (The identity for concatenation)
22
11. ∅ L = L ∅ = ∅ (The annihilator for concatenation)
12. R + R = R (Idempotent law)
13. L (M + N) = LM + LN (Left distributive law)
14. (M + N) L = ML + NL (Right distributive law)
15. ε + RR* = ε + R*R = R*
23
24
Regular Regular Language Regular set
Expression
ϵ L(ϵ) {ϵ}
Ø L(Ø) { }
x L(x) { x}
Y L(y) {y}
25
26
27