links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
How to create a state-machine with SmaCC
Last updated at 12:47 am UTC on 17 January 2006
And here comes a (I guess in most cases overkill) solution for creating state-machines in Squeak: Use SmaCC.

When I remember correctly (and it seems like, as I double-checked here,
http://www.lib.uchicago.edu/keith/tcl-course/topics/regexp.html) finite state machines are equivalent with regular expressions.

And with the SmaCC scanner generator we can produce finite state automatons, which scan some given regular expression. But as I don't know (and think), that one can annotate any side-effects on the scanner of SmaCC, I tried it with the parser, and though it might not produce the most efficient automaton, as it is built for context-free grammars and not only regular expressions, it works quite well for that.

Here is what I did to "program" the "I go shopping" example FSA with SmaCC from http://www.mk.dmu.ac.uk/~gperkins/Smalltalk/State/


<arriving> : arriving(\s)+;
<shopping> : shopping(\s)+;
<waiting> : waiting(\s)+;
<serving> : serving(\s)+;
<finished> : finished(\s);


Arriving: <arriving> Arriving {Transcript show: 'Arriving';cr};
Arriving: <shopping> Shopping {Transcript show: 'Starting to buy things';cr};
Shopping: <shopping> Shopping {Transcript show: 'Still hanging around in the shop';cr};
Shopping: <waiting> Waiting {Transcript show: 'Standing at the queue';cr};
Waiting: <waiting> Waiting {Transcript show: 'Still waiting';cr};
Waiting: <serving> Serving {Transcript show: 'Being served finally';cr};
Shopping: <serving> Serving {Transcript show: 'Being served directly. Wonderful day.';cr};
Serving: <serving> Serving {Transcript show: 'Still being served';cr};
Serving: <finished> Finished {Transcript show: 'Having paid finally';cr};
Finished: <finished> {Transcript show: 'Is there anything else then going shopping?';cr};

After compiling it, I used the following test on the




And the Transcript gave me:

Is there anything else then going shopping?
Having paid finally
Still being served
Being served finally
Standing at the queue
Starting to buy things

...It printed it the other way round, can anybody explain this to me? But basically it works well with SmaCC

Answer: (from the Squeak mailing list)
I think that's because SmaCC generates a LR (Bottom-Up) Parser.

Marcus Denker; marcus at ira.uka.de