Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Parse
  5. {
  6. private String line; // our input buffer
  7. private int [] vars;
  8. private Scanner scan;
  9. private boolean eval;
  10.  
  11. public Parse()
  12. {
  13. line = "";
  14.  
  15. vars = new int[26];
  16. for (int i=0; i<26; i++)
  17. vars[i] = 0;
  18.  
  19. scan = new Scanner(System.in);
  20. }
  21.  
  22. public void run() throws IOException
  23. {
  24. String token;
  25.  
  26. System.out.print("> ");
  27.  
  28. token = getToken();
  29. parseCode(token); // ::= <code> .
  30.  
  31. }
  32.  
  33. public void parseCode(String token) throws IOException
  34. {
  35. do {
  36. parseStmt(token); // ::= <statement> <code> | <statment>
  37. token = getToken();
  38. } while (!token.equals("."));
  39. }
  40.  
  41. public void parseStmt(String token) throws IOException
  42. {
  43. int val;
  44. String text;
  45. if (token.equals("load")) // ::= load <string>
  46. {
  47. token = getToken();
  48. text = parseString(token);
  49.  
  50. line = loadFile(text) + line;
  51. }
  52. else if (token.equals("print")) // ::= print <string> | print <expr>
  53. {
  54. token = getToken();
  55.  
  56. if (token.charAt(0) == '"')
  57. {
  58. text = parseString (token);
  59.  
  60. // execute part
  61. if (eval == true)
  62. System.out.println(text);
  63. }
  64. else
  65. {
  66. val = parseExpr(token);
  67.  
  68. // execute part
  69. if (eval == true)
  70. System.out.println(val);
  71. }
  72. }
  73. else if (token.equals("input")) // ::= input <var>
  74. {
  75. token = getToken();
  76. val = parseVar(token);
  77.  
  78. // execute part
  79. if (eval == true)
  80. {
  81. System.out.print("? ");
  82. val = scan.nextInt();
  83. storeVar(token, val);
  84. }
  85. }
  86. else if (token.equals("if"))
  87. {
  88. token = getToken();
  89. // determines whether the condition was true or not
  90. boolean cond = parseCond(token);
  91. // assigns the boolean value (true or false) to the global eval boolean
  92. eval = cond;
  93. token = getToken();
  94. parseStmt(token);
  95. }
  96. else if (isVar(token))
  97. {
  98. }
  99. else
  100. {
  101. reportError(token);
  102. }
  103. eval = true;
  104. }
  105.  
  106. public int parseExpr(String token)
  107. {
  108. int val;
  109.  
  110. val = parseVal(token);
  111. token = getToken();
  112.  
  113. switch(token.charAt(0))
  114. {
  115. case '+':
  116. token = getToken();
  117. val = val + parseVal(token); // ::= <val> + <val>
  118. break;
  119. case '-':
  120. token = getToken();
  121. val = val - parseVal(token); // ::= <val> - <val>
  122. break;
  123. case '*':
  124. token = getToken();
  125. val = val * parseVal(token); // ::= <val> * <val>
  126. break;
  127. case '/':
  128. token = getToken();
  129. val = val / parseVal(token); // ::= <val> / <val>
  130. break;
  131. default: // oops, a unary experssion
  132. line = token + line;
  133. }
  134.  
  135. return val;
  136. }
  137.  
  138. public String parseString(String token)
  139. {
  140. String text = "";
  141.  
  142. if (!token.equals("\""))
  143. reportError(token);
  144.  
  145. int i;
  146. for (i=0; i<line.length(); i++)
  147. if (line.charAt(i) != '"')
  148. {
  149. if (line.charAt(i) == '\n' || line.charAt(i) == '\r')
  150. reportError(text);
  151. text += line.charAt(i);
  152. }
  153. else
  154. {
  155. line = line.substring(i+1); // removes string including "
  156. break;
  157. }
  158.  
  159. if (i == line.length())
  160. reportError(text);
  161.  
  162. return text;
  163. }
  164.  
  165. public boolean parseCond(String token)
  166. {
  167. int val;
  168. boolean cond = false;
  169. val = parseVal(token);
  170. token = getToken();
  171. switch(token.charAt(0))
  172. {
  173. case '=':
  174. token = getToken(); // skip extra equals sign
  175. token = getToken();
  176. cond = val == parseVal(token); // ::= <val> + <val>
  177. break;
  178. case '<':
  179. token = getToken();
  180. cond = val < parseVal(token); // ::= <val> - <val>
  181. break;
  182. case '>':
  183. token = getToken();
  184. cond = val > parseVal(token); // ::= <val> * <val>
  185. break;
  186. default:
  187. line = token + line;
  188. }
  189.  
  190. return cond;
  191. }
  192.  
  193. public String loadFile(String name) throws IOException
  194. {
  195. String text = "";
  196.  
  197. Scanner fileScan = new Scanner(new File(name));
  198.  
  199. while (fileScan.hasNext())
  200. text += fileScan.nextLine() + "\n";
  201.  
  202. fileScan.close();
  203.  
  204. return text;
  205. }
  206.  
  207. public int parseVal(String token)
  208. {
  209. if (isNumeric(token))
  210. return Integer.parseInt(token); // ::= <num>
  211. else if (isVar(token))
  212. return parseVar(token); // ::= <var>
  213. else
  214. reportError(token);
  215.  
  216. return -1; // should never happen
  217. }
  218.  
  219. public int parseVar(String token)
  220. {
  221. if (!isVar(token))
  222. reportError(token);
  223.  
  224. return vars [ ((int)(token.charAt(0))) - 97 ];
  225. }
  226.  
  227. public void storeVar(String token, int val)
  228. {
  229. vars[ ((int)(token.charAt(0))) - 97 ] = val;
  230. }
  231.  
  232. public boolean isNumeric(String token)
  233. {
  234. for (int i=0; i<token.length(); i++)
  235. if(!Character.isDigit(token.charAt(i)))
  236. return false;
  237.  
  238. return true;
  239. }
  240.  
  241. public boolean isVar(String token)
  242. {
  243. return (token.length() == 1) && isAlpha(token.charAt(0));
  244. }
  245.  
  246. public boolean isAlpha(char ch)
  247. {
  248. return ((int) ch) >= 97 && ((int) ch) <= 122;
  249. }
  250.  
  251. public void reportError(String token)
  252. {
  253. line += "\n";
  254. System.out.println("ERROR: " + token + line.substring(0, line.indexOf('\n')));
  255.  
  256. for (int i = 0; i< token.length()+7; i++)
  257. System.out.print(" ");
  258. System.out.println("^");
  259. System.exit(-1);
  260. }
  261.  
  262. public boolean isBlank(char ch)
  263. {
  264. switch (ch)
  265. {
  266. case ' ':
  267. case '\t':
  268. case '\r': case '\n':
  269. return true;
  270. }
  271. return false;
  272. }
  273.  
  274. public boolean isSymbol(char ch)
  275. {
  276. switch (ch)
  277. {
  278. case '.':
  279. case '+': case '-': case '*' : case '/':
  280. case '=': case '>': case '<':
  281. case '"':
  282. return true;
  283. }
  284. return false;
  285. }
  286.  
  287. public boolean isDelim(char ch)
  288. {
  289. return isBlank(ch) || isSymbol(ch);
  290. }
  291.  
  292. public String skipBlanks(String buffer)
  293. {
  294. int i;
  295. for (i = 0; i<buffer.length(); i++)
  296. if (!isBlank(buffer.charAt(i)))
  297. break;
  298. return line.substring(i);
  299. }
  300.  
  301. public String getToken()
  302. {
  303. int i;
  304. String token;
  305.  
  306. line = skipBlanks(line);
  307. while (line.length() == 0)
  308. {
  309. line = scan.nextLine();
  310. line = skipBlanks(line);
  311. }
  312.  
  313. for (i=0; i<line.length(); i++)
  314. if (isDelim(line.charAt(i)))
  315. {
  316. if (i == 0)
  317. i++;
  318. break;
  319. }
  320.  
  321. token = line.substring(0, i);
  322. line = line.substring(i);
  323.  
  324. return token;
  325. }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement