-
Notifications
You must be signed in to change notification settings - Fork 0
/
MH_Lexer.java
executable file
·163 lines (136 loc) · 5.05 KB
/
MH_Lexer.java
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
// File: MH_Lexer.java
// Date: October 2015
// Java template file for lexer component of Informatics 2A Assignment 1 (2015).
// 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 GenAcceptor implements DFA {
public String lexClass() {return "VAR";};
public int numberOfStates() {return 3;};
int nextState (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 ;
case 2: return 2 ;
default: return 2 ;
}
}
boolean accepting (int state) {return (state == 1) ;}
boolean dead (int state) {return (state == 2) ;}
}
static class NumAcceptor extends GenAcceptor implements DFA {
public String lexClass() {return "NUM";};
public int numberOfStates() {return 3;};
int nextState (int state, char c){
switch(state) {
case 0: if (CharTypes.isDigit(c)) return 1 ; else return 2 ;
case 1: if (CharTypes.isDigit(c)) return 1 ; else return 2 ;
default: return 2 ;
}
}
boolean accepting (int state) {return (state == 1) ;}
boolean dead (int state) {return (state == 2) ;}
}
static class BooleanAcceptor extends GenAcceptor implements DFA {
public String lexClass() {return "BOOLEAN";};
public int numberOfStates() {return 9;};
int nextState (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; else return 8 ;
case 2: if (c=='u') return 3; else return 8 ;
case 3: if (c=='e') return 4; else return 8 ;
case 4: return 8 ;
case 5: if (c=='a') return 6; else return 8 ;
case 6: if (c=='l') return 7; else return 8 ;
case 7: if (c=='s') return 3; else return 8 ;
default: return 8 ;
}
}
boolean accepting (int state) {return (state==4) ;}
boolean dead (int state) {return (state==8) ;}
}
static class SymAcceptor extends GenAcceptor implements DFA {
public String lexClass() {return "SYM";};
public int numberOfStates() {return 3;};
int nextState (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) ;}
boolean dead (int state) {return (state==2) ;}
}
static class WhitespaceAcceptor extends GenAcceptor implements DFA {
public String lexClass() {return "";};
public int numberOfStates() {return 3;};
int nextState (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) ;}
boolean dead (int state) {return (state==2) ;}
}
static class CommentAcceptor extends GenAcceptor implements DFA {
public String lexClass() {return "";};
public int numberOfStates() {return 6;};
int nextState (int state, char c){
switch(state) {
case 0: if(c=='-') return 1 ; else return 5 ;
case 1: if(c=='-') return 2 ; else return 5 ;
case 2: if(c=='-') return 2 ; else if (!CharTypes.isNewline(c) && !CharTypes.isSymbolic(c)) return 3; else return 5;
case 3: if(!CharTypes.isNewline(c)) return 3; else if(CharTypes.isNewline(c)) return 4; else return 5;
default : return 5 ;
}
}
boolean accepting (int state) {return (state==4) ;}
boolean dead (int state) {return (state==5) ;}
}
static class TokAcceptor extends GenAcceptor implements DFA {
String tok ;
int tokLen ;
private char[] string;
private int garbage;
TokAcceptor (String tok) {
this.tok = tok ;
tokLen = tok.length() ;
this.string = tok.toCharArray() ;
this.garbage = tokLen+1 ;
}
public String lexClass() {return tok;};
public int numberOfStates() {return tokLen+2;};
int nextState (int state, char c){
if (state < string.length) {
if (c==string[state]) return state+1;
else return garbage;
}
else return garbage;
}
boolean accepting (int state) {return (state==tokLen) ;};
boolean dead (int state) {return (state==garbage) ;};
}
static DFA[] MH_acceptors = new DFA[] {
new TokAcceptor("Integer"),
new TokAcceptor("Bool"),
new TokAcceptor("if"),
new TokAcceptor("then"),
new TokAcceptor("else"),
new TokAcceptor("("),
new TokAcceptor(")"),
new TokAcceptor(";"),
new WhitespaceAcceptor(),
new CommentAcceptor(),
new BooleanAcceptor(),
new VarAcceptor(),
new NumAcceptor(),
new SymAcceptor()
};
MH_Lexer (Reader reader) {
super(reader,MH_acceptors) ;
}
}