[antlr-interest] Tree rewriting (filter=true, output=ast)

Gavin Lambert antlr at mirality.co.nz
Sat Mar 21 11:14:27 PDT 2009


At 05:16 22/03/2009, Sam Barnett-Cormack wrote:
 >1) All children are either ^(NAMENUM ...), ^(EXTREF ...), LCID 
or
 >NUMBER
 >2) Children are *anything* except ^(NAMENUM ...)
 >3) Children include ^(NAMENUM ...) but are not limited to those
 >mentioned in (1) (ie. the complement of the union of (1) and 
(2)).
[...]
 >valueListValue
 >  : ^(VLIST
 >    (
 >      (~(NAMENUM))*
 >      | v+=(nameAndNumberForm | LCID | NUMBER |
 >externalValueReference)+
 >        -> ^(OBJID $v+)
 >    ))
 >  ;

You should always list your more-specific alternatives above the 
less-specific ones (so have your nameAndNumberForm above the 
~NAMENUM alt).

Also, label+=(block ...) doesn't actually work -- the label always 
ends up being null.

Not sure if the following will work, but it'd be my first attempt 
to get the behaviour you're wanting:

valueListValue
scope { bool hasOther; bool hasNameNum; }
@init { $valueListValue::hasOther = false; 
$valueListValue::hasNameNum = false; }
   : ^(VLIST
     ( v+=nameAndNumberForm { $valueListValue::hasNameNum = true; 
}
     | (v+=externalValueReference | v+=LCID | v+=NUMBER)
     | . { $valueListValue::hasOther = true; }
     )+
     ( { $valueListValue::hasNameNum && $valueListValue::hasOther) 
}? { raise error; }
     | { $valueListValue::hasNameNum }? -> ^(OBJID $v+)
     )?
   ;

(I'm a little unsure about the specific syntax for that last 
bit.  But I think something like that ought to do the trick.)



More information about the antlr-interest mailing list