Advertisement
Guest User

Untitled

a guest
Nov 10th, 2014
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. PARSER_BEGIN(ExpEval)
  2.  
  3. import java.io.*;
  4.  
  5. /** IPIPIPIPIPIPIP */
  6. public class ExpEval {
  7. public static void main(String args[]) {
  8. System.out.println("Reading from " + args[0]);
  9. ExpEval parser;
  10. try
  11. {
  12. parser = new ExpEval(new FileInputStream(args[0]));
  13. }
  14. catch(FileNotFoundException e)
  15. {
  16. System.out.println("File not found : " + args[0]);
  17. return;
  18. }
  19.  
  20. try
  21. {
  22. SimpleNode n = parser.Start();
  23. n.dump("");
  24. System.out.println("Done.");
  25. }
  26. catch (Exception e) {
  27. System.out.println("Error : ");
  28. System.out.println(e.getMessage());
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33.  
  34. PARSER_END(ExpEval)
  35.  
  36. SKIP:
  37. {
  38. " " | "\t" | "\r" | "\n"
  39. }
  40.  
  41. TOKEN: /* NUMBER */
  42. {
  43. <NUMBER: <NATURAL> <FLOATING> >
  44. | <#NATURAL: (["0"-"9"])+ >
  45. | <#FLOATING: ("." (["0"-"9"])+)? >
  46. }
  47.  
  48. TOKEN: /* VARIABLE */
  49. {
  50. <VARIABLE: <LETTER> (<LETTER>|<DIGIT>)*>
  51. | <#LETTER: ["a"-"z", "A"-"Z"] >
  52. | <#DIGIT: ["0"-"9"] >
  53. }
  54.  
  55. TOKEN: /* OPERATOR */
  56. {
  57. <PLUS: "+">
  58. | <MINUS: "-">
  59. | <TIMES: "*">
  60. | <DIVIDE: "/">
  61. | <MODULO: "%">
  62. }
  63.  
  64. void Natural() : {Token t;}
  65. {
  66. t=<NUMBER> { jjtThis.val = t.image; }
  67. }
  68.  
  69. void Number() : {}
  70. {
  71. (Minus())? Natural()
  72. }
  73.  
  74. void Variable() : {Token t;}
  75. {
  76. t=<VARIABLE> { jjtThis.val = t.image; }
  77. }
  78.  
  79. void Plus() : {Token t;}
  80. {
  81. t=<PLUS> {jjtThis.op = "+";}
  82. }
  83.  
  84. void Minus() : {Token t;}
  85. {
  86. t=<MINUS> {jjtThis.op = "-";}
  87. }
  88.  
  89. void Times() : {Token t;}
  90. {
  91. t=<TIMES> {jjtThis.op = "*";}
  92. }
  93.  
  94. void Divide() : {Token t;}
  95. {
  96. t=<DIVIDE> {jjtThis.op = "/";}
  97. }
  98.  
  99. void Modulo() : {Token t;}
  100. {
  101. t=<MODULO> {jjtThis.op = "%";}
  102. }
  103.  
  104. void PlusOp(): {}
  105. {
  106. Plus() product()
  107. }
  108.  
  109. void MinusOp(): {}
  110. {
  111. Minus() product()
  112. }
  113.  
  114. SimpleNode Start(): {}
  115. {
  116. sum() <EOF>
  117. { return jjtThis; }
  118. }
  119.  
  120. /* Somme = Produit, {(+|-), Produit} */
  121. void sum():
  122. {}
  123. {
  124. product() products()
  125. }
  126.  
  127. void products():
  128. {}
  129. {
  130. (PlusOp() | MinusOp())*
  131. }
  132.  
  133. void TimesOp(): {}
  134. {
  135. Times() term()
  136. }
  137.  
  138. void DivideOp(): {}
  139. {
  140. Divide() term()
  141. }
  142.  
  143. void ModuloOp(): {}
  144. {
  145. Modulo() term()
  146. }
  147.  
  148. /* Produit = Terme, {(+|/|%), Terme} */
  149. void product():
  150. {}
  151. {
  152. term() terms()
  153. }
  154.  
  155. void terms():
  156. {}
  157. {
  158. (TimesOp() | DivideOp() | ModuloOp())*
  159. }
  160.  
  161. /* Terme = "(",Somme,")" | Nombre | Variable */
  162. void term():
  163. {}
  164. {
  165. "(" sum() ")" | Number() | Variable()
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement