-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.java
More file actions
46 lines (38 loc) · 1.22 KB
/
Copy pathLexer.java
File metadata and controls
46 lines (38 loc) · 1.22 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
// Kimberley Ni and Eylul Oktay
import java.util.ArrayList;
public class Lexer {
public static final String specials = "()\\ ;.λ="; // special characters
/*
* A lexer (or "tokenizer") converts an input into tokens that
* eventually need to be interpreted.
*
* Given the input
* (\bat .bat flies)cat λg.joy! )
* you should output the ArrayList of strings
* [(, \, bat, ., bat, flies, ), cat, \, g, ., joy!, )]
*
*/
public ArrayList<String> tokenize(String input) {
ArrayList<String> tokens = new ArrayList<String>();
String tokenStr = "";
for (int i = 0; i < input.length(); i++) {
String letter = input.substring(i, i + 1);
// Tokenize as individual: special characters held in constant String variable specials
if (specials.contains(letter)) {
if (!tokenStr.equals("")) {
tokens.add(tokenStr);
tokenStr = ""; // reset
}
if (letter.equals(";")) // comment; at this point don't need to process further
return tokens;
else if (!letter.equals(" "))
tokens.add(letter);
} else {
tokenStr += letter;
}
}
if (!tokenStr.equals("")) // cleans up any last tokens
tokens.add(tokenStr);
return tokens;
}
}