-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.l
276 lines (227 loc) · 7.38 KB
/
scanner.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
%option noyywrap
%x STRING
%x COMMENT
%x MULCOMMENT
%{
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "node.hpp"
#include "merged.hpp"
#include "y.tab.h"
typedef unsigned long long ull;
extern YYSTYPE yylval;
void yyerror(char *);
extern FILE* logout;
extern FILE* errout;
int line_count = 1;
int error_count = 0;
int first_line;
SymbolTable st(10);
std::string str, strtype, strerr;
void common_output(const char* type){
// fprintf(tokenout,"<%s, %s>\n",type,yytext);
fprintf(logout,"Line# %d: Token <%s> Lexeme %s found\n",line_count,type,yytext);
}
void string_output(){
// fprintf(tokenout,"<%s, %s>\n",strtype.c_str(),str.c_str());
fprintf(logout,"Line# %d: Token <%s> Lexeme %s found\n",first_line,strtype.c_str(),strerr.c_str());
}
void error(const char* message){
fprintf(errout, "Line# %d: %s %s\n", line_count,message, yytext);
fprintf(logout, "Line# %d: %s %s\n", line_count,message, yytext);
}
void string_error(const char* message){
fprintf(errout, "Error at line# %d: %s %s\n", line_count,message, strerr.c_str());
}
char convert_escaped(const char escape){
if(escape == '\\') return 92;
if(escape == '\'') return 39;
if(escape == '\"') return 34;
if(escape == 'n') return 10;
if(escape == 't') return 9;
if(escape == 'a') return 7;
if(escape == 'f') return 12;
if(escape == 'r') return 13;
if(escape == 'b') return 8;
if(escape == 'v') return 11;
if(escape == '0')return 0;
return escape;
}
%}
WHITESPACE [ \t\f\v]+
LETTER [a-zA-Z]
DIGIT [0-9]
NEWLINE (\r)?\n
INTEGER {DIGIT}+
DECIMAL {DIGIT}*\.{DIGIT}+
FLOAT {DECIMAL}([eE][+-]?{DIGIT}+)?|{INTEGER}[eE][+-]?{DIGIT}+
IDENTIFIER [_A-Za-z][_A-Za-z0-9]*
PRINTABLE [^\\'\n\r]
CHAR [^'\n\r]
ESCAPED [\\'"ntafrbv0]
STR_PRINTABLE [^\\"\n\r]
%%
{NEWLINE} {line_count++;}
{WHITESPACE} {}
"if" {common_output("IF"); yylval.line = line_count; return IF;}
"else" {common_output("ELSE"); yylval.line = line_count; return ELSE;}
"for" {common_output("FOR"); yylval.line = line_count; return FOR;}
"while" {common_output("WHILE"); yylval.line = line_count; return WHILE;}
"do" {common_output("DO");}
"break" {common_output("BREAK");}
"int" {common_output("INT"); yylval.line = line_count; return INT;}
"char" {common_output("CHAR");}
"float" {common_output("FLOAT"); yylval.line = line_count; return FLOAT;}
"double" {common_output("DOUBLE");}
"void" {common_output("VOID"); yylval.line = line_count; return VOID;}
"return" {common_output("RETURN"); yylval.line = line_count; return RETURN;}
"switch" {common_output("SWITCH");}
"case" {common_output("CASE");}
"default" {common_output("DEFAULT");}
"continue" {common_output("CONTINUE");}
"println" {common_output("PRINTLN"); yylval.line = line_count; return PRINTLN;}
"+" |
"-" {
common_output("ADDOP");
yylval.symbolInfo = new SymbolInfo(yytext, "ADDOP", line_count);
return ADDOP;
}
"*" |
"/" |
"%" {common_output("MULOP"); yylval.symbolInfo = new SymbolInfo(yytext, "MULOP", line_count); return MULOP;}
"++" {common_output("INCOP"); yylval.symbolInfo = new SymbolInfo(yytext, "INCOP", line_count); return INCOP;}
"--" {common_output("DECOP"); yylval.symbolInfo = new SymbolInfo(yytext, "DECOP", line_count); return DECOP;}
"<" |
"<=" |
">" |
">=" |
"==" |
"!=" {common_output("RELOP"); yylval.symbolInfo = new SymbolInfo(yytext, "RELOP", line_count); return RELOP;}
"=" {common_output("ASSIGNOP"); yylval.symbolInfo = new SymbolInfo(yytext, "ASSIGNOP", line_count); return ASSIGNOP;}
"&&" |
"||" {common_output("LOGICOP"); yylval.symbolInfo = new SymbolInfo(yytext, "LOGICOP", line_count); return LOGICOP;}
"&" |
"|" |
"^" |
"<<" |
">>" {common_output("BITOP");}
"!" {common_output("NOT"); yylval.symbolInfo = new SymbolInfo(yytext, "NOT", line_count); return NOT;}
"(" {common_output("LPAREN"); yylval.line = line_count; return LPAREN;}
")" {common_output("RPAREN"); yylval.line = line_count; return RPAREN;}
"{" {
common_output("LCURL");
yylval.line = line_count;
return LCURL;
}
"}" {
common_output("RCURL");
yylval.line = line_count;
return RCURL;
}
"[" {common_output("LSQUARE"); yylval.line = line_count; return LSQUARE;}
"]" {common_output("RSQUARE"); yylval.line = line_count; return RSQUARE;}
"," {common_output("COMMA"); yylval.line = line_count; return COMMA;}
";" {common_output("SEMICOLON"); yylval.line = line_count; return SEMICOLON;}
{IDENTIFIER} {
common_output("ID");
yylval.symbolInfo = new SymbolInfo(yytext, "ID", line_count);
return ID;
}
{INTEGER} {common_output("CONST_INT"); yylval.symbolInfo = new SymbolInfo(yytext, "CONST_INT", line_count); return CONST_INT;}
{FLOAT} {common_output("CONST_FLOAT"); yylval.symbolInfo = new SymbolInfo(yytext, "CONST_FLOAT", line_count); return CONST_FLOAT;}
{DECIMAL}(\.|{DECIMAL})+({INTEGER}|{FLOAT}|\.)* {error("TOO_MANY_DECIMAL_POINTS"); error_count++;}
{DIGIT}+\.[\.]+({INTEGER}|{FLOAT}|\.)* {error("TOO_MANY_DECIMAL_POINTS"); error_count++;}
\.[\.]+{DIGIT}({INTEGER}|{FLOAT}|\.)* {error("TOO_MANY_DECIMAL_POINTS"); error_count++;}
({INTEGER}|{FLOAT})({INTEGER}|{FLOAT}|\.)+ {error("ILLFORMED_NUMBER"); error_count++;}
({INTEGER}|{FLOAT}){IDENTIFIER} {error("INVALID_ID_SUFFIX_NUM_PREFIX"); error_count++;}
'{PRINTABLE}' {
yytext[0] = yytext[1];
yytext[1] = 0;
common_output("CONST_CHAR");
}
'\\{ESCAPED}' {
char c = convert_escaped(yytext[2]);
yytext[0] = c;
yytext[1] = 0;
common_output("CONST_CHAR");
}
'' {error("EMPTY_CONST_CHAR"); error_count++;}
'{CHAR}* {error("UNFINISHED_CONST_CHAR"); error_count++;}
'{CHAR}*\\' {error("UNFINISHED_CONST_CHAR"); error_count++;}
'{PRINTABLE}{CHAR}+' {error("MULTICHAR_CONST_CHAR"); error_count++;}
'\\{CHAR}+' {error("MULTICHAR_CONST_CHAR"); error_count++;}
\" {
str = "";
strerr = "\"";
strtype = "SINGLE LINE STRING";
first_line = line_count;
BEGIN(STRING);
}
<STRING>{STR_PRINTABLE}* {str += yytext; strerr += yytext;}
<STRING>\\{NEWLINE} {
line_count++;
strerr += yytext;
strtype = "MULTI LINE STRING";
}
<STRING>\\({CHAR}|{ESCAPED}) {str += convert_escaped(yytext[1]); strerr += yytext;}
<STRING>\" {
strerr += "\"";
string_output();
BEGIN(INITIAL);
}
<STRING><<EOF>> {
string_error("UNFINISHED_STRING");
error_count++;
BEGIN(INITIAL);
}
<STRING>{NEWLINE} {
string_error("UNFINISHED_STRING");
error_count++;
line_count++;
BEGIN(INITIAL);
}
\/\/ {
str = "//";
strtype = "SINGLE LINE COMMENT";
first_line = line_count;
BEGIN(COMMENT);
}
<COMMENT>\\{NEWLINE} {
str += yytext;
line_count++;
}
<COMMENT>{NEWLINE} {
fprintf(logout, "Line# %d: Token <SINGLE LINE COMMENT> Lexeme %s found\n", first_line, str.c_str());
line_count++;
BEGIN(INITIAL);
}
<COMMENT>[ !-\[\]-~\t\f\v]* {str += yytext;}
<COMMENT>\\/[^{NEWLINE}] {str += "\\";}
\/\* {
str = "/*";
first_line = line_count;
BEGIN(MULCOMMENT);
}
<MULCOMMENT>[ !-\)+-~\t\f\v]* {str += yytext;}
<MULCOMMENT>{NEWLINE} {
str += "\n";
line_count++;
}
<MULCOMMENT>\*/[^\/] {str += "*";}
<MULCOMMENT>\*\/ {
str += "*/";
fprintf(logout, "Line# %d: Token <MULTI LINE COMMENT> Lexeme %s found\n", first_line, str.c_str());
BEGIN(INITIAL);
}
<MULCOMMENT><<EOF>> {
strerr = str;
error_count++;
string_error("UNFINISHED_COMMENT");
BEGIN(INITIAL);
}
. {error("UNRECOGNIZED_CHAR"); error_count++;}
%%