[antlr-interest] parser rule precedence

Ted Villalba ted.villalba at gmail.com
Mon Jul 2 09:41:55 PDT 2007


Hi,

I have created a grammar and expected the topmost rules to be selected
first, but this doesn't seem to be the case. Is there an option or setting
that will force this behavior?
For instance if I have :

grammar WQL;

options{
    k=6;
    output=AST;
    ASTLabelType=CommonTree;
}
query : field
        | field (WS!)+ (BOOL_OP^ (WS!)* query)*

field : tag '=' value  -> ^('=' tag value)
        | qid
        ;

value  : term (WS)+ BOOL_OP (WS)* value -> ^(BOOL_OP term BOOL_OP value)


Then passing a query like: AB=" This is a query string" AND CD=Another
string

What I see  through ANTLRWorks debugging is rather than selecting the first
rule in the query option, it selects the value rule and then throws a
"NoViableAltException".
So, rather than evaluate the query as => field AND field
It appears to try to evaluate it using the value rule first,
creating tag=(value AND value).
This is a pretty bad explanation of what's happening here, but I think the
solution is to force the evaluation of the topmost grammar rules first.
Is there a way to enforce this?

Thanks,
Ted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070702/20bdd3ee/attachment.html 


More information about the antlr-interest mailing list