[antlr-interest] Problem with position of parenthesis term

Harald M. Müller harald_m_mueller at gmx.de
Mon Dec 3 12:43:33 PST 2007


Hallo Herr Hellerhoff!

Your grammar is fine - both your examples worked when I tried them, as far
as I could see. What is your concrete problem? (error message?)

BTW, you should 
a) reorder it according to ANTLR conventions - i.e., put parser rules before
lexer rules; and write the parser rules in top-down order instead of
bottom-up;
b) maybe indicate the start rule by writing a rule like that:
	rule: expression;
- right now, all top-level rules of your grammar call each other
recursively, so ANTLR issues a warning that it does not know what the "top
level rule" is.

Best regards
Harald M. Müller
Cortex-Brainware

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org 
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Friedhelm
> Sent: Monday, December 03, 2007 8:01 PM
> To: antlr
> Subject: [antlr-interest] Problem with position of parenthesis term
> 
> 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