-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalAnalyser.java
More file actions
177 lines (159 loc) · 4.65 KB
/
Copy pathLexicalAnalyser.java
File metadata and controls
177 lines (159 loc) · 4.65 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Collections;
import java.util.stream.Collectors;
public class LexicalAnalyser {
public static List<Token> analyse(String sourceCode) throws LexicalException {
// Turn the input String into a list of Tokens!\
List<Token> result = new ArrayList<Token>();
try {
String[] terminals = split(sourceCode);
for (String terminal : terminals) {
if (terminal.isBlank()) {
continue;
}
Optional<Token> token = tokenFromTerminal(terminal);
if (token.isPresent()) {
result.add(token.get());
} else {
// Stringlit, Char
result.addAll(tokenFromQuote(terminal));
}
}
} catch (LexicalException e) {
throw e;
}
return result;
}
private static String getRegex(String value) {
return "(?=\\" + value + ")" + "|" + "(?<=\\" + value + ")";
}
private static String[] split(String code) {
String regex = "\\s+";
List<String> regexList = Arrays.asList(";", "(", ")", "{", "}", "+", "-", "*", "%", "=", "==", "!=", "<", ">",
"<=", ">=", "&&", "|\\|");
for (String r : regexList) {
regex = regex.concat("|" + getRegex(r));
}
return code.split(regex);
}
private static List<Token> tokenFromQuote(String string) throws LexicalException {
List<Token> result = new ArrayList<Token>();
String regex = "";
Token.TokenType type = Token.TokenType.STRINGLIT;
if (string.charAt(0) == '\'') {
regex = "(?<=\')|(?=\')";
type = Token.TokenType.CHARLIT;
}
else if (string.charAt(0) == '"') {
regex = "(?<=\")|(?=\")";
type = Token.TokenType.STRINGLIT;
} else {
throw new LexicalException("Invalid Type");
}
String[] stringT = string.split(regex);
for (String terminal: stringT) {
if (terminal.matches("[\\d|\\w]+")) {
result.add(new Token(type, terminal));
continue;
}
Optional<Token> token = tokenFromTerminal(terminal);
if (token.isPresent()) {
result.add(token.get());
}
}
return result;
}
private static Optional<Token> tokenFromTerminal(String t) {
Optional<Token.TokenType> type = tokenTypeOf(t);
if (type.isPresent())
return Optional.of(new Token(type.get(), t));
return Optional.empty();
}
private static Optional<Token.TokenType> tokenTypeOf(String t) {
switch (t) {
case "+":
return Optional.of(Token.TokenType.PLUS);
case "-":
return Optional.of(Token.TokenType.MINUS);
case "*":
return Optional.of(Token.TokenType.TIMES);
case "/":
return Optional.of(Token.TokenType.DIVIDE);
case "%":
return Optional.of(Token.TokenType.MOD);
case "=":
return Optional.of(Token.TokenType.ASSIGN);
case "==":
return Optional.of(Token.TokenType.EQUAL);
case "!=":
return Optional.of(Token.TokenType.NEQUAL);
case "<":
return Optional.of(Token.TokenType.LT);
case ">":
return Optional.of(Token.TokenType.GT);
case "<=":
return Optional.of(Token.TokenType.LE);
case ">=":
return Optional.of(Token.TokenType.GE);
case "(":
return Optional.of(Token.TokenType.LPAREN);
case ")":
return Optional.of(Token.TokenType.RPAREN);
case "{":
return Optional.of(Token.TokenType.LBRACE);
case "}":
return Optional.of(Token.TokenType.RBRACE);
case "&&":
return Optional.of(Token.TokenType.AND);
case "||":
return Optional.of(Token.TokenType.OR);
case ";":
return Optional.of(Token.TokenType.SEMICOLON);
case "public":
return Optional.of(Token.TokenType.PUBLIC);
case "class":
return Optional.of(Token.TokenType.CLASS);
case "static":
return Optional.of(Token.TokenType.STATIC);
case "void":
return Optional.of(Token.TokenType.VOID);
case "main":
return Optional.of(Token.TokenType.MAIN);
case "String[]":
return Optional.of(Token.TokenType.STRINGARR);
case "args":
return Optional.of(Token.TokenType.ARGS);
case "int":
case "char":
case "boolean":
return Optional.of(Token.TokenType.TYPE);
case "System.out.println":
return Optional.of(Token.TokenType.PRINT);
case "while":
return Optional.of(Token.TokenType.WHILE);
case "for":
return Optional.of(Token.TokenType.FOR);
case "if":
return Optional.of(Token.TokenType.IF);
case "else":
return Optional.of(Token.TokenType.ELSE);
case "\"":
return Optional.of(Token.TokenType.DQUOTE);
case "\'":
return Optional.of(Token.TokenType.SQUOTE);
case "true":
return Optional.of(Token.TokenType.TRUE);
case "false":
return Optional.of(Token.TokenType.FALSE);
}
if (t.matches("\\d+"))
return Optional.of(Token.TokenType.NUM);
if (Character.isAlphabetic(t.charAt(0)) && t.matches("[\\d|\\w]+")) {
return Optional.of(Token.TokenType.ID);
}
return Optional.empty();
}
}