Presentation is loading. Please wait.

Presentation is loading. Please wait.

99f522FiniteStateModels.ppt - RJL 990927 1 PCmtScan FSM PCmtScan: PState InCode: r=ScanCode(fp) PState InCmt: r=ScanCmt(fp) r == InCmt r == InCode r =

Similar presentations


Presentation on theme: "99f522FiniteStateModels.ppt - RJL 990927 1 PCmtScan FSM PCmtScan: PState InCode: r=ScanCode(fp) PState InCmt: r=ScanCmt(fp) r == InCmt r == InCode r ="— Presentation transcript:

1 99f522FiniteStateModels.ppt - RJL PCmtScan FSM PCmtScan: PState InCode: r=ScanCode(fp) PState InCmt: r=ScanCmt(fp) r == InCmt r == InCode r = EndFile exit (with submachine calls) Initial

2 99f522FiniteStateModels.ppt - RJL FSM-to-Program Machine State S1: /doEntry- Action(S1) NextState(S1,e1) NextState(S1,e2) NextState(S1,e3) e1 e2 e3 State S1: NextState(S1,e1) NextState(S1,e2) NextState(S1,e3) e1/doExitAction(S1,e1) e3/ doExitAction(S1,e3) e2/doExitAction(S1,e2) Moore State Model*Mealy State Model** *: E. F. Moore **: George Mealy 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.

3 99f522FiniteStateModels.ppt - RJL Mealy-to-Moore Conversion State S1: NextState(S1,e1) NextState(S1,e2) NextState(S1,e3) e1/doExitAction(S1,e1) e3/ doExitAction(S1,e3) e2/doExitAction(S1,e2) Mealy State Model** *: E. F. Moore **: George Mealy S1: (action deferred) S11: doEntry- Action(S11) S12: doEntry- Action(S12) S13: doEntry- Action(S13) e1 e2 e3 NextState(S1,e2) NextState(S1,e1) NextState(S1,e3) e0=NULL Mealy-to- Moore model conversion: Add one state S(s,e) per transition, and define: Entry-Action(S11) = Exit-Action(S1,e1) Entry-Action(S12) = Exit-Action(S1,e2) Entry-Action(S13) = Exit-Action(S1,e3) The transitions under e0=NULL are immediate. (Requires at most one new state per transition) Moore State Model:

4 99f522FiniteStateModels.ppt - RJL Mealy model Masquerading as a Moore model (COOL/LCP) S1: 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. ET# (Each event instance e# has an EventType(e#)=ET#.)

5 99f522FiniteStateModels.ppt - RJL Non-equivalence of entry and exit actions S1: switch(EventType(e#)){ case(ET1): doExitAction(S1,ET1); break; case(ET2): doExitAction(S1,ET2); break; case(ET3): doExitAction(S1,ET3); break; } (Each event instance e# has an EventType(e#)=ET#.) S2: switch(EventType(e#)){ case(ETx): doEntryAction(S2,ETx); break; case(ET2): doEntryAction(S2,ET2); break; case(ETz): doEntryAction(S2,ETz); break; } 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). ‘Delayed’ action on exit: ‘Immediate’ action on entry: ET2/A12 ET2/Ax2 Transitions from other states ?

6 99f522FiniteStateModels.ppt - RJL Moore-Mealy Equivalence S1 S2 S3 S4 S5 S6 E2/A12 E3/A23 E4/A24 E5/A25 E6/A36 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.

7 99f522FiniteStateModels.ppt - RJL Binary-Decision Expansions: S1: { if (e==e1) {doExitAction(S1,e1); NS=NextState(S1,e1);} elseif (e==e2){doExitAction(S1,e2); NS=NextState(S1,e2);} else {doExitAction(S1,dflt); NS=NextState(S1,dflt);} } Mealy-model with event-dependence hidden inside a Moore model: (The event-driven behavior is not visible on single-node diagrams). case(e1): do ExitAction(S1,e1); NS1=NextState(S1,e1); case(e2): do ExitAction(S1,e2); NS2=NextState(S1,e2); default: do ExitAction(S1,dflt); NS3=NextState(S1,dflt); e1 e2 [&& !e1] Default [ = (!e2&&!e1)] S1a S1b S1c !e1 !e2 [Fail-exit impossible: default cannot fail] [Actions depend on prior state, whether done at exit or entry] Same model with state split into binary if/elseif../else decisions NS==? NS1 NS2 NS3

8 99f522FiniteStateModels.ppt - RJL PCmtScan FSM PCmtScan(&PState): Pstate: r == InCode? InCode r=ScanCode(fp) Pstate: r == InCmt? InCmt r=ScanCmt(fp) PASS (with action slots) Initialize: PState=inCode Pstate: r == EndFile? EndFile printf(“\nEnd of Input\n”) Pstate: r==Error|default other printErrMsg(PState); close(fp); FAIL

9 99f522FiniteStateModels.ppt - RJL ScanCode FSM ScanCode: EOF: return EndFile [^\(\EOF] ‘(‘ [^\*\(\EOF] ‘(’ ‘*’:return InCmt CState Begin CState Next Initial or ”*)”

10 99f522FiniteStateModels.ppt - RJL ScanComment FSM ScanComment: [^\(\EOF] ‘*‘ [^\*\)\EOF] ‘*’ ‘)’:return InCode CState Begin CState Next “(*” EOF: return EndFile

11 99f522FiniteStateModels.ppt - RJL ScanComment Expansion ScanComment: Begin3: [^\(\EOF] putSym; getSym; Begin1: inChar==‘* putSym; getSym Next1: inChar == ‘*’ putSym; getSym [require “(*”] Note: putSym tests for newline and adds a PState/CState/lineNbr prefix. Begin2: inChar==EOF; printErrMsgB2 Return EndFile Next2: inChar == ‘)’ putSym;fputc(‘\n’) getSym; Return InCode Next4: inChar == [^\*\)\EOF] putSym; getSym ImpossibleExit Next1: inChar == EOF printErrMsgN1 Return EndFile


Download ppt "99f522FiniteStateModels.ppt - RJL 990927 1 PCmtScan FSM PCmtScan: PState InCode: r=ScanCode(fp) PState InCmt: r=ScanCmt(fp) r == InCmt r == InCode r ="

Similar presentations


Ads by Google