[antlr-interest] NoViableAltException in tree grammar

Jim Idle jimi at temporal-wave.com
Thu Jul 10 08:41:34 PDT 2008


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/e5f26f89/attachment.html 


More information about the antlr-interest mailing list