[antlr-interest] parse "search criterion"

Gavin Lambert antlr at mirality.co.nz
Fri May 11 13:58:40 PDT 2007


At 04:49 12/05/2007, Stefan Chrobot wrote:
 >> I want to parse a search criterion (with +, -, "", AND, OR) 
like
 >"google" with antlr. For example:
[...]
 >Well, you need to start with the grammar. Maybe something like
 >this:
 >
 >grammar Query;
 >
 >WS	:	(' ' | '\t')+ { $channel = HIDDEN; };
 >OPERATOR:	'+' | '-' | 'AND' | 'OR';
 >SIMPLE_QUERY
 >	:	('a'..'z' | 'A'..'Z' | '0'..'9')+
 >	|	'"' (~'"')+ '"';
 >query	:	
 >	|	SIMPLE_QUERY ((OPERATOR)? SIMPLE_QUERY)*;

It's not quite that simple since there are both unary and binary 
operators involved.  So you'd probably want something more like 
this:

grammar Query;

WS	:	(' ' | '\t')+ { $channel = HIDDEN; };
UNARY_OP: '+' | '-';
BINARY_OP: 'AND' | 'OR';
PHRASE: '"' (~'"')+ '"';
WORD: ('a'..'z' | 'A'..'Z' | '0'..'9')+;

query: element (BINARY_OP? element)* EOF;
element: UNARY_OP? (WORD | PHRASE);



More information about the antlr-interest mailing list