-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lex
367 lines (314 loc) · 7.99 KB
/
test.lex
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* The scanner definition for NET.
*/
/*
* 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 <signal.h>
# include <stdlib.h>
# include <unistd.h>
# include <tclap/CmdLine.h>
# include <net-parser.H>
# include <net-symtbl.H>
# include "test.tab.h"
# define YYDEBUG 1
size_t curr_lineno = 0;
StringTable string_table;
IdTable id_table;
/* Max size of string constants */
# define MAX_STR_CONST 4097
# define MAX_CWD_SIZE 4097
# define YY_NO_UNPUT /* keep g++ happy */
char string_buf[MAX_STR_CONST]; /* to assemble string constants */
char *string_buf_ptr = string_buf;
bool string_error = false;
inline bool put_char_in_buf(char c)
{
if (string_buf_ptr == &string_buf[MAX_STR_CONST - 1])
{
yylval.error_msg = "String constant too long";
string_error = true;
return false;
}
*string_buf_ptr++ = c;
return true;
}
%}
%x STRING
/*
* Define names for regular expressions here.
*/
/* Keywords */
LOAD [lL][oO][aA][dD]
SAVE [sS][aA][vV][eE]
EXIT [eE][xX][iI][tT]
INFO [iI][nN][fF][oO]
LS [lL][sS]
RM [rR][mM]
SEARCH [sS][eE][aA][rR][cC][hH]
PRODUCER [pP][rR][oO][dD][uU][cC][eE][rR]
PRODUCERS [pP][rR][oO][dD][uU][cC][eE][rR][sS]
PRODUCT [pP][rR][oO][dD][uU][cC][tT]
ID [iI][dD]
REGEX [rR][eE][gG][eE][xX]
LIST [lL][iI][sS][tT]
APPEND [aA][pP][pP][eE][nN][dD]
HELP [hH][eE][lL][pP]
COD [cC][oO][dD]
TYPE [tT][yY][pP][eE]
RIF [rR][iI][fF]
NODE [nN][oO][dD][eE]
REACHABLE [rR][eE][aA][cC][hH][aA][bB][lL][eE]
COVER [cC][oO][vV][eE][rR]
DOT [dD][oO][tT]
PPDOT [pP][pP][dD][oO][tT]
UPSTREAM [uU][pP][sS][tT][rR][eE][aA][mM]
INPUTS [iI][nN][pP][uU][tT][sS]
OUTPUTS [oO][uU][tT][pP][uU][tT][sS]
INPUT [iI][nN][pP][uU][tT]
OUTPUT [oO][uU][tT][pP][uU][tT]
ARCS [aa][rR][cC][sS]
PATH [pP][aA][tT][hH]
RANKS [rR][aA][nN][kK][sS]
SHAREHOLDER [sS][hH][aA][rR][eE][hH][oO][lL][dD][eE][rR]
HOLDING [hH][oO][lL][dD][iI][nN][gG]
ARC [aA][rR][cC]
HEGEMONY [hH][eE][gG][eE][mM][oO][nN][yY]
DEMAND [dD][eE][mM][aA][nN][dD]
PRODPLAN [pP][rR][oO][dD][pP][lL][aA][nN]
PRODUCERSET [pP][rR][oO][dD][uU][cC][eE][rR][sS][eE][tT]
ADDTOPRODUCERSET [aA][dD][dD][tT][oO][pP][rR][oO][dD][uU][cC][eE][rR][sS][eE][tT]
RMFROMPRODUCERSET [rR][mM][fF][rR][oO][mM][pP][rR][oO][dD][uU][cC][eE][rR][sS][eE][tT]
SPACE [ \f\r\t\v]
FLOAT [[:digit:]]+([\.][[:digit:]]+|[\.][[:digit:]]+[eE][+-]?[[:digit:]]+|[eE][+-]?[[:digit:]]+)
INTEGER [[:digit:]]+
VARNAME [[:alpha:]][[:alnum:]_.-]*
%%
{SPACE} /* Ignore spaces */
\n { ++curr_lineno; return '\n'; }
/*
* Keywords are case-insensitive except for the values true and false,
* which must begin with a lower-case letter.
*/
{LOAD} return LOAD;
{SAVE} return SAVE;
{EXIT} return EXIT;
{INFO} return INFO;
{LS} return LS;
{RM} return RM;
{SEARCH} return SEARCH;
{PRODUCER} return PRODUCER;
{PRODUCERS} return PRODUCERS;
{PRODUCT} return PRODUCT;
{REGEX} return REGEX;
{ID} return ID;
{LIST} return LIST;
{APPEND} return APPEND;
{COD} return COD;
{TYPE} return TYPEINFO;
({HELP}|\?) return HELP;
{RIF} return RIF;
{NODE} return NODE;
{REACHABLE} return REACHABLE;
{COVER} return COVER;
{DOT} return DOT;
{PPDOT} return PPDOT;
{UPSTREAM} return UPSTREAM;
{INPUT} return INPUT;
{OUTPUT} return OUTPUT;
{INPUTS} return INPUTS;
{OUTPUTS} return OUTPUTS;
{ARCS} return ARCS;
{PATH} return PATH;
{RANKS} return RANKS;
{SHAREHOLDER} return SHAREHOLDER;
{HOLDING} return HOLDING;
{ARC} return ARC;
{HEGEMONY} return HEGEMONY;
{DEMAND} return DEMAND;
{PRODPLAN} return PRODPLAN;
{PRODUCERSET} return PRODSET;
{ADDTOPRODUCERSET} return ADDPRODSET;
{RMFROMPRODUCERSET} return RMPRODSET;
/*
* The single-characters tokens
*/
[=;] return *yytext;
"[" return *yytext;
"]" return *yytext;
"{" return *yytext;
"}" return *yytext;
"," return *yytext;
/*
* String constants (C syntax)
* Escape sequence \c is accepted for all characters c. Except for
* \n \t \b \f, the result is c.
*
*/
\" { /* start of string */
string_buf_ptr = &string_buf[0];
string_error = false;
BEGIN(STRING);
}
<STRING>[^\\\"\n\0] {
if (not put_char_in_buf(*yytext))
return ERROR;
}
<STRING>\\\n { // escaped string
if (not put_char_in_buf('\n'))
return ERROR;
++curr_lineno;
}
<STRING>\\n {
if (not put_char_in_buf('\n'))
return ERROR;
}
<STRING>\\t {
if (not put_char_in_buf('\t'))
return ERROR;
}
<STRING>\\b {
if (not put_char_in_buf('\b'))
return ERROR;
}
<STRING>\\f {
if (not put_char_in_buf('\f'))
return ERROR;
}
<STRING>\\\0 {
yylval.error_msg = "String contains escaped null character.";
string_error = true;
return ERROR;
}
<STRING>'\n' {
BEGIN(INITIAL);
++curr_lineno;
yylval.error_msg = "Unterminated string constant";
return ERROR;
}
<STRING>\" { /* end of string */
*string_buf_ptr = '\0';
BEGIN(INITIAL);
if (not string_error)
{
yylval.symbol = string_table.addstring(string_buf);
return STRCONST;
}
}
<STRING>\\[^\n\0ntbf] {
if (not put_char_in_buf(yytext[1]))
return ERROR;
}
<STRING>'\0' {
yylval.error_msg = "String contains escaped null character.";
string_error = true;
return ERROR;
}
<STRING><<EOF>> {
yylval.error_msg = "EOF in string constant";
BEGIN(INITIAL);
return ERROR;
}
{FLOAT} { // matches double constant
yylval.symbol = id_table.addstring(yytext);
assert(yylval.symbol);
return FLOATCONST;
}
{INTEGER} { // matches integer constant
yylval.symbol = id_table.addstring(yytext);
assert(yylval.symbol);
return INTCONST;
}
{VARNAME} {
yylval.symbol = string_table.addstring(yytext);
assert(yylval.symbol);
return VARNAME;
}
. {
yylval.error_msg = yytext;
return ERROR;
}
%%
int yywrap()
{
return 1;
}
extern int yyparse();
string get_prompt(size_t i)
{
stringstream s;
s << i << " > ";
return s.str();
}
# ifdef YYDEBUG
int yydebug;
# endif
bool verbose = true;
using namespace TCLAP;
void process_comand_line(int argc, char *argv[])
{
CmdLine cmd("repl", ' ', "0.0");
SwitchArg verbose("v", "verbose", "verbose mode", true);
cmd.add(verbose);
cmd.parse(argc, argv);
::verbose = verbose.getValue();
}
extern ASTList * line_commands;
bool exit_by_ctr_c = false;
void my_handler(int s)
{
if (s == 15)
exit_by_ctr_c = true;
}
int main()
{
# ifdef YYDEBUG
yydebug = 1;
# endif
/* signal (SIGINT,my_handler); */
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = my_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
if (not resize_process_stack(128*1024*1024))
cout << "Warning: cannot resize process stack" << endl
<< endl;
for (size_t i = 0; true;)
{
string prompt = get_prompt(i);
char * line = readline(prompt.c_str());
if (line == nullptr)
break;
YY_BUFFER_STATE bp = yy_scan_string(line);
yy_switch_to_buffer(bp);
int status = yyparse();
if (status == 0)
{
++i;
if (line_commands != nullptr)
{
line_commands->for_each([] (auto c)
{
auto status = c->execute();
if (not status.first)
{
cout << "ERROR: " << status.second << endl;
c->free();
}
});
delete line_commands;
line_commands = nullptr;
}
cout << endl;
}
if (exit_by_ctr_c)
cout << "ctr-c pressed" << endl;
add_history(line);
free(line);
yy_delete_buffer(bp);
}
}