-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.ll
230 lines (179 loc) · 5.83 KB
/
scan.ll
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
/*********************************************************************************************
cfglp : A CFG Language Processor
--------------------------------
About:
Implemented by Uday Khedker (http://www.cse.iitb.ac.in/~uday) for
the courses cs302+cs306: Language Processors (theory and lab) at
IIT Bombay. Release date Jan 6, 2013. Copyrights reserved by Uday
Khedker. This implemenation has been made available purely for
academic purposes without any warranty of any kind.
Please see the README file for some details. A doxygen generated
documentation can be found at http://www.cse.iitb.ac.in/~uday/cfglp
***********************************************************************************************/
%option noyywrap
%{
# include <cstdlib>
using namespace std;
#include "common-classes.hh"
#include "evaluate.hh"
# include "cfglp-ctx.hh"
# include "options.hh"
# include "reg-alloc.hh"
# include "symtab.hh"
# include "ast.hh"
# include "program.hh"
# include "parse.tab.hh"
# include "support.hh"
# include "icode.hh"
/*
Our scanner specification allows us to trap
- the tokens
- the tokan codes, and
- the lexemes
being passed from the scanner to the parser for the
purpose of testing the scanner.
This is enabled by renaming the lex generated function to
lexscan and defining a wrapper function yylex that
- receives the information from lexscan,
- prints the information (if -tokens option has been provided)
- and makes it available to the parser in a transparent manner.
*/
void store_Token_Name(const char * tok_name);
void ignore_Token();
static char * token_name;
static bool show_tokens = false;
static ostream * tokens_fp = NULL;
#define YY_DECL int lexscan(yy::cfglp::semantic_type *yylval, \
yy::cfglp::location_type *yylloc, cfglp_ctx &ctx)
// make location include the current token
# define YY_USER_ACTION yylloc->columns (yyleng);
typedef yy::cfglp::token token;
%}
digit [0-9]
letter [a-z_A-Z_]
operator [-+*/=]
%%
%{
// start where previous token ended
yylloc->step ();
%}
[<>:{}();] {
store_Token_Name ("META CHAR");
return yytext[0];
}
[-]?{digit}+ {
store_Token_Name ("I_NUM");
yylval->ival = atoi(yytext);
return token::I_NUM;
}
[-]?{digit}+[.]{digit}+ {
store_Token_Name ("D_NUM");
yylval->fval = atof(yytext);
return token::D_NUM;
}
[-]?{digit}+[.]{digit}+[e][+-]{digit}+ {
store_Token_Name ("D_NUM");
yylval->fval = atof(yytext);
return token::D_NUM;
}
[<]{letter}+" "{digit}+[>] {
store_Token_Name("BB_ID");
yylval->sval = new std::string (yytext);
return token::BB_ID;
}
{operator} {
store_Token_Name ("OPERATOR");
return yytext[0];
}
int {
store_Token_Name("INT");
return token::INT;
}
float {
store_Token_Name("FLOAT");
return token::FLOAT;
}
double {
store_Token_Name("DOUBLE");
return token::DOUBLE;
}
return {
store_Token_Name("RETURN");
return token::RETURN;
}
static {
store_Token_Name("STATIC");
return token::STATIC;
}
{letter}({letter}|{digit})* {
store_Token_Name("ID");
yylval->sval = new std::string (yytext);
return token::ID;
}
"D"[.]{digit}{4}{digit}* {
store_Token_Name("EXP_VAR");
yylval->sval = new std::string (yytext);
return token::EXP_VAR;
}
{letter}({letter}|{digit})*[.]{digit}+ {
store_Token_Name("ARTIFICIAL_VAR");
yylval->sval = new std::string (yytext);
return token::ARTIFICIAL_VAR;
}
\n {
yylloc->lines(1);
yylineno++;
/*
cout << "scanned line " << yylineno << "\n";
*/
ignore_Token();
}
/* skip over comments and white space */
";;".* |
[ \t] { yylloc->step ();
ignore_Token();
}
. {
stringstream ss;
ss << "Illegal character `" << yytext << "' on line " << yylineno;
report_Violation_of_Condition(false, ss.str());
}
%%
void init_Scanner ()
{
show_tokens = cmd_options.show_Tokens();
if (show_tokens)
tokens_fp = cmd_options.tokens_File();
}
void store_Token_Name(const char * tok_name)
{
if (token_name) delete token_name;
token_name = strdup (tok_name);
}
int yylex(yy::cfglp::semantic_type *yylval,
yy::cfglp::location_type *yylloc, cfglp_ctx &ctx)
{ int token_code;
token_code = lexscan(yylval, yylloc, ctx);
if (show_tokens)
{
if (token_code)
{
string mesg = " Token name has not been set for the lexeme `" + string(yytext) +"'";
CHECK_INVARIANT (token_name, mesg)
*tokens_fp << "Line: " << yylineno << " Token name:" << token_name << "\ttoken code:" << token_code << "\tlexeme: `" << yytext << "'\n";
delete token_name;
token_name = NULL;
}
}
return token_code;
}
void ignore_Token()
{
if (show_tokens)
{
if (yytext[0] == '\n')
*tokens_fp << "Line: "<< yylineno << " Ignored NEWLINE character\n";
else
*tokens_fp << "Line: "<< yylineno << " Ignored lexeme: '" << yytext << "'\n";
}
}