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

Karim Chichakly karimc17 at gmail.com
Mon Jun 7 10:46:26 PDT 2010

Hi Jim,

Thank you.  I am sorry, but I completely missed that on the support page.

I understand your point (and thanks for the tip about pANTLR3_STRING), but
in your example, what is funcCall?  In my full grammar, I also have a branch
that looks for var_id(args), so perhaps funcCall : (args)?  However, the
problem I have is that the grammar I am parsing allows an identifier by
itself (i.e., no distiguishing syntactical features, such as parens) to
represent either a variable or a zero-argument function call.  All function
names are reserved, so I can distinguish zero-argument function calls from
variables via a symbol table lookup.

In the spirit of what you are saying, I think would have to pass the var_ids
through as var_ids and then do the lookup in a follow-on pass that modifies
the AST as needed.  Is this really the best way, i.e., to add another pass?

I enclose my nascent error handler.  As you can see, I am trying to supply
uniform behavior rather than do different things based on the specific error
(all I want is a clear indication of what went wrong and the position where
it went wrong).  Perhaps this is folly.  The error in this case was

Thanks again,


---------- Forwarded message ----------
From: Jim Idle <jimi at temporal-wave.com>
Date: Mon, Jun 7, 2010 at 1:02 PM
Subject: Re: [antlr-interest] Semantic predicate losing token/char position
on error
To: "antlr-interest at antlr.org interest" <antlr-interest at antlr.org>

> -----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

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:

       ( 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!


List: http://www.antlr.org/mailman/listinfo/antlr-interest
-------------- next part --------------
A non-text attachment was scrubbed...
Name: handler.cpp
Type: application/octet-stream
Size: 3694 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100607/2d569ac3/attachment.obj 

More information about the antlr-interest mailing list