Simple calculator with antlr (sin, cos, tan, pi)
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, June 17, 2005 04:12 PM

I want to create some functions to this simple calculator (sin, cos, tan, pi) Any Ideas? Thanks, Jörg class L extends Lexer; options { filter =WS; } ZAHL: ('0'..'9')+; PLUS: '+'; MINUS: '-'; MAL: '*'; GETEILT: '/'; NL: ' '; protected WS: ' '; class lcalc extends Parser; { public static void main (String[] args) throws Exception { L lexer = new L(System.in); lcalc parser = new lcalc(lexer); parser.eingabe(); } } eingabe: (ausdruck   More>>

I want to create some functions to this simple

calculator (sin, cos, tan, pi)



Any Ideas?



Thanks, Jörg



class L extends Lexer;

options { filter =WS; }





ZAHL: ('0'..'9')+;

PLUS: '+'; MINUS: '-';

MAL: '*'; GETEILT: '/';

NL: '
';

protected WS: ' ';



class lcalc extends Parser;

{

public static void main (String[] args)

throws Exception {

L lexer = new L(System.in);

lcalc parser = new lcalc(lexer);

parser.eingabe();

}

}



eingabe: (ausdruck | "
")*;



ausdruck

{ int val; }

: val=summe {System.out.println(val); };



summe returns [int val]

{ int tmp; val=0;}

: val=term

( "+" tmp=term { val += tmp; }

| "-" tmp=term { val -= tmp; } )*;



term returns [int val]

{ int tmp; val=0;}

: val=faktor

( "*" tmp=faktor { val *= tmp; }

| "/" tmp=faktor { val /= tmp; } )*;



faktor returns [int val]

{ val=0;}

: z:ZAHL {val=Integer.parseInt(z.getText()); }

| "(" val=summe ")";

   <<Less
About | Sitemap | Contact