[antlr-interest] Retrieving Line and Column information in AST

Richard Thrippleton richard.thrippleton at progress.com
Wed May 27 02:36:30 PDT 2009


Christian Schladetsch wrote:
<snip>
> // in tree
> statement
>     :    ^('for' cond=continuation step=continuation body=continuation 
> expression)    { AppendOp(Operation::ForLoop); }
>     ...
>     ;
> 
> My question is, how can I access file, line and column information for 
> tokens used by the tree parser? in the example above, how can I access 
> the location of the 'for' token used in the tree grammar to pass to the 
> AppendOp() function?
> 
> Similarly, I'd like to add debug (file+line+column) info for all 
> generated objects.
I don't believe there's any way of getting the filename out of a token 
(but surely this will be global for an entire parsing/treewalking 
session, so could be stored as such?). Line and column can be taken as 
attributes 'line' and 'pos' from tokens though:

^(forTok = 'for' cond=continuation step=continuation body=continuation
         { line = $forTok.line;
           column = $forTok.pos;}




Richard


More information about the antlr-interest mailing list