[Q][DOCS] State machine implementation in Smalltalk / Squeak -->
SmaCC
Markus Gaelli
gaelli at emergent.de
Wed Feb 19 18:32:53 UTC 2003
And here comes a (I guess in most cases overkill)-solution:
Use SmaCC to create the state-machine:
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 scans 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/
Scanner-Page:
<arriving> : arriving(\s)+;
<shopping> : shopping(\s)+;
<waiting> : waiting(\s)+;
<serving> : serving(\s)+;
<finished> : finished(\s)*;
Parser-Page:
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
Test-Page
arriving
arriving
shopping
waiting
serving
serving
finished
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
Arriving
Arriving
--> It printed it the other way round, can anybody explain this to me?
But basically it works well with SmaCC
Markus
Am Mittwoch, 19.02.03 um 08:11 Uhr schrieb Hannes Hirzel:
> Hi
> as Eric Merritt was asking how to implement a state machine in Squeak
> here is my question:
>
> Does anybody have an example of a state machine implementation
> already in the Squeak image which could be taken as an example?
>
> Or a pointer to some Smalltalk code / tutorial how to do it in various
> ways?
>
> Regards
> Hannes Hirzel
>
More information about the Squeak-dev
mailing list
|