0% found this document useful (0 votes)
30 views

Automata Theory Lecture 4 and 5 Introduction To Finite State Machines

Uploaded by

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

Automata Theory Lecture 4 and 5 Introduction To Finite State Machines

Uploaded by

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

•Introduction To

Finite State
Machines
The theory of computation begins with a question: What is a computer? It is
perhaps a silly question, as everyone knows that this “thing I type on” is a
computer.

But these real computers are quite complicated-too much so to allow us to


set up a manageable mathematical theory of them directly.

Instead we use an idealized computer called a computational model.

As with any model in science, a computational model may be accurate in


some ways but perhaps not in others.

Thus we will use several different computational models, depending on the


features we want to focus on.

We begin with the simplest model, called the finite state machine or finite
automaton.
• FINITE AUTOMATA
Finite automata are good models for computers with an extremely limited amount of
memory.

What can a computer do with such a small memory? Many useful things!

In fact, we interact with such computers all the time, as they lie at the heart of
various electromechanical devices.

The controller for an automatic door is one example of such a device. Often found at
supermarket entrances and exits, automatic doors swing open when sensing that a
person is approaching.

An automatic door has a pad in front to detect the presence of a person about to
walk through the doorway.

Another pad is located to the rear of the doorway so that the controller can hold the
door open long enough for the person to pass all the way through and also so that the
door does not strike someone standing behind it as it opens. This configuration is
shown in the following figure.
The controller is in either of two states: "OPEN"
or "CLOSED," representing the corresponding
condition of the door.

As shown in the following figures, there are four


possible input conditions:

"FRONT" (meaning that a person is standing on


Top view of an automatic door the pad in front of the doorway),

"REAR" (meaning that a person is standing on the


pad to the rear of the doorway),

"BOTH" (meaning that people are standing on both


pads), and

State diagram for automatic "NEITHER" (meaning that no one is standing on


door controller either pad).
State transition table for automatic door controller

The controller moves from state to state, depending on the input it


receives. When in the CLOSED state and receiving input NEITHER or
REAR, it remains in the CLOSED state.

In addition, if the input BOTH is received, it stays CLOSED because


opening the door risks knocking someone over on the rear pad.

But if the input FRONT arrives, it moves to the OPEN state. In the
OPEN state, if input FRONT, REAR, or BOTH is received, it remains in
OPEN. If input NEITHER arrives, it returns to CLOSED.
• For example, a controller might start in state CLOSED and receive the series of
input signals FRONT, REAR, NEITHER, FRONT, BOTH, NEITHER, REAR, and
NEITHER.

• It then would go through the series of states CLOSED (starting), OPEN, OPEN,
CLOSED, OPEN, OPEN, CLOSED, CLOSED, and CLOSED

• This controller is a computer that has just a single bit of memory, capable of
recording which of the two states the controller is in.

• Other common devices have controllers with somewhat larger memories. In an


elevator controller a state may represent the floor the elevator is on and the
inputs might be the signals received from the buttons.

• This computer might need several bits to keep track of this information.
Controllers for various household appliances such as dishwashers and electronic
thermostats, as well as parts of digital watches and calculators, are additional
examples of computers with limited memories.
• Finite automata and their probabilistic counterpart Markov chains are useful
tools when we are attempting to recognize patterns in data.

• These devices are used in speech processing and in optical character


recognition.

• Markov chains have even been used to model and predict price changes in
financial markets.

• We will now take a closer look at finite automata from a mathematical


perspective. We will develop a precise definition of a finite automaton,
terminology for describing and manipulating finite automata, and theoretical
results that describe their power and limitations.

• Besides giving you a clearer understanding of what finite automata are and
what they can and cannot do, this theoretical development will allow you to
practice and become more comfortable with mathematical definitions,
• The figure above is called the state diagram of Ml. It has three states, labeled ql,
q2, and q3. The start state, q1, is indicated by the arrow pointing at it from
nowhere.

• The accept state, q2, is the one with a double circle.

• The arrows going from one state to another are called transitions.

• When this automaton receives an input string such as 1101, it processes that string
and produces an output. The output is either accept or reject.

• We will consider only this yes/no type of output for now to keep things simple. The
processing begins in Ml's start state. The automaton receives the symbols from the
input string one by one from left to right. After reading each symbol, Ml moves from
one state to another along the transition that has that symbol as its label.

• When it reads the last symbol, Ml produces its output. The output is accept if Ml is
now in an accept state and reject if it is not.
• For example, when we feed the input string 1101 to the
machine M1 in Figure above, the processing proceeds as
follows.

1. Start in state q1.


2. Read 1, follow transition from q1 to q2.
3. Read 1, follow transition from q2 to q2.
4. Read 0, follow transition from q2 to q3.
5. Read 1, follow transition from q3 to q2-
6. Accept because Ml is in an accept state q2 at the end
of the input.
• Quiz

Test the automaton in the figure with


0101010101 and conclude
• Although state diagrams are easier to grasp intuitively, we need the formal
definition, too, for two specific reasons.

• First, a formal definition is precise. It resolves any uncertainties about


what is allowed in a finite automaton.

• If you were uncertain about whether finite automata were allowed to have
0 accept states or whether they must have exactly one transition exiting
every state for each possible input symbol, you could consult the formal
definition and verify that the answer is yes in both cases.

• Second, a formal definition provides notation. Good notation helps you think
and express your thoughts clearly.
• The language of a formal definition is somewhat arcane, having some similarity to
the language of a legal document. Both need to be precise, and every detail must
be spelled out.

• A finite automaton has several parts.

• It has a set of states and rules for going from one state to another, depending
on the input symbol.
• It has an input alphabet that indicates the allowed input symbols.
• It has a start state and a set of accept states.

• The formal definition says that a finite automaton is a list of those five objects:
set of states, input alphabet, rules for moving, start state, and accept states.

• In mathematical language a list of five elements is often called a 5-tuple. Hence


we define a finite automaton to be a 5-tuple consisting of these five parts.
• We use something called a transition function, frequently denoted δ, to
define the rules for moving.

• If the finite automaton has an arrow from a state “x” to a state “y”
labeled with the input symbol 1, that means that, if the automaton is in
state “x” when it reads a 1, it then moves to state “y”.

• We can indicate the same thing with the transition function by saying that
δ(x, 1) = y. This notation is a kind of mathematical shorthand. Putting it
all together we arrive at the formal definition of finite automata.
• If A is the set of all strings that machine M accepts, we say that A is the
language of machine M and write L(M) = A. We say that M recognizes A or
that M accepts A.

• Because the term accept has different meanings when we refer to machines
accepting strings and machines accepting languages, we prefer the term
recognize for languages in order to avoid confusion.

• A machine may accept several strings, but it always recognizes only one
language.

• If the machine accepts no strings, it still recognizes one language namely,


the empty language ϕ (λ)
• In our example, let
• A = {w‫ ׀‬w contains at least one 1 and an even number of Os follow the last 1}.
• Then L(Ml) = A, or equivalently, M1 recognizes A.
• Students:

•This class don tire me


• Me sef don tire to dey teach tired
student
•Test 1
Show that the figure above accepts strings a, b, aa, bb, bab
aaaabbba, bbbaaab
• FORMAL DEFINITION OF COMPUTATION
• So far we have described finite automata informally, using state
diagrams, and with a formal definition, as a 5-tuple.

• The informal description is easier to grasp at first, but the formal


definition is useful for making the notion precise, resolving any
ambiguities that may have occurred in the informal description.

• Next we do the same for a finite automaton's computation. We already


have an informal idea of the way it computes, and we now formalize it
mathematically.

• Let M = (Ԛ, Ʃ ,δ, qo, F) be a finite automaton and let w = W IW 2 ... wn be


a string where each wi is a member of the alphabet Ʃ. Then M accepts w
if a sequence of states ro, r1 ,… , rn,, in Q exists with three conditions:
1. ro = qo,
2. δ(ri, wi+1) = ri+l, for i = 0, ... , n - 1, and
3. rn, ℇ F.

Condition 1 says that the machine starts in the start state.

Condition 2 says that the machine goes from state to state according to
the transition function.

Condition 3 says that the machine accepts its input if it ends up in an


accept state.

We say that M recognizes language A if A = {wI M accepts w}.


• DEFINITION
A language is called a regular language if some finite automaton recognizes it.
• Finite State Automata/Machines
A finite state automata is a machine that is capable of moving/ transiting
from one state to another.

Upon the receipt of an input symbol from a set of alphabets of a language.

It starts at the initial state and transit from state to state until it gets
to the final state or the accepting state

A finite state machine consists of the following:


 A set “I” called the input alphabet;
A set “S” whose elements are called states;
A function “T”:S × I → S called the transition function;
 A particular element, s0 ∈ S called the initial state;
The functioning of the machine is as follows:
• The machine starts in the initial state s0 .
• The input is a string of characters from the input alphabet
which are read one at a time (from the left).

• At each stage the machine is in some state s ∈ S.

• If the machine is in state s, and the next input character is c


∈ I, the machine moves to state T(s, c) and awaits the next
input character.

• The process continues in this way until all the input characters
have been processed.
• A finite state machine can be represented in two forms
State table
State graph (state transition diagram)

State table transition diagram

Present Next State Output 1


1

State Present Input 0


S0 /0 S1 /1
0 1
s0 s1 s0 0 1
0

s1 s2 s1 1
S2 /1
s2 s2 s0 1 0
• Example
• Given the following
I = {1, 2}; S = {A, B, C, D}; s0 = A.
• T is given by the table:

• Note that the arrow is used to represent the initial state


• Suppose the input to this machine is 1121221121.

• The successive transitions of the machine are:


• Mealy Machines
• To be of any use in computation a finite state machine must have some form of
output

• An obvious way is to have the machine print it

• We need an output alphabet, O, that may or may not be the same as the input
alphabet. Every time the machine reads a character from the input, it outputs a
character as well as changing its state.

• To describe the output of such a machine we can have an additional function that
assigns to each combination of state and input character, an output character.

• In other words we have a function P:S × I → O. When the machine is in state s


and reads the character c, the output is P(s, c).

• A machine of this type is known as a Mealy Machine.


So if the input is 111001111 the output will be
001110011 as shown below
• Moore Machines

• An alternative arrangement for output is to have the machine print a certain


output character as it enters a state.

• These machines are called Moore machines.

• The output is attached to the states rather than to the transitions.

• Formally, the output of a Moore machine is described by a function P: S →


O, where as before, O is the output alphabet.

• The following Moore machine is also a two-step delay machine, behaving


equivalently to the Mealy machine in example above.

• I = O ={0, 1}; S = {A, B, C, D} with s0 = A.



• Look at the state table below and try to pass the following set of
inputs 111001111
• Finite State Acceptors
• The focus on finite state machines will be as acceptors for
languages.

• Given a language, on a given alphabet, it is important to find a


machine that decides whether or not a given string belongs to that
language (lexical analysis)

• Here the output is a simple “YES” or “NO”.

• We’re not interested in partial results.

• We wait for the machine to read the entire input string and only
then are we interested in the answer. (Compiler construction
phases)
• In a finite state acceptor (FSA) the output is determined by
selecting a subset of states as the accepting states or final state.

• If the machine ends in an accepting state, after having read the


entire string, we say that the string has been accepted by the
machine; Otherwise it’s rejected.

• The slight modification to the way we present it as a finite state


acceptor on the table is to replace the 1's in the final column by *'s
and the 0's by blanks, so that the accepting states are those with an
asterisk against them

• So Writing the Moore machine in example above as a finite state


acceptor we have:
The main focus will be on finite state acceptors.

To every FSA there is a language, the set of all


input strings that are accepted by the machine.

We say that an FSA accepts a language, L, if it


accepts every string in L and rejects all others.

An important question is “can every language be


accepted by an FSA?”

The answer is “no”. Only for certain languages


does there exist a corresponding FSA.
• State Diagrams
• It is often convenient to depict a finite state machine pictorially.

• This is done by drawing a small circle for each state, with the name
of the state inside, and drawing arrows connecting the states to
depict the transitions.

• The input character that gives rise to a transition is written beside


the corresponding arrow.

• We indicate the initial state by drawing a short arrow pointing to it.


• Output is depicted in the following ways:

• Mealy Machines:

• The output is written beside the input character for each transition
as follows:

• This indicates that if the machine is in state A and receives input “1”
then it outputs a “0” and moves to state G.
• Moore Machines:
• The output is attached to the name of the state as follows:

This indicates that if the machine is in state A and receives input 1, it


moves to state G and outputs a 0.

• FSA's:
• The accepting states are distinguished by drawing a double ring around
them as follows:

• This indicates that if the machine is in state A and receives input 1, it


moves to state G. If that was the last character of the input string, the
string would be accepted.
The following table and diagram represent the same Mealy machine.
The following table and diagram represent the same Moore
machine.
• The following table and diagram represent the same FSA.
• APPLICATION OF DFA IN LEXICAL ANALYSIS
• The DFA can be implemented as a table-driven or hand-coded
scanner.

• Table-driven scanners are usually generated by some specialized


tools like Flex.

• Because with DFA each input uniquely determines the state to


navigate to and it never backtracks the DFA is the preferred
model for the implementation in generated scanners.

• The usual flow consists of converting a lexical specification


(either regular grammar or regular expression) into NFA and
then NFA is converted into DFA.
• Here is the picture that demonstrates the workflow:
• However, most commercial and open-source compilers use hand-
coded scanners.

• Such scanner is faster than a generated scanner because during


implementation some overhead that is necessary in a generated
scanner can be removed.

• When writing a hand-coded scanner, usually there’s no need for


the explicit conversion of the grammar/regex to a DFA since it’s
possible to implement the scanning algorithm manually directly
from the lexical specification.

• Such hand-coded implementation naturally ends up working like a


DFA.
• Both table-driven and hand-coded scanners function in
the similar way by emulating the DFA.

• They repeatedly read the next character in the input


and emulate the DFA transition caused by that
character.

• After reading an input the scanner checks whether


there are possible transitions using the input.

• If a transition is found it is followed and the scanner


ends up in the new state. If there are no transitions
available the scanner checks if the current state is an
• If that’s the case the scanner recognizes the word and returns
a lexeme and its syntactic category to the calling procedure.

• Otherwise the scanner determines whether or not it has passed


through an accepting state on the way to current state.

• If an accepting state has been encountered, the scanner rolls


back its internal state (current character position) to that
point and reports success.

• Otherwise it reports an error.


• Thank you for listening

You might also like