[antlr-interest] Problems writing a searchbar language
Edgar Espina
espina.edgar at gmail.com
Mon Jan 11 04:04:16 PST 2010
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> 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