[antlr-interest] Semantic predicate losing token/char position on error

Jim Idle jimi at temporal-wave.com
Mon Jun 7 10:02:29 PDT 2010



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Karim Chichakly
> Sent: Monday, June 07, 2010 8:44 AM
> To: antlr-interest at antlr.org interest
> Subject: [antlr-interest] Semantic predicate losing token/char position
> on error
> 
> Hi,
> 
> Thank you again for your previous help.  I now know about
> antlr.markmail.org(perhaps a link from www.antlr.org would help others)

You mean like the one on the support page with a box that you can type your search terms in and a logo saying "Mark mail"? ;-)


> If, however, I add a semantic predicate to that grammar (enclosed) to
> distinguish between X as a function call and X as a variable (which is
> described starting on page 297 of the Definitive ANTLR Reference), I no
> longer get a character position.  All four of the variables involved in
> the position calculation are set to 1, and the start and stop then
> become zero.
> These values are, by the way, a bit peculiar as these fields usually
> hold pointers into the text.  I also note that token->input is now
> NULL.

Well, though this might be shown as an example in the book it isn't really the way to do things. You are trying to make a semantic distinction via syntax rules and that is always going to give you a headache. You should parse as:

var_id:
	( funcCall -> ^(FUNCTION var_id funcCall)
         | -> var_id
      )
   ;

Then check to see if the function construct really was a function when you walk the tree in a verification pass.

I need to see your error reporting function to help you more on the display stuff. It is likely that you are trying to use elements that are not valid for the type of error you are being passed. Not all elements are available for all errors.

Finally, do not use the pANTLR3_STRING stuff unless your grammar is just a small single-shot parse as you will create a new string every time you run that predicate! Call a function, use LT() to get the next token, then use the pointers in the token directly. You will use no memory that way!

Jim





More information about the antlr-interest mailing list