comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: GOTO considered necessary (reworked)
Date: 1997/06/17
Date: 1997-06-17T00:00:00+00:00	[thread overview]
Message-ID: <EBxrpn.67C@world.std.com> (raw)
In-Reply-To: dewar.866548262@merv


In article <dewar.866548262@merv>, Robert Dewar <dewar@merv.cs.nyu.edu> wrote:
>First, to me, the mapping of states into sections of code, and the mapping
>of transitions as gotos, seems pretty direct -- direct enough that I do not
>think it would make sense to dream up some syntactic gizmo -- although as
>Robert Eachus points out, a preprocessor can very well provide the equivalent
>of this syntactic gizmo.

The reason some syntactic gizmo shouldn't be in the language is that
these FSM cases are rare.  If they were common, then we would get enough
benefit from such a syntactic gizmo to make it worthwhile.  (See also
languages like Lisp, where you can create your own gizmos.)

>As for fall through, that is quite a bogus argument. If you map a FSM into
>a loop/case structure with an explicit state variable, then you have to be
>equally careful not to forget to modify the state variable. The error in one
>case is accidental transition to the following state, the error in the other
>is an infinite loop in one state -- not much to call there.

I disagree.  In the loop/case method, you can put "State := Bad_State;"
at the top of the loop, and then "when Bad_State => Assert(False);" in
the case statement.  With the goto solution, you have to use care on
each state, rather than once for the whole mess.

If you want to stay in the same state, then you would write (under "when
Some_State =>", "State := Some_State;".

>Of course in practice, one of the advantages of the goto model is that in
>some cases, it is quite natural to fall through to the following state:
>
>   <<Sign>>
>      code to acquire sign
>
>   <<Digits>>
>      code to acquire digit
>      ..
>
>      if Another_Digit then
>	 goto Digits;
>      else
>	 goto Exponent;
>      end if;
>
>or somesuch, where Sign does fall through to Digits. I find this quite
>readable, and code of this kind can be found in many lexical analyzers
>in compilers.
>
>yes, of course you can often replace a specific instance (like the one above
>if that is all there is to it), with a simpler structure (e.g. a loop for
>the Digits above).

In my experience, a hand-coded lexical analyzer is always better done
with loops and ifs and case statements, rather than as above.  That is,
forget that it's a FSM, and just write it in the natural way.  ("An
identifier is a letter, followed by a sequence of zero or more letters
and digits" translates quite nicely into loops, not gotos.)  I know you
agree, since I've seen the GNAT lexer.  (A tool-generated lexer is a
whole 'nother story, of course -- throw readability to the winds, and go
for efficiency.  Funny thing is, tool-generated lexers are less
efficient, in practise, in my experience.)

>The time that a goto threaded structure becomes appropriate is when you have
>a very involved FSM with many intertwined transitions.

Yes.  In this case, I would use loop/case, whereas you would use goto.
At least we agree on the meta rule: Goto is not *always* evil, and
should be considered on a case-by-case basis.

>Basically an FSM of this kind *fundamentally* corresponds to spaghetti code,
>in that the states are executed in some non-structured sequence depending on
>the input, and therfore cannot be arranged into neat structures.
>
>We have only two methods of handling such spaghetti sequences in typical
>PL's, the code+goto representation, or the loop-case representation. 

I can think of others: E.g. an array of pointers-to-procedures, indexed
by state.

>...One
>might say that neither is perfectly happy, and that is right, since the
>underlying sequence of control is in some sense unhappy. 

That's why I don't like viewing a lexer as an FSM, nor coding it as such.

>...But given this
>choice I prefer the code+goto representation.
>
>As Robert Eachus notes, for a complex FSM, there is considerable merit in
>using one of the standard tools to generate the code.

Well, if tools are generating the code, then this whole argument is
irrelevant.  If a tool is generating it, then efficiency is paramount,
and readability of the generated Ada code is unimportant.  (Unless
you're debugging the tool, of course!)

- Bob




  reply	other threads:[~1997-06-17  0:00 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-11  0:00 GOTO considered necessary (reworked) Samuel Mize
1997-06-11  0:00 ` Bryce Bardin
1997-06-12  0:00 ` Anonymous
1997-06-12  0:00   ` John G. Volan
1997-06-16  0:00     ` Anonymous
1997-06-12  0:00   ` Robert Dewar
1997-06-12  0:00     ` John G. Volan
1997-06-13  0:00       ` Robert A Duff
1997-06-16  0:00         ` John G. Volan
1997-06-17  0:00           ` Robert Dewar
1997-06-17  0:00             ` Robert A Duff [this message]
1997-06-18  0:00               ` Spam Hater
1997-06-20  0:00               ` Robert Dewar
1997-06-21  0:00                 ` Robert A Duff
1997-06-21  0:00                   ` Robert Dewar
1997-06-20  0:00               ` Robert Dewar
1997-06-25  0:00               ` Wolfgang Gellerich
1997-06-25  0:00                 ` Samuel T. Harris
1997-06-25  0:00                 ` Michael F Brenner
1997-06-26  0:00                   ` Wolfgang Gellerich
1997-06-19  0:00             ` Karel Th�nissen
1997-06-19  0:00               ` Karel Th�nissen
1997-06-23  0:00               ` John G. Volan
1997-06-23  0:00                 ` Spam Hater
1997-06-23  0:00                 ` Robert Dewar
1997-06-24  0:00                   ` Brian Rogoff
1997-06-25  0:00                   ` Featuritis not always bad (was re: GOTO considered necessary) Karel Th�nissen
1997-06-26  0:00                     ` Robert Dewar
1997-06-26  0:00                       ` Karel Th�nissen
1997-06-25  0:00                 ` GOTO considered necessary (reworked) Karel Th�nissen
1997-06-23  0:00             ` John G. Volan
1997-06-17  0:00           ` Robert I. Eachus
1997-06-17  0:00           ` Robert A Duff
1997-06-25  0:00             ` Van Snyder
1997-07-21  0:00           ` Shmuel (Seymour J.) Metz
1997-06-12  0:00 ` Michael F Brenner
1997-06-17  0:00   ` Robert Dewar
1997-06-17  0:00     ` Robert A Duff
1997-06-20  0:00       ` Robert Dewar
1997-06-21  0:00         ` Robert A Duff
1997-06-21  0:00           ` Robert Dewar
1997-06-13  0:00 ` Robert A Duff
1997-06-14  0:00   ` Robert Dewar
1997-06-16  0:00     ` Robert A Duff
1997-06-17  0:00       ` Spam Hater
1997-06-17  0:00         ` Robert A Duff
1997-06-19  0:00           ` Spam Hater
1997-06-17  0:00         ` Robert Dewar
1997-06-17  0:00           ` Robert A Duff
1997-06-19  0:00             ` John Herro
1997-06-25  0:00               ` Function result Van Snyder
1997-06-27  0:00                 ` Robert Dewar
1997-06-27  0:00                 ` Jon S Anthony
1997-06-20  0:00             ` GOTO considered necessary (reworked) Robert Dewar
1997-06-17  0:00           ` Spam Hater
1997-06-16  0:00     ` Spam Hater
1997-06-17  0:00       ` Robert Dewar
1997-06-17  0:00         ` Spam Hater
1997-06-14  0:00   ` Samuel Mize
1997-06-14  0:00   ` Samuel Mize
1997-06-14  0:00     ` Matthew Heaney
1997-06-16  0:00 ` Anonymous
1997-06-16  0:00   ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox