-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.l
238 lines (229 loc) · 8.95 KB
/
temp.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
/* File: scanner.l
* ----------------
* Lex input file to generate the scanner
*/
%{
/* The text within this first region delimited by %{ and %} is assumed to
* be C/C++ code and will be copied verbatim to the lex.yy.c file ahead
* of the definitions of the yylex() function. Add other header file inclusions
* or C++ variable declarations/prototypes that are needed by your code here.
*/
#include <string.h>
#include <ctype.h>
#include "scanner.h"
#include "utility.h" // for PrintDebug()
#include "errors.h"
#include "parser.h" // for token codes, yylval
#include "list.h"
/* Macro: YY_USER_ACTION
* ---------------------
* This flex built-in macro can be defined to provide an action which is
* always executed prior to any matched rule's action. Basically, it is
* a way of having a piece of code common to all actions factored out to
* this routine. We already defined it for you and left the empty
* function DoBeforeEachAction ready for your use as needed. It will
* be called once for each pattern scanned from the file, before
* executing its action.
*/
static void DoBeforeEachAction();
#define YY_USER_ACTION DoBeforeEachAction();
#define TAB_SIZE 8
int lineno, colno;
List<const char*> savedlines;
%}
/* The section before the first %% is the Definitions section of the lex
* input file. Here is where you set options for the scanner, define lex
* states, and can set up definitions to give names to regular expressions
* as a simple substitution mechanism that allows for more readable
* entries in the Rules section later.
*/
PUNCTUATION ([!:;,.[\]{}()])
ARITHMETIC ([-+/*%])
RELATIONAL ([<>=])
OPERATOR ({ARITHMETIC}|{RELATIONAL})
DECIMAL ([0-9]+)
HEXADECIMAL (0[xX][0-9a-fA-F]+)
BEG_STRING (\"[^"\n\r]*)
STRING ({BEG_STRING}\")
BOOLEAN (true|false)
INTEGER ({DECIMAL}|{HEXADECIMAL})
FLOAT ({DECIMAL}\.{DECIMAL}?((E|e)(\+|\-)?{DECIMAL})?)
IDENTIFIER ([a-zA-Z][a-zA-Z0-9_]*)
SINGLE_COMMENT ("//"[^\n\r]*)
BEG_COMMENT ("/*")
END_COMMENT ("*/")
%x COPY COMMENT
%option stack
%% /* BEGIN RULES SECTION */
/* All patterns and actions should be placed between the start and stop
* %% markers which delimit the Rules section.
*/
<COPY>.* {
savedlines.Append(strdup(yytext));
colno = 1;
yy_pop_state();
yyless(0);
}
<COPY><<EOF>> { yy_pop_state(); }
<*>\n {
colno = 1; lineno++;
if (YYSTATE == COPY)
savedlines.Append("");
else
yy_push_state(COPY);
}
<*>\r {
colno = 1; lineno++;
if (YYSTATE == COPY)
savedlines.Append("");
else
yy_push_state(COPY);
}
<*>[\t] { colno += TAB_SIZE - colno % TAB_SIZE + 1; }
[ ]+ ;
/* recognize all keywords and return the correct token from scanner.h */
"void" { return T_Void; }
"int" { return T_Int; }
"double" { return T_Double; }
"bool" { return T_Bool; }
"string" { return T_String; }
"class" { return T_Class; }
"interface" { return T_Interface; }
"null" { return T_Null; }
"this" { return T_This; }
"extends" { return T_Extends; }
"implements" { return T_Implements; }
"for" { return T_For; }
"while" { return T_While; }
"if" { return T_If; }
"else" { return T_Else; }
"return" { return T_Return; }
"break" { return T_Break; }
"switch" { return T_Switch; }
"case" { return T_Case; }
"default" { return T_Default; }
"new" { return T_New; }
"NewArray" { return T_NewArray; }
"Print" { return T_Print; }
"ReadInteger" { return T_ReadInteger; }
"ReadLine" { return T_ReadLine; }
/* recognize punctuation and single-char operators
* and return the ASCII value as the token
*/
{PUNCTUATION} |
{OPERATOR} { return yytext[0]; }
/* recognize two-character operators and return the correct token */
"<=" { return T_LessEqual; }
">=" { return T_GreaterEqual; }
"==" { return T_Equal; }
"!=" { return T_NotEqual; }
"[]" { return T_Dims; }
"&&" { return T_And; }
"||" { return T_Or; }
"++" { return T_Increment; }
"--" { return T_Decrement; }
/* recognize int, double, bool and string constants,
* return the correct token
* and set appropriate filed of yylval
*/
{STRING} {
yylval.stringConstant = strdup(yytext);
return T_StringConstant;
}
{BEG_STRING} {
ReportError::UntermString(&yylloc, yytext);
}
{BOOLEAN} {
if (strcmp("true", yytext) == 0)
yylval.boolConstant = true;
else
yylval.boolConstant = false;
return T_BoolConstant;
}
{DECIMAL} {
yylval.integerConstant = strtol(yytext, NULL, 10); return T_IntConstant;
}
{HEXADECIMAL} {
yylval.integerConstant = strtol(yytext, NULL, 16);
return T_IntConstant;
}
{FLOAT} {
yylval.doubleConstant = atof(yytext); return T_DoubleConstant;
}
/* recognize identifiers,
* return the correct token and set appropriate fields of yylval
*/
{IDENTIFIER} {
if (yyleng > MaxIdentLen)
ReportError::LongIdentifier(&yylloc, yytext);
strncpy(yylval.identifier, yytext, MaxIdentLen);
yylval.identifier[MaxIdentLen] = '\0';
return T_Identifier;
}
/* consume single-line comment */
{SINGLE_COMMENT} ;
/* consume multi-line comments
* report unterminated comment
*/
{BEG_COMMENT} { BEGIN COMMENT; }
<COMMENT>. ;
<COMMENT>{END_COMMENT} { BEGIN INITIAL; }
<COMMENT><<EOF>> {
ReportError::UntermComment();
BEGIN INITIAL;
}
/* all other characters are reported as errors */
. {
ReportError::UnrecogChar(&yylloc, yytext[0]);
}
%%
/* The closing %% above marks the end of the Rules section and the beginning
* of the User Subroutines section. All text from here to the end of the
* file is copied verbatim to the end of the generated lex.yy.c file.
* This section is where you put definitions of helper functions.
*/
/* Function: InitScanner
* ---------------------
* This function will be called before any calls to yylex(). It is designed
* to give you an opportunity to do anything that must be done to initialize
* the scanner (set global variables, configure starting state, etc.). One
* thing it already does for you is assign the value of the global variable
* yy_flex_debug that controls whether flex prints debugging information
* about each token and what rule was matched. If set to false, no information
* is printed. Setting it to true will give you a running trail that might
* be helpful when debugging your scanner. Please be sure the variable is
* set to false when submitting your final version.
*/
void InitScanner()
{
PrintDebug("lex", "Initializing scanner");
yy_flex_debug = false;
yy_push_state(COPY);
lineno = 1;
colno = 1;
}
/* Function: DoBeforeEachAction()
* ------------------------------
* This function is installed as the YY_USER_ACTION. This is a place
* to group code common to all actions.
*/
static void DoBeforeEachAction()
{
yylloc.first_line = yylloc.last_line = lineno;
yylloc.first_column = colno;
yylloc.last_column = colno + yyleng - 1;
colno = colno + yyleng;
}
/* Function: GetLinenumbered()
* ---------------------------
* Returns string with contents of line numbered n or NULL if the
* contents of that line are not available. Our scanner copies
* each line scanned and appends each to a list so we can later
* retrieve them to report the context for errors.
*/
const char *GetLineNumbered(int num)
{
if (num <= 0 || num > savedlines.NumElements())
return NULL;
return savedlines.Nth(num - 1);
}