[antlr-interest] simple(?) grammar question using antlrworks

Andrew Houghton aah at roarmouse.org
Mon Apr 23 21:55:51 PDT 2007


Never done this before, no idea what I'm doing, etc.

I'm in need of a way of reading a stream of characters and creating a  
java Map based on the input.  The grammar is simple, and basically  
defines some delimiters, a key (alpha, no whitespace), a value (a  
number, or alphanum w/ spaces, or a list of values, or a Map).  For  
example:

{ a: 'b', c: 4, d: [ 'e', 'f', 5, 'g' ] }

represents a Map with three keys, one of which is a list of values.  
It's likely that I'll need to expand the legal characters in a value  
to allow punctuation, but for now simple alphanum and whitespace  
works (or would work) fine.

I thought this would be fairly simple, and proceeded to flail about  
in ANTLRWorks, but I can't seem to get the grammar to do what I  
want.  Everything compiles, but I keep getting "NoViableAltException"  
and at this point I'm simply confused.  Could I get a brief  
explanation of what I'm doing wrong here?  Thanks for any help..  - a.

grammar PropsHash;

propsHash
	:	LBRACE atom (COMMA atom)* RBRACE EOF
	;
atom
	:	KSTRING COLON value
	;
value
	: NUMBER
	| vstring
	| propsList
	| propsHash
	;
propsList
	:	LBRACKET value (COMMA value)* RBRACKET
	;
vstring
	:	QUOTE VSTRING QUOTE
	;
	
QUOTE 	:	'"' ;
COMMA		:	',' ;
COLON		:	':'	;
LBRACKET:	'['	;
RBRACKET:	']'	;
LBRACE	:	'{'	;
RBRACE	:	'}'	;
NUMBER	:	INT ('.' INT)? ;
protected INT			:	'0'..'9'+ ;
KSTRING	:	( 'a'..'z' |
						'A'..'Z'
					)+ ;
VSTRING	:	( 'a'..'z'
				|		'A'..'Z'
				|		' '
				|		'\t'
				|		'0'..'9'
					)+ ;
WS    	: ( ' '
				|	'\r' '\n'
				|	'\n'
				|	'\t'
					)	;




More information about the antlr-interest mailing list