Advertisement
Guest User

Untitled

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