Moore - Melay Machines
Moore - Melay Machines
There
are two types of finite state machines that generate output −
• Mealy Machine
• Moore machine
Mealy Machine
A Mealy Machine is an FSM whose output depends on the present state as
well as the present input.
Next state
→a b x1 c x1
b b x2 d x3
c d x3 c x1
d d x3 d x2
Next State
Present state Output
Input = 0 Input = 1
→a b c x2
b b d x1
c c d x2
d d d x3
Generally, it has fewer states than Moore Generally, it has more states than Mealy
Machine. Machine.
The value of the output function is a function The value of the output function is a function of
of the transitions and the changes, when the the current state and the changes at the clock
input logic on the present state is done. edges, whenever state changes occur.
Step 2 − Copy all the Moore Machine transition states into this table format.
Step 3 − Check the present states and their corresponding outputs in the
Moore Machine state table; if for a state Q i output is m, copy it into the
output columns of the Mealy Machine state table wherever Q i appears in the
next state.
Example
Let us consider the following Moore machine −
Next State
Present State Output
a=0 a=1
→a d b 1
b a d 0
c c c 0
d b a 1
Step 1 & 2 −
Next State
→a d b
b a d
c c c
d b a
Step 3 −
Next State
=> a d 1 b 0
b a 1 d 1
c c 0 c 0
d b 0 a 1
Step 1 − Calculate the number of different outputs for each state (Q i) that
are available in the state table of the Mealy machine.
Step 2 − If all the outputs of Qi are same, copy state Q i. If it has n distinct
outputs, break Qi into n states as Qin where n = 0, 1, 2.......
Step 3 − If the output of the initial state is 1, insert a new initial state at the
beginning which gives 0 output.
Example
Let us consider the following Mealy Machine −
Next State
→a d 0 b 1
b a 1 d 0
c c 1 c 0
d b 0 a 1
Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain
states ‘a’ and ‘d’. But states ‘b’ and ‘c’ produce different outputs (1 and 0).
So, we divide b into b0, b1 and c into c0, c1.
Next State
Present State Output
a=0 a=1
→a d b1 1
b0 a d 0
b1 a d 1
c0 c1 C0 0
c1 c1 C0 1
d b0 a 0
Chomsky's Normal Form (CNF)
CNF stands for Chomsky normal form. A CFG(context free grammar) is in CNF(Chomsky
normal form) if all production rules satisfy one of the following conditions:
For example:
1. G1 = {S → AB, S → c, A → a, B → b}
2. G2 = {S → aA, A → a, B → c}
The production rules of Grammar G1 satisfy the rules specified for CNF, so the grammar G1 is
in CNF. However, the production rule of Grammar G2 does not satisfy the rules specified for
CNF as S → aZ contains terminal followed by non-terminal. So the grammar G2 is not in CNF.
1. S1 → S
Step 2: In the grammar, remove the null, unit and useless productions. You can refer to the
Simplification of CFG.
Step 3: Eliminate terminals from the RHS of the production if they exist with other non-
terminals or terminals. For example, production S → aA can be decomposed as:
1. S → RA
2. R → a
Step 4: Eliminate RHS with more than two non-terminals. For example, S → ASB can be
decomposed as:
1. S → RS
2. R → AS
Example1:
Convert the given CFG to CNF. Consider the given grammar G1:
1. S → a | aA | B
2. A → aBB | ε
3. B → Aa | b
Solution:
Step 1: We will create a new production S1 → S, as the start symbol S appears on the RHS. The
grammar will be:
1. S1 → S
2. S → a | aA | B
3. A → aBB | ε
4. B → Aa | b
Step 2: As grammar G1 contains A → ε null production, its removal from the grammar yields:
1. S1 → S
2. S → a | aA | B
3. A → aBB
4. B → Aa | b | a
1. S1 → S
2. S → a | aA | Aa | b
3. A → aBB
4. B → Aa | b | a
Also remove the unit production S1 → S, its removal from the grammar yields:
1. S0 → a | aA | Aa | b
2. S → a | aA | Aa | b
3. A → aBB
4. B → Aa | b | a
Step 3: In the production rule S0 → aA | Aa, S → aA | Aa, A → aBB and B → Aa, terminal a
exists on RHS with non-terminals. So we will replace terminal a with X:
1. S0 → a | XA | AX | b
2. S → a | XA | AX | b
3. A → XBB
4. B → AX | b | a
5. X → a
Step 4: In the production rule A → XBB, RHS has more than two symbols, removing it from
grammar yield:
1. S0 → a | XA | AX | b
2. S → a | XA | AX | b
3. A → RB
4. B → AX | b | a
5. X→a
6. R → XB
Example2 – Let us take an example to convert CFG to CNF. Consider the given grammar G1:
S → ASB
A → aAS|a|ε
B → SbS|A|bb
Step 1. As start symbol S appears on the RHS, we will create a new production rule S0->S.
Therefore, the grammar will become:
S0->S
S → ASB
A → aAS|a|ε
B → SbS|A|bb
Step 2. As grammar contains null production A-> ε, its removal from the grammar yields:
S0->S
S → ASB|SB
A → aAS|aS|a
B → SbS| A|ε|bb
Now, it creates null production B→ ε, its removal from the grammar yields:
S0->S
S → AS|ASB| SB| S
A → aAS|aS|a
B → SbS| A|bb
Now, it creates unit production B->A, its removal from the grammar yields:
S0->S
S → AS|ASB| SB| S
A → aAS|aS|a
B → SbS|bb|aAS|aS|a
Also, removal of unit production S0->S from grammar yields:
Also, removal of unit production S->S and S0->S from grammar yields:
S0-> AS|ASB| SB
S → AS|ASB| SB
A → aAS|aS|a
B → SbS|bb|aAS|aS|a
Step 3. In production rule A->aAS |aS and B-> SbS|aAS|aS, terminals a and b exist on RHS with
non-terminates. Removing them from RHS:
S0-> AS|ASB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|bb|XAS|XS|a
X →a
Y→b
S0-> AS|ASB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
Step 4: In production rule S0->ASB, RHS has more than two symbols, removing it from
grammar yields:
S0-> AS|PB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Similarly, S->ASB has more than two symbols, removing it from grammar yields:
S0-> AS|PB| SB
S → AS|QB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
Similarly, A->XAS has more than two symbols, removing it from grammar yields:
S0-> AS|PB| SB
S → AS|QB| SB
A → RS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
Similarly, B->SYS has more than two symbols, removing it from grammar yields:
S0 -> AS|PB| SB
S → AS|QB| SB
A → RS|XS|a
B → TS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
T → SY
Similarly, B->XAX has more than two symbols, removing it from grammar yields:
S0-> AS|PB| SB
S → AS|QB| SB
A → RS|XS|a
B → TS|VV|US|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
T → SY
U → XA
GNF stands for Greibach normal form. A CFG(context free grammar) is in GNF(Greibach
normal form) if all the production rules satisfy one of the following conditions:
For example:
The production rules of Grammar G1 satisfy the rules specified for GNF, so the grammar G1 is
in GNF. However, the production rule of Grammar G2 does not satisfy the rules specified for
GNF as A → ε and B → ε contains ε(only start symbol can generate ε). So the grammar G2 is
not in GNF.
If the given grammar is not in CNF, convert it into CNF. You can refer the following topic to
convert the CFG into CNF: Chomsky normal form
If the context free grammar contains left recursion, eliminate it. You can refer the following
topic to eliminate left recursion: Left Recursion
Step 3: In the grammar, convert the given production rule into GNF form.
If any production rule in the grammar is not in GNF form, convert it.
Example1:
1. S → XB | AA
2. A → a | SA
3. B→b
4. X→a
Solution:
As the given grammar G is already in CNF and there is no left recursion, so we can skip step 1
and step 2 and directly go to step 3.
1. S → XB | AA
2. A → a | XBA | AAA
3. B→b
4. X→a
1. S → aB | AA
2. A → a | aBA | AAA
3. B→b
4. X→a
1. S → aB | AA
2. A → aC | aBAC
3. C → AAC | ε
4. B→b
5. X→a
1. S → aB | AA
2. A → aC | aBAC | a | aBA
3. C → AAC | AA
4. B→b
5. X→a
Example2: Suppose this the production and we need to convert it into GNF.
S → XA|BB
B → b|SB
X → b
A → a
For converting a CNF to GNF first rename the non terminal symbols to A1,A2 till AN in same
sequence as they are used.
A1 = S
A2 = X
A3 = A
A4 = B
A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | A1A4
Now, check for every production Ai → Aj X, where X can be any number of terminal symbols.
If i<j in the production then it is good to go to the next step but if i>=j then change the
production by replacing it with that terminal symbol’s production. if i=j then it is a left recursion
and you need to remove left recursion.
Here for A4, 4 !< 1, so now replace it with A1‘s production rule.
1.
A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | A2A3A4 | A4A4A4
----------------------
2.
A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | bA3A4 | A4A4A4
To replace the left most recursion take a new Non terminal symbol Z, which has the X part or the
trailing part of the left most recursive production once followed by Z and once without Z. Here
in A4A4A4, the part after the first A4 is A4A4, therefore
Z → A4A4 | A4A4Z
Now change the above production rule by putting Z after every previous production of that A i,
and remove the left recursive production.
A1 → A2A3 | A4A4
A2 → b
A3 → a
A4 → b | bA3A4 | bZ | bA3A4Z
Z → A4A4 | A4A4Z
OR
Conclusion
Converting a CFG into GNF is a systematic process that involves renaming non-terminals,
replacing left recursions, and ensuring the production rules fit the GNF criteria. This
transformation is crucial for various applications in compiler design and formal language
processing.