[antlr-interest] Problems writing a searchbar language

Aurélien LARIVE aurelien.larive at 4dconcept.fr
Mon Jan 11 05:20:36 PST 2010


That does not seem to change anything. Did I miss something ?



Edgar Espina a écrit :
> Hi,
>
>  try this:
>
> WS  : ( ' ' | '\t' ) { $channel=HIDDEN; } ;
>
> Regards
>
> On Mon, Jan 11, 2010 at 8:51 AM, Aurélien LARIVE 
> <aurelien.larive at 4dconcept.fr <mailto:aurelien.larive at 4dconcept.fr>> 
> wrote:
>
>     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
>
>
>
>
> -- 
> edgar


More information about the antlr-interest mailing list