[antlr-interest] help with a logical query-like expression grammar

Michael Hartman mphartman at yahoo.com
Wed Mar 5 12:00:34 PST 2003


I need a grammar to describe logical query-like expression like the
kinds you'd see in a SQL WHERE clause.  For example,

A = 1

A = 1 AND B >= 2

A = 1 AND (B <= 2)

A = 1 AND (B != 2 OR C = 3)

A = 1 AND (B = 2 AND NOT (C = 3 OR D = 4))

where A, B, C, and D would be identifiers and 1, 2, 3, and 4 would be
values (essentially quoted strings or numbers but again, just identifiers)

I don't need to evaluate these expressions but rather parse them into
an object model I have.

I can't seem to make the mental connection as to where in the parser
or lexer I make calls to my object model to create and associate
objects that represent the elements of the expression.

For example, A = 1 would translate into an instance of type Expression
with an object of type Operator representing the equals sign.  Another
Expression would represent B <= 2 and yet another would represent the
whole expression A = 1 AND B <= 2 with Operator representing the AND
and two references to the first two Expression instances.

I'm not a computer scientist and I've reviewed the ANTLR doc and
samples but it is very difficult to grasp.  

THis is what I have so far:

class FilterParser extends Parser;

startRule
    :   
    ;

class FilterLexer extends Lexer;

LPAREN:	'('
	;

RPAREN:	')'
	;

EQUALS: '='
  ;

NOTEQUALS: "!="
  ;

GT: '>'
  ;

GTE: '>='
  ;

LT: '<'
  ;

LTE: '<='
  ;

AND: "AND"
  ;

OR: "OR"
  ;

ANDNOT: "AND NOT"
  ;

ORNOT:  "OR NOT"
  ;

ID
options {
	testLiterals = true;
}
	:	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
	;


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list