[antlr-interest] Possible bug in generated code

mzukowski at yci.com mzukowski at yci.com
Mon Nov 4 07:52:39 PST 2002


Could you post the smallest java file that triggers the bug?  This will help
me diagnose it without wild speculation.

Monty

-----Original Message-----
From: Anakreon Mejdi [mailto:amejdi at ertonline.gr]
Sent: Monday, November 04, 2002 7:32 AM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] Possible bug in generated code


Hello antlr users.

I faced a problem with the Parser generated by antlr, and have possibly 
dicovered a bug in the code generated.

 From a rule declared as:
if_rule
   :
   IF^ LPAREN! expression RPAREN! statement
   (
       options {
         warnWhenFollowAmbig = false;
       }
       :
       ELSE! statement
   )?
   ;
wich is copied from java.g the code generated is:

	public final void if_rule() throws RecognitionException, 
TokenStreamException {
		
		returnAST = null;
		ASTPair currentAST = new ASTPair();
		AST if_rule_AST = null;
		
		AST tmp52_AST = null;
		if (inputState.guessing==0) {
			tmp52_AST = (AST)astFactory.create(LT(1));
			astFactory.makeASTRoot(currentAST, tmp52_AST);
		}
		match(IF);
		AST tmp53_AST = null;
		tmp53_AST = (AST)astFactory.create(LT(1));
		match(LPAREN);
		expression();
		if (inputState.guessing==0) {
			astFactory.addASTChild(currentAST, returnAST);
		}
		AST tmp54_AST = null;
		tmp54_AST = (AST)astFactory.create(LT(1));
		match(RPAREN);
		statement();
		if (inputState.guessing==0) {
			astFactory.addASTChild(currentAST, returnAST);
		}
		{
		if ((LA(1)==ELSE) && (_tokenSet_0.member(LA(2)))) {
			AST tmp55_AST = null;
			tmp55_AST = (AST)astFactory.create(LT(1));
			match(ELSE);
			statement();
			if (inputState.guessing==0) {
				astFactory.addASTChild(currentAST,
returnAST);
			}
		}
		else if ((_tokenSet_6.member(LA(1))) &&
(_tokenSet_7.member(LA(2)))) {
		}
		else {
			throw new NoViableAltException(LT(1),
getFilename());
		}
		
		}
		if_rule_AST = (AST)currentAST.root;
		returnAST = if_rule_AST;
	}

My objection to this code is that the ELSE part is optional,
so why  does this code:
		else if ((_tokenSet_6.member(LA(1))) &&
(_tokenSet_7.member(LA(2)))) {
		}
		else {
			throw new NoViableAltException(LT(1),
getFilename());
		}
exists?
The parser fails to parse a file when after the "if" part is matched an 
EOF Token follows.

To prove my theory I removed the
else if ((_tokenSet_6.member(LA(1))) && ....
line and indead the parser worked just fine.

My version of antlr is antlr-2.7.1 and jdk1.4.


If this is after all a bug and is fixed in antlr-2.7.2
I apologize for mentioning again.

Thanks in advance.
Anakreon



 

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


 

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



More information about the antlr-interest mailing list