Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 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. flush = true;
  131. } else
  132. checklex += newchar;
  133. } else if (checktoken == INT) {
  134.  
  135. if (isdigit(newchar))
  136. checklex += newchar;
  137. else {
  138. in.unget();
  139. checkline = linenum;
  140. flush = true;
  141. }
  142. } else if (checktoken == ID) {
  143.  
  144. if (isalnum(newchar))
  145. checklex += newchar;
  146. else {
  147. checkline = linenum;
  148. flush = true;
  149. }
  150. } else if (checktoken == ERR) {
  151. if (isdigit(newchar)) {
  152. checklex += newchar;
  153. checktoken = INT;
  154. continue;
  155. }
  156. if (isalpha(newchar)) {
  157. checklex += newchar;
  158. checktoken = ID;
  159. continue;
  160. }
  161.  
  162. if(isspace(newchar) && newchar != '\n')
  163. continue;
  164.  
  165. switch (newchar) {
  166. case '\"':
  167. checktoken = STR;
  168. break;
  169. case '/':
  170. char nextchar;
  171. nextchar = in.peek();
  172. if (nextchar == '/') {
  173. string s;
  174. while (true) {
  175. char nextchar;
  176. in.get(nextchar);
  177. if (nextchar == '\n') {
  178. linenum++;
  179. break;
  180. }
  181. };
  182. } else {
  183. checktoken = SLASH;
  184. checkline = linenum;
  185. flush = true;
  186. }
  187. break;
  188. case ' ':
  189. break;
  190. case '\n':
  191. linenum++;
  192. break;
  193. case EOF:
  194. checktoken = DONE;
  195. checkline = linenum;
  196. flush = true;
  197. break;
  198. case '+':
  199. checktoken = PLUS;
  200. checkline = linenum;
  201. flush = true;
  202. break;
  203. case '-':
  204. checktoken = MINUS;
  205. checkline = linenum;
  206. flush = true;
  207. break;
  208. case '*':
  209. checktoken = STAR;
  210. checkline = linenum;
  211. flush = true;
  212. break;
  213. case '!':
  214. checktoken = BANG;
  215. checkline = linenum;
  216. flush = true;
  217. break;
  218. case '(':
  219. checktoken = LPAREN;
  220. checkline = linenum;
  221. flush = true;
  222. break;
  223. case ')':
  224. checktoken = RPAREN;
  225. checkline = linenum;
  226. flush = true;
  227. break;
  228. case ';':
  229. checktoken = SC;
  230. checkline = linenum;
  231. flush = true;
  232. break;
  233. default:
  234. checktoken = ERR;
  235. checklex += newchar;
  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