[antlr-interest] Newbie: NoViableAltException

Diehl, Matthew J matthew.j.diehl at intel.com
Tue Jul 24 14:08:43 PDT 2007


You're mixing your lexer and parser rules.  IDENT is eating up all of
the UNRESERVED whenever there's more than one, PCHAR if there's only
one.  Then you're saying that 'bar', which is more than one UNRESERVED,
so it is the token IDENT, needs to be a bunch of PCHARs, but the PCHARs
have already been turned in to the token IDENT.

Try being more vague with your lexer if that's what you're trying to do,
because once the lexer's done, it only spits out one token, and you
can't split it up and determine subtokens.  One easy way to find out
whether your lexer is spitting out what you want it to is by making a
parser rule like the following:
getall
 : ident | unreserved | ...<all of the other lex rules in lowercase>...
 ;

ident
 : IDENT
 ;
unreserved
 : UNRESERVED
 ;
...etc.

<insert original lexer stuff>

Then when you ANTLRWorks it, it will show you exactly what your lexer
spat out.  I used this a lot when making mine.

Matt

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org 
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Buck, Robert
> Sent: Tuesday, July 24, 2007 1:51 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Newbie: NoViableAltException
> 
> Hi,
> 
> (foo=bar)
>      ^
> 
> A "NoViableAltException(0!=[null]) exception occurs at 'bar', 
> given the
> following grammar. Not quite sure why. I did not understand this one.
> What could be happening?
> 
> Thanks in advance,
> 
> Bob
> 
> #####################
> 
> prog
> 	:	tuple
> 	;
> tuple
> 	:	L_PAREN tuple_decl EQUALS ( tupleValue | set ) R_PAREN
> 	;
> set
> 	:	L_CURLY tuple ( COMMA tuple )*
> 	;
> tuple_decl
> 	:	tupleKey ( tuplePI )? 
> 	;
> tuplePI
> 	:	AT_SIGN set
> 	;
> tupleKey
> 	:	IDENT
> 	;
> tupleValue
> 	:	( PCHAR )*
> 	;
> 
> /*------------------------------------------------------------------
>  * LEXER RULES
>  *------------------------------------------------------------------*/
> 
> PCHAR
> 	:	PCT_ENCODED | UNRESERVED
> 	;
> IDENT
> 	:	LETTER ( LETTER | UNDERSCORE | HYPHEN | DIGIT)*
> 	;
> L_PAREN
> 	:	'('
> 	;
> R_PAREN
> 	:	')'
> 	;
> L_CURLY
> 	:	'{'
> 	;
> R_CURLY
> 	:	'}'
> 	;
> COMMA
> 	:	','
> 	;
> EQUALS
> 	:	'='
> 	;
> AT_SIGN
> 	:	'@'
> 	;
> HexLiteral
> 	: '0' ('x'|'X') HEXDIGIT+ 
> 	;
> fragment UNRESERVED
> 	:	LETTER | DIGIT | '-' | '.' | '_' | '~' | ':'
> 	;
> fragment PCT_ENCODED
> 	:	'%' HEXDIGIT HEXDIGIT
> 	;
> fragment HEXDIGIT
> 	:	( DIGIT | 'a'..'f' | 'A'..'F' )
> 	;
> fragment LETTER
> 	:	('a'..'z')|('A'..'Z')
> 	;
> fragment DIGIT
> 	:	'0'..'9'
> 	;
> fragment L_BRACKET
> 	:	'['
> 	;
> fragment R_BRACKET
> 	:	']'
> 	;
> fragment UNDERSCORE
> 	:	'_'
> 	;
> fragment HYPHEN
> 	:	'-'
> 	;
> 


More information about the antlr-interest mailing list