-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcool.flex
More file actions
98 lines (78 loc) · 2.02 KB
/
cool.flex
File metadata and controls
98 lines (78 loc) · 2.02 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
/*
* The scanner definition for COOL.
*/
/*
* 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 <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>
/* The compiler assumes these identifiers. */
#define yylval cool_yylval
#define yylex cool_yylex
/* Max size of string constants */
#define MAX_STR_CONST 1025
#define YY_NO_UNPUT /* keep g++ happy */
extern FILE *fin; /* we read from this file */
/* define YY_INPUT so we read from the FILE fin:
* This change makes it possible to use this scanner in
* the Cool compiler.
*/
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
if ( (result = fread( (char*)buf, sizeof(char), max_size, fin)) < 0) \
YY_FATAL_ERROR( "read() in flex scanner failed");
char string_buf[MAX_STR_CONST]; /* to assemble string constants */
char *string_buf_ptr;
extern int curr_lineno;
extern int verbose_flag;
extern YYSTYPE cool_yylval;
/*
* Add Your own definitions here
*/
%}
/*
* Define names for regular expressions here.
*/
DARROW =>
INTEGER [0-9]+
a [aA]
i [iI]
e [eE]
s [sS]
f [fF]
n [nN]
l [lL]
o [oO]
p [pP]
t [tT]
w [wW]
%%
/*
* Nested comments
*/
/*
* The multiple-character operators.
*/
{DARROW} { return (DARROW); }
{INTEGER} { cool_yylval.symbol = inttable.add_string(yytext);
return (INT_CONST);
}
{BOOL} {cool_yylval.boolean = stringtable.add_string(yytext);
return (BOOL_CONST);
}
{
/*
* Keywords are case-insensitive except for the values true and false,
* which must begin with a lower-case letter.
*/
/*
* String constants (C syntax)
* Escape sequence \c is accepted for all characters c. Except for
* \n \t \b \f, the result is c.
*
*/
%%