[antlr-interest] Failing on case test: how?

Hendrik Maryns qwizv9b02 at sneakemail.com
Wed Nov 12 09:04:26 PST 2008


Hi,

I have two rules as follows:

firstOrderVariable returns [FirstOrderVariable result]
	: IDENTIFIER { $result = getFirstOrderVariable($IDENTIFIER.text); }
	;

secondOrderVariable returns [SecondOrderVariable result]
	: IDENTIFIER { $result = getSecondOrderVariable($IDENTIFIER.text); }
	;

with

IDENTIFIER
  : ( ~( CLOSE | OPEN | WS ) )+
  ;

OPEN : '(' ;
CLOSE : ')' ;
fragment WS : ( ' ' | '\t' ) ;

Most of the times, it is no problem they are identical, since the rest
of the surrounding rule will do the disambiguation.  Unfortunately, in
some cases, it won’t.  I would like to separate them in that first-order
variables should start with a lowercase letter, but second-order
variables with an uppercase letter.  So I’d like to insert some semantic
predicate which checks the case of the first letter and fails as
appropriate.  Is this possible, and if yes, how?

If it isn’t I can make sure the surrounding rules do the disambiguation,
but this solution would be nicer.

Originally, I had a lexer rule FIRST_ORDER_VARIABLE: LOWER ( LOWER |
UPPER )* and similar for SECOND_ORDER_VARIABLE, but that didn’t work
properly since that way it was not possible to match other kind of stuff
with the IDENTIFIER rule.  (It would have been lexed as FOV, but an ID
was expected, so the lexer would barf.  It might be that I interpret the
problem incorrectly, but at least now it works.)  Also just allowing
letters is too simple, I want to allow (almost) every possible Unicode
token.

TIA, H.
-- 
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 257 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20081112/ae0c3662/attachment.bin 


More information about the antlr-interest mailing list