[antlr-interest] Strange problem with ending symbol of Top rule

gwind04 luc_ost at hotmail.com
Mon Mar 29 08:54:34 PST 2004


Hi,

We found a problem when we were trying to develop a grammar as 
following:

...
class CPreExpr extends Parser;
constant_expression
	:	//logicalOrExpression
		// ...
		primary_expression
		( 	"QUESTION"
			constant_expression
			"COLON"
			constant_expression
 		)?
	;
primary_expression
	:
	"Something"
	|
	"LPAREN" constant_expression "RPAREN"
	;

ANTLR 2.7.3 generated the following codes with it:

void CPreExpr::constant_expression() {
	
	try {      // for error handling
		primary_expression();
		{
		switch ( LA(1)) {
*********	case LITERAL_QUESTION:
		{
			match(LITERAL_QUESTION);
			constant_expression();
			match(LITERAL_COLON);
			constant_expression();
			break;
		}
********	case LITERAL_COLON:
********	case LITERAL_RPAREN:
		{
			break;
		}
		default:
		{
			throw ANTLR_USE_NAMESPACE(antlr)
NoViableAltException(LT(1), getFilename());
		}
		}
		}
	}
...	
}

If we put an ending symbol in the top rule, such as EOF, the code 
generated was:
   ...
********	case LITERAL_QUESTION:
		{
			match(LITERAL_QUESTION);
			constant_expression();
			match(LITERAL_COLON);
			constant_expression();
			break;
		}
********      case ANTLR_USE_NAMESPACE(antlr)Token::EOF_TYPE:
		{
			break;
		}
	      default:
		{
         throw ANTLR_USE_NAMESPACE(antlr)NoViableAltException(LT(1), 
getFilename());
		}
		}
		}
	match(ANTLR_USE_NAMESPACE(antlr)Token::EOF_TYPE);
...

The second part codes were exactly what we wanted. It seemed an 
ending symbol is necessary in the Top rule.May we know, where 
possibly we can find the corresponding explanation, the reason of the 
problem, in ANTLR documents?

Any opinion is appreciated.



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list