Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Effective uses of c `goto' statement
Date: Sun, 06 Dec 2020 18:43:25 -0800
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <86zh2qibea.fsf@linuxsc.com>
References: <87czzszjhs.fsf@fedora.osfans.org> <20201202120809.633@kylheku.com> <86sg8kkyjb.fsf@linuxsc.com> <_CPyH.64249$7D7.63135@fx03.iad> <86ft4jkry3.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="bcc5e78e97e66444c1bdd30cf2244811"; logging-data="14821"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZNuNvYY+i1vhe+6NFxxMBayCYTq68oDo="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:+jT16J8GC47jJt15XN+HOLlX2Fg= sha1:sZwBQdB67Q67jlDspLtA3zoYNpY=
Xref: csiph.com comp.lang.c:157009
Richard Damon writes:
> On 12/6/20 8:03 AM, Tim Rentsch wrote:
>
>> Richard Damon writes:
>>
>>> On 12/5/20 11:28 AM, Tim Rentsch wrote:
>>>
>>>> Kaz Kylheku <563-365-8930@kylheku.com> writes:
>>>>
>>>> [...]
>>>>
>>>>> Finite automata re easily implemented without goto, but only goto
>>>>> offers the most efficient possible representation with the fewest
>>>>> state variables (possibly with no state variable at all).
>>>>
>>>> Unless you can offer some sort of argument supporting this
>>>> assertion, I'm inclined to believe it is not the case.
>>>
>>> Well, you can build the Finite automata where the state you are in
>>> is the last label you did a 'goto' to (with an optimization that if
>>> it is the next lable, the goto can be implied). That basically
>>> eliminates the state variable (maybe some state works better in a
>>> state variable if you have a series of states that are similar and
>>> just use a counter).
>>
>> Yes, I understood that already.
>>
>>> The non-goto method is typically a switch statement based on the
>>> state variable, all in a while loop. There each state changes the
>>> variable and breaks from the switch, and then loops in the while to
>>> go to the next state.
>>
>> A common way to implement a state machine is with a switch()
>> inside a loop of some sort (e.g., for() or while()). I got that
>> already also.
>>
>> My question is about other possibilities that do not use goto
>> but also do not use state variables.
>
> I think the key is that the state needs to be stored somehow. It
> can be explicitly in a variable, or implicitly in the processor
> address, but it needs to be somewhere.
Yes, no doubt about that.
> It could perhaps be also done in functions that return the address
> of the function that is the next state, and that address is directly
> called and not saved so no 'state variable' every really existed.
Not in its usual sense, for sure. Written in a simple way, we
might see something like this:
SomeSortOfFunction f = ...;
while( f ) f = f( &fsm_data );
> The tricky part here is that in C you can not define a function of
> the self-referenctial type of a function that returns a pointer to a
> function of its type, so it needs to return a different type of
> function pointer that is casts to its type and then called.
We can't define a type in C that is directly recursive, but we
can achieve a similar effect using structs and pointers:
struct function_returning_itself_s;
typedef struct function_returning_itself_s *FtoF;
typedef struct function_returning_itself_s {
FtoF (*it)( long * );
} FtoF_object[1];
FtoF
wits_end( long *data ){
static FtoF_object me = {{ wits_end }};
if( !data ) return 0;
return me;
}
void
run_fsm( FtoF f, long *data ){
while( f ) f = f->it( data );
}