[antlr-interest] Semantic predicates that aren't & hoisting

Monty Zukowski monty at codetransform.com
Thu Mar 10 07:45:09 PST 2005


David Jung wrote:

> 
> void conditionallyInsertSEMI()
> {
>   if (<last token wasn't '}'>)
>     Insert_SEMI_into_token_stream();
> }
> 
> However, I don't know how (or if) it is possible to insert
> a token into the stream during parsing like that.
> 

Not such a good idea from the parser, but a fine use of a TokenStream. 
See http://www.codetransform.com/filterexample.html
The idea for you would be to write enough of a parser to recognize the 
right brace, check for a following semi, and insert a semi if there 
isn't one.  The big question is if you can make your parser accept a 
SEMI after every right brace or if you need to make the filter smart 
enough to tell when the brace is part of an expr or not.

Actually, if you can rearrange it so that the parser can handle a semi 
after a brace everywhere, you might be able to accomplish the whole 
thing in the lexer.

> 
> Does anyone who is an experienced ANTLR user know if it is
> possible to do what I want cleanly? Or if there is a way
> to force hoisting or to insert a token?

You can't hoist, you have to manually do it by copy and paste.  It still 
may leave you with warnings though.  Don't insert a token from the 
parser, you must do that before it gets to the parser somehow.

Ter, this would make an interesting test for ANTLR 3.

Monty
www.codetransform.com


More information about the antlr-interest mailing list