-
Notifications
You must be signed in to change notification settings - Fork 0
/
minijava.flex
144 lines (119 loc) · 5.07 KB
/
minijava.flex
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
import java_cup.runtime.Symbol;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.Location;
%%
%public
%class Lexer
%cup
%implements sym
%char
%line
%column
%{
StringBuffer string = new StringBuffer();
String className = null;
public Lexer(java.io.Reader in, ComplexSymbolFactory sf){
this(in);
symbolFactory = sf;
}
ComplexSymbolFactory symbolFactory;
private Symbol symbol(String name, int sym) {
return symbolFactory.newSymbol(name, sym, new Location(yyline+1,yycolumn+1,yychar), new Location(yyline+1,yycolumn+yylength(),yychar+yylength()));
}
private Symbol symbol(String name, int sym, Object val) {
Location left = new Location(yyline+1,yycolumn+1,yychar);
Location right= new Location(yyline+1,yycolumn+yylength(), yychar+yylength());
return symbolFactory.newSymbol(name, sym, left, right,val);
}
private Symbol symbol(String name, int sym, Object val,int buflength) {
Location left = new Location(yyline+1,yycolumn+yylength()-buflength,yychar+yylength()-buflength);
Location right= new Location(yyline+1,yycolumn+yylength(), yychar+yylength());
return symbolFactory.newSymbol(name, sym, left, right,val);
}
private void error(String message) {
System.out.println("Error at line "+(yyline+1)+", column "+(yycolumn+1)+" : "+message);
}
%}
%eofval{
return symbolFactory.newSymbol("EOF", EOF, new Location(yyline+1,yycolumn+1,yychar), new Location(yyline+1,yycolumn+1,yychar+1));
%eofval}
IntLiteral = 0 | [1-9][0-9]*
new_line = \r|\n|\r\n;
white_space = {new_line} | [ \t\f]
IdName = [a-z][A-Za-z0-9_]*
ClassName = [A-Z][A-Za-z0-9_]*
%state STRING
%state BLOCKCOMMENT
%state LINECOMMENT
%%
<YYINITIAL>{
/* keywords */
"class" { return symbol("class", CLASS); }
"while" { return symbol("while", WHILE); }
"readln" { return symbol("readln", READLN); }
"println" { return symbol("println", PRINTLN); }
"if" { return symbol("if", IF); }
"else" { return symbol("else", ELSE); }
"this" { return symbol("this", THIS); }
"new" { return symbol("new", NEW); }
"null" { return symbol("null", NULL); }
"Void main" { return symbol("mainfunc", MAINFUNC); }
"return" { return symbol("return", RETURN); }
/* literals */
{IntLiteral} { return symbol("IntConst",INTCONST, new Integer(Integer.parseInt(yytext()))); }
"true" { return symbol("true", BOOLCONST, true); }
"false" { return symbol("false", BOOLCONST, false); }
/* operators */
"+" { return symbol("+",PLUS); }
"-" { return symbol("+",MINUS); }
"*" { return symbol("+",TIMES); }
"/" { return symbol("+",DIV); }
"<" { return symbol("<",LT); }
">" { return symbol(">",GT); }
"<=" { return symbol("<=",LEQ); }
">=" { return symbol(">=",GEQ); }
"==" { return symbol("==",EQ); }
"!=" { return symbol("!=",NEQ); }
"||" { return symbol("||",OR); }
"&&" { return symbol("&&",AND); }
"!" { return symbol("!",NOT); }
/* assignment */
"=" { return symbol("=", ASSIGN); }
/* blocks and statements */
"(" { return symbol("(",LPAREN); }
")" { return symbol(")",RPAREN); }
"{" { return symbol("{", LBLOCK); }
"}" { return symbol("}", RBLOCK); }
";" { return symbol(";", ENDSTMT); }
"," { return symbol(",", COMMA); }
"." { return symbol(",", DOT); }
/* identifiers */
{IdName} { String name = yytext(); return symbol("<id>"+name, IDNAME, name); }
{ClassName} { String name = yytext(); return symbol("<class>"+name, CLASSNAME, name); }
{white_space} { /* ignore */ }
"\"" { string.setLength(0); yybegin(STRING); }
"/*" { yybegin(BLOCKCOMMENT); }
"//" { yybegin(LINECOMMENT); }
}
<STRING> {
\" { yybegin(YYINITIAL);
return symbol("String", STRING_LITERAL, string.toString()); }
[^\n\r\"\\]+ { string.append( yytext() ); }
\\t { string.append('\t'); }
\\n { string.append('\n'); }
\\r { string.append('\r'); }
\\\" { string.append('\"'); }
\\ { string.append('\\'); }
}
<BLOCKCOMMENT> {
"*/" { yybegin(YYINITIAL); }
[^] {}
}
<LINECOMMENT> {
\n { yybegin(YYINITIAL); }
[^] {}
}
/* error fallback */
[^] { /* throw new Error("Illegal character <"+ yytext()+">");*/
error("Illegal character <"+ yytext()+">");
}