[antlr-interest] Problem with position of parenthesis term
Friedhelm
fhellerhoff at yahoo.de
Mon Dec 3 11:00:42 PST 2007
Maybe it is my misunderstanding, but I have problems regarding
parenthese is my logic (please see my grammar below)
I have added rules for handling AND and OR and for expressions in
parenthesis.
But now, depending on the positition of the parenthesis term, the lexer
does not evaluate correctly.
working: AGE>0 AND (AGE<10 OR WEIGHT<500)
NOT working (AGE<10 OR WEIGHT<500) AND AGE>0
What is my mistake? Does anyone have an example with similar logic?
Thanks for any answer,
Fridi
------------------------------------------------------------------------------------------------------------------------------------------------------
grammar Rule1;
options {
language = CSharp;
output=AST;
ASTLabelType=CommonTree;
}
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
tokens
{
AND = 'AND';
OR = 'OR';
}
// ensure that ANTLR will NOT catch exceptions itself
@rulecatch {
catch (RecognitionException e) {
throw e;
}
}
WS : (' '
| '\t'
| '\n'
| '\r')
{ channel=HIDDEN; }
;
LPAR
: '('
;
RPAR
: ')'
;
// grösser als (greather than)
GT
: '>'
;
// kleiner als (less than)
LT_
: '<'
;
// kleiner oder gleich (less equals)
LEQ
: '<='
;
// grösser oder gleich (less equals)
GEQ
: '>='
;
// gleich (equals)
EQ
: '='
;
// Eine Zahl
INT
: DIGIT+
;
DATE
: '\"' '0'..'3' '0'..'9' '\.' '0'..'1' '0'..'2' '\.' DIGIT DIGIT
DIGIT DIGIT '\"'
;
fragment
DIGIT
: '0'..'9'
;
fragment
LETTER
: 'A'..'Z' | 'a'..'z'
;
IDENT
: LETTER (INT | LETTER | '_')+
;
STRING
: '\"'! (~('\"'|'\r'|'\n'|'\.'))* '\"'!
;
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
comparable
: IDENT
| INT
| DATE // put date before string!
| STRING
;
scalarCondition
: IDENT (GT|LT_|LEQ|GEQ|EQ)^ comparable
;
condition
: LPAR! expression RPAR!
| scalarCondition
;
term
: condition (AND^ condition)*
;
expression
: term (OR^ term )*
;
More information about the antlr-interest
mailing list