[antlr-interest] bug in tree parser code gen related to rule scopes?

Mark Volkmann r.mark.volkmann at gmail.com
Wed Dec 5 13:45:53 PST 2007


I have the following in my tree grammar MathTree.g.

polynomial returns [Polynomial result]
scope {
  Polynomial current;
}
  :	^(POLYNOMIAL term*) {
    $result = $polynomial.current = new Polynomial();
  }
  ;

term
  :	^(TERM coefficient=NUMBER) {
    $polynomial.current.addTerm(new Term(toDouble(coefficient)));
  }
  |	^(TERM coefficient=NUMBER variable=NAME) {
    $polynomial.current.addTerm(new Term(toDouble(coefficient),
$variable.text));
  }
  |	^(TERM variable=NAME exponent=NUMBER) {
    $polynomial.current.addTerm(new Term($variable.text, toDouble(exponent)));
  }
  |	^(TERM coefficient=NUMBER variable=NAME exponent=NUMBER) {
    $polynomial.current.addTerm(
      new Term(toDouble(coefficient), $variable.text, toDouble(exponent)));
  }
  ;

The generated MathTree.java class contains the following.

    protected static class polynomial_scope {
        Polynomial current;
    }
    protected Stack polynomial_stack = new Stack();

and then later this code

    retval.result = polynomial_stack.current = new Polynomial();

Clearly this is wrong because current is not a field of
java.util.Stack, it is a field of the class polynomial_scope.
Did I stumble across a bug or did I do something wrong in my tree grammar?

-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list