[antlr-interest] Boolean and Arithmetic expression difficulty

Suneel Chinchanikar suneel.chinchanikar at xecomit.com
Mon Jun 20 00:23:07 PDT 2005


Hello all,

I am building a simple expression parser, which needs to understand general
boolean expressions similar to the following:

( ( ( 3 + 5 ) == (6 + 2) ) OR ( (5 - 3) == ( 1 + 1) ) ) AND ( ( 1 + 9 ) ==
( 3 + 7 ) )

My main difficulty is that an arithmetic expression can be surrounded by
brackets and a boolean expression can also be surrounded by brackets.

I have tried the following grammar.

I am getting nondeterminism error on the LB token.

Can somebody help?

Thanks
Suneel

=====================

options {
	language = "CSharp";
}

class reqdFields extends Parser;
options {
	buildAST = true;	// uses CommonAST by default
}

rule: logical_or_exp SEMI!;

logical_or_exp: logical_and_exp (OR^ logical_and_exp)*
		;

logical_and_exp: bool_term (AND^ bool_term)*
	         ;

bool_term: LB! logical_or_exp RB!
  |	plus_minus_exp cond_operator plus_minus_exp
  |	bool_field
  ;

plus_minus_exp: mult_div_exp (PLUS_MINUS^ mult_div_exp)*
			  ;

mult_div_exp : arith_term (MULT_DIV^ arith_term)*
	         ;

arith_term	: LB! plus_minus_exp RB!
	  	|	REAL
  		;

bool_field : TRUE | FALSE ;

cond_operator : EQUALS ;

class reqdFieldsLexer extends Lexer;

WS	:	(' '
	|	'\t'
	|	'\n'
	|	'\r')
		{ _ttype = Token.SKIP; }
	;

PLUS_MINUS :'+'|'-';

MULT_DIV :'*'|'/';

TRUE : "TRUE" ;
FALSE : "FALSE" ;

REAL: INT(DOT! INT!)?;

protected
DIGIT
	:	'0'..'9'
	;

protected
INT	:	(DIGIT)+
	;

DOT : "." ;

AND: "AND"
	;
OR : "OR" ;

LB : '(' ;
RB : ')';

EQUALS: "==" ;

SEMI : 	';' ;

=======================



This message was checked by MailScan for WorkgroupMail.
www.workgroupmail.com 



More information about the antlr-interest mailing list