-
Notifications
You must be signed in to change notification settings - Fork 4
/
lexer.l
202 lines (168 loc) · 3.56 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
%option noyywrap
%option nomain
%option yylineno
%option case-insensitive
%{
#include <stdio.h>
#include "quadrupelcode.h"
#include "parser.tab.h"
// unary plus and minus can only appear at line beginnings and after opening paranthesises or *,/,%
int unary_flag=0;
%}
NEWLINE (\n|\n\l|\r\n)
WHITESPACE (" "|\t)
SINGLELINE_COMMENT ("//".*{NEWLINE})
MULTILINE_COMMENT ("/*".*"*/")
PREPROCESSOR ("#".*{NEWLINE})
INTEGER (0|[1-9][0-9]*)
FLOAT (0\.0|[1-9][0-9]*\.[0-9]+)
IDENTIFIER ([a-zA-Z_][a-zA-Z0-9_]*)
%%
({NEWLINE}+|{WHITESPACE}+) {
// ignore whitespaces and newlines
//printf("LEXER: found whitespace or newline sequence\n");
}
({SINGLELINE_COMMENT}|{MULTILINE_COMMENT}) {
// ingore comments
printf("LEXER: found comment\n");
}
{PREPROCESSOR} {
// ignore preprocessor directives
printf("LEXER: found preprocessor directive\n");
}
^("+"|"-") {
// must be unary
if('+' == yytext[0]) {
printf("LEXER: found unary +\n");
return(U_PLUS);
} else {
printf("LEXER: found unary -\n");
return(U_MINUS);
}
unary_flag = 0;
}
("("|")"|"{"|"}"|"<"|">"|"="|";"|","|"!") {
printf("LEXER: found %c\n", yytext[0]);
unary_flag = 1;
return(yytext[0]);
}
("/"|"*"|"%") {
printf("LEXER: found %c, changed to unary mode}\n", yytext[0]);
unary_flag = 1;
return(yytext[0]);
}
("+"|"-") {
if(1 == unary_flag) {
if('+' == yytext[0]) {
printf("LEXER: found unary +\n");
return(U_PLUS);
} else {
printf("LEXER: found unary -\n");
return(U_MINUS);
}
unary_flag = 0;
} else {
printf("LEXER: found %c\n", yytext[0]);
return(yytext[0]);
}
}
"void" {
printf("LEXER: found VOID\n");
return(VOID);
}
"int" {
printf("LEXER: found INT\n");
return(INT);
}
"float" {
printf("LEXER: found FLOAT\n");
return(FLOAT);
}
"constant" {
printf("LEXER: found NUMERICAL CONSTANT\n");
return(CONSTANT);
}
"if" {
printf("LEXER: found IF\n");
return(IF);
}
"else" {
printf("LEXER: found ELSE\n");
return(ELSE);
}
"return" {
printf("LEXER: found RETURN\n");
return(RETURN);
}
"do" {
printf("LEXER: found DO\n");
return(DO);
}
"while" {
printf("LEXER: found WHILE\n");
return(WHILE);
}
"for" {
printf("LEXER: found FOR\n");
return(FOR);
}
"++" {
printf("LEXER: found ++\n");
return(INC_OP);
}
"--" {
printf("LEXER: found --\n");
return(DEC_OP);
}
"==" {
printf("LEXER: found ==\n");
return(EQUAL);
}
"!=" {
printf("LEXER: found !=\n");
return(NOT_EQUAL);
}
">=" {
printf("LEXER: found >=\n");
return(GREATER_OR_EQUAL);
}
"<=" {
printf("LEXER: found <=\n");
return(LESS_OR_EQUAL);
}
"<<" {
printf("LEXER: found <<\n");
return(SHIFTLEFT);
}
"&&" {
printf("LEXER: found &&=\n");
return(LOG_AND);
}
"||" {
printf("LEXER: found ||\n");
return(LOG_OR);
}
{INTEGER} {
printf("LEXER: found integer constant: %d\n", atoi(yytext));
yylval.expr.value = strdup(yytext);
yylval.expr.cType = C_CONSTANT;
yylval.expr.type = ST_INT;
unary_flag = 0;
return(CONSTANT);
}
{FLOAT} {
printf("LEXER: found float constant: %f\n", atof(yytext));
yylval.expr.value = strdup(yytext);
yylval.expr.cType = C_CONSTANT;
yylval.expr.type = ST_REAL;
unary_flag = 0;
return(CONSTANT);
}
{IDENTIFIER} {
printf("LEXER: found identifier: %s\n", yytext);
yylval.str = malloc(strlen(yytext)*sizeof(char)+1);
strcpy(yylval.str, yytext);
unary_flag = 0;
return(IDENTIFIER);
}
%%