[antlr-interest] NoViableAltException in tree grammar

Tom Smith yotommy at gmail.com
Thu Jul 10 09:40:53 PDT 2008


Jim:

Thanks for the helpful and speedy response.  By trying to craft the simplest
grammar I could that would replicate the problem I am having in a much
larger grammar, I made the (poor) choice to eliminate most of the lexer
rules.  As you point out, that was ill-advised.  I revised the grammar in my
example to include the lexer rules, but without changing the top rule, and
the problem disappeared.  (See below.)

Now I have to go back to my larger grammar and see if I can craft a more
representative test case to help me diagnose the NoViableAltException.
Also: I own and use the ANTLR 3 book, and also use ANTLRworks (though I am
sure not to full advantage). =;^)

Thanks,
Tom.

grammar Test;

options {
    output = AST;
}

top
    :    before after
    |    before
    |    after
    ;

before
    :    BEFORE
    ;

after
    :    AFTER
    ;

BEFORE
    :    'before'
    ;

AFTER
    :    'after'
    ;

WS
    :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
    ;


On Thu, Jul 10, 2008 at 11:41 AM, Jim Idle <jimi at temporal-wave.com> wrote:

>  On Thu, 2008-07-10 at 10:05 -0400, Tom Smith wrote:
>
> Hello,
>
> I'm stumped by the case below.  I had assumed the difficulties would begin
> with ambiguities in my grammars, but I'm already stuck with a
> NoViableAltException.
>
> I need to be able to handle the following cases:
> - a followed by b
> - a only
> - b only
>
> My initial parser works OK on all three types of input, but my tree parser
> yields a NoViableAltException when presented with the third case.  That is
> surprising to me since the alternatives have the same form in both parsers.
>
> Suggestions welcome!
>
>
> AS you are new to ANTLR, I strongly suggest that you do not use quoted
> strings in the parser, but create real rules for the lexer. This will avoid
> confusion between your literal strings and where the tokens are coming from
> etc. You are probably experimenting with tree rewrite rules, but you don't
> need them in this case as you are not shaping the tree. Then, while ANTLR
> will generally work things out for you, you should really factor all your
> parser rules for common sequences, then rewrite the token stream with
> different root nodes if you need to distinguish between combinations. This
> will generally give you the most streamlined parser and an AST with no
> ambiguities (which is what you want). So:
>
>
> top
>     : BEFORE AFTER?
>     | AFTER
>     ;
>
> Will work for both parser and tree parser (assuming that this was in fact
> your entire grammar) Your lexer is then:
>
> BEFORE  : 'before'    ;
> AFTER   : 'after'     ;
> WS      : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;} ;
>
> If you have not already done so, and can afford it, I suggest you buy the
> ANTLR 3 book as it has a lot of good information. Stealing pieces of grammar
> from the examples is also a good strategy.
>
> Jim
>
>  Thanks,
> Tom.
> ===
> grammar Test;
>
> options {
>     output = AST;
> }
>
> top
>     :    before after
>     ->  before after
>     |    before
>     ->  before
>     |    after
>     ->  after
>     ;
>
> before
>     :    'before'
>     ;
>
> after
>     :    'after'
>     ;
>
> WS
>     :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
>     ;
>
> ===
> tree grammar TestTree;
>
> options {
>     ASTLabelType = CommonTree;
> }
>
> top
>     :    before after
>     |    before
>     |    after
>     ;
>
> before
>     :    'before'
>     ;
>
> after
>     :    'after'
>     ;
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080710/1133af51/attachment-0001.html 


More information about the antlr-interest mailing list