[antlr-interest] AST Rewrite Question

Gavin Lambert antlr at mirality.co.nz
Wed Jan 7 21:54:36 PST 2009


At 16:22 8/01/2009, Ben Gillis wrote:
>per the Definitive Guide to ANTLR, it *seems* like this would 
>work:
>
>myRule : 'KEYWORD' '=' keyvalue=(INTEGER | FLOATNUMBER | 
>HEXNUMBER | identifier | QUOTED_STRING) -> ^(KEYWORD $keyvalue)

You can't assign a label to a () block.  But you can assign one to 
a rule:

myRule : 'KEYWORD' '=' keyvalue=myRuleValue -> ^(KEYWORD 
$keyvalue);
myRuleValue : INTEGER | FLOATNUMBER | HEXNUMBER | identifier | 
QUOTED_STRING;

For that matter, you could leave out the label entirely:

myRule : 'KEYWORD' '=' myRuleValue -> ^(KEYWORD $myRuleValue);
myRuleValue : INTEGER | FLOATNUMBER | HEXNUMBER | identifier | 
QUOTED_STRING;

or:

myRule : 'KEYWORD'^ '='! myRuleValue;
myRuleValue : INTEGER | FLOATNUMBER | HEXNUMBER | identifier | 
QUOTED_STRING;

or:

myRule : 'KEYWORD'^ '='! (INTEGER | FLOATNUMBER | HEXNUMBER | 
identifier | QUOTED_STRING);


(And, yes, ANTLR really ought to issue a compile-time error when 
you try that [or just silently make it work], instead of producing 
code that will generate runtime errors.  Stuff like this will 
hopefully be resolved once ANTLR v3 finally gets self-hosted.)



More information about the antlr-interest mailing list