-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcool.lex
83 lines (63 loc) · 2.31 KB
/
cool.lex
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
/*
* The scanner definition for COOL.
*/
import java_cup.runtime.Symbol;
%%
%{
/* Stuff enclosed in %{ %} is copied verbatim to the lexer class
* definition, all the extra variables/functions you want to use in the
* lexer actions should go here. Don't remove or modify anything that
* was there initially. */
// Max size of string constants
static int MAX_STR_CONST = 1025;
// For assembling string constants
StringBuffer string_buf = new StringBuffer();
private int curr_lineno = 1;
int get_curr_lineno() {
return curr_lineno;
}
private AbstractSymbol filename;
void set_filename(String fname) {
filename = AbstractTable.stringtable.addString(fname);
}
AbstractSymbol curr_filename() {
return filename;
}
%}
%init{
/* Stuff enclosed in %init{ %init} is copied verbatim to the lexer
* class constructor, all the extra initialization you want to do should
* go here. Don't remove or modify anything that was there initially. */
// empty for now
%init}
%eofval{
/* Stuff enclosed in %eofval{ %eofval} specifies java code that is
* executed when end-of-file is reached. If you use multiple lexical
* states and want to do something special if an EOF is encountered in
* one of those states, place your code in the switch statement.
* Ultimately, you should return the EOF symbol, or your lexer won't
* work. */
switch(yy_lexical_state) {
case YYINITIAL:
/* nothing special to do in the initial state */
break;
/* If necessary, add code for other states here, e.g:
case COMMENT:
...
break;
*/
}
return new Symbol(TokenConstants.EOF);
%eofval}
%class CoolLexer
%cup
%%
<YYINITIAL>"=>" { /* Sample lexical rule for "=>" arrow.
Further lexical rules should be defined
here, after the last %% separator */
return new Symbol(TokenConstants.DARROW); }
. { /* This rule should be the very last
in your lexical specification and
will match match everything not
matched by other lexical rules. */
System.err.println("LEXER BUG - UNMATCHED: " + yytext()); }