-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.l
297 lines (275 loc) · 4.18 KB
/
lex.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
%{
#include <string.h>
#include <stdio.h>
#include "y.tab.h"
void count();
//////////// Start added code ///////////
/////////// End added code ///////////
int linecount=1;
%}
%option noyywrap
%%
\n {
////printf("nl\n");
linecount++;
//count(); return NEWLINE;
}
"/*" {
comment();
}
"#include" {
return INCLUDE;
}
(\t|" "|\r|\f|\v) {
//printf("white space\n");
}
">" {
////printf("great\n");
count(); return GREAT;
}
"<" {
////printf("less\n");
count(); return LESS;
}
">=" {
count(); return GREATEQUALS;
}
"<=" {
////printf("less equals\n");
count(); return LESSEQUALS;
}
"\"" {
return QUOTE;
}
"{" {
//printf("openbrace\n");
count(); return OPENBRACE;
}
"}" {
//printf("closebrace\n");
count(); return CLOSEBRACE;
}
"->" {
count(); return ARROW;
}
";" {
//printf("semicolon\n");
count(); return SEMICOLON;
}
"+" {
count(); return PLUS;
}
"-" {
count(); return MINUS;
}
"/" {
count(); return DIVIDE;
}
"%" {
count(); return MOD;
}
"++" {
count(); return PLUSPLUS;
}
"--" {
count(); return MINUSMINUS;
}
"!" {
count(); return NOT;
}
"==" {
count(); return EQUALSEQUALS;
}
"=" {
count(); return EQUALS;
}
"!=" {
count(); return NOTEQUALS;
}
"+=" {
count(); return PLUSEQUALS;
}
"-=" {
count(); return MINUSEQUALS;
}
"*=" {
count(); return MULEQUALS;
}
"/=" {
count(); return DIVEQUALS;
}
"%=" {
count(); return MODEQUALS;
}
"," {
count(); return COMMA;
}
"*" {
count(); return STAR;
}
"&&" {
count(); return AND;
}
"&" {
count(); return ADDR;
}
"||" {
//printf("OR\n");
count(); return OR;
}
"(" {
//printf("openpar\n");
count(); return OPENPAR;
}
")" {
//printf("closepar\n");
count(); return CLOSEPAR;
}
"[" {
//printf("open bracket BITCH\n");
count(); return OPENBRACKET;
}
"]" {
count(); return CLOSEBRACKET;
}
"typedef" {
count(); return TYPEDEF;
}
"include" {
count(); return INCLUDE;
}
"return" {
count(); return RETURN;
}
"break" {
count(); return BREAK;
}
"signed" {
count(); return SIGNED;
}
"enum" {
count(); return ENUM;
}
"if" {
count(); return IF;
}
"else" {
count(); return ELSE;
}
"struct" {
count(); return STRUCT;
}
"do" {
count(); return DO;
}
"while" {
count(); return WHILE;
}
"for" {
count(); return FOR;
}
"static" {
count(); return STATIC;
}
"sizeof" {
count(); return SIZEOF;
}
"void" {
count(); return VOID;
}
"continue" {
count(); return CONTINUE;
}
"const" {
count(); return CONST;
}
"unsigned" {
count(); return UNSIGNED;
}
"int" {
//printf("int \n");
count(); return INT;
}
"char" {
count(); return CHAR;
}
"short" {
count(); return SHORT;
}
"long" {
count(); return LONG;
}
"float" {
count(); return FLOAT;
}
"double" {
count(); return DOUBLE;
}
\/\/[^\n]*\n {
}
"//#!" {
count(); return BAZINGA;
}
\'[\\]?[^\n\']?\' {
yylval.wd = strdup(yytext);
count(); return CHARACTER;
}
\"[^\n\"]*\" {
//printf("asfd\n");
yylval.wd = strdup(yytext);
count(); return WORD;
}
[0-9]+ {
//printf("numeral %s\n",yytext);
yylval.wd = strdup(yytext);
count(); return NUMERAL;
}
[0-9]+\.[0-9]* {
//printf("yytext val %s\n",yytext);
yylval.wd=strdup(yytext);
count(); return NUMERAL;
}
[a-zA-Z_][a-zA-Z_0-9]* {
//printf("\nid %s\n",yytext);
yylval.wd=strdup(yytext);
count(); return IDENTIFIER;
}
"." {
//printf("dot\n");
count(); return DOT;
}
":" {
count(); return COLON;
}
. {
////printf(".\n");
//count(); return NOTOKEN;
}
%%
int column = 0;
comment()
{
char c, c1;
loop:
while ((c = input()) != '*' && c != 0)
putchar(c);
if ((c1 = input()) != '/' && c != 0)
{
unput(c1);
goto loop;
}
if (c != 0)
putchar(c1);
}
void count()
{
int i;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n')
column = 0;
else if (yytext[i] == '\t')
column += 8 - (column % 8);
else
column++;
ECHO;
}