-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcool.flex
254 lines (222 loc) · 5.8 KB
/
cool.flex
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
/*
* The scanner definition for COOL.
*/
/*
* Stuff enclosed in %{ %} in the first section is copied verbatim to the
* output, so headers and global definitions are placed here to be visible
* to the code in the file. Don't remove anything that was here initially
*/
%{
#include <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>
/* The compiler assumes these identifiers. */
#define yylval cool_yylval
#define yylex cool_yylex
/* Max size of string constants */
#define MAX_STR_CONST 1025
#define YY_NO_UNPUT /* keep g++ happy */
extern FILE *fin; /* we read from this file */
/* define YY_INPUT so we read from the FILE fin:
* This change makes it possible to use this scanner in
* the Cool compiler.
*/
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
if ( (result = fread( (char*)buf, sizeof(char), max_size, fin)) < 0) \
YY_FATAL_ERROR( "read() in flex scanner failed");
char string_buf[MAX_STR_CONST]; /* to assemble string constants */
int string_buf_idx;
extern int curr_lineno;
extern int verbose_flag;
extern YYSTYPE cool_yylval;
/*
* Add Your own definitions here
*/
int num_open = 0;
bool null_in_string = false;
%}
/*
* Define names for regular expressions here.
*/
A [Aa]
B [Bb]
C [Cc]
D [Dd]
E [Ee]
F [Ff]
G [Gg]
H [Hh]
I [Ii]
J [Jj]
K [Kk]
L [Ll]
M [Mm]
N [Nn]
O [Oo]
P [Pp]
Q [Qq]
R [Rr]
S [Ss]
T [Tt]
U [Uu]
V [Vv]
W [Ww]
X [Xx]
Y [Yy]
Z [Zz]
%Start COMMENT ONELINECOMMENT STRING
%%
/*
* Comments
*/
<COMMENT><<EOF>> {
BEGIN (0);
cool_yylval.error_msg = "EOF in comment";
return (ERROR);
}
<INITIAL>"*)" {
cool_yylval.error_msg = "Unmatched *)";
return (ERROR);
}
<INITIAL,COMMENT>"(*" { num_open++; BEGIN (COMMENT); }
<COMMENT>. ;
<COMMENT>\n { curr_lineno++; }
<COMMENT>"*)" { num_open--; if(num_open == 0) BEGIN (0); }
<INITIAL>-- { BEGIN (ONELINECOMMENT); }
<ONELINECOMMENT>[^\n] ;
<ONELINECOMMENT>\n { curr_lineno++; BEGIN (0); }
/*
* Strings
*/
<STRING><<EOF>> {
BEGIN (0);
cool_yylval.error_msg = "EOF in string constant";
return (ERROR);
}
<INITIAL>\" {
BEGIN (STRING);
null_in_string = false;
string_buf_idx = 0;
}
<STRING>\n { /* Unescaped newline breaks the string */
curr_lineno++;
BEGIN (0);
cool_yylval.error_msg = "Unterminated string constant";
return (ERROR);
}
<STRING>\\\n { /* Escaped newline is ok */
string_buf[string_buf_idx++] = '\n';
curr_lineno++;
}
<STRING>\\\0|\0 { /* Null poisons the string but \ followed by 0 is ok */
null_in_string = true;
}
<STRING>[^\"] {
string_buf[string_buf_idx++] = yytext[0];
}
<STRING>\\. {
switch (yytext[1]) {
case 'b': string_buf[string_buf_idx++] = '\b'; break;
case 't': string_buf[string_buf_idx++] = '\t'; break;
case 'n': string_buf[string_buf_idx++] = '\n'; break;
case 'f': string_buf[string_buf_idx++] = '\f'; break;
default: string_buf[string_buf_idx++] = yytext[1]; break;
}
}
<STRING>\" {
BEGIN (0);
if (null_in_string) {
cool_yylval.error_msg = "String contains null character";
return (ERROR);
} else if (string_buf_idx >= MAX_STR_CONST) {
cool_yylval.error_msg = "String constant too long";
return (ERROR);
} else {
string_buf[string_buf_idx] = '\0';
cool_yylval.symbol = stringtable.add_string(string_buf);
return (STR_CONST);
}
}
/*
* Keywords
*/
<INITIAL>{C}{L}{A}{S}{S} { return CLASS; }
<INITIAL>{E}{L}{S}{E} { return ELSE; }
<INITIAL>{F}{I} { return FI; }
<INITIAL>{I}{F} { return IF; }
<INITIAL>{I}{N} { return IN; }
<INITIAL>{I}{N}{H}{E}{R}{I}{T}{S} { return INHERITS; }
<INITIAL>{I}{S}{V}{O}{I}{D} { return ISVOID; }
<INITIAL>{L}{E}{T} { return LET; }
<INITIAL>{L}{O}{O}{P} { return LOOP; }
<INITIAL>{P}{O}{O}{L} { return POOL; }
<INITIAL>{T}{H}{E}{N} { return THEN; }
<INITIAL>{W}{H}{I}{L}{E} { return WHILE; }
<INITIAL>{C}{A}{S}{E} { return CASE; }
<INITIAL>{E}{S}{A}{C} { return ESAC; }
<INITIAL>{N}{E}{W} { return NEW; }
<INITIAL>{O}{F} { return OF; }
<INITIAL>{N}{O}{T} { return NOT; }
<INITIAL>t{R}{U}{E} {
cool_yylval.boolean = true;
return (BOOL_CONST);
}
<INITIAL>f{A}{L}{S}{E} {
cool_yylval.boolean = false;
return (BOOL_CONST);
}
/*
* Special symbols
*/
<INITIAL>\+ { return ('+'); }
<INITIAL>\/ { return ('/'); }
<INITIAL>- { return ('-'); }
<INITIAL>\* { return ('*'); }
<INITIAL>= { return ('='); }
<INITIAL>< { return ('<'); }
<INITIAL>\. { return ('.'); }
<INITIAL>~ { return ('~'); }
<INITIAL>, { return (','); }
<INITIAL>; { return (';'); }
<INITIAL>: { return (':'); }
<INITIAL>\( { return ('('); }
<INITIAL>\) { return (')'); }
<INITIAL>@ { return ('@'); }
<INITIAL>\{ { return ('{'); }
<INITIAL>\} { return ('}'); }
<INITIAL><- { return (ASSIGN); }
<INITIAL><= { return (LE); }
<INITIAL>=> { return (DARROW); }
/*
* Integers, identifiers, and whitespace
*/
<INITIAL>[0-9]+ {
cool_yylval.symbol = inttable.add_string(yytext);
return (INT_CONST);
}
<INITIAL>[a-z][0-9a-zA-Z_]* {
cool_yylval.symbol = idtable.add_string(yytext);
return (OBJECTID);
}
<INITIAL>[A-Z][0-9a-zA-Z_]* {
cool_yylval.symbol = idtable.add_string(yytext);
return (TYPEID);
}
<INITIAL>\n { curr_lineno++; }
<INITIAL>[ \f\r\t\v] ;
<INITIAL>. {
cool_yylval.error_msg = yytext;
return (ERROR);
}
/*
* Keywords are case-insensitive except for the values true and false,
* which must begin with a lower-case letter.
*/
/*
* String constants (C syntax)
* Escape sequence \c is accepted for all characters c. Except for
* \n \t \b \f, the result is c.
*
*/
%%