[antlr-interest] Optional keyword causes (correct) warnings in ANTLR

Ramon Verbruggen Ramon.Verbruggen at quintiq.com
Tue Apr 15 07:57:57 PDT 2008


I am working on a grammar for a Delphi/Java like programming language.
In a statementbody the return statement can only be the last statement,
if it is present at all.

I have made a small grammar to illustrate the problem:

grammar ANTLRQuill;

statementBody:		statementList returnStatement? EOF;
	
returnStatement:		'return' expression ';'*; // <== return
keyword should be optional
	
statementList:		(statement ';'*)*;
	
statement:			addressableElement;

expression:			addressableElement ( '*'
addressableElement )*;

addressableElement:	Identifier ( '.' Identifier '()' )*;
	
Identifier:			('a'..'z')+;


Now, I would like to make the 'return' keyword optional, but when I
just insert the question mark after the return keyword, ANTLR rightfully
complains that it has several ways of matching Identifier, and that it
will always choose alternative one. I understand the warning, and I see
that ANTLR's right, but I don't see how else to solve this problem.

I've tried a lot of different possibilities already, and searched the
archives and the ANTLR book, but haven't been able to find a way to make
the return keyword optional. Obviously, the actual grammar is much
bigger and a bit more complex than the example above, so especially
rules statement, expression and addressableElement have more
alternatives, and the expression hierarchy has the usual 'ladder' of
expression types to obtain the correct levels of precedence.

Any help is appreciated.

Best regards,


Ramon Verbruggen


This message contains information that may be privileged or confidential
and is the property of Quintiq. It is only intended for the person to
whom it is addressed. If you are not the intended recipient, you are not
authorized to read, print, retain, copy, disseminate, distribute or use
this message or any part thereof. If you have received this message in
error, please notify the sender immediately and delete all copies of
this message. Please note that e-mails are susceptible to change,
therefore they are not binding.


More information about the antlr-interest mailing list