Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. package Assignment13;
  2.  
  3. /**
  4. * A CalcLexer provides a simple scanner for a
  5. * CalcParser. We hold the string being parsed, and the
  6. * CalcParser uses us to read the string as a sequence
  7. * of tokens.
  8. */
  9. public class CalcLexer {
  10. /**
  11. * The string being parsed, held in a
  12. * StringTokenizer.
  13. */
  14. private java.util.StringTokenizer tokens;
  15.  
  16. /**
  17. * The error message. This will be null if there
  18. * has been no error.
  19. */
  20. private String errorMessage = null;
  21.  
  22. /**
  23. * The current token.
  24. */
  25. private int tokenChar;
  26.  
  27. /**
  28. * If the current token is NUMBER_TOKEN, this is
  29. * the number in question.
  30. */
  31. private double tokenNum;
  32.  
  33. /**
  34. * Non-character values for tokenChar. By choosing
  35. * negative values we are certain not to collide
  36. * with any char values stored in the int tokenChar.
  37. */
  38. public static final int NUMBER_TOKEN = -1;
  39. public static final int EOLN_TOKEN = -2;
  40.  
  41. /**
  42. * Constructor for a CalcLexer. Our parameter is the
  43. * string to be tokenized.
  44. * @param s the String to be tokenized
  45. * @throws CalcError
  46. */
  47. public CalcLexer(String s) {
  48.  
  49. // We use a StringTokenizer to tokenize the string.
  50. // Our delimiters are the operators, parens, and
  51. // white space. By making the third parameter true
  52. // we instruct the StringTokenizer to return those
  53. // delimiters as tokens.
  54.  
  55. tokens = new java.util.StringTokenizer(
  56. s," \t\n\r+-*/()",true);
  57.  
  58. // Start by advancing to the first token. Note that
  59. // this may get an error, which would set our
  60. // errorMessage instead of setting tokenChar.
  61.  
  62. advance();
  63. }
  64.  
  65. /**
  66. * Advance to the next token. We don't return
  67. * anything; the caller must use nextToken() to see
  68. * what that token is.
  69. */
  70. public void advance() {
  71.  
  72. // White space is returned as a token by our
  73. // StringTokenizer, but we will loop until something
  74. // other than white space has been found.
  75.  
  76. while (true) {
  77.  
  78. // If we're at the end, make it an EOLN_TOKEN.
  79.  
  80. if (!tokens.hasMoreTokens()) {
  81. tokenChar = EOLN_TOKEN;
  82. return;
  83. }
  84.  
  85. // Get a token--if it looks like a number,
  86. // make it a NUMBER_TOKEN.
  87.  
  88. String s = tokens.nextToken();
  89. char c1 = s.charAt(0);
  90.  
  91. try {
  92. if(s.length() > 1 && !Character.isDigit(c1)) {
  93. errorMessage = "Illegal format for a number";
  94. throw (new CalcError(errorMessage));
  95. }
  96.  
  97. else if (s.length()>1 || Character.isDigit(c1)) {
  98.  
  99. tokenNum = Double.valueOf(s).doubleValue();
  100. tokenChar = NUMBER_TOKEN;
  101. return;
  102. }
  103.  
  104. // Any other single character that is not
  105. // white space is a token.
  106.  
  107. else if (!Character.isWhitespace(c1)) {
  108. tokenChar = c1;
  109. return;
  110. }
  111. } catch (CalcError e) {
  112. System.out.println(e);
  113. }
  114. }
  115. }
  116.  
  117. /**
  118. * Return our error message. This will be null if no
  119. * error has occurred.
  120. *
  121. * @return error String or null if no error
  122. */
  123. public String getErrorMessage() {
  124. return errorMessage;
  125. }
  126.  
  127. /**
  128. * Return the value of a numeric token. This should
  129. * only be called when nextToken() reports a
  130. * NUMBER_TOKEN.
  131. *
  132. * @return the double value of the number
  133. */
  134. public double getNum() {
  135. return tokenNum;
  136. }
  137.  
  138. /**
  139. * Return the next token. Repeated calls will
  140. * return the same token again; the caller should
  141. * use advance() to advance to another token.
  142. * @return the next token as an int
  143. */
  144. public int nextToken() {
  145. return tokenChar;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement