-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet-lex.lex
More file actions
206 lines (168 loc) · 3.97 KB
/
net-lex.lex
File metadata and controls
206 lines (168 loc) · 3.97 KB
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
/*
* 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 <net-parser.H>
# include "net.tab.h"
YYSTYPE netyylval;
size_t curr_lineno = 0;
# define yylval netyylval
/* Max size of string constants */
# define MAX_STR_CONST 4097
# define MAX_CWD_SIZE 4097
# define YY_NO_UNPUT /* keep g++ happy */
/* define YY_INPUT so we read thorugh readline */
/* # undef YY_INPUT */
/* # define YY_INPUT(buf, result, max_size) result = get_input(buf, max_size); */
char string_buf[MAX_STR_CONST]; /* to assemble string constants */
char *string_buf_ptr = string_buf;
/*
* Add Your own definitions here
*/
long nested_comment_counter = 0;
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 NESTED_COMMENT SINGLE_COMMENT STRING
/*
* Define names for regular expressions here.
*/
/* Keywords */
LOAD [lL][oO][aA][dD]
SAVE [sS][aA][vV][eE]
RIF [rR][iI][fF]
COD [cC][oO][dD]
EXIT [eE][xX][iI][tT]
DIGIT [0-9]
UPPER_LETTER [A-Z]
LOWER_LETTER [a-z]
ANY_LETTER ({UPPER_LETTER}|{LOWER_LETTER})
SPACE [ \f\r\t\v]
NEWLINE \n
INTEGER {DIGIT}+
ID {INTEGER}
VARNAME {ANY_LETTER}([_\.-]|{ANY_LETTER}|{DIGIT})*
%%
{SPACE} { cout << "SPACE" << endl; /* Ignore spaces */ }
{NEWLINE} { ++curr_lineno; cout << "MATCH NEWLINE" << endl; return NEWLINE; }
/*
* Keywords are case-insensitive except for the values true and false,
* which must begin with a lower-case letter.
*/
{LOAD} { cout << "MATCH LOAD" << endl; return LOAD; }
{SAVE} return SAVE;
{RIF} return RIF;
{COD} return COD;
{EXIT} return EXIT;
/*
* The single-characters tokens
*/
[=;] 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>{NEWLINE} {
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 = strdup(string_buf); // TODO: ojo con este memory leak
cout << "MATCH STRCONST" << endl;
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;
}
{ID} { // matches integer constant
yylval.symbol = yytext;
return ID;
}
{VARNAME} {
yylval.symbol = yytext;
return VARNAME;
}
"*)" {
yylval.error_msg = "Unmatched *)";
return ERROR;
}
. {
cout << "LEX ERROR" << endl;
yylval.error_msg = yytext;
return ERROR;
}
%%
int yywrap()
{
return 1;
}