Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <Expr> ::= <Term> { + <term> |- <term> }
  2. <Term> ::= <factor> { * <factor> | / <factor> }
  3. <factor> ::= intnum | <expr> )
  4.  
  5. spirit> ./eval
  6. $3 + 2
  7. + 3 2 = 5
  8. $4 + 5 * 6
  9. + 4 * 5 *6 = 34
  10. $(2 + 6) * ( 10 -7)
  11. *+ 26 - 10 7 = 24
  12. $.
  13. spirit>
  14.  
  15.  
  16.  
  17.  
  18. public class InterpreterDemo {
  19. public static boolean precedence( char a, char b ) {
  20. String high = "*/", low = "+-";
  21. if (a == '(') return false; // if (a == '(' && b == ')') return false;
  22. if (a == ')' && b == '(') { System.out.println( ")-(" ); return false; }
  23. if (b == '(') return false;
  24. if (b == ')') return true;
  25. if (high.indexOf( a ) > -1 && low.indexOf( b ) > -1) return true;
  26. if (high.indexOf( a ) > -1 && high.indexOf( b ) > -1) return true;
  27. if (low.indexOf( a ) > -1 && low.indexOf( b ) > -1) return true;
  28. return false;
  29. }
  30. public static String convertToPostfix( String in ) {
  31. StkChar opstk = new StkChar();
  32. StringBuffer out = new StringBuffer();
  33. String opers = "+-*/()";
  34. char topsym = '+';
  35. boolean empty;
  36.  
  37. for (int i = 0; i < in.length(); i++)
  38. if (opers.indexOf( in.charAt(i) ) == -1)
  39. out.append( in.charAt(i) );
  40. else {
  41. while ( ! (empty = opstk.isEmpty())
  42. && precedence( topsym = opstk.pop(), in.charAt(i) ))
  43. out.append( topsym );
  44. if ( ! empty) opstk.push( topsym );
  45. if (empty || in.charAt(i) != ')') opstk.push( in.charAt(i) );
  46. else topsym = opstk.pop();
  47. }
  48. while ( ! opstk.isEmpty()) out.append( opstk.pop() );
  49. return out.toString();
  50. }
  51. public static int evaluate( String in ) {
  52. StkInt stack = new StkInt();
  53. String opers = "+-*/";
  54. for (int a, b, i=0; i < in.length(); i++)
  55. if (opers.indexOf( in.charAt(i) ) == -1)
  56. stack.push( in.charAt(i)-48 );
  57. else {
  58. b = stack.pop(); a = stack.pop();
  59. if (in.charAt(i) == '+') a = a + b;
  60. else if (in.charAt(i) == '-') a = a - b;
  61. else if (in.charAt(i) == '*') a = a * b;
  62. else if (in.charAt(i) == '/') a = a / b;
  63. stack.push( a );
  64. }
  65. return stack.pop();
  66. }
  67. public static void main( String[] args ) {
  68. System.out.print( args[0] );
  69. String postfix = convertToPostfix( args[0] );
  70. System.out.print( " -- " + postfix );
  71. System.out.println( " -- " + evaluate( postfix ) );
  72. }
  73.  
  74. class StkChar {
  75. private char[] arr = new char[9];
  76. private int sp = -1;
  77. void push( char ch ) { if ( ! isFull()) arr[++sp] = ch; }
  78. char pop() { if (isEmpty()) return ''; return arr[sp--]; }
  79. boolean isFull() { return sp == arr.length-1; }
  80. boolean isEmpty() { return sp == -1; }
  81. }
  82.  
  83. class StkInt {
  84. private int[] arr = new int[9];
  85. private int sp = -1;
  86. void push( int ch ) { if ( ! isFull()) arr[++sp] = ch; }
  87. int pop() { if (isEmpty()) return 0; return arr[sp--]; }
  88. boolean isFull() { return sp == arr.length-1; }
  89. boolean isEmpty() { return sp == -1; }
  90. }}
  91.  
  92. 2+3*4-5+6 -- 234*+5-6+ -- 15
  93. (2+3)*4-5+6 -- 23+4*5-6+ -- 21
  94. 2+3*(4-5)+6 -- 2345-*+6+ -- 5
  95. 2+3*((4-5)+6) -- 2345-6+*+ -- 17
  96. (3-(4*(5+6))/(7-8))*9/4 -- 3456+*78-/-9*4/ -- 105
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement