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

Johannes Luber jaluber at gmx.de
Tue Apr 24 04:03:36 PDT 2007


Andrew Houghton wrote:
> 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.

There are two things I've noticed. The first one is that you used
"protected" instead "fragment". ANTLRworks uses ANTLR3 so it shouldn't
have worked ever. The second one is that you put EOF at the end of a
recursive rule. Obviously you end to have EOF everytime you enter a new
recursion level, making in the end alternatives unavailable. The
solution is create a separate start rule like I did below. BTW, may I
use parts of your grammar in my tutorial
<http://www.antlr.org/wiki/display/ANTLR3/Quick+Starter+on+Parser+Grammars+-+No+Past+Experience+Required>?
I believe you applied my advice about EOF there and I have overlooked
this special case while writing the text.

Best regards,
Johannes Luber


input
	:	propsHash EOF
	;

propsHash
    :    LBRACE atom (COMMA atom)* RBRACE
    ;
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)? ;
fragment 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