[antlr-interest] C# tree parser memoize issue?

David Daeschler david.daeschler at gmail.com
Thu Mar 3 11:45:18 PST 2011


I have a grammar that contains a 3d vector type.

Parsing the grammar for my test case yields the following AST:
http://graph.gafol.net/dZxqGkwww

<http://graph.gafol.net/dZxqGkwww>During the code generation step I am using
a tree grammar with backtracking and memoization turned on.

With memoization the following generates an error:

float f;
vector v = <1.0,1.0,1.0> * <f,f,f>;
^Z
Gen.g: node from line 2:28 mismatched tree node: f expecting FLOAT_LITERAL

Turning off memoization fixes the issue.

The grammar fragments involved are (trying to cut down as much as I can):

expression
@init {MyAst t = (MyAst )input.LT(1);}
@after { $expression.st = DoPromotion(t, $expression.st); }
: ^(EXPR expr) -> {$expr.st}
;

expr
: multiplicativeExpression-> {$multiplicativeExpression.st}
;

multiplicativeExpression
: mult -> {$mult.st}
| primary -> {$primary.st}
;

mult
: ^(op='*' l=multiplicativeExpression r=multiplicativeExpression)
->
mul(subtemplate={TemplateMapping.Multiplication[$l.start.evalType.TypeIndex,
$r.start.evalType.TypeIndex]}, lexpr={$l.st}, rexpr={$r.st})
;
primary
@init {MyAst t = (MyAst)input.LT(1);} @after { $primary.st = DoPromotion(t,
$primary.st); }
| vecConst -> {$vecConst.st}
| vecLiteral -> {$vecLiteral.st}
| expression -> {$expression.st}
;

vecConst
: ^(VECTOR_LITERAL x=FLOAT_LITERAL y=FLOAT_LITERAL z=FLOAT_LITERAL) ->
vconst(x={$x.text}, y={$y.text}, z={$z.text})
;

vecLiteral
: ^(VECTOR_LITERAL x=expr y=expr z=expr) ->
buildvec(x={$x.st}, y={$y.st}, z={$z.st})
;

Sorry for the verbosity of this post, and thank you ahead of time for any
help.
-- 
David Daeschler


More information about the antlr-interest mailing list