help-bison
[Top][All Lists]
Advanced

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

Re: 2nd try


From: Laurence Finston
Subject: Re: 2nd try
Date: Wed, 28 Jul 2004 00:52:49 +0200
User-agent: IMHO/0.98.3+G (Webmail for Roxen)

-----------
> Hmm, I sent an email, but it doesn't seem to show up.
> Resending just in case
 
I got it both times.

> question 3:
 
> If I get an error in lexer, for example closing comment w/o opening
> comment, how can I get 
 
> the parser to ignore the token or maybe a serie of tokens?
> For example if I have a construct IF outside of a function in C++ I need
> to ignore the whole 
 
> thing and continue parsing next function.
> Is it just a matter of smart error token usage?
 
I think it's always best to catch errors in `yylex()' whenever possible, and
then not pass the tokens to `yyparse()' at all.  When it's not possible, I'm
afraid you're stuck with handling errors by means of error tokens.
For comments, it might even pay to scan ahead in `yylex()' and make sure that
they're terminated correctly.


> question 5:
 
> For example if I have a piece of program like this:
 
> a=1
> b=2;
 
> when parser reads "b" it errors, I print "missing semicolon" but then I
> want to continue 
 
> parsing. How do I put back "b" so that b=2; is parsed successfully.
 
There may be a way of accessing the old look-ahead token directly, I don't
know.  If you've written your own `yylex()' and you're keeping track of your
position in the input file, then you can use the normal C or C++ commands for
repositioning and continue scanning from wherever you like.
Of course, this won't work if you're reading from standard input.

You might also find another technique useful, which I call "faking" tokens. If
you're generating a reentrant parser (which I recommend), you can pass an
object to `yyparse()' by means of a `void*' argument, and this object is, in
turn, passed to `yylex()'.  

The object I use includes a stack of `structs' that contain an `int' and a
couple of other data members that I won't go into now.  As long as this stack
isn't empty, whenever `yyparse()' calls `yylex()' to get another token, the
latter takes the the `int' from the top `struct' on the stack,
pops the latter, and returns the `int', without scanning from the input
stream.  
When the stack is finally empty, the next call to `yylex()' will again scan
from the input stream.

While I call this "faking", the tokens are, of course, no more and no less
real than tokens scanned from the input stream in the ordinary way.
 
Laurence Finston



reply via email to

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