<s>
Automata-based	B-Application
programming	I-Application
is	O
a	O
programming	O
paradigm	O
in	O
which	O
the	O
program	O
or	O
part	O
of	O
it	O
is	O
thought	O
of	O
as	O
a	O
model	O
of	O
a	O
finite-state	B-Architecture
machine	I-Architecture
(	O
FSM	O
)	O
or	O
any	O
other	O
(	O
often	O
more	O
complicated	O
)	O
formal	O
automaton	O
(	O
see	O
automata	B-Application
theory	I-Application
)	O
.	O
</s>
<s>
Finite-state	O
machine-based	O
programming	O
is	O
generally	O
the	O
same	O
,	O
but	O
,	O
formally	O
speaking	O
,	O
does	O
not	O
cover	O
all	O
possible	O
variants	O
,	O
as	O
FSM	O
stands	O
for	O
finite-state	B-Architecture
machine	I-Architecture
,	O
and	O
automata-based	B-Application
programming	I-Application
does	O
not	O
necessarily	O
employ	O
FSMs	O
in	O
the	O
strict	O
sense	O
.	O
</s>
<s>
The	O
following	O
properties	O
are	O
key	O
indicators	O
for	O
automata-based	B-Application
programming	I-Application
:	O
</s>
<s>
Any	O
communication	O
between	O
the	O
automaton	O
steps	O
is	O
only	O
possible	O
via	O
the	O
explicitly	O
noted	O
set	O
of	O
variables	O
named	O
the	O
automaton	O
state	B-Application
.	O
</s>
<s>
Between	O
any	O
two	O
steps	O
,	O
the	O
program	O
cannot	O
have	O
implicit	O
components	O
of	O
its	O
state	B-Application
,	O
such	O
as	O
local	O
variables	O
 '	O
values	O
,	O
return	O
addresses	O
,	O
the	O
current	B-General_Concept
instruction	I-General_Concept
pointer	I-General_Concept
,	O
etc	O
.	O
</s>
<s>
That	O
is	O
,	O
the	O
state	B-Application
of	O
the	O
whole	O
program	O
,	O
taken	O
at	O
any	O
two	O
moments	O
of	O
entering	O
an	O
automaton	O
step	O
,	O
can	O
only	O
differ	O
in	O
the	O
values	O
of	O
the	O
variables	O
being	O
considered	O
as	O
the	O
automaton	O
state	B-Application
.	O
</s>
<s>
Another	O
reason	O
for	O
using	O
the	O
notion	O
of	O
automata-based	B-Application
programming	I-Application
is	O
that	O
the	O
programmer	O
's	O
style	O
of	O
thinking	O
about	O
the	O
program	O
in	O
this	O
technique	O
is	O
very	O
similar	O
to	O
the	O
style	O
of	O
thinking	O
used	O
to	O
solve	O
mathematical	O
tasks	O
using	O
Turing	B-Architecture
machines	I-Architecture
,	O
Markov	O
algorithms	O
,	O
etc	O
.	O
</s>
<s>
Upon	O
encountering	O
the	O
end-of-file	B-Language
condition	O
(	O
regardless	O
of	O
the	O
stage	O
)	O
,	O
we	O
stop	O
.	O
</s>
<s>
A	O
traditional	O
program	O
in	O
C	B-Language
which	O
performs	O
the	O
above	O
task	O
could	O
look	O
like	O
this	O
:	O
</s>
<s>
The	O
same	O
task	O
can	O
be	O
solved	O
by	O
thinking	O
in	O
terms	O
of	O
finite-state	B-Architecture
machines	I-Architecture
.	O
</s>
<s>
Note	O
that	O
the	O
parsing	B-Language
of	O
a	O
line	O
has	O
three	O
stages	O
:	O
skipping	O
the	O
leading	O
whitespace	O
characters	O
,	O
printing	O
the	O
characters	O
of	O
the	O
first	O
word	O
and	O
skipping	O
the	O
trailing	O
characters	O
.	O
</s>
<s>
The	O
program	O
implements	O
the	O
work	O
of	O
a	O
finite-state	B-Architecture
machine	I-Architecture
shown	O
in	O
the	O
state	B-Application
diagram	I-Application
.	O
</s>
<s>
With	O
an	O
explicit	O
function	O
step	O
for	O
the	O
automation	B-Application
step	O
,	O
the	O
program	O
better	O
demonstrates	O
this	O
property	O
:	O
</s>
<s>
the	O
only	O
information	O
passed	O
from	O
the	O
previous	O
step	O
to	O
the	O
next	O
is	O
the	O
explicitly	O
specified	O
automaton	O
state	B-Application
.	O
</s>
<s>
A	O
finite	B-Architecture
automaton	I-Architecture
can	O
be	O
defined	O
by	O
a	O
state-transition	B-Application
table	I-Application
whose	O
rows	O
stand	O
for	O
the	O
current	O
states	O
,	O
columns	O
stand	O
for	O
the	O
inputs	O
,	O
and	O
cells	O
stand	O
for	O
the	O
next	O
states	O
and	O
actions	O
to	O
perform	O
.	O
</s>
<s>
+	O
State	B-Application
diagram	I-Application
thumb|500px|The	O
state	B-Application
diagram	I-Application
of	O
a	O
finite-state	B-Architecture
machine	I-Architecture
that	O
prints	O
the	O
first	O
word	O
of	O
each	O
line	O
of	O
an	O
input	O
stream	O
.	O
</s>
<s>
The	O
machine	O
follows	O
exactly	O
one	O
transition	O
on	O
each	O
step	O
,	O
depending	O
on	O
the	O
current	O
state	B-Application
and	O
the	O
encountered	O
character	O
.	O
</s>
<s>
With	O
an	O
explicit	O
two-dimensional	O
array	O
transitions	O
for	O
the	O
state-transition	B-Application
table	I-Application
,	O
the	O
program	O
uses	O
this	O
approach	O
:	O
</s>
<s>
If	O
the	O
implementation	O
language	O
supports	O
object-oriented	B-Language
programming	I-Language
,	O
a	O
simple	O
refactoring	O
of	O
the	O
program	O
is	O
to	O
encapsulate	B-Application
the	O
automaton	O
into	O
an	O
object	O
,	O
thus	O
hiding	O
its	O
implementation	O
details	O
.	O
</s>
<s>
The	O
program	O
in	O
C++	B-Language
using	O
object-oriented	B-Language
style	O
could	O
look	O
like	O
this	O
:	O
</s>
<s>
—	O
To	O
minimize	O
changes	O
not	O
directly	O
related	O
to	O
the	O
subject	O
of	O
the	O
article	O
,	O
the	O
input/output	B-General_Concept
getchar	O
and	O
putchar	O
functions	O
from	O
the	O
standard	O
library	O
of	O
C	B-Language
are	O
being	O
used	O
.	O
</s>
<s>
The	O
state	B-Application
design	O
pattern	O
is	O
a	O
way	O
for	O
an	O
object	O
to	O
change	O
its	O
behavior	O
at	O
runtime	O
according	O
to	O
its	O
internal	O
state	B-Application
without	O
resorting	O
to	O
large	O
conditional	O
statements	O
or	O
table	O
lookups	O
thanks	O
to	O
virtual	O
function	O
calls	O
.	O
</s>
<s>
Its	O
main	O
advantage	O
over	O
code	O
using	O
large	O
conditional	O
statements	O
is	O
that	O
state-specific	O
code	O
is	O
distributed	O
across	O
different	O
objects	O
rather	O
than	O
localized	O
in	O
a	O
monolithic	O
block	O
,	O
which	O
improves	O
maintainability	O
.	O
</s>
<s>
Its	O
main	O
advantages	O
over	O
code	O
using	O
state-transition	B-Application
tables	I-Application
are	O
that	O
virtual	O
function	O
calls	O
are	O
often	O
more	O
efficient	O
than	O
table	O
lookups	O
,	O
that	O
state-transition	O
criteria	O
are	O
more	O
explicit	O
than	O
in	O
tabular	O
format	O
,	O
and	O
that	O
it	O
is	O
easier	O
to	O
add	O
actions	O
accompanying	O
state	B-Application
transitions	I-Application
.	O
</s>
<s>
The	O
program	O
using	O
the	O
state	B-Application
design	O
pattern	O
could	O
look	O
like	O
this	O
:	O
</s>
<s>
Automata-based	B-Application
programming	I-Application
indeed	O
closely	O
matches	O
the	O
programming	O
needs	O
found	O
in	O
the	O
field	O
of	O
automation	B-Application
.	O
</s>
<s>
a	O
sequence	O
of	O
stages	O
stepping	O
according	O
to	O
input	B-General_Concept
data	I-General_Concept
(	O
from	O
captors	O
)	O
;	O
</s>
<s>
The	O
example	O
presented	O
above	O
could	O
be	O
expressed	O
according	O
to	O
this	O
view	O
like	O
in	O
the	O
following	O
pseudo-code	B-Language
( 	O
 '	O
set	O
 '	O
activates	O
a	O
logic	O
variable	O
,	O
'	O
reset	O
 '	O
inactivates	O
a	O
logic	O
variable	O
,	O
'	O
:	O
'	O
assigns	O
a	O
variable	O
,	O
and	O
'	O
=	O
'	O
tests	O
for	O
equality	O
)	O
:	O
</s>
<s>
The	O
separation	O
of	O
routines	O
expressing	O
cycle	O
progression	O
on	O
one	O
side	O
,	O
and	O
actual	O
action	O
on	O
the	O
other	O
(	O
matching	O
input	B-General_Concept
and	I-General_Concept
output	I-General_Concept
)	O
allows	O
clearer	O
and	O
simpler	O
code	O
.	O
</s>
<s>
In	O
the	O
field	O
of	O
automation	B-Application
,	O
stepping	O
from	O
step	O
to	O
step	O
depends	O
on	O
input	B-General_Concept
data	I-General_Concept
coming	O
from	O
the	O
machine	O
itself	O
.	O
</s>
<s>
Like	O
in	O
GUI	B-Application
programming	O
,	O
changes	O
in	O
the	O
machine	O
state	B-Application
can	O
thus	O
be	O
considered	O
as	O
events	O
causing	O
the	O
passage	O
from	O
a	O
state	B-Application
to	O
another	O
,	O
until	O
the	O
final	O
one	O
is	O
reached	O
.	O
</s>
<s>
Automata-based	B-Application
programming	I-Application
is	O
widely	O
used	O
in	O
lexical	B-Application
and	O
syntactic	B-Language
analyses	I-Language
.	O
</s>
<s>
Besides	O
that	O
,	O
thinking	O
in	O
terms	O
of	O
automata	O
(	O
that	O
is	O
,	O
breaking	O
the	O
execution	O
process	O
down	O
to	O
automaton	O
steps	O
and	O
passing	O
information	O
from	O
step	O
to	O
step	O
through	O
the	O
explicit	O
automaton	O
state	B-Application
)	O
is	O
necessary	O
for	O
event-driven	B-Application
programming	I-Application
as	O
the	O
only	O
alternative	O
to	O
using	O
parallel	O
processes	O
or	O
threads	O
.	O
</s>
<s>
The	O
notions	O
of	O
states	O
and	O
state	B-Architecture
machines	I-Architecture
are	O
often	O
used	O
in	O
the	O
field	O
of	O
formal	B-Application
specification	I-Application
.	O
</s>
<s>
For	O
instance	O
,	O
UML-based	O
software	O
architecture	O
development	O
uses	O
state	B-Application
diagrams	I-Application
to	O
specify	O
the	O
behaviour	O
of	O
the	O
program	O
.	O
</s>
<s>
Also	O
various	O
communication	O
protocols	O
are	O
often	O
specified	O
using	O
the	O
explicit	O
notion	O
of	O
state	B-Application
(	O
e.g.	O
,	O
)	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
execution	O
of	O
a	O
program	O
written	O
in	O
the	O
Refal	B-Language
language	O
is	O
described	O
as	O
a	O
sequence	O
of	O
steps	O
of	O
a	O
so-called	O
abstract	O
Refal	B-Language
machine	O
;	O
the	O
state	B-Application
of	O
the	O
machine	O
is	O
a	O
view	O
(	O
an	O
arbitrary	O
Refal	B-Language
expression	O
without	O
variables	O
)	O
.	O
</s>
<s>
Continuations	O
in	O
the	O
Scheme	B-Language
language	I-Language
require	O
thinking	O
in	O
terms	O
of	O
steps	O
and	O
states	O
,	O
although	O
Scheme	B-Language
itself	O
is	O
in	O
no	O
way	O
automata-related	O
(	O
it	O
is	O
recursive	O
)	O
.	O
</s>
<s>
To	O
make	O
it	O
possible	O
for	O
the	O
call/cc	O
feature	O
to	O
work	O
,	O
implementation	O
needs	O
to	O
be	O
able	O
to	O
catch	O
a	O
whole	O
state	B-Application
of	O
the	O
executing	O
program	O
,	O
which	O
is	O
only	O
possible	O
when	O
there	O
is	O
no	O
implicit	O
part	O
in	O
the	O
state	B-Application
.	O
</s>
<s>
Such	O
a	O
caught	O
state	B-Application
is	O
the	O
very	O
thing	O
called	O
continuation	O
,	O
and	O
it	O
can	O
be	O
considered	O
as	O
the	O
state	B-Application
of	O
a	O
(	O
relatively	O
complicated	O
)	O
automaton	O
.	O
</s>
<s>
Automata-based	O
techniques	O
were	O
used	O
widely	O
in	O
the	O
domains	O
where	O
there	O
are	O
algorithms	O
based	O
on	O
automata	B-Application
theory	I-Application
,	O
such	O
as	O
formal	O
language	O
analyses	O
.	O
</s>
<s>
One	O
of	O
the	O
earliest	O
mentions	O
of	O
automata-based	B-Application
programming	I-Application
as	O
a	O
general	O
technique	O
is	O
found	O
in	O
the	O
paper	O
by	O
Peter	O
Naur	O
,	O
1963	O
.	O
</s>
<s>
The	O
author	O
calls	O
the	O
technique	O
Turing	B-Architecture
machine	I-Architecture
approach	O
,	O
however	O
no	O
real	O
Turing	B-Architecture
machine	I-Architecture
is	O
given	O
in	O
the	O
paper	O
;	O
instead	O
,	O
the	O
technique	O
based	O
on	O
steps	O
and	O
states	O
is	O
described	O
.	O
</s>
<s>
The	O
notion	O
of	O
state	B-Application
is	O
not	O
exclusive	O
property	O
of	O
automata-based	B-Application
programming	I-Application
.	O
</s>
<s>
Generally	O
speaking	O
,	O
state	B-Application
(	O
or	O
program	O
state	B-Application
)	O
appears	O
during	O
execution	O
of	O
any	O
computer	B-Application
program	I-Application
,	O
as	O
a	O
combination	O
of	O
all	O
information	O
that	O
can	O
change	O
during	O
the	O
execution	O
.	O
</s>
<s>
current	O
value	O
of	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
.	O
</s>
<s>
These	O
can	O
be	O
divided	O
to	O
the	O
explicit	O
part	O
(	O
such	O
as	O
values	O
stored	O
in	O
variables	O
)	O
and	O
the	O
implicit	O
part	O
(	O
return	O
addresses	O
and	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
)	O
.	O
</s>
<s>
Having	O
said	O
this	O
,	O
an	O
automata-based	O
program	O
can	O
be	O
considered	O
as	O
a	O
special	O
case	O
of	O
an	O
imperative	B-Application
program	I-Application
,	O
in	O
which	O
implicit	O
part	O
of	O
the	O
state	B-Application
is	O
minimized	O
.	O
</s>
<s>
The	O
state	B-Application
of	O
the	O
whole	O
program	O
taken	O
at	O
the	O
two	O
distinct	O
moments	O
of	O
entering	O
the	O
step	O
code	O
section	O
can	O
differ	O
in	O
the	O
automaton	O
state	B-Application
only	O
.	O
</s>
<s>
In	O
the	O
theory	O
of	O
object-oriented	B-Language
programming	I-Language
,	O
an	O
object	O
is	O
said	O
to	O
have	O
an	O
internal	O
state	B-Application
and	O
is	O
capable	O
of	O
receiving	O
messages	O
,	O
responding	O
to	O
them	O
,	O
sending	O
messages	O
to	O
other	O
objects	O
and	O
changing	O
its	O
internal	O
state	B-Application
during	O
message	O
handling	O
.	O
</s>
<s>
Thus	O
,	O
on	O
the	O
one	O
hand	O
,	O
objects	O
from	O
object-oriented	B-Language
programming	I-Language
can	O
be	O
considered	O
as	O
automata	O
(	O
or	O
models	O
of	O
automata	O
)	O
whose	O
state	B-Application
is	O
the	O
combination	O
of	O
private	O
fields	O
,	O
and	O
one	O
or	O
more	O
methods	O
are	O
considered	O
to	O
be	O
the	O
step	O
.	O
</s>
<s>
When	O
the	O
automata-based	O
approach	O
is	O
used	O
within	O
an	O
object-oriented	B-Language
language	I-Language
,	O
an	O
automaton	O
model	O
is	O
usually	O
implemented	O
by	O
a	O
class	O
,	O
the	O
state	B-Application
is	O
represented	O
with	O
private	O
fields	O
of	O
the	O
class	O
,	O
and	O
the	O
step	O
is	O
implemented	O
as	O
a	O
method	O
;	O
such	O
a	O
method	O
is	O
usually	O
the	O
only	O
non-constant	O
public	O
method	O
of	O
the	O
class	O
(	O
besides	O
constructors	O
and	O
destructors	O
)	O
.	O
</s>
<s>
Other	O
public	O
methods	O
could	O
query	O
the	O
state	B-Application
but	O
do	O
n't	O
change	O
it	O
.	O
</s>
<s>
All	O
the	O
secondary	O
methods	O
(	O
such	O
as	O
particular	O
state	B-Application
handlers	O
)	O
are	O
usually	O
hidden	O
within	O
the	O
private	O
part	O
of	O
the	O
class	O
.	O
</s>
