You are on page 1of 12

a

State1 a State2

Figure1: Representation of a+

since a+ indicates at least one occurrence of a


from a current state state1, the fsm has to transit
to another state and consume as many 'a's as possible
and on occurrence of any other symbol can traverse to
the next state
a

a State2 State3
State1

a'

Figure2: Representation of a*

since a* indicates zero or one or more occurrence of “a”


the fsm will have a bypass path “a'” which is used when any
other symbol is consumed instead of a letter a. if there
are letters a being consumed they move through the
intermediate State2 as shown.
a a'
State1 State2 State3

a'

Figure3: Representation of a?

since a? indicates zero or one occurrence of a


the fsm reaches its final state State3 either via the bypass
path which indicates a zero occurrence of “a” OR via
the State2 which shall only consume a single occurrence of “a”
Problem statement : To convert regex 1+0(10)+11 into a statemachine

Solution Approach:

● Let's denote the inner 10 as an X, so the regex is 1+0X+11


● We split it into smaller parts of the FSM
● First part as 1+
● Followed by 0
● Followed by X+
● Followed by two consecutive 1s
1. From the atomic regex representations mentioned in
previous slides, 1+ is represented as in Figure4
1

State1 1 State2

Figure4: Representation of 1+

2. To consume a 0, the representation of the state diagram


would be as in Figure5

0
State3

Figure5: Representation of 0

3. To consume a (X)+, the representation of the state


diagram would be in Figure6
X

StateX X StateY

Figure6: Representation of X+
4. To consume two consecutive 1s, the representation of the
state diagram would be as in Figure7

1 1
State9 State10

Figure6: Representation of 11
LEVEL 2 Combine the individual state machines graphs
(1. and 2. for now!)

State1 1 State2 0 State3

In case of binary stream sequence match, the only possible


values of the symbols are 0 and 1. Hence there can be only
two path in the deterministic finite automaton. A path for
consuming the 0 symbol and another for consuming the 1
symbol.

Any path that is not defined above, for instance the 0 path
from State1 leads to the initial state. So State1's 0 path
leads to itself.

0
1

State1 1 State2 0 State3

Graph1
Expanding 3. Consumption of the symbol X where X is 10

StateX X StateY

10

StateX 10 StateY

StateX4 1

0 1

1 0 StateX3
StateX1 StateX2 0

Graph2
Joining Graph1 and Graph2

0
1

State1 1 State2 0 State3

StateX4 1

0 1

1 0 StateX3 0
StateX1 StateX2

Note the transition from State3 to StateX1 is on


an empty symbol which means reaching State3 is
same as reaching StateX1
Simplifying the Graph by removing the empty symbol

0
1

State1 1 State2 0

StateX4 1

0 1
0 1

1 0 StateX3 0
StateX1 StateX2

StateX1 s 0 path shall transit to State1 since an


occurrence of 0 breaks the path
StateX2 s 1 path shall transit to State2 since an
occurrence of 1 breaks the path but might be a
starting of another valid sequence of 1+
Adding the last graph to this final graph we get the
final finite state machine

0
1

State1 1 State2 0

1
State9 State10

0 1
0 1

State 1 State 0 State 0


X1 X2 X3

Note that StateX4 has become State9, because StateX4


already detects a 1 following a 10, hence is
equivalent to the first state of the 4. and so can be
merged with State9
Re-organized State machine

State1
0 State7
Out<=1
Out<=0
1
1

State6
1 State2
Out<=0 0
Out<=0

0
1 0 1

0
State3 State4 0 State5
Out<=0 Out<=0
1 Out<=0

You might also like