forked from NesManrique/LenguajesII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.l
222 lines (181 loc) · 5.71 KB
/
lexer.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
%{
#include "ast.cpp"
#include "parser.hpp"
#include <math.h>
#include <string>
#include <limits>
#define SAVE_TOKEN yylval.string = new std::string(yytext, yyleng)
#define SAVE_STRING yylval.string = new std::string(string_buf)
#define MAX_STR_CONST 1024
int yycolumn = 1;
#define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno;\
yylloc.first_column = yycolumn; yylloc.last_column = yycolumn+yyleng-1;\
yycolumn +=yyleng;
char string_buf[MAX_STR_CONST];
char *string_buf_ptr;
int string_len;
extern "C" int yywrap() { }
%}
DIGIT [0-9]
DOUBLE {DIGIT}+\.{DIGIT}+([eE][+-]?{DIGIT}+)?
%x COMMENT
%x STRING
%x STRING2
%x COMMENTL
%x CHARC
%%
<INITIAL>{
"/*" BEGIN(COMMENT);
\" {string_buf_ptr=string_buf;BEGIN(STRING);}
\' {string_buf_ptr=string_buf;BEGIN(STRING2);}
"//" BEGIN(COMMENTL);
"#" BEGIN(CHARC);
{DIGIT}+ {if((yylval.integer=strtol(yytext,NULL,0))<std::numeric_limits<int>::min()){
fprintf(stderr,"Warning: Underflow detected in line %d\n", yylineno);
}else if((yylval.integer=strtol(yytext,NULL,0))>std::numeric_limits<int>::max()){
fprintf(stderr,"Warning: Overflow detected in line %d\n",yylineno);}
return INT;}
{DOUBLE} { errno=0;
if((yylval.floating=strtod(yytext,NULL))==HUGE_VAL){
perror("Warning floating point number cause overflow\n");
}else if((yylval.floating=strtod(yytext,NULL))==0 &&
errno==ERANGE){
perror("Warning floating point number cause underflow\n");
}
return FLOAT;}
[(){}\[\],.!=<>%] {return yytext[0];}
"is" {return '=';}
"==" {return EQ;}
"<=" {return LEQ;}
">=" {return GEQ;}
"!=" {return NEQ;}
"->" {return ACCESS;}
"&&" |
"and" {return AND;}
"||" |
"or" {return OR;}
"^" |
"if" {return IF;}
"then" {return THEN;}
"else" {return ELSE;}
"true" {return TRUE;}
"false" {return FALSE;}
"while" {return WHILE;}
"do" {return DO;}
"stop" {return STOP;}
"next" {return NEXT;}
"for" {return FOR;}
"from" {return FROM;}
"in" {return IN;}
"to" {return TO;}
"return" {return RETURN;}
"step" {return STEP;}
"+" |
"plus" {return '+';}
"-" |
"minus" {return '-';}
"/" |
"dividedby" {return '/';}
"*" |
"times" {return '*';}
"not" {return '!';}
"string" {return STRIN;}
"register" {return REGISTER;}
"union" {return UNION;}
"arrayof" {return ARRAY;}
/*"function" {return FUN;}*/
[A-Z][A-Za-z0-9_]* {SAVE_TOKEN; return TYPEID;}
[A-Za-z][A-Za-z0-9_]* {SAVE_TOKEN; return ID;}
\n {yylineno++;
yycolumn=1;}
[ \t] /* Ignore whitespace */
}
<COMMENT>{
"*/" BEGIN(INITIAL);
[^*\n]+ // eat comment in chunks
"*" // eat the lone star
\n yylineno++;
}
<COMMENTL>{
[^\n]+
\n {yylineno++;BEGIN(INITIAL);}
}
<STRING>{
\" { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
/* return string constant token type and
* value to parser
*/
SAVE_STRING;
cout << "yylval " << *yylval.string <<endl;
printf("str buf %s\n",string_buf);
return STR;
}
\n {/* error - unterminated string constant */
/* generate error message */
printf("Unterminated string\n");
yycolumn=1;
}
\\n *string_buf_ptr++ = '\n';
\\v *string_buf_ptr++ = '\v';
\\t *string_buf_ptr++ = '\t';
\\r *string_buf_ptr++ = '\r';
\\b *string_buf_ptr++ = '\b';
\\f *string_buf_ptr++ = '\f';
\\" *string_buf_ptr++ = '"';
[^\\\n\"]+ {char *yptr = yytext;
while ( *yptr )
*string_buf_ptr++ = *yptr++;
}
}
<STRING2>{
\' { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
/* return string constant token type and
* value to parser
*/
SAVE_STRING;
cout << "yylval " << yylval.string <<endl;
return STR;
}
\n {/* error - unterminated string constant */
/* generate error message */
printf("Unterminated string\n");
yycolumn = 1;
}
[^\']+ {char *yptr = yytext;
while ( *yptr )
*string_buf_ptr++ = *yptr++;
}
}
<CHARC>{
\n {printf("Character Error\n");
yycolumn = 1;}
\\n {yylval.Char = '\n';
BEGIN(INITIAL);
return CHAR;}
\\v {yylval.Char = '\v';
BEGIN(INITIAL);
return CHAR;}
\\t {yylval.Char = '\t';
BEGIN(INITIAL);
return CHAR;}
\\r {yylval.Char = '\r';
BEGIN(INITIAL);
return CHAR;}
\\b {yylval.Char = '\b';
BEGIN(INITIAL);
return CHAR;}
\\f {yylval.Char = '\f';
BEGIN(INITIAL);
return CHAR;}
\\s {yylval.Char = ' ';
BEGIN(INITIAL);
return CHAR;}
. {yylval.Char = yytext[0];
BEGIN(INITIAL);
return CHAR;}
}
%%