Toc m2 Note
Toc m2 Note
com/c/EDULINEFORCSE
STUDENTS
MODULE 2
MOORE MACHINE
λ : Q→∆
MEALY MACHINE
(q₀, a) → 1
λ : Q×Σ→∆ (q₀, b) → 0
(q₁, b) → 0
(q₁, a) → 1
a b
q₀ q₀ q₁
1 0
STEP
1. Draw a table for all pairs of states (P, Q)
2. Mark all pairs where P Є F and Q ₵ F
3. If there are many unmarked pairs (P, Q) such that [(P,x),
(Q,x)] is marked , then mark [P, Q] (where x is an input symbol)
. Repeat this until no more markings can be made.
4. Combine all the unmarked pairs and make them a single state in
the minimized DFA.
Example A B C D E F
A
d B
c D
E
Step 1: Draw a table for all pairs F
of states (P, Q)
A B C D E F
A d
C
c
D
Step 3
(B,A) - (B, 0) - A
(A, 0) – B unmarked
(B, 1) – D
(A, 1) – C unmarked
(D,C) - (D, 0) –E
d
(C, 0) – E no such column
(D, 1) – F
c (C, 1) – F no such column
Step 3 continuation
(E,C) - (E, 0) - E
(C, 0) – E no such column
(E, 1) – F
(C, 1) – F no such column
(E,D) - (E, 0) –E
d
(D, 0) – E no such column
(E, 1) – F
c (D, 1) – F no such column
Step 3 continuation
(F, A) - (F, 0) - F
(A, 0) – B unmarked
(F, 1) – F
(A, 1) – C marked. So we
should mark FA
d (F, B) - (F, 0) –F
(B, 0) –A FA is marked .so
c
we should mark FB also.
Final table
Step 4
Combine the unmarked pairs
(A,B), (D,C), (E,C), (E,D)
Minimal DFA
1 equivalent states
q₀ q₁ q₂ q₃ q₄
2 equivalent states
q₀ q₂ q₁ q₃ q₄
3 equivalent states
[q₀,q₂] [q₁] [q₃] [q₄]
ie, No further division is possible
1 equivalent states
[q₁ q₂] [q₀]
No further division is possible
1 equivalent states
q₀ q₁ q₂ q₃
2 equivalent
• To find 2 equivalent states, we use 1 equivalent states. Ie,
[q₀ q₁] [q₂] [q₃]
• Check 2 equivalent of (q₀ q₁)
2 equivalent states
q₀ q₁ q₂ q₃
So, 2 equivalent states are
[q₀] [q₁] [q₂] [q₃]
GRAMMAR
A grammar describes how to form strings from a language's
alphabet that are valid according to the language's syntax.
A grammar is usually thought of as a language generator.
Formal Definition
• A Grammar is a 4-tuple such that G = (V , T , P , S) where
V = Finite non-empty set of non-terminal symbols (Variables)
T = Finite set of terminal symbols
P = Finite non-empty set of production rules
S = Start symbol
Prepared By Mr. EBIN PM, AP, IESCE EDULINE32
Example 1 of Grammar
• Consider a grammar G = (V , T , P , S) where-
V={S} // Set of Non-Terminal symbols
T={a,b} // Set of Terminal symbols
P = { S → aSbS , S → bSaS , S → ε } // Set of production rules
S={S} // Start symbol
This grammar generates the strings having equal number of a’s and
b’s
Example 2 of Grammar
Consider a grammar G = (V , T , P , S) where-
Chomsky Hierarchy
Noam Chomsky gave a mathematical model of grammar in 1956
which is effective for writing computer languages.
According to Chomsky hierarchy, grammars are divided of 4 types:
Chomsky Hierarchy
Derivations
The process of deriving a string is called as derivation.
The geometrical representation of a derivation is called as a parse
tree or derivation tree.
Leftmost Derivation
Find Leftmost derivation of the S →AB
Following Grammar ? →aBB
S → AB | ε →aSbB
A → aB →aεbB
B → Sb →abB
Derive the string abb →abSb
→abεb
→abb
Rightmost Derivation
Find Rightmost derivation of the S →AB
Following Grammar ? →ASb
S → AB | ε →Aεb
A → aB →aBb
B → Sb →aSbb
Derive the string abb →aεbb
→abb
REGULAR EXPRESSIONS
The language accepted by finite automata can be easily described
by simple expressions called Regular Expressions.
It is the most effective way to represent any language.
The languages accepted by some regular expression are referred
to as Regular languages.
A regular expression can also be described as a sequence of
pattern that defines a string.
Regular expressions are used to match character combinations in
strings.
Example 1:
Write the regular expression for the language accepting all
combinations of a's, over the set ∑ = {a}
Solution:
• All combinations of a's means a may be zero, single, double and so
on.
• If a is appearing zero times, that means a null string. That is we
expect the set of {ε, a, aa, aaa, ....}.
• So we give a regular expression for this as:
RE = a*
Example 2:
Write the regular expression for the language accepting all
combinations of a's except the null string, over the set ∑ = {a}
Solution:
• The regular expression has to be built for the language
L = {a, aa, aaa, ....}
• This set indicates that there is no null string. So we can denote
regular expression as:
RE =
Example 3:
Write the regular expression for the language accepting all the
string containing any number of a's and b's.
Solution:
The regular expression will be:
RE = (a + b)*
• This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, .....},
any combination of a and b.
• The (a + b)* shows any combination with a and b even a null
string.
Example 4:
Write the regular expression for the language accepting all the
string which are starting with 1 and ending with 0, over ∑ = {0, 1}
Solution:
• In a regular expression, the first symbol should be 1, and the last
symbol should be 0.
RE = 1 (0+1)* 0
Conversion of RE to FA
Rules:
1. a+b (a or b, a|b) 2. (ab)
a
a b
q₀ q₁ q₀ q₁ q₂
b
3. ∗ (Repetition / a closure)
a
q₀
Example: 1
Convert the regular expression b𝐚∗ b to Finite Automata
L= {bb, bab, baab,…….}
Example: 2
Convert the regular expression (a+b)c to Finite Automata
L= {ac, bc}
Example: 3
Convert the regular expression a ∗ to Finite Automata
Example: 3
Convert the regular expression ∗
(abb| b ) to Finite
Automata
Rule: 2
• The NFA for the regular expression (RE) that accept an input
symbol a is given by
Rule : 3
• If N1 and N2 are NFA for the regular Expression R1 and R2,then
(i) NFA for R1+R2 (R1|R2)
Example : 1
Construct NFA for the RE (a|b)*a for a
W can split the RE like
R1 = a
R2 = b
R3 = R1 | R2 for b
R4 = R3*
R5 = R4.R1
We can construct NFA for each one
for (a|b)*a
Example 2
• Draw NFA to represent the regular expression ab|(a|b)a
for a for ab
for ab|(a|b)
for ab|(a|b)a
Conversion of FA to RE
Here we are using state elimination method
Rule 1
Initial state should not have any incoming edge from other state.
If incoming edge is present, then create new initial state
Rule 2a
Final state should not have any outgoing edge. If outgoing edge is
present, then create new final state
Rule 2b
If more than one Final state is present, then convert it into one
state
Rule 3
Eliminate the state one by one other than the initial & final state
Example 1
a, b, c
A B
RE = (a+b+c)
Example 2
ab
q₀ q₁
RE = ab
Example 3
RE = ab*c
Example 4 Step - 1
Incoming edge is present from other state to
initial state . So , create new initial state
Step - 2
Outgoing edge is present from final state
So , create new final state
Step - 3
Delete all intermediate states one by one
0 ε
q₀ B qf
Step - 3
Eliminate the state B
10
0(10)*
q₀ qf
RE = 0(10)*