Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. package expression;
  2.  
  3. /**
  4. * @author Bogdan Timchenko
  5. */
  6. public class CheckedParser implements Parser{
  7. private String unparse;
  8. private int left;
  9. private int len;
  10.  
  11. public TripleExpression parse(String str) throws RuntimeException {
  12. unparse = str;
  13. left = 0;
  14. len = unparse.length();
  15. TripleExpression exp;
  16. exp = PlusMinus();
  17. unparse = "";
  18. len = 0;
  19. left = 0;
  20. return exp;
  21. }
  22.  
  23. private TripleExpression PlusMinus() throws RuntimeException {
  24. TripleExpression acc = MulDiv();
  25. TripleExpression res;
  26. while (len > left) {
  27. delSpaces();
  28. char sign = unparse.charAt(left);
  29. if (!(sign == '+' || sign == '-')) {
  30. break;
  31. }
  32. left++;
  33. res = MulDiv();
  34. if (sign == '+') {
  35. acc = new CheckedAdd(acc, res);
  36. } else {
  37. acc = new CheckedSubtract(acc, res);
  38. }
  39. }
  40. if (len > left) {
  41. throw new RuntimeException("Cannot parse: " + '"' + unparse.substring(left) + '"');
  42. }
  43. return acc;
  44. }
  45.  
  46. private TripleExpression MulDiv() throws RuntimeException {
  47. TripleExpression acc = Bracket();
  48. TripleExpression res;
  49. while (true) {
  50. delSpaces();
  51. if (left == len) {
  52. return acc;
  53. }
  54. char sign = unparse.charAt(left);
  55. if (!(sign == '*' || sign == '/')) {
  56. return acc;
  57. }
  58. left++;
  59. res = Bracket();
  60. if (sign == '*') {
  61. acc = new CheckedMultiply(acc, res);
  62. } else {
  63. acc = new CheckedDivide(acc, res);
  64. }
  65. }
  66. }
  67.  
  68. private TripleExpression Bracket() throws RuntimeException {
  69. delSpaces();
  70. char firstCh = unparse.charAt(left);
  71. if (firstCh == '(') {
  72. left++;
  73. TripleExpression res = PlusMinus();
  74. if (unparse.charAt(left) != ')') {
  75. throw new RuntimeException("Missing ')'");
  76. }
  77. left++;
  78. return res;
  79. }
  80. return ConstVar();
  81. }
  82.  
  83. private TripleExpression ConstVar() throws RuntimeException {
  84. delSpaces();
  85. String var = unparse.substring(left, left + 1);
  86. if (var.equals("x") || var.equals("y") || var.equals("z")) {
  87. left++;
  88. return new Variable(var);
  89. }
  90. return Minus();
  91. }
  92.  
  93. private TripleExpression Minus() throws RuntimeException {
  94. delSpaces();
  95. if (unparse.charAt(left) == '-') {
  96. if (len - left > 1 && !Character.isDigit(unparse.charAt(left + 1))) {
  97. left++;
  98. return new CheckedNegate(Bracket());
  99. }
  100. }
  101. return Num();
  102. }
  103.  
  104. private TripleExpression Num() throws RuntimeException {
  105. delSpaces();
  106. int i = 0;
  107. if (unparse.charAt(left) == '-') {
  108. i++;
  109. }
  110. while (i < len - left && Character.isDigit(unparse.charAt(left + i))) {
  111. i++;
  112. }
  113. if (i == 0) {
  114. throw new RuntimeException("Unexpected symbol: '" + unparse.charAt(left) + "'");
  115. }
  116. int num = Integer.parseInt(unparse.substring(left, left + i));
  117. left += i;
  118. return new Const(num);
  119. }
  120.  
  121. private void delSpaces() {
  122. while (left < len) {
  123. if (!Character.isWhitespace(unparse.charAt(left))) {
  124. break;
  125. }
  126. left++;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement