[antlr-interest] Infinite lookahead/syntactic predicates
Paul Johnson
gt54-antlr at cyconix.com
Fri Jun 24 01:38:32 PDT 2005
Akhilesh Mritunjai wrote:
> Hi
>
> Its fairly simple using syntactic lookahead, let the
> lexer do all grunt work:
>
> protected HEX_CONST: ('0'..'9'|'A'..'F'|'a'..'f')+;
> protected DEC_CONST: ('0'..'9')+
> protected BIN_CONST: ('0'|'1')+
> protected FOO_CONST: ....
The only advantage of this approach is that you can detect errors during
lexing, rather than slightly later; the disadvantage is that the lexer
has to do lookahead. You also end up having to call different integer
conversion functions (one for binary, one for hex, etc) to reduce your
constants to a common base, which reduces your flexibility.
I personally prefer the universal approach - parse the bit between
quotes as an arbitrary string (not '0..9'!), get the base specifier,
call 'strtol(..., base)' or equivalent to try to make sense of the answer.
Paul
More information about the antlr-interest
mailing list