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

mzukowski at yci.com mzukowski at yci.com
Wed Mar 5 13:24:27 PST 2003


There is an Oracle SQL grammar available from antlr.org.  Your best bet
might be to steal from it.  

In fact your examples are very much like the calculator example included in
the distribution.  I recommend starting there.

Also don't forget Ter's recent lectures at
http://www.cs.usfca.edu/~parrt/course/652/index.html

Monty

-----Original Message-----
From: Michael Hartman [mailto:mphartman at yahoo.com]
Sent: Wednesday, March 05, 2003 12:01 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] help with a logical query-like expression
grammar


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/ 


 

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



More information about the antlr-interest mailing list