Advertisement
Guest User

Untitled

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