Very Horrible Question About Goto's

Eric Jacobs none at none
Wed Apr 19 21:43:20 CEST 2000


In article <2hkL4.8514$5D.18609 at ozemail.com.au>,
"Jason Stokes" <jstok at bluedog.apana.org.au> wrote:
> 
>     >Richard Jones wrote in message
>><6325E90C8B6ED21189080001FA7E2ADA0D4051 at IBA1>...
>     >I'm trying to create a converter from our own internal language
> (horrid) >into python.  My only problem so far is that the original language
> uses >*LOTS* of goto's.  Is there any way that this can be replicated within
>>python ?
> 
> 
>     Yes and no.  It should be possible to translate arbitary gotos into
> Python constructs, but the results won't be readable by a human programmer.
> I'm not quite sure how right now, but I could think about it.  Because the
> Goto is such an unstructured construct, it's hard to translate it into
> structured terms without bizarre circumlocutions.

In fact, it may not even be directly possible in a general case. Here's
a loop with two entry points:

     if (something) goto entry2
  entry1:
     ... (1)
  entry2:
     ... (2)
     goto entry1

The idea here is called reducibility; it means the edges in the flow graph
which create cycles must point to blocks which have already been encountered
on the path to that edge ("back edges"). Here, the second goto violates that
requirement. This means that it can't be reduced to a hierarchy of blocks
(whether Pythonic blocks or otherwise.)

It IS possible to translate it by duplicating code:

      if (something) {
         ... (2)
      }
   entry1:
      ... (1)
      ... (2)
      goto entry1

and that's how most C compilers would probably handle it anyway (for
optimization purposes). Formally, it's not the same flow graph, but it
does the same things.



More information about the Python-list mailing list