[antlr-interest] Line/char positions in C runtime

Ramon Verbruggen Ramon.Verbruggen at quintiq.com
Thu Nov 29 00:41:36 PST 2007


Hi Stuart,

Sorry for the late reply, I hope someone beat me to it :-)

I'm an ANTLR newbie myself, but I also use the C runtime (thanks Jim!)
to generate ASTs.

Maybe I am misinterpreting (no pun intended) your question, but I just
use the line and position information that is already in the AST nodes:

ANTLR3_UINT64 linenr = treenode->getLine(treenode); //linenr is one
based
ANTLR3_UINT32 column = treenode->getCharPositionInLine(treenode);
//column is zero based

If you use imaginary tokens for AST construction, these do not have
their locations set automatically, but you can set those from your
grammar, see 'the book', section 7.5, page 188-189, 'Deriving Imaginary
Nodes from Real Tokens'

I did notice however that if linenr = 1 and column > 0 I have to add 1
to the column to get the correct text location (don't ask me why...)

I hope this helps,

Ramon Verbruggen


> Hiya,
> 
> I'm still in my early days of ANTLRing, but I've dug around the
mailing
> lists and can't find much help on this issue; any suggestions or
pointers
> very welcome.
> 
> Basically, I want to get line/character position information into the
AST
> when using the C runtime - and although I've found stuff on doing
this in
> Java, I'm not as clear about how to extend the C runtime
architecture. I'm
> using C so I can integrate the ANTLR parser into a Perl XS module -
all this
> works fine. I'm using gcc on Windows, MinGW and CDT under Eclipse. 
> 
> The good news is that all this works fine and I can get the AST into
Perl
> and process it there with no problems. The bad news is I have no idea
how to
> map the AST positioning back to the original text, which would be a
big step
> forward.
> 
> Any ideas or suggestions gratefully received
> --Stuart



This message contains information that may be privileged or confidential
and is the property of Quintiq. It is only intended for the person to
whom it is addressed. If you are not the intended recipient, you are not
authorized to read, print, retain, copy, disseminate, distribute or use
this message or any part thereof. If you have received this message in
error, please notify the sender immediately and delete all copies of
this message. Please note that e-mails are susceptible to change,
therefore they are not binding.


More information about the antlr-interest mailing list