help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: goto/label


From: Laurence Finston
Subject: Re: goto/label
Date: Sat, 13 Mar 2004 23:08:02 +0100
User-agent: IMHO/0.98.3+G (Webmail for Roxen)

I think this concerns the scanner rather than the parser, since the latter
doesn't "know" about the locations of the source of the tokens handled by the
parser unless the scanner "tells" it about them.  Nor does it "know" anything
about the original form of the input.

It clearly won't work to implement a `goto' statement like the one in your
example that jumps to a position further down in the input if the input is
read from standard input or a pipe.  Also, a jump to an earlier label would
only work in this case if you were to store the input in a buffer or a file. 
Otherwise, it will no longer exist.

For input read in from a file, I would suggest adding a first scanning pass to
find the labels.  Their names could then be stored in a structure along with
the file name and the position in the file, e.g.,

struct Label
{
   char* name;
   char* filename;
   unsigned int position;
}

The `Labels' could be stored in a `map' using the `name' as the key if you're
using C++, in which case you needn't store the name in the `Label'. 
Otherwise, you'll have to use some other data structure and devise a way of
searching for the right `Label'.  I suggest a binary tree.  If you have a lot
of labels and they have more complicated names than `A', `B', or `C', you
might want to consider a hash table.

When a `goto' statement is encountered, the Bison rule could find the `Label',
close the current input file and open the one whose name is `Label.name', if
necessary, and jump to 
`Label.position' using `lseek()' or whatever the appropriate function is in
C++.  
I don't have my C manual handy;  there might be a special type for positions
in files, I don't remember.

Then `yylex()' can continue scanning from that position.

However, unless you really want `goto' statements for some reason, I think
that doing this might not be making the best use of Bison.

Laurence Finston

-------------------
> At 16:10 +0000 2004/03/11, Miki Miki wrote:
> >I was wondering if anyone could help me how to implement a grammar rule for
> >GOTO statement
> >and also how to allow the option that a label can be aded to any statement
> >in the program.
> >
> >Essentially program should be able to jump to the label specified after
GOTO
> >statement and execute
> >that part of code. see example.
> 






reply via email to

[Prev in Thread] Current Thread [Next in Thread]