Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 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());
  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. flush = true;
  131. } else
  132. checklex += newchar;
  133. } else if (checktoken == INT) {
  134.  
  135. if (newchar == ' ' || newchar == EOF || newchar == '\n' || oper.find(newchar) != std::string::npos) {
  136. in.unget();
  137. checkline = linenum;
  138. flush = true;
  139. } else if (isdigit(newchar))
  140. checklex += newchar;
  141. else {
  142. checktoken = ERR;
  143. checkline = linenum;
  144. flush = true;
  145. }
  146. } else if (checktoken == ID) {
  147. if (newchar == ' ' || newchar == EOF || newchar == '\n' || oper.find(newchar) != std::string::npos) {
  148. in.unget();
  149. checkline = linenum;
  150. flush = true;
  151. } else if (isalnum(newchar))
  152. checklex += newchar;
  153. else {
  154. checktoken = ERR;
  155. checkline = linenum;
  156. flush = true;
  157. }
  158. } else if (checktoken == ERR) {
  159. if (isdigit(newchar)) {
  160. checklex += newchar;
  161. checktoken = INT;
  162. continue;
  163. }
  164. if (isalpha(newchar)) {
  165. checklex += newchar;
  166. checktoken = ID;
  167. continue;
  168. }
  169.  
  170.  
  171. switch (newchar) {
  172. case '\"':
  173. checktoken = STR;
  174. break;
  175. case '/':
  176. char nextchar;
  177. nextchar = in.peek();
  178. if (nextchar == '/') {
  179. string s;
  180. while (true) {
  181. char nextchar;
  182. in.get(nextchar);
  183. if (nextchar == '\n') {
  184. linenum++;
  185. break;
  186. }
  187. };
  188. } else {
  189. checktoken = SLASH;
  190. checkline = linenum;
  191. flush = true;
  192. }
  193. break;
  194. case ' ':
  195. break;
  196. case '\n':
  197. linenum++;
  198. break;
  199. case EOF:
  200. checktoken = DONE;
  201. checkline = linenum;
  202. flush = true;
  203. break;
  204. case '+':
  205. checktoken = PLUS;
  206. checkline = linenum;
  207. flush = true;
  208. break;
  209. case '-':
  210. checktoken = MINUS;
  211. checkline = linenum;
  212. flush = true;
  213. break;
  214. case '*':
  215. checktoken = STAR;
  216. checkline = linenum;
  217. flush = true;
  218. break;
  219. case '!':
  220. checktoken = BANG;
  221. checkline = linenum;
  222. flush = true;
  223. break;
  224. case '(':
  225. checktoken = LPAREN;
  226. checkline = linenum;
  227. flush = true;
  228. break;
  229. case ')':
  230. checktoken = RPAREN;
  231. checkline = linenum;
  232. flush = true;
  233. break;
  234. case ';':
  235. checktoken = SC;
  236. checkline = linenum;
  237. flush = true;
  238. break;
  239. }
  240. }
  241.  
  242.  
  243. if (flush) {
  244. checktoken = getKeywords(checklex, checktoken);
  245. nextLex = Lex(checktoken, checklex, checkline);
  246. return nextLex;
  247. }
  248. }
  249.  
  250. }
  251.  
  252. Token getKeywords(string str, Token orig) {
  253. if (orig == ID) {
  254. if (str.compare("print") == 0) {
  255. return PRINT;
  256. } else if (str.compare("if") == 0) {
  257. return IF;
  258. } else if (str.compare("loop") == 0) {
  259. return LOOP;
  260. } else if (str.compare("let") == 0) {
  261. return LET;
  262. } else if (str.compare("begin") == 0) {
  263. return BEGIN;
  264. } else if (str.compare("end") == 0) {
  265. return END;
  266. }
  267. }
  268.  
  269. return orig;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement