Title: PCmtScan FSM
1PCmtScan FSM
(with submachine calls)
PCmtScan
Initial
r InCmt
PState InCode rScanCode(fp)
PState InCmt rScanCmt(fp)
r InCode
r EndFile
r EndFile
exit
exit
2FSM-to-Program Machine
Moore State Model
Mealy State Model
e1/doExitAction(S1,e1)
State S1 /doEntry- Action(S1)
e1
State S1
NextState(S1,e1)
NextState(S1,e1)
e2/doExitAction(S1,e2)
e2
NextState(S1,e2)
NextState(S1,e2)
e3/ doExitAction(S1,e3)
e3
NextState(S1,e3)
NextState(S1,e3)
As shown by the arguments of the Action and
NextState functions above, (1) In a Moore model,
actions depend on the state only, not the event
causing a state transition. (2) In a Mealy
model, actions depend on both the state and the
event causing a state transition. We assume the
transition is instantaneous, so the action occurs
at the (simultaneous) time of exit from original
state and entry to the destination state. (3)
The NextState function depends on both the
current state AND the event in both models.
E. F. Moore George Mealy
3Mealy-to-Moore Conversion
(Requires at most one new state per transition)
Mealy State Model
e1/doExitAction(S1,e1)
State S1
NextState(S1,e1)
e2/doExitAction(S1,e2)
e3/ doExitAction(S1,e3)
NextState(S1,e2)
Add one state S(s,e) per transition, and
define Entry-Action(S11) Exit-Action(S1,e1) Ent
ry-Action(S12) Exit-Action(S1,e2) Entry-Action(S
13) Exit-Action(S1,e3) The transitions under
e0NULL are immediate.
Mealy-to- Moore model conversion
NextState(S1,e3)
Moore State Model
S1 (action deferred)
e1
S11 doEntry- Action(S11)
e0NULL
NextState(S1,e1)
e2
e3
S12 doEntry- Action(S12)
e0NULL
NextState(S1,e2)
S13 doEntry- Action(S13)
e0NULL
NextState(S1,e3)
E. F. Moore George Mealy
4Mealy model Masquerading as a Moore model
(COOL/LCP)
(Each event instance e has an EventType(e)ET.)
ET
S1 ltwait for event to arrivegt
switch(EventType(e)) case(ET1)
doExitAction(S1,ET1)
break case(ET2) doExitAction(S1,ET2)
break
case(ET3) doExitAction(S1,ET3)
break // action done at
exit from state S1 State NextState(S1,
ET)
Note Actions are done when an event arrives that
causes exit from current state
they cannot be done based on the event that
causes entry to this state because the inverse
relation PriorState(NewState, responsibleEvent)
is not necessarily single-valued, so the machine
may be non-deterministic.
5Non-equivalence of entry and exit actions
(Each event instance e has an EventType(e)ET.)
ET2/Ax2
Delayed action on exit
S1 ltwait for next event to arrivegt
switch(EventType(e))
case(ET1) doExitAction(S1,ET1)
break case(ET2)
doExitAction(S1,ET2)
break case(ET3) doExitAction(S1,ET3)
break
ET2/A12
Transitions from other states
Immediate action on entry
S2 switch(EventType(e)) case(ETx)
doEntryAction(S2,ETx)
break case(ET2) doEntryAction(S2,ET2
) break
case(ETz) doEntryAction(S2,ETz)
break ltexit when
next event arrivesgt
?
Warning Entry-Action(S2,e2) and
Exit-Action(S1,e2) are equivalent, since both are
state and event-dependent. However, other
transitions into state S2 from different sources
may be caused by the same event and require
different Actions (unless the machine is also
backward-deterministic).
6Moore-Mealy Equivalence
IFF A23 Action(S2,E3), A24 Action(S2,E4) and
A25 Action(S2,E5) are equivalent, they may all
be merged into one common S2 Exit-Action, done
just before leaving Prior-State S2. IFF A12
Action(S1,E2), A24 Action(S2,E4) and A36
Action(S3,E6) are equivalent, they may all be
merged into one common S5 Entry-Action, done just
after entering Next-State S5. Otherwise, one
extra state may be inserted to split each
transition, with a single entry and exit, after
which the above conditions are not required.
S1
S4
E2/A12
E3/A23
E4/A24
S2
S5
E5/A25
E6/A36
S3
S6
7Binary-Decision Expansions
Mealy-model with event-dependence hidden inside a
Moore model (The event-driven behavior is not
visible on single-node diagrams).
Same model with state split into binary
if/elseif../else decisions
e1
S1a
case(e1) do ExitAction(S1,e1)
NS1NextState(S1,e1)
NS1
NS?
S1 if (ee1)
doExitAction(S1,e1)
NSNextState(S1,e1) elseif
(ee2)doExitAction(S1,e2)
NSNextState(S1,e2) else
doExitAction(S1,dflt)
NSNextState(S1,dflt)
!e1
e2 !e1
S1b
case(e2) do ExitAction(S1,e2)
NS2NextState(S1,e2)
NS2
!e2
Default (!e2!e1)
Fail-exit impossible default cannot fail
S1c
default do ExitAction(S1,dflt)
NS3NextState(S1,dflt)
NS3
Actions depend on prior state, whether done at
exit or entry
8PCmtScan FSM
(with action slots)
PCmtScan(PState)
Initialize PStateinCode
Pstate r InCode? InCode rScanCode(fp)
Pstate r InCmt? InCmt rScanCmt(fp)
Pstate r EndFile? EndFile printf(\nEnd
of Input\n)
PASS
Pstate rErrordefault other
printErrMsg(PState) close(fp)
FAIL
9ScanCode FSM
ScanCode
Initial or )
(
CState Begin
CState Next
\\(\EOF
(
\(\EOF
return InCmt
EOF return EndFile
10ScanComment FSM
ScanComment
(
CState Next
CState Begin
\\)\EOF
\(\EOF
)return InCode
EOF return EndFile
11ScanComment Expansion
ScanComment
Note putSym tests for newline and adds
a PState/CState/lineNbr prefix.
require (
Next1 inChar putSym getSym
Begin1 inChar putSym getSym
Next2 inChar )
putSymfputc(\n) getSym
Return InCode
Begin2 inCharEOF printErrMsgB2
Next1 inChar EOF printErrMsgN1
Return EndFile
Begin3 \(\EOF putSym
getSym
Return EndFile
Next4 inChar \\)\EOF
putSym getSym
ImpossibleExit
ImpossibleExit