[antlr-interest] Default values in tree rewrite rules

John B. Brodie jbb at acm.org
Fri Aug 11 09:53:49 PDT 2006


Emond Papegaaij asked:

>I've got the following parser rule in which 'superclass' is optional:
>  nodeDecl
>   : (options {k = 1;} : (nodeNames ':')=> bindings=nodeNames ':')?
>     (declAbstract=ABSTRACT)?
>     type=targetClass
>     ('extends' superclass=targetClass)?
>     ( CBR_OPEN body CBR_CLOSE | SEMI )
>     -> ^(NODE_SPEC $type $declAbstract? $superclass? body? $bindings?)
>   ;
>
>Now I want superclass to become the tree ^(TYPE["Node"]) when it is not 
>defined in the input (and it will no longer be optional in the rewrite 
>rule). Is this possible? All I've come up with is:
>     ('extends' superclass=targetClass)? {
>       if (superclass == null) {
>         superclass = new targetClass_return();
>         superclass.tree = adaptor.create(TYPE, "Node");
>       }
>     }
>Which is quite nasty.

indeed. does this work?

nodeDecl
   : (options {k = 1;} : (nodeNames ':')=> bindings=nodeNames ':')?
     (declAbstract=ABSTRACT)?
     type=targetClass
     sc=superClass
     ( CBR_OPEN body CBR_CLOSE | SEMI )
     -> ^(NODE_SPEC $type $declAbstract? $sc body? $bindings?)
   ;

superClass :
     ( /* empty */ -> ^(TYPE["Node"]) )
   | ( ('extends' targetClass) -> targetClass )
;

Hope this helps...
   -jbb


More information about the antlr-interest mailing list