[antlr-interest] Re: HELP!!!: with left recursion

aaanwar aaanwar at yahoo.com
Mon Nov 3 03:06:59 PST 2003


ok this is what I have done:

still some problem ... ? anything you can spot.

/*
exp	--> 	let  decs in exp end
	| 	if exp then exp end
	| 	if exp then exp else exp end
	| 	for ID :=  exp to exp do exp end
	| 	while exp do exp end
	| 	lvalue
	| 	lvalue :=exp
	| 	builtin
	| 	ID ( arg_seq  )
	| 	ID ( )
	| 	exp &  exp 
	| 	exp |  exp 
	| 	exp *  exp 
	| 	exp /  exp  
	| 	exp +  exp  
	| 	exp -  exp  
	| 	exp =  exp 
	| 	exp > exp 
	| 	exp >= exp 
	| 	exp < exp 
	| 	exp <= exp 
	| 	exp <> exp 
	| 	INTEGER_LITERAL
	| 	STRING_LIT	
	| 	( exp_seq )
	| 	( )
*/

expr 			: assignExpr;
assignExpr		: booleanExpr ( ":=" booleanExpr )*;
booleanExpr		: compExpr ( ("&" | "|" ) compExpr ) *;
compExpr		: addExpr ( ("=" | "<" | "<=" | ">" | ">=" 
| "<>" ) addExpr ) *;
addExpr			:  multExpr (  "+"  multExpr  ) *;
multExpr		: uniaryExpr( ( "*" | "/" ) uniaryExpr) *;
uniaryExpr		: ("-")* exp ;

exp			: 	LET decs "in" exp END 
				| 	IF exp THEN exp (ELSE exp)? 
END 
				| 	FOR ID ASSIGN  exp TO exp DO 
exp END 
				| 	WHILE exp DO exp END 
				| 	lvalue (ASSIGN exp)? 
				| 	builtin 
				| 	ID LBRACKET (arg_seq )? 
RBRACKET 
				| 	INTEGER_LITERAL 
				| 	STRING_LIT 
				| 	LBRACKET exp_seq RBRACKET 
				| 	LBRACKET RBRACKET ;



--- In antlr-interest at yahoogroups.com, "lgcraymer" <lgc at m...> wrote:
> Look at any of the language grammars--java.g may be the easiest--
and 
> borrow the expression grammar from there as a first step.  Also, it 
> helps to sort the "if" and "for" statements out of the expression 
> grammar.  The big problem is usually preserving operator precedence.
> 
> --Loring
> 
> --- In antlr-interest at yahoogroups.com, "aaanwar" <aaanwar at y...> 
> wrote:
> > Greetings,
> > 
> > Any advise on this:
> > 
> > exp	--> 	let  decs in exp end
> > 	| 	if exp then exp end
> > 	| 	if exp then exp else exp end
> > 	| 	for ID :=  exp to exp do exp end
> > 	| 	while exp do exp end
> > 	| 	lvalue
> > 	| 	lvalue :=exp
> > 	| 	builtin
> > 	| 	ID ( arg_seq  )
> > 	| 	ID ( )
> > 	| 	exp &  exp 
> > 	| 	exp |  exp 
> > 	| 	exp *  exp 
> > 	| 	exp /  exp  
> > 	| 	exp +  exp  
> > 	| 	exp -  exp  
> > 	| 	exp =  exp 
> > 	| 	exp > exp 
> > 	| 	exp >= exp 
> > 	| 	exp < exp 
> > 	| 	exp <= exp 
> > 	| 	exp <> exp 
> > 	| 	INTEGER_LITERAL
> > 	| 	STRING_LIT	
> > 	| 	( exp_seq )
> > 	| 	( )
> > 
> > I tried this but no luck:
> > 
> > exp				: expr (binaryOp expr)*;
> > expr			: 	LET decs "in" exp END 
> > 				| 	IF exp THEN exp (ELSE exp)? 
> > END 
> > 				| 	FOR ID ASSIGN  exp TO exp DO 
> > exp END 
> > 				| 	WHILE exp DO exp END 
> > 				| 	lvalue (ASSIGN exp)? 
> > 				| 	builtin 
> > 				| 	ID LBRACKET (arg_seq )? 
> > RBRACKET 
> > 				| 	INTEGER_LITERAL 
> > 				| 	STRING_LIT 
> > 				| 	LBRACKET exp_seq RBRACKET 
> > 				| 	LBRACKET RBRACKET ;
> > 				
> > binaryOp		:  AMPERSAND
> > 				| OR
> > 				| STAR
> > 				| DIV
> > 				| PLUS
> > 				| MINUS
> > 				| EQUAL
> > 				| GT
> > 				| GTEQ
> > 				| LT
> > 				| LTEQ
> > 				| NOTEQ;


 

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




More information about the antlr-interest mailing list