Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- options
- {
- JDK_VERSION = "1.5";
- STATIC = false;
- }
- PARSER_BEGIN(Calculator)
- public class Calculator
- {
- public static void main(String args[]) throws
- ParseException
- {
- Calculator parser = new Calculator(System.in);
- while (true)
- {
- parser.parseLine();
- }
- }
- }
- PARSER_END(Calculator)
- SKIP :
- {
- " "
- | "\r"
- | "\t"
- }
- TOKEN:
- {
- < NUMBER: (<DIGIT>)+ ( "."(<DIGIT>)+ )? >| < DIGIT: ["0"-"9"] >| < EOL: "\n" >
- }
- void parseLine():
- {
- double a;
- }
- {
- a=expr() < EOL > {System.out.println(a); }
- |< EOL >
- |< EOF > {System.exit(-1); }
- }
- double expr():
- {
- double a;
- double b;
- }
- {
- a=term()
- (
- "+" b=term() { a += b; }
- |"-" b=term() { a -= b; }
- )*
- { return a; }
- }
- double term():
- {
- double a;
- double b;
- }
- {
- a=unary()
- (
- "*" b=term() { a *= b; }
- |"/" b=term() { a /= b; }
- )*
- { return a; }
- }
- double unary():
- {
- double a;
- }
- {
- "-" a=element() { return -a; }
- |a=element() { return a; }
- }
- double element():
- {
- Token t;
- double a;
- }
- {
- t = < NUMBER > { return Double.parseDouble(t.toString()); }
- | "(" a=expr() ")" { return a; }
- }
Add Comment
Please, Sign In to add comment