Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include "lex.h"
  6.  
  7. using namespace std;
  8.  
  9. Lex getNextToken(istream &in, int &linenum);
  10.  
  11. Token getKeywords(string str, Token orig);
  12.  
  13. ostream &operator<<(ostream &out, const Lex &tok);
  14.  
  15. string printnamedex(const Lex &tok);
  16.  
  17. ostream &operator<<(ostream &out, const Lex &tok) {
  18. if (tok.GetToken() == ID || tok.GetToken() == INT || tok.GetToken() == STR || tok.GetToken() == ERR) {
  19. out << printnamedex(tok) << tok.GetLexeme() << ")";
  20. } else {
  21. out << printnamedex(tok);
  22. }
  23. return out;
  24. }
  25.  
  26. string printnamedex(const Lex &tok) {
  27. string retval;
  28.  
  29. switch (tok.GetToken()) {
  30. case ID:
  31. retval = "ID(";
  32. break;
  33. case INT:
  34. retval = "INT(";
  35. break;
  36. case STR:
  37. retval = "STR(";
  38. break;
  39.  
  40. case PRINT:
  41. retval = "PRINT";
  42. break;
  43. case ERR:
  44. retval = "Error on line ";
  45. retval += to_string(tok.GetLinenum()+1);
  46. retval += " (";
  47. break;
  48. case LET:
  49. retval = "LET";
  50. break;
  51. case IF:
  52. retval = "IF";
  53. break;
  54. case LOOP:
  55. retval = "LOOP";
  56. break;
  57. case BEGIN:
  58. retval = "BEGIN";
  59. break;
  60. case END:
  61. retval = "END";
  62. break;
  63. case PLUS:
  64. retval = "PLUS";
  65. break;
  66. case MINUS:
  67. retval = "MINUS";
  68. break;
  69. case STAR:
  70. retval = "STAR";
  71. break;
  72. case SLASH:
  73. retval = "SLASH";
  74. break;
  75. case BANG:
  76. retval = "BANG";
  77. break;
  78. case LPAREN:
  79. retval = "LPAREN";
  80. break;
  81. case RPAREN:
  82. retval = "RPAREN";
  83. break;
  84. case SC:
  85. retval = "SC";
  86. break;
  87. case DONE:
  88. retval = "DONE";
  89. break;
  90. }
  91. return retval;
  92. }
  93.  
  94. Lex getNextToken(istream &in, int &linenum) {
  95. char newchar = '\0';
  96.  
  97. string oper = ";+-!\\*()";
  98.  
  99. Token checktoken = ERR;
  100. string checklex = "";
  101. int checkline = -1;
  102.  
  103. bool flush = false;
  104.  
  105. Lex nextLex;
  106.  
  107. while (true) {
  108. in.get(newchar);
  109.  
  110. if (in.eof()) {
  111. nextLex = Lex(DONE, "", linenum);
  112. return nextLex;
  113. }
  114.  
  115.  
  116. if (checktoken == STR) {
  117. if (newchar == '\\') {
  118. char escaped;
  119. in.get(escaped);
  120. if (escaped == 'n') {
  121. checklex += '\n';
  122. } else
  123. checklex += escaped;
  124. } else if (newchar == '\"') {
  125. checkline = linenum;
  126. flush = true;
  127. } else if (newchar == EOF || newchar == '\n') {
  128. checktoken = ERR;
  129. checkline = linenum;
  130. if(newchar == '\n')
  131. checklex += "\n";
  132. flush = true;
  133. } else
  134. checklex += newchar;
  135. } else if (checktoken == INT) {
  136.  
  137. if (isdigit(newchar))
  138. checklex += newchar;
  139. else {
  140. in.unget();
  141. checkline = linenum;
  142. flush = true;
  143. }
  144. } else if (checktoken == ID) {
  145.  
  146. if (isalnum(newchar))
  147. checklex += newchar;
  148. else {
  149. checkline = linenum;
  150. flush = true;
  151. }
  152. } else if (checktoken == ERR) {
  153. if (isdigit(newchar)) {
  154. checklex += newchar;
  155. checktoken = INT;
  156. continue;
  157. }
  158. if (isalpha(newchar)) {
  159. checklex += newchar;
  160. checktoken = ID;
  161. continue;
  162. }
  163.  
  164. if(isspace(newchar) && newchar != '\n')
  165. continue;
  166.  
  167. switch (newchar) {
  168. case '\"':
  169. checktoken = STR;
  170. break;
  171. case '/':
  172. char nextchar;
  173. nextchar = in.peek();
  174. if (nextchar == '/') {
  175. string s;
  176. while (true) {
  177. char nextchar;
  178. in.get(nextchar);
  179. if (nextchar == '\n') {
  180. linenum++;
  181. break;
  182. }
  183. }
  184. } else {
  185. checktoken = SLASH;
  186. checkline = linenum;
  187. flush = true;
  188. }
  189. break;
  190. case ' ':
  191. break;
  192. case '\n':
  193. linenum++;
  194. break;
  195. case EOF:
  196. checktoken = DONE;
  197. checkline = linenum;
  198. flush = true;
  199. break;
  200. case '+':
  201. checktoken = PLUS;
  202. checkline = linenum;
  203. flush = true;
  204. break;
  205. case '-':
  206. checktoken = MINUS;
  207. checkline = linenum;
  208. flush = true;
  209. break;
  210. case '*':
  211. checktoken = STAR;
  212. checkline = linenum;
  213. flush = true;
  214. break;
  215. case '!':
  216. checktoken = BANG;
  217. checkline = linenum;
  218. flush = true;
  219. break;
  220. case '(':
  221. checktoken = LPAREN;
  222. checkline = linenum;
  223. flush = true;
  224. break;
  225. case ')':
  226. checktoken = RPAREN;
  227. checkline = linenum;
  228. flush = true;
  229. break;
  230. case ';':
  231. checktoken = SC;
  232. checkline = linenum;
  233. flush = true;
  234. break;
  235. default:
  236. checktoken = ERR;
  237. checklex += newchar;
  238. checkline = linenum;
  239. flush = true;
  240. break;
  241. }
  242. }
  243.  
  244.  
  245. if (flush) {
  246. checktoken = getKeywords(checklex, checktoken);
  247. nextLex = Lex(checktoken, checklex, checkline);
  248. return nextLex;
  249. }
  250. }
  251.  
  252. }
  253.  
  254. Token getKeywords(string str, Token orig) {
  255. if (orig == ID) {
  256. if (str.compare("print") == 0) {
  257. return PRINT;
  258. } else if (str.compare("if") == 0) {
  259. return IF;
  260. } else if (str.compare("loop") == 0) {
  261. return LOOP;
  262. } else if (str.compare("let") == 0) {
  263. return LET;
  264. } else if (str.compare("begin") == 0) {
  265. return BEGIN;
  266. } else if (str.compare("end") == 0) {
  267. return END;
  268. }
  269. }
  270.  
  271. return orig;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement