Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %option noyywrap
- %option yylineno
- %{
- #include <bits/stdc++.h>
- using namespace std;
- int error_count = 0;
- FILE *f_log = fopen("log.txt", "w");
- FILE *f_token = fopen("token.txt", "w");
- class symbol_info
- {
- public:
- string name = "", type = "";
- symbol_info *next = 0;
- symbol_info() {}
- symbol_info(string n, string t) : name(n), type(t) {}
- };
- class scope_table
- {
- const int n = 7;
- symbol_info **arr;
- public:
- scope_table()
- {
- arr = new symbol_info*[n];
- for (int i = 0; i < n; i++)
- arr[i] = new symbol_info();
- }
- ~scope_table()
- {
- for (int i = 0; i < n; i++)
- delete(arr[i]);
- }
- long polynomial_hash(string s)
- {
- long val = 0, _pow = 1;
- for (int i = 0; i < s.size(); i++, _pow *= 29)
- val = (val + int(s[i]) * _pow) % int(1e9 + 7);
- return val % n;
- }
- void insert(string name, string type)
- {
- bool err = 0;
- symbol_info *temp = arr[polynomial_hash(name)];
- if (!temp) temp = new symbol_info();
- if (temp->name.empty())
- {
- temp->name = name;
- temp->type = type;
- fprintf(f_log, "\n");
- print();
- return;
- }
- while (temp->next && !err)
- {
- err |= temp->name == name && temp->type == type;
- temp = temp->next;
- }
- if (err || (temp->name == name && temp->type == type)) return;
- temp->next = new symbol_info(name, type);
- fprintf(f_log, "\n");
- print();
- }
- void print()
- {
- for (int i = 0; i < n; i++)
- {
- symbol_info *temp = arr[i];
- if (temp->name.empty()) continue;
- fprintf(f_log, "%d --> ", i);
- while (temp)
- {
- if (temp->name.empty()) continue;
- fprintf(f_log, "<%s, %s> ", temp->name.c_str(), temp->type.c_str());
- temp = temp->next;
- }
- fprintf(f_log, "\n");
- }
- fprintf(f_log, "\n");
- }
- } table;
- %}
- CHARACTER [a-zA-Z0-9]|\\n|\\t|\\a|\\f|\\r|\\b|\\v|\\0|\\\"|\\\\
- REAL_NUMBER ([0-9]+\.?[0-9]*)|(\.[0-9]+)
- ALNUMS ((\\.)|[^"\\\n])*
- NUMBER [0-9]+
- %%
- if {
- fprintf(f_token, "<IF> ");
- }
- for {
- fprintf(f_token, "<FOR> ");
- }
- do {
- fprintf(f_token, "<DO> ");
- }
- int {
- fprintf(f_token, "<INT> ");
- }
- float {
- fprintf(f_token, "<FLOAT> ");
- }
- void {
- fprintf(f_token, "<IF> ");
- }
- switch {
- fprintf(f_token, "<SWITCH> ");
- }
- default {
- fprintf(f_token, "<DEFAULT> ");
- }
- else {
- fprintf(f_token, "<ELSE> ");
- }
- while {
- fprintf(f_token, "<WHILE> ");
- }
- break {
- fprintf(f_token, "<BREAK> ");
- }
- char {
- fprintf(f_token, "<CHAR> ");
- }
- double {
- fprintf(f_token, "<DOUBLE> ");
- }
- return {
- fprintf(f_token, "<RETURN> ");
- }
- case {
- fprintf(f_token, "<CASE> ");
- }
- continue {
- fprintf(f_token, "<CONTINUE> ");
- }
- {NUMBER} {
- fprintf(f_token, "<CONST_INT, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <CONST_INT> Lexeme %s found\n", yylineno, yytext);
- table.insert(string(yytext),"CONST_INT");
- }
- {REAL_NUMBER}(E-?{NUMBER})? {
- fprintf(f_token, "<CONST_FLOAT, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <CONST_FLOAT> Lexeme %s found\n", yylineno, yytext);
- table.insert(string(yytext),"CONST_FLOAT");
- }
- \'{CHARACTER}\' {
- fprintf(f_token, "<CONST_CHAR, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <CONST_CHAR> Lexeme %s found\n", yylineno, yytext);
- table.insert(string(yytext),"CONST_CHAR");
- }
- [a-zA-Z_][a-zA-Z0-9_]* {
- fprintf(f_token, "<ID, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <ID> Lexeme %s found\n", yylineno, yytext);
- table.insert(string(yytext),"ID");
- }
- \+|\- {
- fprintf(f_token, "<ADDOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <ADDOP> Lexeme %s found\n", yylineno, yytext);
- }
- \*|\/|\% {
- fprintf(f_token, "<MULOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <ADDOP> Lexeme %s found\n", yylineno, yytext);
- }
- \+\+|\-\- {
- fprintf(f_token, "<INCOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <INCOP> Lexeme %s found\n", yylineno, yytext);
- }
- \<|\<\=|\>|\>\=|\=\=|\!\= {
- fprintf(f_token, "<RELOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <RELOP> Lexeme %s found\n", yylineno, yytext);
- }
- \= {
- fprintf(f_token, "<ASSIGNOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <ASSIGNOP> Lexeme %s found\n", yylineno, yytext);
- }
- \&\&|\|\| {
- fprintf(f_token, "<LOGICOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <LOGICOP> Lexeme %s found\n", yylineno, yytext);
- }
- \&|\||\^|\<\<|\>\> {
- fprintf(f_token, "<BITOP, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <BITOP> Lexeme %s found\n", yylineno, yytext);
- }
- \! {
- fprintf(f_token, "<NOT, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <NOT> Lexeme %s found\n", yylineno, yytext);
- }
- \( {
- fprintf(f_token, "<LPAREN, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <LPAREN> Lexeme %s found\n", yylineno, yytext);
- }
- \) {
- fprintf(f_token, "<RPAREN, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <RPAREN> Lexeme %s found\n", yylineno, yytext);
- }
- \{ {
- fprintf(f_token, "<LCURL, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <LCURL> Lexeme %s found\n", yylineno, yytext);
- }
- \} {
- fprintf(f_token, "<RCURL, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <RCURL> Lexeme %s found\n", yylineno, yytext);
- }
- \[ {
- fprintf(f_token, "<LTHIRD, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <LTHIRD> Lexeme %s found\n", yylineno, yytext);
- }
- \] {
- fprintf(f_token, "<RTHIRD, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <RTHIRD> Lexeme %s found\n", yylineno, yytext);
- }
- \, {
- fprintf(f_token, "<COMMA, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <COMMA> Lexeme %s found\n", yylineno, yytext);
- }
- \; {
- fprintf(f_token, "<SEMICOLON, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <SEMICOLON> Lexeme %s found\n", yylineno, yytext);
- }
- \"{ALNUMS}(\\[\n]{ALNUMS})*\" {
- fprintf(f_token, "<STRING, %s> ", yytext);
- fprintf(f_log, "Line No. %d: Token <STRING> Lexeme %s found\n", yylineno, yytext);
- }
- \/\/{ALNUMS}(\\[\n]{ALNUMS})* {
- fprintf(f_log, "Line No. %d: Token <COMMENT> Lexeme %s found\n", yylineno, yytext);
- }
- \/\*([^*/]|(\*+[^*/]))*\*+\/ {
- fprintf(f_log, "Line No. %d: Token <COMMENT> Lexeme %s found\n", yylineno, yytext);
- }
- ([0-9]*\.)+{NUMBER}(\.[0-9]*)+ {
- fprintf(f_log, "Error at line %d: Too many decimal point %s\n", yylineno, yytext);
- error_count++;
- }
- {REAL_NUMBER}E\-?{REAL_NUMBER} {
- fprintf(f_log, "Error at line %d: Ill formed number %s\n", yylineno, yytext);
- error_count++;
- }
- ({NUMBER}[a-zA-Z_]+)|({NUMBER}[a-zA-Z]+) {
- fprintf(f_log, "Error at line %d: Invalid prefix on ID or invalid suffix on Number %s\n", yylineno, yytext);
- error_count++;
- }
- \'({CHARACTER}|[ ])({CHARACTER}|[ ])+\' {
- fprintf(f_log, "Error at line %d: Multi character constant error %s\n", yylineno, yytext);
- error_count++;
- }
- (\'{CHARACTER}?)|(\'\\\') {
- fprintf(f_log, "Error at line %d: Unterminated character %s\n", yylineno, yytext);
- error_count++;
- }
- \'\' {
- fprintf(f_log, "Error at line %d: Empty character constant error %s\n", yylineno, yytext);
- error_count++;
- }
- \"{ALNUMS}(\\[\n]{ALNUMS})*\n {
- fprintf(f_log, "Error at line %d: Unterminated String %s\n", yylineno, yytext);
- error_count++;
- }
- \/\*([^*/]|(\*+[^*/]))* {
- fprintf(f_log, "Error at line %d: Unterminated comment %s\n", yylineno, yytext);
- error_count++;
- }
- [ \n\t\f\r\v] {
- }
- . {
- fprintf(f_log, "Unrecognized lexeme %s\n", yytext);
- }
- %%
- int main(int argc, char *argv[])
- {
- if (argc != 2)
- {
- printf("Please provide input file name and try again\n");
- return 0;
- }
- yyin = fopen(argv[1], "r");
- if (yyin == NULL)
- {
- printf("Cannot open specified file\n");
- return 0;
- }
- yylex();
- fprintf(f_log, "\n");
- table.print();
- fprintf(f_log, "Total lines: %d\n", yylineno);
- fprintf(f_log, "Total errors: %d\n", error_count);
- fclose(yyin);
- fclose(f_token);
- fclose(f_log);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement