-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMH_Lexer.java
More file actions
196 lines (161 loc) · 6.2 KB
/
Copy pathMH_Lexer.java
File metadata and controls
196 lines (161 loc) · 6.2 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
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
// File: MH_Lexer.java
// Java template file for lexer component of Informatics 2A Assignment 1.
// Concerns lexical classes and lexer for the language MH (`Micro-Haskell').
import java.io.* ;
class MH_Lexer extends GenLexer implements LEX_TOKEN_STREAM {
static class VarAcceptor extends Acceptor implements DFA {
// add code here
// start = 0
// accepting = 1
// dead = 2
public String lexClass() { return "VAR"; } ;
public int numberOfStates() { return 3; } ;
int next (int state, char c) {
switch (state) {
case 0: if (CharTypes.isSmall(c)) return 1; else return 2;
case 1: if (CharTypes.isSmall(c) || CharTypes.isLarge(c) || CharTypes.isDigit(c) || c == '\'') return 1; else return 2;
default: return 2;
}
}
boolean accepting (int state) {return (state == 1); }
int dead() {return 2; }
}
static class NumAcceptor extends Acceptor implements DFA {
// add code here
// start = 0
// accepting = 1 and 2
// dead = 3
public String lexClass() { return "NUM"; } ;
public int numberOfStates() { return 4; } ;
int next (int state, char c) {
switch (state) {
case 0: if (c == '0') return 1; else if (CharTypes.isDigit(c) && (c != 0)) return 2;
case 1: return 3;
case 2: if (CharTypes.isDigit(c)) return 2; else return 3;
default: return 3;
}
}
boolean accepting (int state) {return (state == 1 || state == 2);}
int dead() {return 3;}
}
static class BooleanAcceptor extends Acceptor implements DFA {
// add code here
// start = 0
// accepting = 4
// dead = 8
public String lexClass() { return "BOOLEAN"; };
public int numberOfStates() { return 9; };
int next (int state, char c) {
switch (state) {
case 0: if (c == 'T') return 1; else if (c == 'F') return 5; else return 8;
case 1: if (c == 'r') return 2; return 8;
case 2: if (c == 'u') return 3; return 8;
case 3: if (c == 'e') return 4; return 8;
case 4: return 8;
case 5: if (c == 'a') return 6; return 8;
case 6: if (c == 'l') return 7; return 8;
case 7: if (c == 's') return 3; return 8;
default: return 8;
}
}
boolean accepting (int state) {return (state == 4);}
int dead() {return 8;}
}
static class SymAcceptor extends Acceptor implements DFA {
// add code here
// start = 0
// accepting = 1
// dead = 2
public String lexClass() { return "SYM"; };
public int numberOfStates() { return 3; };
int next (int state, char c) {
switch (state) {
case 0: if (CharTypes.isSymbolic(c)) return 1; else return 2;
case 1: if (CharTypes.isSymbolic(c)) return 1; else return 2;
default: return 2;
}
}
boolean accepting (int state) {return (state == 1);}
int dead() {return 2;}
}
static class WhitespaceAcceptor extends Acceptor implements DFA {
// add code here
// start = 0
// accepting = 1
// dead = 2
public String lexClass() { return ""; };
public int numberOfStates() { return 3; };
int next (int state, char c) {
switch (state) {
case 0: if (CharTypes.isWhitespace(c)) return 1; else return 2;
case 1: if (CharTypes.isWhitespace(c)) return 1; else return 2;
default: return 2;
}
}
boolean accepting (int state) {return (state == 1);}
int dead() {return 2;}
}
static class CommentAcceptor extends Acceptor implements DFA {
// add code here
// start = 0
// accepting = 2, 3
// dead = 4
public String lexClass() { return ""; };
public int numberOfStates() { return 5; };
int next (int state, char c) {
switch (state) {
case 0: if (c == '-') return 1; else return 4;
case 1: if (c == '-') return 2; else return 4;
case 2: if (c == '-') return 2; else if (!CharTypes.isSymbolic(c) || !CharTypes.isNewline(c)) return 3; return 4;
case 3: if (!CharTypes.isNewline(c)) return 3; return 4;
default: return 4;
}
}
boolean accepting (int state) {return (state == 2 || state == 3);}
int dead() {return 4;}
}
static class TokAcceptor extends Acceptor implements DFA {
String tok ;
int tokLen ;
TokAcceptor (String tok) {this.tok = tok ; tokLen = tok.length() ;}
// add code here
//int totalStates = tokLen + 2;
//int garbageState = tokLen + 1;
public String lexClass() { return tok; };
public int numberOfStates() { return tokLen + 2; };
int next (int state, char c) {
if (state < tokLen) {
if (c == tok.charAt(state)) {
return state + 1;
} else {
return tokLen + 1;
}
}
return tokLen + 1;
}
boolean accepting (int state) { return (state == tokLen); }
int dead() {return tokLen + 1;}
}
// add definitions of MH_acceptors here
static DFA varAcceptor = new VarAcceptor();
static DFA numAcceptor = new NumAcceptor();
static DFA booleanAcceptor = new BooleanAcceptor();
static DFA symAcceptor = new SymAcceptor();
static DFA whitespaceAcceptor = new WhitespaceAcceptor();
static DFA commentAcceptor = new CommentAcceptor();
static DFA integerAcceptor = new TokAcceptor("Integer");
static DFA boolAcceptor = new TokAcceptor("Bool");
static DFA ifAcceptor = new TokAcceptor("if");
static DFA thenAcceptor = new TokAcceptor("then");
static DFA elseAcceptor = new TokAcceptor("else");
static DFA openBracketAcceptor = new TokAcceptor("(");
static DFA closeBracketAcceptor = new TokAcceptor(")");
static DFA semicolonAcceptor = new TokAcceptor(";");
static DFA[] MH_acceptors = new DFA[] {ifAcceptor, elseAcceptor, thenAcceptor, integerAcceptor,
boolAcceptor, booleanAcceptor, numAcceptor, varAcceptor,
symAcceptor, openBracketAcceptor, closeBracketAcceptor, semicolonAcceptor,
whitespaceAcceptor, commentAcceptor};
MH_Lexer (Reader reader) {
super(reader,MH_acceptors) ;
}
}