Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.*;
  3.  
  4. /**
  5. * Created by tangmaolei on 9/26/16.
  6. */
  7.  
  8.  
  9. //all terminal symbols: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, (, ), $, ++, --}
  10. enum TokenType {
  11. DIGIT,
  12. PLUS,
  13. MINUS,
  14. LEFTPAREN,
  15. RIGHTPAREN,
  16. REFERENCE,
  17. INCREMENT,
  18. DECREMENT
  19. }
  20.  
  21. class Token {
  22. private TokenType mType;
  23. private int mValue;
  24. private int mLineNum;
  25.  
  26. public Token(TokenType type, int line) {
  27. this.mType = type;
  28. this.mLineNum = line;
  29. }
  30.  
  31. public Token(TokenType type, int value, int line) {
  32. this.mType = type;
  33. this.mValue = value;
  34. this.mLineNum = line;
  35. }
  36.  
  37. public TokenType getType() {
  38. return mType;
  39. }
  40.  
  41. public int getValue() {
  42. return mValue;
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return mType.name();
  48. }
  49. }
  50.  
  51.  
  52. public class Parse {
  53.  
  54. // class Token {
  55. // private TokenType mType;
  56. // private int mValue;
  57. // private int mLineNum;
  58. //
  59. // public Token(TokenType type, int line) {
  60. // this.mType = type;
  61. // this.mLineNum = line;
  62. // }
  63. //
  64. // public Token(TokenType type, int value, int line) {
  65. // this.mType = type;
  66. // this.mValue = value;
  67. // this.mLineNum = line;
  68. // }
  69. //
  70. // public TokenType getType() {
  71. // return mType;
  72. // }
  73. //
  74. // public int getValue() {
  75. // return mValue;
  76. // }
  77. // }
  78.  
  79. // convert a file stream into a string
  80. public static String convertStreamToString(InputStream is) {
  81. Scanner myScanner = new Scanner(is);
  82. myScanner.useDelimiter("\\A");
  83. if (myScanner.hasNext()) {
  84. return myScanner.next();
  85. }
  86. else
  87. return "";
  88. }
  89.  
  90. public static void printErrorInfo(int lineNum) {
  91. System.out.printf("Parse error in line <%d>\n", lineNum);
  92. }
  93.  
  94. //all terminal symbols: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, (, ), $, ++, --}
  95. public static List<Token> scanner(String input) {
  96. List<Token> tokenList = new ArrayList<Token>();
  97. int index = 0;
  98. int range = input.length();
  99. int line = 1;
  100. while (index < range) {
  101. char pattern = input.charAt(index);
  102. switch (pattern) {
  103. case '+':
  104. // INCREMENT
  105. if (input.charAt(index+1) == '+') {
  106. tokenList.add(new Token(TokenType.INCREMENT, line));
  107. index = index + 2;
  108. }
  109. // PLUS
  110. else {
  111. tokenList.add(new Token(TokenType.PLUS, line));
  112. index++;
  113. }
  114. break;
  115. case '-':
  116. // DECREMENT
  117. if (input.charAt(index+1) == '-') {
  118. tokenList.add(new Token(TokenType.DECREMENT, line));
  119. index = index + 2;
  120. }
  121. // MINUS
  122. else {
  123. tokenList.add(new Token(TokenType.MINUS, line));
  124. index++;
  125. }
  126. break;
  127. // LEFT-PARENTHESES
  128. case '(':
  129. tokenList.add(new Token(TokenType.LEFTPAREN, line));
  130. index++;
  131. break;
  132. // RIGHT-PARENTHESES
  133. case ')':
  134. tokenList.add(new Token(TokenType.RIGHTPAREN, line));
  135. index++;
  136. break;
  137. // DIGIT
  138. case '0':
  139. case '1':
  140. case '2':
  141. case '3':
  142. case '4':
  143. case '5':
  144. case '6':
  145. case '7':
  146. case '8':
  147. case '9':
  148. tokenList.add(new Token(TokenType.DIGIT, Character.getNumericValue(pattern), line));
  149. index++;
  150. break;
  151. // REFERENCE SIGN
  152. case '$':
  153. tokenList.add(new Token(TokenType.REFERENCE, line));
  154. index++;
  155. break;
  156. // COMMENTS
  157. case '#':
  158. while (pattern != '\n') {
  159. index++;
  160. pattern = input.charAt(index);
  161. }
  162. break;
  163. case '\t':
  164. case ' ':
  165. index++;
  166. break;
  167. case '\r':
  168. case '\n':
  169. index++;
  170. line++;
  171. break;
  172. default:
  173. printErrorInfo(line);
  174. // FOR TEST
  175. for ( Token token: tokenList.toArray(new Token[0])) {
  176. System.out.println(token);
  177. }
  178. System.exit(1);
  179. }
  180. }
  181. // FOR TEST
  182. // for ( Token token: tokenList.toArray(new Token[0])) {
  183. // System.out.println(token);
  184. // }
  185. return tokenList;
  186. }
  187.  
  188. //TODO: paser
  189. public static String paser(List<Token> tokenList) {
  190. return "";
  191. }
  192.  
  193. public static void main(String[] args) {
  194.  
  195. // convert file stream into a string
  196. String inputString = convertStreamToString(System.in);
  197.  
  198. // Test: input
  199. // System.out.println(inputString);
  200.  
  201. List<Token> list = scanner(inputString);
  202. // FOR TEST
  203. // for ( Token token: list.toArray(new Token[0])) {
  204. // System.out.println(token);
  205. // }
  206. String result = paser(list);
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement