[antlr-interest] ANTLR Optional statements

jg z zjg1964 at yahoo.com
Fri Apr 5 09:16:31 PST 2002




> On Fri, Apr 05, 2002 at 07:30:58AM -0800, jg z
> wrote:
> > 			case ANTLR_USE_NAMESPACE(antlr)Token::EOF_TYPE:
> > 			{
> > 				break;
> > 			}
> 
> > The problem is, if there is no "ID", it would go
> into
> > default and throw an Exception.  It shouldn't do
> like
> > this as an optional statement, should it?
> 
> I suspect there is a problem in the context of the
> class_head clause. If I
> copy paste the production into an empty grammar it
> produces code like
> above. (Correctly bailing out on EOF)
> 
> If I add a production after class_head and
> regenerate the lookahead of the
> next production cleanly shows up in the switch
> (providing a break out
> without exception). (With prerelease version)
> 
> e.g input like:
> 
> class_head
>    :    
>     (  STRUCT
>     |  UNION
>     |  CLASS
>     )
>     (ID ( base_clause)? )?
>    ;
> 
> stuff: ID "=" DIGIT;
> 
> body:	"{" ( stuff )+ "}" ;
> 
> base_clause: SUMTHIN ;
> 
> Generates (relevant bit only (slightly edited)):
> 
> switch ( LA(1)) {
> case ID:
> {
> 	match(ID);
> 	{
> 		switch ( LA(1)) {
> 		case SUMTHIN: {
> 			base_clause();
> 			break;
> 		}
> 		case 10: {	break;	}
> 		default:
> 			{
> 				throw NoViableAltException(LT(1),
> getFilename());
> 			}
> 		}
> 	}
> 	break;
> }
> case 10:
> 	{
> 		break;
> 	}
> default:
> 	{
> 		throw NoViableAltException(LT(1), getFilename());
> 	}
> }
> 
> Which seems correct to me. (the added case 10's) I
> suspect that the problem
> is not in this actual rule but in some rules around
> it.
> 
> Cheers,
> 
> Ric

Thank you, Ric,
    I tried to manually change the .cpp code to make
it look like yours,

		switch ( LA(1)) {
		case ID:
		{match(ID);
			{
			switch ( LA(1)) {
			case COLON:
			{
				base_clause();
				break;
			}
//case ANTLR_USE_NAMESPACE(antlr)Token::EOF_TYPE:
			case 10:{break;	}
			default:{throw
ANTLR_USE_NAMESPACE(antlr)NoViableAltException(LT(1),
getFilename());	}
                                        }}
		break;
		}
//case ANTLR_USE_NAMESPACE(antlr)Token::EOF_TYPE:
		case 10:{break;	}
		default:
		{throw
ANTLR_USE_NAMESPACE(antlr)NoViableAltException(LT(1),
getFilename());	}              }

When I run it, without ID in the end of class_head in
my test file, it still goes into default and throw
Exception.

     Cheers.


__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

 

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



More information about the antlr-interest mailing list