This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.l
361 lines (303 loc) · 6.62 KB
/
Lexer.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
%x STRINGSTATE
%x COMMENTMULTI
%X COMMENTSINGLE
%{
#include <bits/stdc++.h>
#include "SymbolTable.cpp"
#include "y.tab.h"
extern YYSTYPE yylval;
using namespace std;
int numberOfLines = 1;
int numberOfErrors = 0;
extern FILE* errors;
int linestart;
string s2 = "";
string s1 = "";
%}
/* Declare Lex styled macros */
WHITESPACE [ \t\r\f\v]
LETTERS [a-zA-Z]
DIGIT [0-9]
EXP [Ee][+-]?[0-9]+
NEWLINE \n
IDENTIFIER [a-zA-Z_][a-zA-Z0-9_]*
MULOP "*"|"/"|"%"
RELOP "<"|"<="|">"|">="|"!="|"=="
LOGICOP "||"|"&&"
BITOP "|"|"&"|"<<"|">>"|"^"|"~"
FLOATINGPOINT (({DIGIT}+([\.]?{DIGIT}*))|({DIGIT}*([\.]?{DIGIT}+))){EXP}?
CHARACTERLITERALS '[^\n\\]'
TOOMANYDECIMAL {DIGIT}*([\.]{DIGIT}*)+([\.]+{DIGIT}*)+{EXP}?
ILLFORMEDERROR {DIGIT}*([\.]+{DIGIT}*)*?([Ee][+-]?([0-9]+)?([\.]*{DIGIT}*)+)+
INVALIDIDENTIFIER {FLOATINGPOINT}{IDENTIFIER}{FLOATINGPOINT}*
MULTICHARACTERERROR '[^\',][^\n\',]+'
UNFINISHEDCHARACTER '[\\]'|'[^\n\']|'\\[nt\\afr"bv0]|'\\
EMPTYCHARERROR ''
UNRECOGNIZEDCHARACTER '(\\[^nt\\afr"bv0\n])+'
%%
{WHITESPACE}+ {}
{NEWLINE} {numberOfLines++;}
"if" {
return IF;
}
"else" {
return ELSE;
}
"for" {
return FOR;
}
"while" {
return WHILE;
}
"do" {
return DO;
}
"int" {
return INT;
}
"char" {
return CHAR;
}
"float" {
return FLOAT;
}
"double" {
return DOUBLE;
}
"void" {
return VOID;
}
"return" {
return RETURN;
}
"continue" {
return CONTINUE;
}
"printf" {
return PRINTLN;
}
"println" {
return PRINTLN;
}
{DIGIT}+ {
SymbolInfo *s = new SymbolInfo(yytext, "CONST_INT");
yylval.symbolInfoPointer = s;
return CONST_INT;
}
{FLOATINGPOINT} {
SymbolInfo *s = new SymbolInfo(yytext, "CONST_FLOAT");
yylval.symbolInfoPointer = s;
return CONST_FLOAT;
}
{CHARACTERLITERALS} {
SymbolInfo *s= new SymbolInfo(yytext,"CONST_CHAR");
yylval.symbolInfoPointer = s;
return CONST_CHAR;
}
'\\[nt\\\'afr"bv0]' {
switch(yytext[2])
{
case 't': yytext[0] = '\t';
break;
case 'n': yytext[0] = '\n';
break;
case '\\': yytext[0] = '\\';
break;
case '"' : yytext[0] = '"';
break;
case 'a' : yytext[0] = '\a';
break;
case 'f' : yytext[0] = '\f';
break;
case 'r' : yytext[0] = '\r';
break;
case 'b' : yytext[0] = '\b';
break;
case 'v' : yytext[0] = '\v';
break;
case '0' : yytext[0] = '\0';
break;
case '\'': yytext[0] = '\'';
break;
default: break;
}
yytext[1] = '\0';
SymbolInfo *s= new SymbolInfo(yytext,"CONST_CHAR");
yylval.symbolInfoPointer = s;
return CONST_CHAR;
}
"+"|"-" {
SymbolInfo *s= new SymbolInfo(yytext,"ADDOP");
yylval.symbolInfoPointer = s;
return ADDOP;
}
"++" {
SymbolInfo *s= new SymbolInfo(yytext,"INCOP");
yylval.symbolInfoPointer = s;
return INCOP;
}
"--" {
SymbolInfo *s= new SymbolInfo(yytext,"DECOP");
yylval.symbolInfoPointer = s;
return DECOP;
}
{MULOP} {
SymbolInfo *s= new SymbolInfo(yytext,"MULOP");
yylval.symbolInfoPointer = s;
return MULOP;
}
{RELOP} {
SymbolInfo *s= new SymbolInfo(yytext,"RELOP");
yylval.symbolInfoPointer = s;
return RELOP;
}
{LOGICOP} {
SymbolInfo *s= new SymbolInfo(yytext,"LOGICOP");
yylval.symbolInfoPointer = s;
return LOGICOP;
}
{BITOP} {
SymbolInfo *s= new SymbolInfo(yytext,"BITOP");
yylval.symbolInfoPointer = s;
return BITOP;
}
"=" {
return ASSIGNOP;
}
"!" {
SymbolInfo *s= new SymbolInfo(yytext,"BITOP");
yylval.symbolInfoPointer = s;
return NOT;
}
"(" {
return LPAREN;
}
")" {
return RPAREN;
}
"{" {
return LCURL;
}
"}" {
return RCURL;
}
"[" {
return LTHIRD;
}
"]" {
return RTHIRD;
}
"," {
return COMMA;
}
";" {
return SEMICOLON;
}
{IDENTIFIER} {
SymbolInfo *s= new SymbolInfo(yytext,"ID");
yylval.symbolInfoPointer = s;
return ID;
}
"//" {
BEGIN COMMENTSINGLE;
}
<COMMENTSINGLE>\n {
numberOfLines++;
BEGIN INITIAL;
}
<COMMENTSINGLE>[^\n] {
}
<COMMENTSINGLE>\\\n {
numberOfLines++;
}
"/*" {
s1 = s1+yytext;
linestart = numberOfLines;
BEGIN COMMENTMULTI;
}
<COMMENTMULTI>"*/" {
s1 = "";
BEGIN INITIAL;
}
<COMMENTMULTI>\n {
s1 = s1+yytext;
numberOfLines++;
}
<COMMENTMULTI>.|[^*\n] {
s1 = s1+yytext;
}
<COMMENTMULTI><<EOF>> {
fprintf(errors,"Error observed at Line No:%d: Unterminated Comment:\n\n",linestart);
s1 = "";
numberOfErrors++;
fprintf(errors,"Total Line No:%d\n",--numberOfLines);
fprintf(errors,"Total Errors:%d\n\n",numberOfErrors);
return 0;
}
\" {
s1 = s1+yytext;
linestart = numberOfLines;
BEGIN STRINGSTATE;
}
<STRINGSTATE>\" {
s1 = s1+yytext;
s1 = "";
s2 = "";
BEGIN INITIAL;
}
<STRINGSTATE>\n {
numberOfLines++;
fprintf(errors,"Error observed at Line No:%d: Unterminated string: %s\n\n",linestart,s1.c_str());
s1 = "";
numberOfErrors++;
BEGIN INITIAL;
}
<STRINGSTATE>\\\n {
numberOfLines++;
s1 = s1+yytext;
}
<STRINGSTATE>\\[nt\\afr"bv0] {
}
<STRINGSTATE>[^\n"\\] {
}
<STRINGSTATE>\\ {
}
{TOOMANYDECIMAL} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Too many decimal point: %s\n\n",numberOfLines,yytext);
}
{ILLFORMEDERROR} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Ill formed Number: %s\n\n",numberOfLines,yytext);
}
{INVALIDIDENTIFIER} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Invalid prefix on ID or invalid suffix on Number: %s\n\n",numberOfLines,yytext);
}
{UNRECOGNIZEDCHARACTER} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Unrecognized character constant error: %s\n\n",numberOfLines,yytext);
}
{MULTICHARACTERERROR} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Multi character constant error: %s\n\n",numberOfLines,yytext);
}
{EMPTYCHARERROR} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Empty character constant error: %s\n\n",numberOfLines,yytext);
}
{UNFINISHEDCHARACTER} {
numberOfErrors++;
fprintf(errors,"Error at line %d: Unterminated character: %s\n\n",numberOfLines,yytext);
}
. {
numberOfErrors++;
fprintf(errors,"Error at line %d: Unrecognized character constant error: %s\n\n",numberOfLines,yytext);
}
<<EOF>> {
return 0;
}
%%
int yywrap(void){
return 1;
}