[antlr-interest] Problems writing a searchbar language
Aurélien LARIVE
aurelien.larive at 4dconcept.fr
Mon Jan 11 03:51:57 PST 2010
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
More information about the antlr-interest
mailing list