[antlr-interest] Special Characters in Identifiers

Martin Probst mail at martin-probst.com
Mon May 15 07:54:30 PDT 2006


Hi,

> I am not yet an expert in writing grammars, until now I only worked  
> with the generated AST.

so good luck getting one, it's actually not that difficult with ANTLR.

> We want to allow * and ? in classnames or methodnames e.g.
>
> class k*
> class k?
> class hello*orld
>
> Could that work with my own extension with something like:
>
> identifierStarQuestion
>     :    IDENT
>         ( STAR IDENT )*
>         ( QUESTION IDENT  )*
>     ;

Probably not, this does not allow a star at the end of an identifier  
(e.g. always needs trailing IDENT) and it doesn't allow question  
marks before stars, e.g. foo?bar*baz is not allowed, too. Probably

identifierStarQuestion:
	IDENT ( STAR | QUESTION )? identifierStarQuestion?;

This will allow everything as an identifier which starts with an  
identifier, and then contains any amount of STARs and QUESTION marks,  
but not two consecutive wildcards. It may or may not end in an  
indentifier part. Please note that this is running recursive, so you  
shouldn't put code into identifierStarQuestion that is supposed to  
run only once - you can fix that by using another intermediate rule.  
This might lead to ambiguity wrt. the grammar (Java?), but I'm not  
sure. ANTLR will probably tell you :-)

Martin


More information about the antlr-interest mailing list