-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl22_scanner.l
286 lines (234 loc) · 10.6 KB
/
l22_scanner.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
%option c++ prefix="l22_scanner_" outfile="l22_scanner.cpp"
%option stack noyywrap yylineno 8bit debug
%{
// make relevant includes before including the parser's tab file
#include <string>
#include <vector>
#include <sstream>
#include <cstring>
#include <cdk/ast/sequence_node.h>
#include <cdk/ast/expression_node.h>
#include <cdk/ast/lvalue_node.h>
#include "l22_parser.tab.h"
// output stream for building string literals
static std::ostringstream strlit;
static bool restart_functions = false;
static int lLevelsToPop = 0, lIndex = 0;
static std::vector<int> indents; // known indentation levels
inline bool valid(int indent) {
lLevelsToPop = lIndex = -1;
bool indentOk = false;
for (size_t ix = 0; ix < indents.size(); ix++) {
if (indent == indents[ix]) {
lLevelsToPop = indents.size() - ix - 1;
lIndex = ix;
indentOk = true;
}
}
return indentOk;
}
// don't change this
#define yyerror LexerError
%}
SPACE [ ]
%x X_STRING X_COMMENT X_BACKSLASH X_SKIP
%x X_NEWLINE
%%
/*yydebug=1; set_debug(1);*/
{
//std::cerr << "################" << std::endl;
//set_debug(true);
if (restart_functions) {
//std::cerr << "POPPING FUNCTIONS (" << indents.size() << ")" << std::endl;
indents.pop_back();
if (indents.size() == 0) restart_functions = false;
return '}';
}
else {
if (lLevelsToPop > 0) {
//std::cerr << "POPPING LEVELS (" << lLevelsToPop << ")" << std::endl;
lLevelsToPop--;
indents.pop_back();
return '}';
}
}
}
/* ====================================================================== */
/* ====[ 3.2.2 - INDENTATION ]==== */
/* ====================================================================== */
\n yy_push_state(X_NEWLINE); /* delay decision */
<X_NEWLINE>^{SPACE}+ {
yy_pop_state();
if (valid(yyleng)) {
//std::cerr << "CASE 1" << std::endl;
if (lLevelsToPop == 0) {
// at the same level: ignore index
// do not return tINDENT;
//std::cerr << "CASE 1a (levels to pop = 0)" << std::endl;
return ';';
}
else {
// nothing is done here: the scanner will ignore the input while levels > 0
// and send block-end tokens to the parser
//std::cerr << "CASE 1b (levels to pop = " << lLevelsToPop << ")" << std::endl;
// at least one has to be returned here...
if (lLevelsToPop > 0) {
//std::cerr << "POPPING LEVELS (" << lLevelsToPop << ")" << std::endl;
lLevelsToPop--;
indents.pop_back();
return '}';
}
}
}
else if (indents.size() == 0) {
// first block to open
//std::cerr << "CASE 2" << std::endl;
indents.push_back(yyleng);
return '{';
}
else if (yyleng > indents.back()) {
// this is like the one before, but not the first
//std::cerr << "CASE 3" << std::endl;
indents.push_back(yyleng);
return '{';
}
else {
// something wrong: bad indent: communicate to user
//std::cerr << "CASE 4" << std::endl;
//std::cerr << "bad indent at line " << yylineno;
}
}
<X_NEWLINE>\n ; // ignore successive newline chars
<X_NEWLINE>. {
// in this case, we have a new function starting
// first, return whatever we are seeing to the input
yyless(0);
yy_pop_state();
if (indents.size() > 0) {
//std::cerr << "should restart " << indents.size() << " functions at " << yylineno << std::endl;
indents.pop_back();
if (indents.size() > 0) { restart_functions = true; }
return '}';
}
else return ';'; // top-level declarations
}
/* ====================================================================== */
/* ====[ 3.3 - COMMENTS ]==== */
/* ====================================================================== */
{SPACE}*"..."\n ;
";".* ; /* ignore comments */
"(*" yy_push_state(X_COMMENT);
<X_COMMENT>"(*" yy_push_state(X_COMMENT);
<X_COMMENT>"*)" yy_pop_state();
<X_COMMENT>. ; /* ignore comments */
/* ====================================================================== */
/* ====[ 3.6 - Operadores de expressões ]==== */
/* ====================================================================== */
">=" return tGE;
"<=" return tLE;
"==" return tEQ;
"!=" return tNE;
"input" return tINPUT;
"and" return tAND;
"or" return tOR;
"not" return tNOT;
"sizeof" return tSIZEOF;
/* ====================================================================== */
/* ====[ 3.9 - Literais ]==== */
/* ====================================================================== */
"int" return tINT;
"double" return tDOUBLE;
"text" return tTEXT;
"void" return tVOID;
"null" return tNULL;
"\"" yy_push_state(X_STRING); yylval.s = new std::string("");
<X_STRING>\\ yy_push_state(X_BACKSLASH);
<X_STRING>"\"" {
yylval.s = new std::string(strlit.str());
strlit.str("");
yy_pop_state();
return tSTRING;
}
<X_STRING>. strlit << *yytext;
<X_BACKSLASH>"0" yy_push_state(X_SKIP);
<X_BACKSLASH>n strlit << '\n'; yy_pop_state();
<X_BACKSLASH>r strlit << '\r'; yy_pop_state();
<X_BACKSLASH>t strlit << '\t'; yy_pop_state();
<X_BACKSLASH>\\ strlit << '\\'; yy_pop_state();
<X_BACKSLASH>[0-6]{1,3} {
strlit << (char)(unsigned char)strtoul(yytext, NULL, 7);
if (errno == ERANGE) yyerror("Overflow string base 7");
yy_pop_state();
}
<X_BACKSLASH>. strlit << *yytext; yy_pop_state();
<X_SKIP>"\"" {
yy_pop_state();yy_pop_state();yy_pop_state();
yylval.s = new std::string(strlit.str());
strlit.str("");
return tSTRING;
}
<X_SKIP>. ;
0[0-6]+ { yylval.i = strtoul(yytext, NULL, 7);
if (errno == ERANGE) yyerror("Overflow base 7");
else return tINTEGER;
}
0[0-9]+ yyerror("Invalid base 7 number.");
0|[1-9][0-9]* { yylval.i = strtol(yytext, nullptr, 10);
if (errno == ERANGE) yyerror("Overflow base 10");
else return tINTEGER;
}
([0-9]*\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+)([Ee][-+]?[0-9]+)? { yylval.d = strtod(yytext, NULL);
if (errno == ERANGE) yyerror("Overflow double");
else return tREAL;
}
/* ====================================================================== */
/* ====[ 4.5 - Símbolos globais ]==== */
/* ====================================================================== */
"public" return tPUBLIC;
"foreign" return tFOREIGN;
"use" return tUSE;
"var" return tVAR;
/* ====================================================================== */
/* ====[ 5 - Funções ]==== */
/* ====================================================================== */
"@" return tAT;
"->" return tARROW;
/* ====================================================================== */
/* ====[ 6.2 - Instrução condicional ]==== */
/* ====================================================================== */
"if" return tIF;
"else:" return tELSE;
"then:" return tTHEN;
"elif" return tELIF;
/* ====================================================================== */
/* ====[ 6.2 - Instrução condicional ]==== */
/* ====[ 6.3 - Instrução de iteração ]==== */
/* ====[ 6.4 - Instrução de terminação ]==== */
/* ====[ 6.5 - Instrução de continuação ]==== */
/* ====[ 6.6 - Instrução de retorno ]==== */
/* ====[ 6.8 - Instrução de impressão ]==== */
/* ====================================================================== */
"while" return tWHILE;
"do:" return tDO;
"stop" return tSTOP;
"again" return tAGAIN;
"return" return tRETURN;
"write" return tWRITE;
"writeln" return tWRITELN;
/* ====================================================================== */
/* ====[ 5.4 - Função principal e execução de programas ]==== */
/* ====================================================================== */
"begin" return tBEGIN;
"end" return tEND;
/* ====================================================================== */
/* ====[ 3.8 - Identificadores (nomes) ]==== */
/* ====================================================================== */
[A-Za-z]([A-Za-z_]|[[:digit:]])* yylval.s = new std::string(yytext); return tIDENTIFIER;
/* ====================================================================== */
/* ====[ Other symbols ]==== */
/* ====================================================================== */
[-()<>=+*/%;{}.:,\[\]?] return *yytext;
[\n]+ ; /* ignore whitespace */
[ ]+ ; /* ignore whitespace */
. yyerror("Unknown character");
%%