Advertisement
Guest User

Untitled

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