-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lexical.java
229 lines (212 loc) · 5.77 KB
/
Lexical.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/*
* **************************************************
* LEXICAL ANALYZER
* read input file and construct tokens
* Read file for each token when parser needs one
* **************************************************
*/
class Lexical {
private String line = null, type = null;
private BufferedReader br;
private String token;
private ArrayList<String> letters = new ArrayList<String>(); //Collection of allowed letters
private ArrayList<String> digits = new ArrayList<String>(); //Collection of allowed digits
private ArrayList<String> operators = new ArrayList<String>(); //Collection of allowed operators
private ArrayList<String> spaces = new ArrayList<String>(); //Collection of space and horizontal tab
private ArrayList<String> punctions = new ArrayList<String>(); //Punctuations
public ArrayList<String> reserved = new ArrayList<String>(); //Reserved tokens of grammar
//Constructor to open file for reading
Lexical(){ };
Lexical(String fileName) {
try {
br = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Initialize the allowed LETTERS, DIGITS and OPERATOS
char ch = 'a';
for (int i = 0 ; i < 26 ; i++)
letters.add(""+ch++);
ch = 'A';
for (int i = 0 ; i < 26 ; i++)
letters.add(""+ch++);
for (int i = 0 ; i < 10 ; i++)
digits.add(""+i);
String[] s = new String[] {"+", "-", "*", "<", ">", "&", ".", "@", "/", ":", "=", "~", "|", "$", "!", "#", "%",
"^", "_", "[", "]", "{", "}", "\"", "`", "?"};
operators.addAll(Arrays.asList(s));
s = new String[] {" ", "\t", "\n"};
spaces.addAll(Arrays.asList(s));
s = new String[] {"(", ")", ";", ","};
punctions.addAll(Arrays.asList(s));
s = new String[] {"let", "in", "fn", "where", "aug", "or", "not", "gr", "ge", "ls", "le", "eq", "ne", "within", "and", "rec"};
reserved.addAll(Arrays.asList(s));
}
boolean isOperatorSymbol(String token) {
return this.operators.contains(token);
}
//Read a line from FILE
private String readFile() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//Parse the current line and select a token to return. If EndOfLine reached, make line = null for next line reading.
private String parse() {
StringBuilder sb = new StringBuilder();
boolean cont;
do {
cont = false;
if (line == null)
return "";
if (line.equals("")) {
line = readFile();
cont = true;
}
//IDENTIFIER
else if (letters.contains(line.charAt(0)+"")) {
type = "<IDENTIFIER>"; //TYPE can be needed by the parser corresponding to a token
while (true) {
sb.append(line.charAt(0)+""); //Append that character to the current token
line = line.substring(1); //Trim the first character from the LINE for next reading
if (line.length() == 0) {
line = null;
break;
}
if (letters.contains(line.charAt(0)+""))
continue;
else if (digits.contains(line.charAt(0)+""))
continue;
else if (line.charAt(0) == '_')
continue;
else
break;
}
}
//INTEGER
else if (digits.contains(line.charAt(0)+"")) {
type = "<INTEGER>";
while (true) {
sb.append(line.charAt(0)+"");
line = line.substring(1);
if (line.length() == 0) {
line = null;
break;
}
if (digits.contains(line.charAt(0)+""))
continue;
else
break;
}
}
//COMMENT
else if (line.charAt(0) == '/' && line.charAt(1) == '/') {
type = "<DELETE>";
line = readFile();
if (line == null)
return "";
cont = true;
}
//OPERATOR
else if (operators.contains(line.charAt(0)+"")) {
type = "<OPERATOR>";
while (true) {
sb.append(line.charAt(0)+"");
line = line.substring(1);
if (line.length() == 0) {
line = null;
break;
}
if (operators.contains(line.charAt(0)+""))
continue;
else
break;
}
}
//STRING
else if (line.charAt(0) == '\'') {
type = "<STRING>";
while (true) {
sb.append(line.charAt(0));
line = line.substring(1);
if (line.length() == 0) {
line = null;
break;
}
if (line.charAt(0) == '\\') { //If escape character, don't consider the next character
sb.append(line.charAt(0));
line = line.substring(1);
sb.append(line.charAt(0));
line = line.substring(1);
}
if (line.charAt(0) == '\'') {
sb.append(line.charAt(0));
line = line.substring(1);
if (line.length() == 0)
line = null;
break;
}
}
}
//SPACES
else if (spaces.contains(line.charAt(0)+"")) {
type = "<DELETE>";
line = line.substring(1);
if (line.length() == 0)
line = readFile();
if (line == null)
return "";
cont = true;
}
//PUNCTUATIONS
else if (punctions.contains(line.charAt(0)+"")) {
type = "<PUNCTIONS>";
sb.append(line.charAt(0));
line = line.substring(1);
if (line.length() == 0)
line = null;
}
} while (cont == true);
return sb.toString();
}
//Get a new token for parsing. Read a new line if the previous one is finished.
public String getToken() {
token = "";
if (line == null) {
line = readFile();
}
if (line != null) {
token = parse();
if (token.equals(""))
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else { //No more contents left in file to read
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
token = "";
}
return token;
}
//Get type of the last returned token
public String getType() {
if (token.equals(""))
type = "<EOF>";
return type;
}
}