[LC++]State Machines
Roberto Diaz
roberto at vivaldi.dhis.org
Fri Dec 21 08:04:35 UTC 2001
> More easier to do so using array of function pointers
Do you mean in plain C? in such a case I agree. but I have to say also that I
am finding that all the C++ payload usually becames very useful in the
long-term..
A scheleton could be something like this:
class State
{
public:
State (string params);
state takeSymbol (Symbol s);
private:
bool isCompletion;
};
class StateMachine
{
public:
StateMachine (const char *def, cons char *symbolsSource) {}
state run ();
bool completion (); // checks if the state machine has completed
bool parseError (); // the language passed is not parsed by this machine
private:
list <state> states; // our array of states
bool completion;
};
The definition of the a machine ca be a text file,, something like this:
state, simbol0, simbol1,......simboln completion
0 (goto state)1 10 ........ 22 no
........ this for all the states ..........
run() just calls the first entry of the array states .takeSymbol() wich in
turn returns or a state (an index) or the constant state::completion/state::noaccept (for example)..
Then just define the state machine in a text file and:
StateMachine m("path", "source");
m.run();
if (m.completion()) {}
if (m.parseError()) {}
You have a general state machine (for regular grammars)..
--
Saludos
Roberto Díaz <roberto at vivaldi.dhis.org>
PLEASE HELP SAVE faqs.org!
http://www.faqs.org/save_faqs-org.html
More information about the tuxCPProgramming
mailing list