Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1.

0 While Loop Code - Java

The language used in this assignment in Java. The loop used in this assignment is
While Loop. Refer to Diagram 1 below, this code is to verify the input has the substring
“aba” or “bab”. At the beginning of the while loop the user requires to enter either ‘a’ or ‘b’
as an input. If the users’ input is in the correct format, the input will add to the value
(variable). Whenever users enter input that does not equal to ‘a’ or ‘b’, the input will be
rejected and print an “Incorrect format” message to the user. Every time the users enter an
input, this program will check whether the value (variable) contains the “aba” or “bab”. If the
checking result is true, it will change the flag(variable) to true. When the users have done the
input, they are required to enter ‘e’ as an input to end the while loop. The program will check
if the input has the substring “aba” or “bab” according to the flag. If the flag is true, the input
has the substring “aba” or “bab”, else the input has not substring “aba” or “bab”.

Diagram 1- While Loop Code (Verify input has the substring “aba” or “bab”)
2.0 NFA
2.1 NFA Diagram

Diagram 2- NFA

2.2 Quintuple Table

Quintuple

q0 q0

F q5

q {q0,q1,q2,q3,q4,q5}

∑ {a,b,e}

Table 1 - Quintuple Table

In the quintuple table, ‘q0’ represents the start state, the start state in this nfa is ‘q0’.
‘F’ represents the final state, the final state of this NFA is ‘q5’. ‘q’ is the finite set of states,
there are 6 states in this NFA which are {q0, q1, q2, q3, q4, q5}. ‘∑’ is the finite input
alphabet of the NFA diagram. There are 3 input alphabets in this NFA which are {a,b,e}.
State Transition Table

Q a b e

q0 q0,q1 q0,q3 Ø

q1 Ø q2 Ø

q2 q5 Ø Ø

q3 q4 Ø Ø

q4 Ø q5 Ø

q5 q5 q5 q5

Table 2 - Transition Table

By using a transition table, it helps to understand the flow of every input. In the
transition table, it clearly states the next destination of each state in the NFA.
3.0 DFA
3.1 DFA (without simplified)

δ a b e
q0 q0q1 q0q3 qØ

qØ qØ qØ qØ

q0q1 q0q1 q0q2q3 qØ

q0q3 q0q1q4 q0q3 qØ

q0q2q3 q0q1q4q5 q0q3 qØ

q0q1q4 q0q1 q0q2q3q5 qØ

q0q1q4q5 q0q1q5 q0q2q3q5 q5

q0q2q3q5 q0q1q4q5 q0q3q5 q5

q0q3q5 q0q1q4q5 q0q3q5 q5

q0q1q5 q0q1q5 q0q2q3q5 q5

q5 q5 q5 q5
Table 3 - Transition Table for Diagram 3 - DFA (without simplified)

The transition table above shows the input flows in DFA. It helps to indicate the next
destination of each input in the DFA.
Diagram 3 - DFA (without simplified)

The diagram above (Diagram 3 - DFA (without simplified)) shows the DFA converted
from NFA (Diagram 2 - NFA).
3.2 DFA (simplified)

Diagram 4 - DFA (simplified)

Referring to the (Diagram 3 - DFA (without simplified)), the DFA is too complicated
and there are lots of redundant states. Therefore a simplified DFA (Diagram 4 - DFA
(simplified)) is generated, which removes the extra states.
In the Diagram3 - DFA(without simplified), it has more than one accept state which
are state q0q3q5, state q5, state q0q2q3q5, state q0q1q5, state q5. After the input is reached in
either one of these states, it will always go to the Accept state no matter if the input is 'a', 'b',
or 'e'. Therefore, they are totally redundant and they can merge to a single state which is
called q5. State q5 will become the only Accept state in this nfa.
4.0 Regular Expression

Step 1- Add Extra State q6

Diagram 5 -Regular Expression Step1(Add Extra State q6)

At the Diagram 5 -Regular Expression Step1(Add Extra State q6), the state q6 is
added to the diagram. This is because in Diagram 4 - DFA (simplified), the accept state q5
has a loop to itself. By adding a new state to separate the loop and the accept state will make
the elimination process more easier.
Step 2- Eliminate state qØ

Diagram 6 -Regular Expression Step2(Eliminate state qØ)

In the Diagram 6 -Regular Expression Step2(Eliminate state qØ), the state qØ is


eliminated. There are 7 remaining states in the diagram which are state q0,state q6, state q5,
state q0q1q4, state q0q3, state q0q2q3 and state q0q1.
Step 3- Eliminate state q0q1

Diagram 7 -Regular Expression Step3(Eliminate state q0q1)

In the Diagram 7 -Regular Expression Step3(Eliminate state q0q1), the state q0q1 is
eliminated. There are 6 remaining states in the diagram which are state q0, state q6, state q5,
state q0q1q4, state q0q3 and state q0q2q3.
Step 4- Eliminate state q0q2q3

Diagram 8 -Regular Expression Step4(Eliminate state q0q2q3)

In the Diagram 8 -Regular Expression Step4(Eliminate state q0q2q3), the state


q0q2q3 is eliminated. There are 5 remaining states in the diagram which are state q0,state q6,
state q5, state q0q1q4 and state q0q3.

Step 5- Eliminate state q0q3

Diagram 9 -Regular Expression Step5(Eliminate state q0q3)

In the Diagram 9 -Regular Expression Step5(Eliminate state q0q3), the state q0q3 is
eliminated. There are 4 remaining states in the diagram which are state q0,state q6, state q5
and state q0q1q4.
Step 6- Eliminate state q0q1q4

Diagram 10 -Regular Expression Step6(Eliminate state q0q1q4)

In the Diagram 10 -Regular Expression Step6(Eliminate state q0q1q4), the state


q0q1q4 is eliminated. There are 3 remaining states in the diagram which are state q0,state q6,
and state q5.

Step 7- Eliminate state q5

Diagram 11 -Regular Expression Step7(Eliminate state q5)

In the Diagram 11 -Regular Expression Step7(Eliminate state q5), the state q5 is


eliminated. There are 2 remaining states in the diagram which are start state q0 and also
accept state q6. The regular expression of Diagram 4 - DFA (simplified) states at below.

[aa*ba]+[(aa*bb+bb*a)aa*bb(b+aa*ba)]

You might also like