Question

I have an expressions in ANTLR3

num_addition
    : num_multi ((plus^|minus^) num_multi)*
    ;

I want to change the tokens 'plus' and 'minus' to a different token So i tried to made this rewrite rule.

num_addition
    : num_multi (plus num_multi)* -> ^(num_multi ^(plus_special num_multi)*)
    | num_multi (minus num_multi)* -> ^(num_multi ^(minus_special num_multi)*)
    ;

If I do this the generation in ANTLRWORKS seems to take longer but it generated the correct grammar and tokens. If I apply this style to other rules such as 'multi' and 'divide' and 'equalequal' etc. it eventually gets to the point where ANTLRWORKS wont do anything when I press Generate.

There are no errors according to ANTLRWORKS but when I pres generate nothing happens.

Am I incorrectly rewriting for what I want to achieve?

Was it helpful?

Solution

You can't inject just any production or terminal into your AST which are not matched in the parser rule you're creating the AST for. In your case, you can insert plus or minus since they are matched by the parser rule, but you can't insert plus_special or minus_special since these are not matched by the parser rule num_addition.

You can inject imaginary tokens though.

Try something like this:

grammar T;

tokens {
  // Some imaginary tokens:
  PLUS_SPECIAL;
  MINUS_SPECIAL;
}

// ...

num_addition
 : (a=num_multi -> $a) ( PLUS  b=num_multi -> ^(PLUS_SPECIAL  $num_addition $b)
                       | MINUS b=num_multi -> ^(MINUS_SPECIAL $num_addition $b)
                       )*
 ;

// ...

PLUS : '+';
MINUS : '-';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top