IcaroPeretti

compiladoresDeMerda

Aug 5th, 2021 (edited)
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. options
  2. {
  3.   JDK_VERSION = "1.5";
  4.   STATIC = false;
  5. }
  6.  
  7. PARSER_BEGIN(Calculator)
  8.  
  9. public class Calculator
  10. {
  11.     public static void main(String args[]) throws
  12.     ParseException
  13. {
  14.     Calculator parser = new Calculator(System.in);
  15.     while (true)
  16.     {
  17.     parser.parseLine();
  18.     }
  19.   }
  20. }
  21.  
  22. PARSER_END(Calculator)
  23.  
  24. SKIP :
  25. {
  26. " "
  27. | "\r"
  28. | "\t"
  29. }
  30.  
  31. TOKEN:
  32. {
  33. < NUMBER: (<DIGIT>)+ ( "."(<DIGIT>)+ )? >| < DIGIT: ["0"-"9"] >| < EOL: "\n" >
  34. }
  35.  
  36. void parseLine():
  37. {
  38.   double a;
  39. }
  40. {
  41.   a=expr() < EOL > {System.out.println(a); }
  42.   |< EOL >
  43.   |< EOF > {System.exit(-1); }
  44. }
  45.  
  46. double expr():
  47. {
  48.   double a;
  49.   double b;
  50. }
  51.  
  52. {
  53.   a=term()
  54.   (
  55.     "+" b=term() { a += b; }
  56.    |"-" b=term() { a -= b; }
  57.  
  58.   )*
  59.   { return a; }
  60. }
  61.  
  62. double term():
  63. {
  64.   double a;
  65.   double b;
  66. }
  67. {
  68.   a=unary()
  69.   (
  70.     "*" b=term() { a *= b; }
  71.    |"/" b=term() { a /= b; }
  72.  
  73.   )*
  74.   { return a; }
  75. }
  76.  
  77. double unary():
  78. {
  79.   double a;
  80. }
  81. {
  82.   "-" a=element() { return -a; }
  83.   |a=element() { return a; }
  84. }
  85.  
  86. double element():
  87. {
  88.   Token t;
  89.   double a;
  90. }
  91. {
  92.   t = < NUMBER > { return Double.parseDouble(t.toString()); }
  93.   | "(" a=expr() ")" { return a; }
  94. }
  95.  
Add Comment
Please, Sign In to add comment