[antlr-interest] Problems writing a searchbar language

Jim Idle jimi at temporal-wave.com
Mon Jan 11 09:36:34 PST 2010


You need to rewrite the absence of AND as the AND keyword for a start as your SPACE becomes the binary operator AND, and so should not just be ignored.

andexpression
     : notexpression ( andWord^ notexpression )*
     ;

andWord : a=AND -> $a
        |       -> AND
        ;

Then you probably want a root node and a rule that consumes to EOF:

search: orexpression EOF
          -> ^(QUERY orexpression)
      ;

And tree:

prog : ^(QUERY expr)
     ;


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Aurélien LARIVE
> Sent: Monday, January 11, 2010 3:52 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Problems writing a searchbar language
> 
> Hi,
> 
> I'm currently writing a small grammar to parse a searchbar language and
> I'm failing at making whitespaces behave like the AND keyword.
> 
> Here is my grammar :
> 
> grammar SearchBar;
> 
> options {
>     output=AST;
> }
> 
> WS  : ( ' ' | '\t' ) { skip(); } ;
> AND : 'AND' ;
> OR  : 'OR' ;
> NOT : 'NOT' ;
> LEFT_PAREN  : '(' ;
> RIGHT_PAREN : ')' ;
> TERM        : ~(' '|'\t'|'"'|RIGHT_PAREN|LEFT_PAREN|NOT|OR|AND)* ;
> QUOTEDTERM  : '"' ~('"')* '"' ;
> 
> orexpression
>     : andexpression ( OR^ andexpression )*
>     ;
> 
> andexpression
>     : notexpression ( (AND^)? notexpression )*
>     ;
> 
> notexpression
>     : (NOT^)? searchterm
>     ;
> 
> searchterm
>     : TERM
>     | QUOTEDTERM
>     | LEFT_PAREN! orexpression RIGHT_PAREN!
>     ;
> 
> And here is my tree grammar :
> 
> tree grammar SearchBarEval;
> 
> options {
>     ASTLabelType=CommonTree;
>     tokenVocab=SearchBar;
> }
> 
> prog
>     : expr+ ;
> 
> expr returns [XMSExpression expression]
>     : ^(OR a=expr b=expr) {
>         $expression = new Or($a.expression, $b.expression);
>     }
>     | ^(AND a=expr b=expr) {
>         $expression = new And($a.expression, $b.expression);
>     }
>     | ^(NOT a=expr) {
>         $expression = new Not($a.expression);
>     }
>     | TERM {
>         $expression = new Term($TERM.text);
>     }
>     | QUOTEDTERM {
>         $expression = new QuotedTerm($QUOTEDTERM.text);
>     }
>     ;
> 
> When I try to evaluate, for example, the input 'apples bananas
> tomatos',
> I only get the Term 'apples'. I understand why I'm having this problem
> but I was unable to find a good solution.
> 
> Thanks in advance,
> 
> --
> Aurélien
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list