0% found this document useful (0 votes)
29 views15 pages

Moore - Melay Machines

Uploaded by

Haha Haha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views15 pages

Moore - Melay Machines

Uploaded by

Haha Haha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Finite automata may have outputs corresponding to each transition.

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.

It can be described by a 6 tuple (Q, ∑, O, δ, X, q 0) where −

• Q is a finite set of states.


• ∑ is a finite set of symbols called the input alphabet.
• O is a finite set of symbols called the output alphabet.
• δ is the input transition function where δ: Q × ∑ → Q
• X is the output transition function where X: Q × ∑ → O
• q0 is the initial state from where any input is processed (q0 ∈ Q).

The state table of a Mealy Machine is shown below −

Next state

Present state input = 0 input = 1

State Output State Output

→a b x1 c x1

b b x2 d x3

c d x3 c x1

d d x3 d x2

The state diagram of the above Mealy Machine is −


Moore Machine
Moore machine is an FSM whose outputs depend on only the present state.

A Moore machine can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −

• Q is a finite set of states.


• ∑ is a finite set of symbols called the input alphabet.
• O is a finite set of symbols called the output alphabet.
• δ is the input transition function where δ: Q × ∑ → Q
• X is the output transition function where X: Q → O
• q0 is the initial state from where any input is processed (q0 ∈ Q).

The state table of a Moore Machine is shown below −

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

The state diagram of the above Moore Machine is −


Mealy Machine vs. Moore Machine
The following table highlights the points that differentiate a Mealy Machine
from a Moore Machine.

Mealy Machine Moore Machine

Output depends both upon the present state


Output depends only upon the present state.
and the present input

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.

In Moore machines, more logic is required to


Mealy machines react faster to inputs. They decode the outputs resulting in more circuit
generally react in the same clock cycle. delays. They generally react one clock cycle
later.

Moore Machine to Mealy Machine


Algorithm 4
Input − Moore Machine

Output − Mealy Machine


Step 1 − Take a blank Mealy Machine transition table format.

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

Now we apply Algorithm 4 to convert it to Mealy Machine.

Step 1 & 2 −

Next State

Present State a=0 a=1

State Output State Output

→a d b

b a d

c c c

d b a

Step 3 −
Next State

Present State a=0 a=1

State Output State Output

=> a d 1 b 0

b a 1 d 1

c c 0 c 0

d b 0 a 1

Mealy Machine to Moore Machine


Algorithm 5
Input − Mealy Machine

Output − Moore Machine

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

Present State a=0 a=1

Next State Output Next State Output

→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:

• Start symbol generating ε. For example, A → ε.


• A non-terminal generating two non-terminals. For example, S → AB.
• A non-terminal generating a terminal. For example, S → a.

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.

Steps for converting CFG into CNF


Step 1: Eliminate start symbol from the RHS. If the start symbol T is at the right-hand side of
any production, create a new production as:

1. S1 → S

Where S1 is the new start symbol.

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

Now, as grammar G1 contains Unit production S → B, its removal yield:

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

Hence, for the given grammar, this is the required CNF.

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:

S0-> AS|ASB| SB| S


S → AS|ASB| SB| S
A → aAS|aS|a
B → SbS|bb|aAS|aS|a

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

Also, B->bb can’t be part of CNF, removing it from grammar yields:

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

So this is the required CNF for given grammar.


Greibach Normal Form (GNF)

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:

• A start symbol generating ε. For example, S → ε.


• A non-terminal generating a terminal. For example, A → a.
• A non-terminal generating a terminal which is followed by any number of non-terminals. For
example, S → aASB.

For example:

1. G1 = {S → aAB | aB, A → aA| a, B → bB | b}


2. G2 = {S → aAB | aB, A → aA | ε, B → bB | ε}

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.

Steps for converting CFG into GNF


Step 1: Convert the grammar into CNF.

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

Step 2: If the grammar exists left recursion, eliminate it.

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.

The production rule A → SA is not in GNF, so we substitute S → XB | AA in the production


rule A → SA as:

1. S → XB | AA
2. A → a | XBA | AAA
3. B→b
4. X→a

The production rule S → XB and B → XBA is not in GNF, so we substitute X → a in the


production rule S → XB and B → XBA as:

1. S → aB | AA
2. A → a | aBA | AAA
3. B→b
4. X→a

Now we will remove left recursion (A → AAA), we get:

1. S → aB | AA
2. A → aC | aBAC
3. C → AAC | ε
4. B→b
5. X→a

Now we will remove null production C → ε, we get:

1. S → aB | AA
2. A → aC | aBAC | a | aBA
3. C → AAC | AA
4. B→b
5. X→a

The production rule S → AA is not in GNF, so we substitute A → aC | aBAC | a | aBA in


production rule S → AA as:

1. S → aB | aCA | aBACA | aA | aBAA


2. A → aC | aBAC | a | aBA
3. C → AAC
4. C → aCA | aBACA | aA | aBAA
5. B→b
6. X→a
The production rule C → AAC is not in GNF, so we substitute A → aC | aBAC | a | aBA in
production rule C → AAC as:

1. S → aB | aCA | aBACA | aA | aBAA


2. A → aC | aBAC | a | aBA
3. C → aCAC | aBACAC | aAC | aBAAC
4. C → aCA | aBACA | aA | aBAA
5. B→b
6. X→a

Hence, this is the GNF form for the grammar G.

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

Therefore, now the new production rule is,

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

Here A4A4A4 in production rule of A4 is the example of left recursion.

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

The Last step is to replace the production to the form of either

Ai → x (any single terminal symbol)

OR

Ai → xX (any single terminal followed by any number of non terminals)

So here we need to replace A2 in production rule of A1 and so on.

A1 → bA3 | bA4 | bA3A4A4 | bZA4 | bA3A4ZA4


A2 → b
A3 → a
A4 → b | bA3A4 | bZ | bA3A4Z
Z → bA4 | bA3A4A4 | bZA4 | bA3A4ZA4 | bA4Z | bA3A4A4Z | bZA4Z | bA3A4ZA4Z

The respective grammar is non in GNF form.

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.

You might also like