[antlr-interest] Problem with imaginary nodes start index

Dario Bertini berdario at gmail.com
Tue Jun 1 18:40:59 PDT 2010


Hi, i've a problem with my grammar...

my target is java, and i have a parser that outputs an AST, and a 
treewalker that rewrites it thanks to stringtemplate.

i've tried to add some dummy inline templates to some of my treewalker's 
rules, but when i run it throws this exception:

Exception in thread "main" java.lang.IllegalArgumentException: replace: 
range invalid: -1..-1(size=519)
     at 
org.antlr.runtime.TokenRewriteStream.replace(TokenRewriteStream.java:263)
     at 
org.antlr.runtime.TokenRewriteStream.replace(TokenRewriteStream.java:250)
     at qicvgwalker.containerrow(qicvgwalker.java:1457)
     at qicvgwalker.def(qicvgwalker.java:1260)
     at qicvgwalker.prog(qicvgwalker.java:314)
     at ExampleRunner.main(ExampleRunner.java:47)

if i go to line 1457 of the treewalker, i can see that the problem is 
caused by
input.getTreeAdaptor().getTokenStartIndex(retval.start)
returning -1

this supposedly means that it couldn't find the retval.start token... 
that is the token i defined as an imaginary node

i'm using imaginary nodes also in other places of the code (along the 
needed template rewritings), but these doesn't throw up any problem... 
instead this is the second time i get this exception: the first time i 
managed to workaround it, but this time i cant' afford to do it

here are the relevant rules from my parser:

prog     :    row? (ENDL row?)* -> ^(ROW row)*;

row     :    def COMMENT? |COMMENT ;

containerrow    :    innerdef COMMENT? |COMMENT ;

innerdef:
     def
   | ID ID '(' point (',' FLOAT (',' FLOAT)?)? ')' -> ^(ID ID ^(POSITION 
point) ^(SCALE FLOAT)? ^(ANGLE FLOAT)?);

here is the relevant AST subtree generated ("println"ed through 
((CommonTree) parser.prog().getTree()).toStringTree() ) :

(ROW (container frattale (POSITION 0 0) (ROW (line linea1 (INITPOSITION 
(frattale x) (frattale y)) (FINALPOSITION (+ (frattale x) 1) (frattale 
y)))) (ROW (frattale frattale1 (POSITION (+ (frattale x) 1) (frattale 
y)) (SCALE 0.35) (ANGLE 0.35))) (ROW (frattale frattale2 (POSITION (+ 
(frattale x) 2) (+ (frattale y) 1)) (SCALE 0.35) (ANGLE 0.35))) (ROW 
(line linea2 (INITPOSITION (+ (frattale x) 3) (frattale y)) 
(FINALPOSITION (+ (frattale x) 4) (frattale y))))))

here are the options of my treewalker:

options {
     tokenVocab=qicvg ;
     ASTLabelType=CommonTree;
     output=template;
     rewrite=true;
}

(i'm using the rewrite mode since, until now, practically every rule has 
a simple substitution template without recursion problems)

and here is the problematic treewalker rule:

containerrow :    ^(ROW innerdef comment?)-> template() "dummy" | ^(ROW 
comment);

retval.start here should be ROW, and apparently the treewalker can't 
find its starting index... if i remove "-> template() "dummy" " all goes 
fine... here are other relevant treewalker rules:

innerdef returns [String id]:
     def {$id=$def.id;} -> template() "dummy"
   | ^(ID ID ^(POSITION point) ^(SCALE FLOAT) ^(ANGLE FLOAT)) -> 
template() "dummy";

comment: COMMENT -> template(c={$COMMENT.text}) "\<!--<c>--\>" ;



hope someone here can point me out to where the problem is...
thanks in advance



More information about the antlr-interest mailing list