[antlr-interest] Simple expression grammar

Maciej Bakalarz shipmen at gmail.com
Thu May 15 08:42:04 PDT 2008


Hi again !

OK I have written my grammar for boolean expressions mentioned earlier 
(like ( (a>=3 || b<=5)&& c>=4 )  ).

But my grammar accept also expressions like "abc def ght" and many other 
strange constructions.

Do you have any clues how to change it to accept only boolean 
expressions, -->nothing more<-- ....

This is grammar:

grammar GuardCondition;

options {
     output=AST;
     ASTLabelType=CommonTree;
backtrack=true;
memoize=true;
}

    @rulecatch {
        catch ( RecognitionException ex ) {
            throw ex;
        }
    }

prog:
	logical_or_expression+
	;

logical_or_expression:
	logical_and_expression (OR_OP^ logical_and_expression)*
	;

logical_and_expression
	: rel_expression (AND_OP^ rel_expression)*
	;

rel_expression
	: expression (REL_OP^ expression)*
	| LPAREN! prog RPAREN!
	| NEG^ LPAREN! prog RPAREN!	
	;
expression
	: ID | INT | NEG^ ID
	;

NEG	:	'!';
LPAREN 	:	'(';
RPAREN	:	')';
REL_OP   	: 	 '==' | '<' | '>' | '<=' | '>='|'!=';
OR_OP 	: 	'||';
AND_OP 	:	 '&&';
ID  	:  	ALFA ( ALFA | INT ) *;
ALFA	:	('a'..'z'|'A'..'Z') ;
INT 	:  	 '0'..'9'+;
WS  	:   	(' '|'\t')+ {skip();};


Thanks
Maciej Bakalarz



More information about the antlr-interest mailing list