/* This function rerturns the fsm to be equivalent to (fsm1|fsm2) * XXX : Use with caution. This modifies fsm1, but the return value must be * stored back in fsm1 for this to be effective. * * DO NOT try something like "fsm1 | fsm1", this will ave */ FSM * FSM::operator | (FSM& rhs) { /* Create a new (empty) start and end FSM */ FSM *startFSM = new FSM(); FSM *endFSM = new FSM(); /* Concatenate one of the two fsm-s with the start and end */ this->concatenate (*endFSM); startFSM->concatenate (*this); /* Create a transition to the start state of other fsm */ rhs.concatenate (*endFSM); /* Create a transition from the end state of other fsm */ startFSM->startState.createTransition ('e', EPSILON, &(rhs.startState)); return startFSM; }
/* This function returns the fsm that accepts the repetition of strings * accepted by given fsm. */ FSM * FSM::repeat () { /* Create a new (empty) FSM. */ FSM *startFSM = new FSM(); FSM *endFSM = new FSM(); /* Loop back from the accept state of the fsm to the start state */ acceptState->createTransition ('e', EPSILON, &startState); acceptState->isFinalState = true; /* Concatenate the endFSM state with fsm */ this->concatenate (*endFSM); /* Concatenate the startFSM with 'this' fsm */ startFSM->concatenate (*this); /* Create a epsilon-transition from startFSM to endFSM */ startFSM->startState.createTransition ('e', EPSILON, startFSM->acceptState); return startFSM; }