Antlr - keep line number and position in tree grammar?

Is there an easy way to add line number information to the generated nodes in the tree grammar?

eg. parser grammar

rule: a '+' b -> ^(PLUS a b);


tree grammar:

rule: ^(PLUS a b) { print_message_with_line_number_of(a); };


I tried looking in a.start.token etc, but the ones I looked at were zeros.


source to share

1 answer

If the parser rule a

contains a real token as its root, then this works:

 : ^(PLUS a b) {System.out.println("line=" + $a.start.getLine());}


However, if it a

has an imaginary token as its root:

grammar T;

tokens {

a : SomeToken -> ^(IMAG SomeToken)


then the token IMAG

has (obviously) no line number associated with it (it's really not in the input!). In such cases, you need to manually create a token, set the line number for that token, and insert it into the root of your AST. It will look like this:

grammar T;

tokens {

@parser::members {
  private CommonToken token(String text, int type, int line) {
    CommonToken t = new CommonToken(type, text);
    return t;

a : SomeToken -> ^({token("imag", IMAG, $SomeToken.getLine())} SomeToken)


Thus, the root IMAG

will get the same line number as SomeToken




All Articles