Liste des Groupes | Revenir à cl forth |
I have worked out an example:Sorry about a late reply but I've just got round to looking more closely at your FSM example
https://www.complang.tuwien.ac.at/forth/programs/fsm-ae.4th
Let's first look at the example: The example recognizes and printsBut you can get rid of the SWAP because in GForth
numbers in a text and ignores everything else. It terminates when it
sees '$'. It has two states, one for being inside a number and one
for outside:
state outside-num
state inside-num
(Note that this is not the standar Forth word STATE).
Then we define transitions:
: out->out ( c-addr -- c-addr1 )
count outside-num transition ;
' out->out outside-num all-transitions
The out->out transition is the simplest one: It fetches the next char
(with COUNT) and switches to OUTSIDE-NUM. TRANSITION already starts
the dispatch for that state and the next char; this (and maybe also
COUNT) could be put in the general FSM interpreter (START-DFA), but by
having TRANSITION in the individual transition actions (e.g.,
OUT->OUT), the implementation is more flexible, as we will see.
At first OUT->OUT is put in transitions from OUTSIDE-NUM for all
characters using ALL-TANSITIONS; later the transitions of various
characters are overwritten:
' out->in '9' 1+ '0' outside-num some-transitions
' out->stop '$' outside-num one-transition
Note that the stack effect comment for out->out is from the start of
the word to the start of the next state-transition word; the actual
stack effect depends on the implementation of transition.
For more state transitions and the corresponding transition words see
the source code.
Example usage:
s" 123 abc 456 df$" drop outside-num start-dfa \ prints "123 456"
Now for the implementations: States are just arrays of xts, indexed by
the character, and the xt is that of the transition from the state
with that character.
The implementation without EXECUTE-EXIT looks as follows:
: transition ( c addr -- xt )
\ addr specifies the next state
]] swap th @ [[ ; immediate
: stop ( c-addr -- 0 )
drop 0 ;
: start-dfa ( c-addr addr -- )
swap count rot transition
begin ( ... xt )
execute dup
0= until
drop ;
TRANSITION could be a plain colon definition here, but it is a macro
in order to make it more competetive in Gforth with the EXECUTE-EXIT
variant. Here the termination is performed by replacing the next
c-addr with 0 and testing for 0 in the loop.
An alternative implementation is to use EXECUTE-EXIT to tail-call the
next transition:
: transition ( c addr -- )
]] swap th @ execute-exit [[ ; immediate
: stop ( -- )
\ let the ";" behind the STOP do the stopping
]] drop [[ ; immediate
: start-dfa ( c-addr addr -- )
\ let the dfa work on the string starting at c-addr, with initial
\ state addr
swap count rot transition ;
Here TRANSITION contains the EXECUTE in the form of EXECUTE-EXIT, and
so each transition word directly calls the next one, and no loop is
necessary; with EXECUTE this would fill the return stack after a few
thousand transitions, but EXECUTE-EXIT takes the return address off
the return stack before EXECUTEing the next word and therefore can
perform an indefinite number of state transitions.
So how do we get out of the state machine? By not performing a
transition; instead we simply return to the caller of START-DFA.
I looked at the generated code and thought that we can get rid of the
SWAP in the transition code by using the generalized constant folding
feature of Gforth. This replaces the definition of TRANSITION with:
: noopt-transition-compile, ( xt -- )
\ basic compile, implementation for TRANSITION
drop ]] swap th @ execute-exit [[ ;
Les messages affichés proviennent d'usenet.