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.55 KB | None | 0 0
  1. %option noyywrap
  2. %option yylineno
  3.  
  4. %{
  5.  
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. int 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", yylineno, 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", yylineno, 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", yylineno, 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", yylineno, 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", yylineno, yytext);
  208. }
  209.  
  210. \*|\/|\% {
  211. fprintf(f_token, "<MULOP, %s> ", yytext);
  212. fprintf(f_log, "Line No. %d: Token <ADDOP> Lexeme %s found\n", yylineno, yytext);
  213. }
  214.  
  215. \+\+|\-\- {
  216. fprintf(f_token, "<INCOP, %s> ", yytext);
  217. fprintf(f_log, "Line No. %d: Token <INCOP> Lexeme %s found\n", yylineno, yytext);
  218. }
  219.  
  220. \<|\<\=|\>|\>\=|\=\=|\!\= {
  221. fprintf(f_token, "<RELOP, %s> ", yytext);
  222. fprintf(f_log, "Line No. %d: Token <RELOP> Lexeme %s found\n", yylineno, yytext);
  223. }
  224.  
  225. \= {
  226. fprintf(f_token, "<ASSIGNOP, %s> ", yytext);
  227. fprintf(f_log, "Line No. %d: Token <ASSIGNOP> Lexeme %s found\n", yylineno, yytext);
  228. }
  229.  
  230. \&\&|\|\| {
  231. fprintf(f_token, "<LOGICOP, %s> ", yytext);
  232. fprintf(f_log, "Line No. %d: Token <LOGICOP> Lexeme %s found\n", yylineno, yytext);
  233. }
  234.  
  235. \&|\||\^|\<\<|\>\> {
  236. fprintf(f_token, "<BITOP, %s> ", yytext);
  237. fprintf(f_log, "Line No. %d: Token <BITOP> Lexeme %s found\n", yylineno, yytext);
  238. }
  239.  
  240. \! {
  241. fprintf(f_token, "<NOT, %s> ", yytext);
  242. fprintf(f_log, "Line No. %d: Token <NOT> Lexeme %s found\n", yylineno, yytext);
  243. }
  244.  
  245. \( {
  246. fprintf(f_token, "<LPAREN, %s> ", yytext);
  247. fprintf(f_log, "Line No. %d: Token <LPAREN> Lexeme %s found\n", yylineno, yytext);
  248. }
  249.  
  250. \) {
  251. fprintf(f_token, "<RPAREN, %s> ", yytext);
  252. fprintf(f_log, "Line No. %d: Token <RPAREN> Lexeme %s found\n", yylineno, yytext);
  253. }
  254.  
  255. \{ {
  256. fprintf(f_token, "<LCURL, %s> ", yytext);
  257. fprintf(f_log, "Line No. %d: Token <LCURL> Lexeme %s found\n", yylineno, yytext);
  258. }
  259.  
  260. \} {
  261. fprintf(f_token, "<RCURL, %s> ", yytext);
  262. fprintf(f_log, "Line No. %d: Token <RCURL> Lexeme %s found\n", yylineno, yytext);
  263. }
  264.  
  265. \[ {
  266. fprintf(f_token, "<LTHIRD, %s> ", yytext);
  267. fprintf(f_log, "Line No. %d: Token <LTHIRD> Lexeme %s found\n", yylineno, yytext);
  268. }
  269.  
  270. \] {
  271. fprintf(f_token, "<RTHIRD, %s> ", yytext);
  272. fprintf(f_log, "Line No. %d: Token <RTHIRD> Lexeme %s found\n", yylineno, yytext);
  273. }
  274.  
  275. \, {
  276. fprintf(f_token, "<COMMA, %s> ", yytext);
  277. fprintf(f_log, "Line No. %d: Token <COMMA> Lexeme %s found\n", yylineno, yytext);
  278. }
  279.  
  280. \; {
  281. fprintf(f_token, "<SEMICOLON, %s> ", yytext);
  282. fprintf(f_log, "Line No. %d: Token <SEMICOLON> Lexeme %s found\n", yylineno, 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", yylineno, yytext);
  288. }
  289.  
  290. \/\/{ALNUMS}(\\[\n]{ALNUMS})* {
  291. fprintf(f_log, "Line No. %d: Token <COMMENT> Lexeme %s found\n", yylineno, yytext);
  292. }
  293.  
  294. \/\*([^*/]|(\*+[^*/]))*\*+\/ {
  295. fprintf(f_log, "Line No. %d: Token <COMMENT> Lexeme %s found\n", yylineno, yytext);
  296. }
  297.  
  298. ([0-9]*\.)+{NUMBER}(\.[0-9]*)+ {
  299. fprintf(f_log, "Error at line %d: Too many decimal point %s\n", yylineno, yytext);
  300. error_count++;
  301. }
  302.  
  303. {REAL_NUMBER}E\-?{REAL_NUMBER} {
  304. fprintf(f_log, "Error at line %d: Ill formed number %s\n", yylineno, yytext);
  305. error_count++;
  306. }
  307.  
  308. ({NUMBER}[a-zA-Z_]+)|({NUMBER}[a-zA-Z]+) {
  309. fprintf(f_log, "Error at line %d: Invalid prefix on ID or invalid suffix on Number %s\n", yylineno, yytext);
  310. error_count++;
  311. }
  312.  
  313. \'({CHARACTER}|[ ])({CHARACTER}|[ ])+\' {
  314. fprintf(f_log, "Error at line %d: Multi character constant error %s\n", yylineno, yytext);
  315. error_count++;
  316. }
  317.  
  318. (\'{CHARACTER}?)|(\'\\\') {
  319. fprintf(f_log, "Error at line %d: Unterminated character %s\n", yylineno, yytext);
  320. error_count++;
  321. }
  322.  
  323. \'\' {
  324. fprintf(f_log, "Error at line %d: Empty character constant error %s\n", yylineno, yytext);
  325. error_count++;
  326. }
  327.  
  328. \"{ALNUMS}(\\[\n]{ALNUMS})*\n {
  329. fprintf(f_log, "Error at line %d: Unterminated String %s\n", yylineno, yytext);
  330. error_count++;
  331. }
  332.  
  333. \/\*([^*/]|(\*+[^*/]))* {
  334. fprintf(f_log, "Error at line %d: Unterminated comment %s\n", yylineno, yytext);
  335. error_count++;
  336. }
  337.  
  338. [ \n\t\f\r\v] {
  339.  
  340. }
  341.  
  342. . {
  343. fprintf(f_log, "Unrecognized lexeme %s\n", yytext);
  344. }
  345.  
  346. %%
  347.  
  348. int main(int argc, char *argv[])
  349. {
  350. if (argc != 2)
  351. {
  352. printf("Please provide input file name and try again\n");
  353. return 0;
  354. }
  355.  
  356. yyin = fopen(argv[1], "r");
  357.  
  358. if (yyin == NULL)
  359. {
  360. printf("Cannot open specified file\n");
  361. return 0;
  362. }
  363.  
  364. yylex();
  365. fprintf(f_log, "\n");
  366. table.print();
  367. fprintf(f_log, "Total lines: %d\n", yylineno);
  368. fprintf(f_log, "Total errors: %d\n", error_count);
  369.  
  370. fclose(yyin);
  371. fclose(f_token);
  372. fclose(f_log);
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement