[antlr-interest] tree construction, associative operators, NullPointerException
Johan Grande
johan.grande at crans.org
Thu Aug 7 06:39:22 PDT 2008
Hello,
Perhaps it is because I didn't ask the question : doesn't anybody see to
what is due this problem / how I can do what I want to do ?
The solution I use until now (non binary trees) is not at all
satisfying, because not only I would have binary trees, but my
"solution" doesn't work for rules with more than one operator, for example :
,----
| equalityExpression
| : instanceOfExpression ( ('==' | '!=') instanceOfExpression )*
| ;
`----
Or is there something useful I didn't provide ?
Thank you for your help,
Johan
Johan Grande a écrit :
> Hi,
>
> I work with ANTLR v3.1b2 on the Java grammar made by Terence Parr
> (http://antlr.org/grammar/1152141644268/Java.g), and I try to construct
> ASTs using rewrite rules.
>
> I get runtime errors when I try to construct a binary tree for an
> expression with an associative operator.
>
>
> I try to parse the following code :
> ,--
> | class Test {
> | boolean titi = true & false;
> | }
> `--
>
>
> When I try construct an operand list under the '&' operator, with the
> '&' rule like this :
> ,--
> | andExpression
> | : equalityExpression ( '&' equalityExpression )*
> | -> ^(AssociativeOperation And ^(ExpressionList
> equalityExpression+))
> | ;
> `--
> everything works fine.
>
>
> But when I try to construct a binary tree, as shown at the beginning of
> this page : http://www.antlr.org/wiki/display/ANTLR3/Tree+construction ,
> with the '&' rule like this :
> ,--
> | andExpression
> | : (a=equalityExpression -> $a)
> | ( '&' b=equalityExpression -> ^(Cons And $andExpression $b) )*
> | ;
> `--
> (the other rules are unchanged), I get a NullPointerException.
>
> The problem comes from "$andExpression" : if I replace it by
> "EmptyExpression", it runs.
>
>
> The imaginary tokens I use and the corresponding classes are generated
> by GOM (http://tom.loria.fr), from an abstract data structure
> description, with a syntax like this :
> --------
> Expression =
> | AssociativeOperation( nop:AssociativeOperator,
> operands:ExpressionList )
> | Cons( nop:AssociativeOperator, lhs:Expression, rhs:Expression )
> | UnaryOperation( uop:UnaryOperator, operand:Expression )
> | EmptyExpression()
> // [...]
> --------
>
>
> Here is the complete stack trace :
> --------------------------------------------------------------------------------
>
> java.lang.NullPointerException
> at
> parser.ast.types.expressionlist.ConsExpressionList.hashFunction(Unknown
> Source)
> at
> parser.ast.types.expressionlist.ConsExpressionList.initHashCode(Unknown
> Source)
> at
> parser.ast.types.expressionlist.ConsExpressionList.make(Unknown Source)
> at
> parser.ast.types.expressionlist.ExpressionList.append(Unknown Source)
> at parser.ast.AstTree.addChild(Unknown Source)
> at
> org.antlr.runtime.tree.BaseTreeAdaptor.addChild(BaseTreeAdaptor.java:104)
> at parser.JavaParser.exclusiveOrExpression(Unknown Source)
> at parser.JavaParser.inclusiveOrExpression(Unknown Source)
> at parser.JavaParser.conditionalAndExpression(Unknown Source)
> at parser.JavaParser.conditionalOrExpression(Unknown Source)
> at parser.JavaParser.conditionalExpression(Unknown Source)
> at parser.JavaParser.expression(Unknown Source)
> at parser.JavaParser.variableInitializer(Unknown Source)
> at parser.JavaParser.variableDeclarator(Unknown Source)
> at parser.JavaParser.variableDeclarators(Unknown Source)
> at parser.JavaParser.fieldDeclaration(Unknown Source)
> at parser.JavaParser.memberDeclaration(Unknown Source)
> at parser.JavaParser.memberDecl(Unknown Source)
> at parser.JavaParser.classBodyDeclaration(Unknown Source)
> at parser.JavaParser.classBody(Unknown Source)
> at parser.JavaParser.normalClassDeclaration(Unknown Source)
> at parser.JavaParser.classDeclaration(Unknown Source)
> at parser.JavaParser.classOrInterfaceDeclaration(Unknown Source)
> at parser.JavaParser.typeDeclaration(Unknown Source)
> at parser.JavaParser.compilationUnit(Unknown Source)
> at Main.main(Unknown Source)
> --------------------------------------------------------------------------------
>
>
> Here you can find the complete grammar ('&' rule line 1043), and some
> GOM-generated classes : http://perso.crans.org/~grande/antlr/ .
>
>
> Thank you for your help,
> Johan
>
>
>
More information about the antlr-interest
mailing list