-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECLexer.java
More file actions
63 lines (53 loc) · 1.68 KB
/
Copy pathECLexer.java
File metadata and controls
63 lines (53 loc) · 1.68 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
package cmsc141.mp1.ec;
import java.util.ArrayList;
public class ECLexer {
public ECLexer() {
}
public String[] tokenize(String program) {
ArrayList<String> lexemes = new ArrayList<String>();
String entity = "";
boolean hasStartCommentTag = false;
boolean hasStartStringTag = false;
for (int i = 0; i < program.length(); i++) {
char currentChar = program.charAt(i);
if (Character.isWhitespace(currentChar) &&
!entity.equals("") && !hasStartCommentTag &&
!hasStartStringTag) {
lexemes.add(entity);
entity = "";
} else if (Character.isWhitespace(currentChar) &&
!entity.equals("") && !hasStartCommentTag &&
hasStartStringTag) {
entity += currentChar + "";
} else if (currentChar == '\'' &&
!hasStartStringTag && !hasStartCommentTag) {
entity += currentChar + "";
hasStartStringTag = true;
} else if (currentChar == '\'' &&
hasStartStringTag && !hasStartCommentTag) {
entity += currentChar + "";
hasStartStringTag = false;
} else if (currentChar == '/' &&
program.charAt(i+1) == '*' &&
!hasStartCommentTag && !hasStartStringTag) {
hasStartCommentTag = true;
i++;
} else if (currentChar == '*' &&
program.charAt(i+1) == '/' &&
hasStartCommentTag && !hasStartStringTag) {
hasStartCommentTag = false;
i++;
} else if (!Character.isWhitespace(currentChar) &&
!hasStartCommentTag) {
entity += currentChar + "";
}
}
lexemes.remove(0);
lexemes.remove(0);
// add 'end' at the end
lexemes.add(entity);
System.out.println("Tokenized lexemes\n" + lexemes);
String[] lexemesArr = lexemes.toArray(new String[lexemes.size()]);
return lexemesArr;
}
}