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.

+3


source to share


1 answer


If the parser rule a

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

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

      

However, if it a

has an imaginary token as its root:

grammar T;

tokens {
  IMAG;
}

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 {
  IMAG;
}

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

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

      

Thus, the root IMAG

will get the same line number as SomeToken

.

+4


source







All Articles