[antlr-interest] Clarification on documentation

Olivier Dragon dragonoe at mcmaster.ca
Tue Aug 23 05:37:39 PDT 2005


On Tue, Aug 23, 2005 at 03:18:19PM +0400, Alexey Demakov wrote:
> Of course:
> 
> http://www.antlr.org/doc/lexer.html#Keywords_and_literals
> http://www.antlr.org/doc/lexer.html#Common_prefixes

For keywords and literals the documentation is good. For the common
prefixes I always thought the explanation was horrible. You can't
distinguish common prefixes of arbitrary length by increasing the
lookahead. You can however deal with them without syntactic predicates,
which the documentation doesn't mention.

What I like to do instead is a bit of DFA-style rules. For example,
instead of having:

INTEGER: ('0'..'9')+ ;
REAL: ('0'..'9')+ '.' ('0'..'9')+;

Which will cause you endless non-determinism pain and suffering, you can
do this:

INTEGER:
	('0'..'9')+
	( '.' ('0'..'9')+ { $setType(REAL); } )?
	;

Hope this helps,
-Olivier

-- 
          __-/|    ? ?     |\-__
     __--/  /  \   (^^)   /  \  \--__
  _-/   /   /  /\ / ( )  /\  \   \   \-_
 /  /   /  /  /  (   ^^ ~  \  \  \   \  \
 / Oli Dragon    ( dragonoe at mcmaster.ca \
/  B.Eng. Sfwr   (     )    \    \  \    \
/  /  /    /__--_ (   ) __--__\    \  \  \
|  /  /  _/        \_ \_       \_  \  \  |
 \/  / _/            \_ \_       \_ \  \/
  \_/ /                -\_\        \ \_/
    \/                    )         \/
                        *~
        ___--<***************>--___
       [http://dragon.homelinux.org]
        ~~~--<***************>--~~~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20050823/27616422/attachment.bin


More information about the antlr-interest mailing list