[antlr-interest] C# and '?' cardinality operator in AST rewrite rules

Kay Roepke kroepke at classdump.org
Sat Jan 6 15:42:52 PST 2007


On 7. Jan 2007, at 0:23 , Benoit Miller wrote:

> When targeting C#, and given the following grammar snippet:
>
> foo : attrList? 'int' ID
>       -> ^(attrList? VAR_INT ID)
>     ;
>
> Antlr will generate parser code that references attrList no matter  
> what when building the AST. If attrList isn't present in the input,  
> an exception is thrown.
>
> If I use '+' instead on attrList in the AST rewrite rule (keeping  
> the '?' in the rule itself), then it works fine.
>
> Looking at http://www.antlr.org/wiki/display/ANTLR3/Tree 
> +construction implies that all cardinality operators (?, *, +)  
> should work in AST rewrite rules.
>
> Is this a bug, or a feature?
>
> Not really a problem since '+' works fine, I'm just wondering if I  
> understand this correctly. I'm using antlr-3.0b5.


Hi Ben,

keep in mind that ^(attrList VAR_INT ID) is the tree:

          attrList
           /    \
       VAR_INT   ID

Making the root node optional is probably not well defined in any  
case...

I guess that's the problem with using ? for the root node in a  
rewrite. + works because
there's always at least one node to make the root. The rest will  
probably spill over into
the children list. I haven't actually tried this, but it should be  
what happens here.

cheers,
-k
-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list