[antlr-interest] should sempred questions be asked on trailing optional tokens?
Gavin Lambert
antlr at mirality.co.nz
Thu May 8 06:43:14 PDT 2008
At 00:14 9/05/2008, Mark Wright wrote:
>simple_type_specifier
> : {sa.isUnsigned((TokenStream)input)}?
> 'unsigned'
> | {sa.isUnsignedInt((TokenStream)input)}?
> 'unsigned' 'int'
> | {sa.isSigned((TokenStream)input)}?
> 'signed'
> | {sa.isSignedInt((TokenStream)input)}?
> 'signed' 'int'
> | {sa.isInt((TokenStream)input)}?
> 'int'
> ;
Just out of curiosity, what are these sempred functions actually
examining? Is it just the one-or-two token lookahead sufficient
to disambiguate these alts, or are you looking even further ahead
or doing something more esoteric?
Because if it's just those, then I'd use a synpred instead -- I
think it's cleaner:
simple_type_specifier
: ('unsigned' 'int') => 'unsigned' 'int'
| ('unsigned') => 'unsigned'
| ('signed' 'int') => 'signed' 'int'
| ('signed') => 'signed'
| 'int'
;
Or even:
simple_type_specifier
: (('unsigned') => 'unsigned') 'int'?
| (('signed') => 'signed') 'int'?
| 'int'
;
Although normally you shouldn't even need to use any predicates
here at all; just order the alts correctly. I know you said they
were required for some reason in your larger grammar, but the only
explanation for that I can imagine at the moment is if 'int' were
a valid variable name (which seems like a silly thing to
allow). Maybe there's another case I haven't thought of?
More information about the antlr-interest
mailing list