[antlr-interest] ANTLR Optional statements

Ric Klaren klaren at cs.utwente.nl
Fri Apr 5 07:54:38 PST 2002


Hi,

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
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
	  Innovation makes enemies of all those who prospered under the old
	regime, and only lukewarm support is forthcoming from those who would
					prosper under the new. --- Niccolò Machiavelli


 

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



More information about the antlr-interest mailing list