Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 KB | None | 0 0
  1. %option noyywrap
  2. %option line_count
  3.  
  4. %{
  5.  
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. int line_count = 1, error_count = 0;
  11.  
  12. FILE *f_log = fopen("log.txt", "w");
  13. FILE *f_token = fopen("token.txt", "w");
  14.  
  15. class symbol_info
  16. {
  17. public:
  18. string name = "", type = "";
  19. symbol_info *next = 0;
  20.  
  21. symbol_info() {}
  22. symbol_info(string n, string t) : name(n), type(t) {}
  23. };
  24.  
  25. class scope_table
  26. {
  27. const int n = 7;
  28. symbol_info **arr;
  29.  
  30. public:
  31. scope_table()
  32. {
  33. arr = new symbol_info*[n];
  34.  
  35. for (int i = 0; i < n; i++)
  36. arr[i] = new symbol_info();
  37. }
  38.  
  39. ~scope_table()
  40. {
  41. for (int i = 0; i < n; i++)
  42. delete(arr[i]);
  43. }
  44.  
  45. long polynomial_hash(string s)
  46. {
  47. long val = 0, _pow = 1;
  48.  
  49. for (int i = 0; i < s.size(); i++, _pow *= 29)
  50. val = (val + int(s[i]) * _pow) % int(1e9 + 7);
  51.  
  52. return val % n;
  53. }
  54.  
  55. void insert(string name, string type)
  56. {
  57. bool err = 0;
  58. symbol_info *temp = arr[polynomial_hash(name)];
  59.  
  60. if (!temp) temp = new symbol_info();
  61.  
  62. if (temp->name.empty())
  63. {
  64. temp->name = name;
  65. temp->type = type;
  66. fprintf(f_log, "\n");
  67. print();
  68. return;
  69. }
  70.  
  71. while (temp->next && !err)
  72. {
  73. err |= temp->name == name && temp->type == type;
  74. temp = temp->next;
  75. }
  76.  
  77. if (err || (temp->name == name && temp->type == type)) return;
  78. temp->next = new symbol_info(name, type);
  79. fprintf(f_log, "\n");
  80. print();
  81. }
  82.  
  83. void print()
  84. {
  85. for (int i = 0; i < n; i++)
  86. {
  87. symbol_info *temp = arr[i];
  88. if (temp->name.empty()) continue;
  89.  
  90. fprintf(f_log, "%d --> ", i);
  91.  
  92. while (temp)
  93. {
  94. if (temp->name.empty()) continue;
  95.  
  96. fprintf(f_log, "<%s, %s> ", temp->name.c_str(), temp->type.c_str());
  97. temp = temp->next;
  98. }
  99.  
  100. fprintf(f_log, "\n");
  101. }
  102.  
  103. fprintf(f_log, "\n");
  104. }
  105. } table;
  106.  
  107. %}
  108.  
  109. CHARACTER [a-zA-Z0-9]|\\n|\\t|\\a|\\f|\\r|\\b|\\v|\\0|\\\"|\\\\
  110. REAL_NUMBER ([0-9]+\.?[0-9]*)|(\.[0-9]+)
  111. ALNUMS ((\\.)|[^"\\\n])*
  112. NUMBER [0-9]+
  113.  
  114. %%
  115.  
  116. if {
  117. fprintf(f_token, "<IF> ");
  118. }
  119.  
  120. for {
  121. fprintf(f_token, "<FOR> ");
  122. }
  123.  
  124. do {
  125. fprintf(f_token, "<DO> ");
  126. }
  127.  
  128. int {
  129. fprintf(f_token, "<INT> ");
  130. }
  131.  
  132. float {
  133. fprintf(f_token, "<FLOAT> ");
  134. }
  135.  
  136. void {
  137. fprintf(f_token, "<IF> ");
  138. }
  139.  
  140. switch {
  141. fprintf(f_token, "<SWITCH> ");
  142. }
  143.  
  144. default {
  145. fprintf(f_token, "<DEFAULT> ");
  146. }
  147.  
  148. else {
  149. fprintf(f_token, "<ELSE> ");
  150. }
  151.  
  152. while {
  153. fprintf(f_token, "<WHILE> ");
  154. }
  155.  
  156. break {
  157. fprintf(f_token, "<BREAK> ");
  158. }
  159.  
  160. char {
  161. fprintf(f_token, "<CHAR> ");
  162. }
  163.  
  164. double {
  165. fprintf(f_token, "<DOUBLE> ");
  166. }
  167.  
  168. return {
  169. fprintf(f_token, "<RETURN> ");
  170. }
  171.  
  172. case {
  173. fprintf(f_token, "<CASE> ");
  174. }
  175.  
  176. continue {
  177. fprintf(f_token, "<CONTINUE> ");
  178. }
  179.  
  180. {NUMBER} {
  181. fprintf(f_token, "<CONST_INT, %s> ", yytext);
  182. fprintf(f_log, "Line No. %d: Token <CONST_INT> Lexeme %s found\n", line_count, yytext);
  183. table.insert(string(yytext),"CONST_INT");
  184. }
  185.  
  186. {REAL_NUMBER}(E-?{NUMBER})? {
  187. fprintf(f_token, "<CONST_FLOAT, %s> ", yytext);
  188. fprintf(f_log, "Line No. %d: Token <CONST_FLOAT> Lexeme %s found\n", line_count, yytext);
  189. table.insert(string(yytext),"CONST_FLOAT");
  190. }
  191.  
  192. \'{CHARACTER}\' {
  193. fprintf(f_token, "<CONST_CHAR, %s> ", yytext);
  194. fprintf(f_log, "Line No. %d: Token <CONST_CHAR> Lexeme %s found\n", line_count, yytext);
  195. table.insert(string(yytext),"CONST_CHAR");
  196. }
  197.  
  198. [a-zA-Z_][a-zA-Z0-9_]* {
  199. fprintf(f_token, "<ID, %s> ", yytext);
  200. fprintf(f_log, "Line No. %d: Token <ID> Lexeme %s found\n", line_count, yytext);
  201. table.insert(string(yytext),"ID");
  202.  
  203. }
  204.  
  205. \+|\- {
  206. fprintf(f_token, "<ADDOP, %s> ", yytext);
  207. fprintf(f_log, "Line No. %d: Token <ADDOP> Lexeme %s found\n", line_count, yytext);
  208. }
  209.  
  210. \*|\/|\% {
  211. fprintf(f_token, "<MULOP, %s> ", yytext);
  212. fprintf(f_log, "Line No. %d: Token <ADDOP> Lexeme %s found\n", line_count, yytext);
  213. }
  214.  
  215. \+\+|\-\- {
  216. fprintf(f_token, "<INCOP, %s> ", yytext);
  217. fprintf(f_log, "Line No. %d: Token <INCOP> Lexeme %s found\n", line_count, yytext);
  218. }
  219.  
  220. \<|\<\=|\>|\>\=|\=\=|\!\= {
  221. fprintf(f_token, "<RELOP, %s> ", yytext);
  222. fprintf(f_log, "Line No. %d: Token <RELOP> Lexeme %s found\n", line_count, yytext);
  223. }
  224.  
  225. \= {
  226. fprintf(f_token, "<ASSIGNOP, %s> ", yytext);
  227. fprintf(f_log, "Line No. %d: Token <ASSIGNOP> Lexeme %s found\n", line_count, yytext);
  228. }
  229.  
  230. \&\&|\|\| {
  231. fprintf(f_token, "<LOGICOP, %s> ", yytext);
  232. fprintf(f_log, "Line No. %d: Token <LOGICOP> Lexeme %s found\n", line_count, yytext);
  233. }
  234.  
  235. \&|\||\^|\<\<|\>\> {
  236. fprintf(f_token, "<BITOP, %s> ", yytext);
  237. fprintf(f_log, "Line No. %d: Token <BITOP> Lexeme %s found\n", line_count, yytext);
  238. }
  239.  
  240. \! {
  241. fprintf(f_token, "<NOT, %s> ", yytext);
  242. fprintf(f_log, "Line No. %d: Token <NOT> Lexeme %s found\n", line_count, yytext);
  243. }
  244.  
  245. \( {
  246. fprintf(f_token, "<LPAREN, %s> ", yytext);
  247. fprintf(f_log, "Line No. %d: Token <LPAREN> Lexeme %s found\n", line_count, yytext);
  248. }
  249.  
  250. \) {
  251. fprintf(f_token, "<RPAREN, %s> ", yytext);
  252. fprintf(f_log, "Line No. %d: Token <RPAREN> Lexeme %s found\n", line_count, yytext);
  253. }
  254.  
  255. \{ {
  256. fprintf(f_token, "<LCURL, %s> ", yytext);
  257. fprintf(f_log, "Line No. %d: Token <LCURL> Lexeme %s found\n", line_count, yytext);
  258. }
  259.  
  260. \} {
  261. fprintf(f_token, "<RCURL, %s> ", yytext);
  262. fprintf(f_log, "Line No. %d: Token <RCURL> Lexeme %s found\n", line_count, yytext);
  263. }
  264.  
  265. \[ {
  266. fprintf(f_token, "<LTHIRD, %s> ", yytext);
  267. fprintf(f_log, "Line No. %d: Token <LTHIRD> Lexeme %s found\n", line_count, yytext);
  268. }
  269.  
  270. \] {
  271. fprintf(f_token, "<RTHIRD, %s> ", yytext);
  272. fprintf(f_log, "Line No. %d: Token <RTHIRD> Lexeme %s found\n", line_count, yytext);
  273. }
  274.  
  275. \, {
  276. fprintf(f_token, "<COMMA, %s> ", yytext);
  277. fprintf(f_log, "Line No. %d: Token <COMMA> Lexeme %s found\n", line_count, yytext);
  278. }
  279.  
  280. \; {
  281. fprintf(f_token, "<SEMICOLON, %s> ", yytext);
  282. fprintf(f_log, "Line No. %d: Token <SEMICOLON> Lexeme %s found\n", line_count, yytext);
  283. }
  284.  
  285. \"{ALNUMS}(\\[\n]{ALNUMS})*\" {
  286. fprintf(f_token, "<STRING, %s> ", yytext);
  287. fprintf(f_log, "Line No. %d: Token <STRING> Lexeme %s found\n", line_count, yytext);
  288. line_count = yylineno;
  289. }
  290.  
  291. \/\/{ALNUMS}(\\[\n]{ALNUMS})* {
  292. fprintf(f_log, "Line No. %d: Token <COMMENT> Lexeme %s found\n", line_count, yytext);
  293. line_count = yylineno;
  294. }
  295.  
  296. \/\*([^*/]|(\*+[^*/]))*\*+\/ {
  297. fprintf(f_log, "Line No. %d: Token <COMMENT> Lexeme %s found\n", line_count, yytext);
  298. line_count = yylineno;
  299. }
  300.  
  301. ([0-9]*\.)+{NUMBER}(\.[0-9]*)+ {
  302. fprintf(f_log, "Error at line %d: Too many decimal point %s\n", line_count, yytext);
  303. error_count++;
  304. }
  305.  
  306. {REAL_NUMBER}E\-?{REAL_NUMBER} {
  307. fprintf(f_log, "Error at line %d: Ill formed number %s\n", line_count, yytext);
  308. error_count++;
  309. }
  310.  
  311. ({NUMBER}[a-zA-Z_]+)|({NUMBER}[a-zA-Z]+) {
  312. fprintf(f_log, "Error at line %d: Invalid prefix on ID or invalid suffix on Number %s\n", line_count, yytext);
  313. error_count++;
  314. }
  315.  
  316. \'({CHARACTER}|[ ])({CHARACTER}|[ ])+\' {
  317. fprintf(f_log, "Error at line %d: Multi character constant error %s\n", line_count, yytext);
  318. error_count++;
  319. }
  320.  
  321. (\'{CHARACTER}?)|(\'\\\') {
  322. fprintf(f_log, "Error at line %d: Unterminated character %s\n", line_count, yytext);
  323. error_count++;
  324. }
  325.  
  326. \'\' {
  327. fprintf(f_log, "Error at line %d: Empty character constant error %s\n", line_count, yytext);
  328. error_count++;
  329. }
  330.  
  331. \"{ALNUMS}(\\[\n]{ALNUMS})*\n {
  332. fprintf(f_log, "Error at line %d: Unterminated String %s\n", line_count, yytext);
  333. error_count++;
  334. line_count = yylineno;
  335. }
  336.  
  337. \/\*([^*/]|(\*+[^*/]))* {
  338. fprintf(f_log, "Error at line %d: Unterminated comment %s\n", line_count, yytext);
  339. error_count++;
  340. line_count = yylineno;
  341. }
  342.  
  343. [ \n\t\f\r\v] {
  344.  
  345. }
  346.  
  347. \n {
  348. line_count++;
  349. }
  350.  
  351. . {
  352. fprintf(f_log, "Unrecognized lexeme %s\n", yytext);
  353. }
  354.  
  355. %%
  356.  
  357. int main(int argc, char *argv[])
  358. {
  359. if (argc != 2)
  360. {
  361. printf("Please provide input file name and try again\n");
  362. return 0;
  363. }
  364.  
  365. yyin = fopen(argv[1], "r");
  366.  
  367. if (yyin == NULL)
  368. {
  369. printf("Cannot open specified file\n");
  370. return 0;
  371. }
  372.  
  373. yylex();
  374. fprintf(f_log, "\n");
  375. table.print();
  376. fprintf(f_log, "Total lines: %d\n", line_count);
  377. fprintf(f_log, "Total errors: %d\n", error_count);
  378.  
  379. fclose(yyin);
  380. fclose(f_token);
  381. fclose(f_log);
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement