[antlr-interest] Parser ambiguity, predicate help?

Ted Villalba ted.villalba at gmail.com
Thu Sep 20 16:05:31 PDT 2007


Hi,
For posterity, I think I fixed this by adding another rule specifically to
separate terms that occurred between parenthesis from those that occurred
outside parenthesis.
This new addition seemed to fix the parsing.

So:

start   : (query)+
        ;

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_  : vterms+ (operator^ value)*
        | LPAREN! value RPAREN! ( operator^ value)*
        ;

tag        : WCHAR -> ^(WCHAR)
        ;

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

vterms    : WCHAR+ -> ^(TERMS WCHAR+)
        | QUOTE (WCHAR)+ QUOTE -> ^(TERMS WCHAR+)// strip QUOTEs
        | QUOTE (boolTerm)+ QUOTE -> ^(TERMS boolTerm+)// strip QUOTEs
        ;



Thank you,
Ted

On 9/20/07, Ted Villalba <ted.villalba at gmail.com> wrote:
>
> 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/0707b415/attachment-0001.html 


More information about the antlr-interest mailing list