[antlr-interest] tree construction problem

OJAY78 at gmx.de OJAY78 at gmx.de
Fri May 26 00:16:53 PDT 2006


Could someone please help me!

Hi @ all,


I am trying desperately to write my grammar with antlr and hope anyone can help me a bit. 
Because I am new in this topic I spent a few days with reading about parsing generators. I read a lot about automata for the recognitions of the tokens and contextfree grammars for the parsing. As I understand that a bit better I startet to read a few ANTLR tutorials and found one with a grammar for a calculator. I extended this example a bit but now I get stuck. What I have is a grammar for a calculator and relational Operators for Integers Example
((4+4)>(2+3*1)) -->1(true)

This was a big step for me to get to that. What I want is to generate a language which can handle expressions like this.

((myfunction+myfunction1)>5) AND (myBoolFunction OR myBoolFunction)

Myfunction could be any function that I want to implement. 

What I have done so far is to write a grammar and a lexxer. The grammar uses the buildAST=true option so that I build my parsetree manualy, at least I understand this so. Finally I created a treewalker for this tree.

Ok here is the code of the calculator that works

class ExpressionParser extends Parser;
options { buildAST=true; }
mathexpr 	: LPAREN^ sumExpr RPAREN! ;
sumExpr  	: prodExpr ((PLUS^|MINUS^) prodExpr)* ;
prodExpr 	: powExpr ((MUL^|DIV^|MOD^) powExpr)* ;
powExpr  	: relationalExpression (POW^ relationalExpression)? ;
relationalExpression	  : atom ((EQUALS^|GT^|LT^) atom)*;
atom     : INT | mathexpr ;  



class ExpressionLexer extends Lexer;

PLUS  	: '+' ;
MINUS 	: '-' ;
MUL   	: '*' ;
DIV   	: '/' ;
MOD   	: '%' ;
POW   	: '^' ;
SEMI  	: ';' ;
protected DIGIT : '0'..'9' ;
INT   	: (DIGIT)+ ;
RPAREN	: ')';
LPAREN	: '(';
EQUALS	:'=';
LT		:'<';
GT		:'>';

{import java.lang.Math;
}
class ExpressionTreeWalker extends TreeParser;
options {

}
mathexpr returns [double r]
  { double a,b; r=0; } 


  : #(LPAREN a=mathexpr)	 	{ r=a;   }
  | #(PLUS   a=mathexpr b=mathexpr)  	{ r=a+b; }
  | #(MINUS  a=mathexpr b=mathexpr)  	{ r=a-b; }
  | #(MUL    a=mathexpr b=mathexpr)  	{ r=a*b; }
  | #(DIV    a=mathexpr b=mathexpr)  	{ r=a/b; }
  | #(MOD    a=mathexpr b=mathexpr)  	{ r=a%b; }
  | #(POW    a=mathexpr b=mathexpr)  	{ r=Math.pow(a,b); }
  | #(EQUALS a=mathexpr b=mathexpr) 	{ if (a==b) r=1; else r=0;  	}
  | #(LT     a=mathexpr b=mathexpr)	{ if (a<b)  r=1; else r=0; } 
  | #(GT     a=mathexpr b=mathexpr)	{ if (a>b)  r=1; else r=0; }
  | i:INT 		{ r=(double)Integer.parseInt(i.getText()); };


Now I want to extend this grammar with that

expression	:term (OR^ term | XOR^ term)*;
term		:function (AND^ function)*;
function	:boolFunction | mathexpr;
boolFunction:MYFUNCTIONS | mathexpr;

but I do not really understand how I have to build the tree. Can anyone help how to get that work together?

I am thankful for any advise

Best Regards 
Oliver
-- 


Bis zu 70% Ihrer Onlinekosten sparen: GMX SmartSurfer!
      Kostenlos downloaden: http://www.gmx.net/de/go/smartsurfer
    


More information about the antlr-interest mailing list