[antlr-interest] Parser ambiguity, predicate help?

Ted Villalba ted.villalba at gmail.com
Thu Sep 20 12:00:05 PDT 2007


Hi,

Thanks in advance for your time investment here.
I have an ambiguity in my parser that I'm not sure how to resolve.
I have a query language that in one instance treats some text as an
operator, and in another treats it as text.
1. SO= eats shoots and leaves //this and is treated as text
2. SO= (eats shoots and leaves) // treated as an operator

Seems whenever I can get it working for one, I break the other. Not sure if
a predicate will fix this , but looking for some input here.
Here is the complete grammar:

grammar WQL;

options{
    output=AST;
    ASTLabelType=CommonTree;
}

tokens{ TAG; VALUE; TERMS; QUERY;} //imaginary token types

@header{
import java.util.HashMap;
}

@members {

HashMap fieldMap = new HashMap();

}





start   : ( query  {System.out.println("AST:\n"+$query.tree.toStringTree());}
)+
        ;

query   : field (BOOL_OP^ query|field)*
    | LPAREN! query RPAREN! (BOOL_OP^ query|field)*
    ;

field     : tag '=' LPAREN value RPAREN -> ^('=' tag value)
    | tag '='  terms -> ^('=' tag ^(TERMS terms) )
        | qid
        ;

value   :  value_ -> ^(VALUE value_) ;

value_  : terms  (operator^ value)*
    | LPAREN! value RPAREN! ( operator^ value)*
    ;

//keyBOOL : {input.LT(1).getText().equals("NEAR")}? terms;

tag    : WCHAR -> ^(WCHAR)
    ;

terms    : (WCHAR|boolTerm)+
    | QUOTE! (WCHAR|boolTerm)+ QUOTE! // strip QUOTEs
    ;

boolTerm: b=BOOL_OP|WOK_OP { $b.setType(WCHAR); } ;

qid     : '#'!DIGIT
        ;

operator: BOOL_OP|WOK_OP;

QUOTE    : '"';
BOOL_OP    : 'AND'|'and'|'OR'|'or'|'NOT'|'not';
WOK_OP    : 'SAME'|'same'|'NEAR'('/'DIGIT+)*|'near'('/'DIGIT+)*;
DIGIT   : ('0'..'9');
WS      : (' '|'\t'|'\r'|'\n'|'??'|'$'|'?'|'*')+ {$channel=HIDDEN;};
LPAREN    : '(' ;
RPAREN    : ')' ;
WCHAR   : ~('='|'('| ')'|'"'|' '|'\t'|'\n'|'\r'|'#'|'??')+;
//QCHAR    : ~('"')+;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070920/adff6bc9/attachment.html 


More information about the antlr-interest mailing list