[antlr-interest] Simple grammar with error

Johannes Luber jaluber at gmx.de
Sun Sep 16 03:09:56 PDT 2007


Randall R Schulz wrote:
> On Saturday 15 September 2007 15:18, Johannes Luber wrote:
>> Cédric Berger wrote:
>>> ...
>>>> OPEN_PAREN : '(' ;
>>>> CLOSE_PAREN: ')' ;
>>> I keep wondering why people do that...
>>> Is that just to obfuscate the grammar and make it less readable
>>> or is there a real rationale here?
>>>
>>> Cedric
>> Only if you separate lexer and parser grammars. Otherwise it's better
>> to use the implicit token variety.
>>
>> Johannes Luber
> 
> Why is it better not to define names for fixed terminals?
> 
> Randall Schulz
> 

It may be only my preferred aesthetically style but if I see '(' instead
OPEN_PAREN I know instantly what input is required. Otherwise I have to
parse the name in my head. Additionally, in many languages a particular
operator has overloaded meanings. An example:

addressof_expression
	:	BITWISE_AND unary_expression
	;

where

BITWISE_AND : '&';

Whenever I read BITWISE_AND I have to replace it with '&' and reparse it
as OP_ADDRESS. I can't use it in the grammar itself because there won't
be OP_ADDRESS tokens. Of course, I could do a rewrite, but it may not be
worth the effort. In such cases, I'd wish ANTLR would allow to map
BITWISE_AND and OP_ADDRESS to the same token (although the debugger may
be confused).

Best regards,
Johannes Luber


More information about the antlr-interest mailing list