Presentation is loading. Please wait.

Presentation is loading. Please wait.

PCmtScan FSM (with submachine calls) PCmtScan: Initial r == InCmt

Similar presentations


Presentation on theme: "PCmtScan FSM (with submachine calls) PCmtScan: Initial r == InCmt"— Presentation transcript:

1 PCmtScan FSM (with submachine calls) PCmtScan: Initial r == InCmt
PState InCode: r=ScanCode(fp) PState InCmt: r=ScanCmt(fp) r == InCode PState = InCode; while (1) { //exit iff (PState == EndFile || PState == Error) // fprintf(stdout, "%cPState = %d%c",LF,PState,LF); switch((int)PState){ case InCode:{PState = ScanCode(fp); break;} case InCmt: {PState = ScanComment(fp); break;}; case EndFile:{fprintf(stdout, "\nEnd of Input; PState = %d;\n", PState); exit(1); } case Error: default:{ PState = Error; fprintf(stderr, "\nError:Invalid PState = %d\n", PState); }; }; // end switch(PState) }; // end while r = EndFile r = EndFile exit exit

2 FSM-to-Program Machine
Moore State Model* Mealy State Model** State S1: /doEntry- Action(S1) e1/doExitAction(S1,e1) e1 State S1: NextState(S1,e1) NextState(S1,e1) e2/doExitAction(S1,e2) e2 NextState(S1,e2) e3/ doExitAction(S1,e3) NextState(S1,e2) 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

3 Mealy-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) Entry-Action(S12) = Exit-Action(S1,e2) Entry-Action(S13) = Exit-Action(S1,e3) The transitions under e0=NULL are immediate. Mealy-to- Moore model conversion: NextState(S1,e3) Moore State Model: S1: (action deferred) e1 S11: doEntry- Action(S11) e0=NULL NextState(S1,e1) e2 e3 S12: doEntry- Action(S12) e0=NULL NextState(S1,e2) S13: doEntry- Action(S13) e0=NULL NextState(S1,e3) *: E. F. Moore **: George Mealy

4 Mealy model Masquerading as a Moore model (COOL/LCP)
(Each event instance e# has an EventType(e#)=ET#.) S1: <wait for event to arrive> switch(EventType(e#)){ case(ET1): doExitAction(S1,ET1); break; case(ET2): doExitAction(S1,ET2); case(ET3): doExitAction(S1,ET3); } // action done at exit from state S1 State = NextState(S1, ET#); 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.

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

6 Moore-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

7 Binary-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); NS1=NextState(S1,e1); NS1 NS==? 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);} } !e1 e2 [&& !e1] S1b case(e2): do ExitAction(S1,e2); NS2=NextState(S1,e2); NS2 !e2 [Fail-exit impossible: default cannot fail] Default [ = (!e2&&!e1)] S1c default: do ExitAction(S1,dflt); NS3=NextState(S1,dflt); [Actions depend on prior state, whether done at exit or entry] NS3

8 PCmtScan FSM (with action slots) PCmtScan(&PState): Initialize:
PState=inCode Pstate: r == InCode? InCode r=ScanCode(fp) Pstate: r == InCmt? InCmt r=ScanCmt(fp) PState = InCode; while (1) { //exit iff (PState == EndFile || PState == Error) // fprintf(stdout, "%cPState = %d%c",LF,PState,LF); switch((int)PState){ case InCode:{PState = ScanCode(fp); break;} case InCmt: {PState = ScanComment(fp); break;}; case EndFile:{fprintf(stdout, "\nEnd of Input; PState = %d;\n", PState); exit(1); } case Error: default:{ PState = Error; fprintf(stderr, "\nError:Invalid PState = %d\n", PState); }; }; // end switch(PState) }; // end while Pstate: r == EndFile? EndFile printf(“\nEnd of Input\n”) PASS Pstate: r==Error|default other printErrMsg(PState); close(fp); FAIL

9 ScanCode FSM ScanCode: Initial or ”*)” ‘(‘ CState Begin CState Next
[^\*\(\EOF] ‘(’ CState = Begin; while (1){ getSym; if feof(fp) return EndFile; switch(CState){ case Begin: { switch(inChar) { case '(' : { putSym; CState = Next; break; }; case '\n': { putSym; break;} case EOF: { return EndFile;} default: { // [^\(\n\EOF] putSym; break; } }; // end switch(inChar) }; // end case Begin case Next: {../*similar*/...} [^\(\EOF] ‘*’:return InCmt EOF: return EndFile

10 ScanComment FSM ScanComment: “(*” ‘*‘ CState CState Next Begin
[^\*\)\EOF] ‘*’ case Begin: {…/*similar to case Next*/..} case Next:{ getSym; switch (inChar){ case '*': {putSym; break;}; case ')': { putSym; fprintf(stdout,"%c",LF); // LineFeed return InCode; } case EOF: {return EndFile;} default: { // [^\)\*\EOF] CState = Begin; break; } // end default case } // end switch(inChar) }; // end case Next default: {…error if not on above state..) [^\(\EOF] ‘)’:return InCode EOF: return EndFile

11 ScanComment Expansion
Note: putSym tests for newline and adds a PState/CState/lineNbr prefix. [require “(*”] Next1: inChar == ‘*’ putSym; getSym Begin1: inChar==‘* putSym; getSym Next2: inChar == ‘)’ putSym;fputc(‘\n’) getSym; Return InCode Begin2: inChar==EOF; printErrMsgB2 case Begin: {…/*similar to case Next*/..} case Next:{ getSym; switch (inChar){ case '*': {putSym; break;}; case ')': { putSym; fprintf(stdout,"%c",LF); // LineFeed return InCode; } case EOF: {return EndFile;} default: { // [^\)\*\EOF] CState = Begin; break; } // end default case } // end switch(inChar) }; // end case Next default: {…error if not on above state..) Next1: inChar == EOF printErrMsgN1 Begin3: [^\(\EOF] putSym; getSym; Return EndFile Return EndFile Next4: inChar == [^\*\)\EOF] putSym; getSym ImpossibleExit ImpossibleExit


Download ppt "PCmtScan FSM (with submachine calls) PCmtScan: Initial r == InCmt"

Similar presentations


Ads by Google