-
Notifications
You must be signed in to change notification settings - Fork 0
/
cminusminus.jlex
330 lines (270 loc) · 10.1 KB
/
cminusminus.jlex
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
import java_cup.runtime.*; // defines the Symbol class
// The generated scanner will return a Symbol for each token that it finds.
// A Symbol contains an Object field named value; that field will be of type
// TokenVal, defined below.
//
// A TokenVal object contains the line number on which the token occurs as
// well as the number of the character on that line that starts the token.
// Some tokens (literals and IDs) also include the value of the token.
class TokenVal {
// fields
int linenum;
int charnum;
// constructor
TokenVal(int line, int ch) {
linenum = line;
charnum = ch;
}
}
class IntLitTokenVal extends TokenVal {
// new field: the value of the integer literal
int intVal;
// constructor
IntLitTokenVal(int line, int ch, int val) {
super(line, ch);
intVal = val;
}
}
class IdTokenVal extends TokenVal {
// new field: the value of the identifier
String idVal;
// constructor
IdTokenVal(int line, int ch, String val) {
super(line, ch);
idVal = val;
}
}
class StrLitTokenVal extends TokenVal {
// new field: the value of the string literal
String strVal;
// constructor
StrLitTokenVal(int line, int ch, String val) {
super(line, ch);
strVal = val;
}
}
// The following class is used to keep track of the character number at which
// the current token starts on its line.
class CharNum {
static int num=1;
}
%%
DIGIT= [0-9]
WHITESPACE= [\040\t]
LETTER= [a-zA-Z]
ESCAPEDCHAR= [nt'\"?\\]
NOTNEWLINEORESCAPEDCHAR= [^\nnt'\"?\\]
NOTNEWLINEORQUOTE= [^\n\"]
NOTNEWLINEORQUOTEORESCAPE= [^\n\"\\]
%implements java_cup.runtime.Scanner
%function next_token
%type java_cup.runtime.Symbol
%eofval{
return new Symbol(sym.EOF);
%eofval}
%line
%%
"bool" { Symbol S = new Symbol(sym.BOOL, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"int" { Symbol S = new Symbol(sym.INT, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"void" { Symbol S = new Symbol(sym.VOID, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"true" { Symbol S = new Symbol(sym.TRUE, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"false" { Symbol S = new Symbol(sym.FALSE, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"struct" { Symbol S = new Symbol(sym.STRUCT, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"cin" { Symbol S = new Symbol(sym.CIN, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"cout" { Symbol S = new Symbol(sym.COUT, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"if" { Symbol S = new Symbol(sym.IF, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"else" { Symbol S = new Symbol(sym.ELSE, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"while" { Symbol S = new Symbol(sym.WHILE, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"return" { Symbol S = new Symbol(sym.RETURN, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
"repeat" { Symbol S = new Symbol(sym.REPEAT, new TokenVal(yyline+1, CharNum.num));
CharNum.num += yytext().length();
return S;
}
({LETTER}|"_")({LETTER}|{DIGIT}|"_")* {
Symbol S = new Symbol(sym.ID,
new IdTokenVal(yyline+1, CharNum.num, yytext()));
CharNum.num += yytext().length();
return S;
}
{DIGIT}+ { double val = Double.parseDouble(yytext());
int intVal;
if (val > Integer.MAX_VALUE) {
ErrMsg.warn(yyline+1, CharNum.num,
"integer literal too large; using max value");
intVal = Integer.MAX_VALUE;
} else {
intVal = Integer.parseInt(yytext());
}
Symbol S = new Symbol(sym.INTLITERAL,
new IntLitTokenVal(yyline+1, CharNum.num, intVal));
CharNum.num += yytext().length();
return S;
}
\"({NOTNEWLINEORQUOTEORESCAPE}|\\{ESCAPEDCHAR})*\" {
String strVal = yytext();
Symbol S = new Symbol(sym.STRINGLITERAL,
new StrLitTokenVal(yyline+1, CharNum.num, strVal));
CharNum.num += yytext().length();
return S;
}
\"({NOTNEWLINEORQUOTEORESCAPE}|\\{ESCAPEDCHAR})* {
// unterminated string
ErrMsg.fatal(yyline+1, CharNum.num,
"unterminated string literal ignored");
}
\"({NOTNEWLINEORQUOTEORESCAPE}|\\{ESCAPEDCHAR})*\\{NOTNEWLINEORESCAPEDCHAR}({NOTNEWLINEORQUOTE})*\" {
// bad escape character
ErrMsg.fatal(yyline+1, CharNum.num,
"string literal with bad escaped character ignored");
CharNum.num += yytext().length();
}
\"({NOTNEWLINEORQUOTEORESCAPE}|\\{ESCAPEDCHAR})*(\\{NOTNEWLINEORESCAPEDCHAR})?({NOTNEWLINEORQUOTEORESCAPE}|\\{ESCAPEDCHAR})*\\? {
// bad escape character
ErrMsg.fatal(yyline+1, CharNum.num,
"unterminated string literal with bad escaped character ignored");
}
\n { CharNum.num = 1; }
{WHITESPACE}+ { CharNum.num += yytext().length(); }
("//"|"#")[^\n]* { // comment - ignore. Note: don't need to update char num
// since everything to end of line will be ignored
}
"{" { Symbol S = new Symbol(sym.LCURLY, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"}" { Symbol S = new Symbol(sym.RCURLY, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"(" { Symbol S = new Symbol(sym.LPAREN, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
")" { Symbol S = new Symbol(sym.RPAREN, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
";" { Symbol S = new Symbol(sym.SEMICOLON, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"," { Symbol S = new Symbol(sym.COMMA, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"." { Symbol S = new Symbol(sym.DOT, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"<<" { Symbol S = new Symbol(sym.WRITE, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
">>" { Symbol S = new Symbol(sym.READ, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"++" { Symbol S = new Symbol(sym.PLUSPLUS, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"--" { Symbol S = new Symbol(sym.MINUSMINUS, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"+" { Symbol S = new Symbol(sym.PLUS, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"-" { Symbol S = new Symbol(sym.MINUS, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"*" { Symbol S = new Symbol(sym.TIMES, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"/" { Symbol S = new Symbol(sym.DIVIDE, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"!" { Symbol S = new Symbol(sym.NOT, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"&&" { Symbol S = new Symbol(sym.AND, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"||" { Symbol S = new Symbol(sym.OR, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"==" { Symbol S = new Symbol(sym.EQUALS, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"!=" { Symbol S = new Symbol(sym.NOTEQUALS, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"<" { Symbol S = new Symbol(sym.LESS, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
">" { Symbol S = new Symbol(sym.GREATER, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
"<=" { Symbol S = new Symbol(sym.LESSEQ, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
">=" { Symbol S = new Symbol(sym.GREATEREQ, new TokenVal(yyline+1, CharNum.num));
CharNum.num += 2;
return S;
}
"=" { Symbol S = new Symbol(sym.ASSIGN, new TokenVal(yyline+1, CharNum.num));
CharNum.num++;
return S;
}
. { ErrMsg.fatal(yyline+1, CharNum.num,
"illegal character ignored: " + yytext());
CharNum.num++;
}