TOC (UNIT-II)
TOC (UNIT-II)
Computation
UNIT-II
BY
V.SRAVANTHI
SYLLABUS
UNIT - II
Regular Languages: Regular sets, regular expressions, identity rules,
Constructing finite Automata for a given regular expressions, Conversion
of Finite Automata to Regular expressions.
Context Free Grammars: Definition, Ambiguity in context free grammars.
Simplification of Context Free Grammars. Chomsky normal form,
Greibach normal form, Enumeration of properties of CFLs (proofs
omitted), Chomsky's hierarchy of languages.
Regular Languages:
Regular Languages: A language is said to be
regular if and only if some finite machine
recognizes it.
A language that is not regular if
Are not recognized by an FSM
Regular Expression
Just as finite automata are used to recognize patterns of
strings, regular expressions are used to generate patterns of
strings. A regular expression is an algebraic formula whose
value is a pattern consisting of a set of strings, called the
language of the expression.
Operands in a regular expression can be:
characters from the alphabet over which the regular
expression is defined.
variables whose values are any pattern defined by a regular
expression.
epsilon which denotes the empty string containing no
characters.
null which denotes the empty set of strings
Operators used in regular expressions
Set of strings of a’s and b’s of any length including the null string. So L = { ε, a, b, aa ,
(a+b)*
ab , bb , ba, aaa…….}
Set of strings of a’s and b’s ending with the string abb. So L = {abb, aabb, babb, aaabb,
(a+b)*abb
ababb, …………..}
Set consisting of even number of 1’s including empty string, So L= {ε, 11, 1111,
(11)*
111111, ……….}
Set of strings consisting of even number of a’s followed by odd number of b’s , so L =
(aa)*(bb)*b
{b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}
String of a’s and b’s of even length can be obtained by concatenating any combination
(aa + ab + ba + bb)* of the strings aa, ab, ba and bb including null, so L = {aa, ab, ba, bb, aaab, aaba,
…………..}
**Example**
Identity rules for regular
expression
The two regular expression’s P and Q are
4. ΦR=R Φ= Φ
5. Φ+R=R
6. R+R=R
7. RR*=R*R=R+
8. (R*)*=R*
9. Ε+RR*=R*
10. (P+Q)R=PR+QR
11. (P+Q)*=(P*Q*)*=(P*+Q*)*
12. R*(ε+R)=( ε+R)R*=R*
13. (R+ε)*=R*
14. Ε+R*=R*
15. (PQ)*P=P(QP)*
16. R*R+R=R*R
17. L+=LL*=L*L
Arden's Theorem
Proof-1: solution
R = Q + RP ......(i)
Now, replacing R by R = QP*, we get,
R = Q + QP*P and by Taking Q as common,
R = Q( ∈ + P*P) = QP*
Hence Proved
(As we know that ∈ + R*R = R*). Hence proved. Thus, R = QP* is the
solution of the equation R = Q + RP.
Proof-2: solution
Consider R = Q + RP
Now, replace R by R = Q + RP,
R = Q + (Q + RP)P = Q + QP + RP2
Again, replace R by R = Q + RP :-
R = Q + QP + (Q + RP) P2 = Q + QP + QP2 + RP3 . .
R = Q + QP + QP2 + .. + Q + RP(n+1)
Now, replace R by R = QP*, we get,
R = Q + QP + QP2 + .. + QPn+ QP*
Taking Q as common,
R = Q( ∈ + P + P2 + .. + Pn + P*P(n+1) = QP* [As ∈ + P + P2 + ..
+ Pn + P*P(n+1)
Solution −
Here the initial state is q1 and the final state is q2
Now we write down the equations −
q1 = q1 0 + ε
q 2 = q 1 1 + q2 0
q 3 = q 2 1 + q3 0 + q3 1
Now, we will solve these three equations −
q1 = ε0* [As, εR = R]
So, q1 = 0*
q2 = 0*1 + q20
So, q2 = 0*1(0)* [By Arden’s theorem]
Hence, the regular expression is 0*10*.
q3 = q21 + q30 + q31
q3=0*10*+q30 + q31
q3=0*10*+q3 (0 +1)
Q+RP
q3=0*10*(0 +1)*
State Elimination method
Rule-1 :
The initial state of the DFA must not have
one.
These states may be eliminated in any order.
In the end, Only an initial state going to the
regular expression.
NOTE
The state elimination method can be applied
***Examples***
Constructing Finite automata for given
Regular Expression
Equivalence of NFA & RE
1. Convert the following RA into its
equivalent
1 (0 + 1)* 0
Solution
We will concatenate three expressions "1", "(0 + 1)*" and "0"
Conversion RE to FA
Examples…
There are two Pumping Lemmas defined for
1.Regular Language
2.Context –Free Languages
Example:
1. Prove that L = {anbn| n ≥ 0} is not
regular.
Regular Grammar
It can be defined as G={V,T,P,S} where
V=Set of symbols called Non terminals
T=Set of symbols called Terminals
P=Set of Production rules
S=start symbol where S belongs to V
Regular Grammar can be of two forms:
Right Linear Regular Grammar
Left Linear Regular Grammar
**Examples**
Conversion of RG to RE
a) Convert RG to FA
b) Convert FA to RE(Arden’s theorem)
**Examples**
Context free grammar
Example
The grammar ({A}, {a, b, c}, P, A),
P : A → aA, A → abc.
***CFG construction
Examples**
Capabilities of CFG
**Examples**
Sentential Form and Partial Derivation Tree
A partial derivation tree is a sub-tree of a
derivation tree/parse tree such that either all
of its children are in the sub-tree or none of
them are in the sub-tree.
Example
If in any CFG the productions are −
following −
be
The stepwise derivation of the above string is shown
as below −
X → X+X
→ a+X
→ a +X*X
→ a+a*X
→ a+a*a
The rightmost derivation for the above
string "a+a*a" may be −
The stepwise derivation of the above string is
shown as below −
X → X*X
→ X*a
→ X+X*a
→ X+a*a
→ a+a*a
**Example**
Ambiguity in CFG
If a context free grammar G has more than
one derivation tree for some string w ∈
L(G), it is called an ambiguous grammar.
There exist multiple right-most or left-most
derivations for some string generated from
that grammar.
Example: Check whether the grammar G with
production rules −X → X+X | X*X |X| a is
ambiguous or not.
Solution:
Let’s find out the derivation tree for the string
X → X*X
→ X+X*X
→ a+ X*X
→ a+a*X
→ a+a*a
Since there are two parse trees for a single
string "a+a*a", the grammar G is ambiguous.
**Examples**
Simplification of Context Free Grammars.
Simplification of grammar means reduction of
grammar by removing useless symbols.
The properties of reduced grammar :
1. Each variable (i.e. non-terminal) and each
terminal of G appears in the derivation of
some word in L.
2. There should not be any production as X →
Y where X and Y are non-terminal.
3. If ε is not in the language L then there
need not to be the production X → ε.
Removal of Useless Symbols
Final G is
T → aaB | aaT
B → ab | b
Elimination of ε Production
In case the given grammar is not present in CNF, first convert it into
CNF.
Step 2: Change the names of non terminals symbols into some Ai
notations in ascending order of i
Step 3: For Ai Aj
If i=j means left recursion, so eliminate it.
If i<j means accepted by GNF
If i>j means not in GNF ,make it to get i<j
Non-deterministic
Type-2 Context-free pushdown
automaton
Linear-bounded non-
Type-1 Context-sensitive deterministic Turing
machine
Recursively
Type-0 Turing machine
enumerable
Enumeration of properties of
CFLs
Context-free languages are closed under −
Union
Concatenation
Kleene Star operation
Union
Let L1 and L2 be two context free languages. Then L1 ∪ L2 is also context free.
Example
Let L1 = { anbn , n > 0}. Corresponding grammar G1 will have P: S1 → aAb|ab
Example
Union of the languages L1 and L2, L = L1L2 = { anbncmdm }
Kleene Star
If L is a context free language, then L* is also context free.
Example
Let L = { anbn , n ≥ 0}. Corresponding grammar G will have P: S → aAb| ε