[antlr-interest] Imaginary node modifies tree unexpectedly. Bug, Grrr, or not what I expected?

The Researcher researcher0x00 at gmail.com
Thu Mar 31 15:21:59 PDT 2011


Imaginary node modifies tree unexpectedly.
Bug, Grrr, or not what I expected?

In the following grammar, rule a invokes rule b twice sequentially.
The first b is captured as b1, and the second b is captured as b2.
If rule b does not create an imaginary token, then both the trees for b1 and
b2 are created as expected.
However, if rule b creates an imaginary token, then tree b1 is created as
expected at first.
When b2 is created, b1 is changed and now holds the value of both b1 and b2,
when only b1 is expected.

====
grammar InvalidTreeBuild001;

options
{
   language = 'CSharp2';
   output = AST;
}

// Imaginary Tokens
tokens
{
 I1;
 I2;
}

a : b1=b b2=b
 ;

// No imaginary created
//b
// : IntegerDigit   -> ^(I1 IntegerDigit)
// ;

// Imaginary created
b
 : IntegerDigit   -> ^(I1 IntegerDigit)  ^(I2 IntegerDigit["0"])
 ;

WS  : (' '  | '\r' | '\t' | '\u000C' | '\n') {$channel=HIDDEN;}
 ;

IntegerDigit
 : ('0'..'9')
 ;
=====
Input
1 2

-----

For No imaginary created
Before b2=b
b1 = (I1 1)
b2 = nil

After b2=b
b1 = (I1 1)
b2 = (I1 2)

-----

For Imaginary created
Before b2=b
b1 1 = (I1 1) (I2 0)
b2 = nil

After b2=b
b1 = (I1 1) (I2 0) (I1 2) (I2 0)  <-  Why did (I1 2) (I2 0) get added to b1?
b2 = (I1 2) (I2 0)


More information about the antlr-interest mailing list