Skip to content

Commit c75abb9

Browse files
Update LexicalAnalyser.java
Cleaned up code
1 parent 06e635b commit c75abb9

File tree

1 file changed

+40
-51
lines changed

1 file changed

+40
-51
lines changed

Assignment2/src/com/company/LexicalAnalyser.java

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// package com.company;
21
import java.util.List;
32
import java.util.Optional;
43
import java.util.Collections;
@@ -8,67 +7,62 @@
87

98
public class LexicalAnalyser {
109

11-
public static List<Token> analyse(String sourceCode) throws LexicalException {
12-
// Turn the input String into a list of Tokens!
13-
String[] splitList = sourceCode.split(" ");
10+
public static List<Token> analyse(String sourceCode) throws LexicalException {
11+
// Turn the input String into a list of Tokens!
12+
String[] splitList = sourceCode.split(" ");
1413
List<String> tokenCase = Arrays.asList(new String[]{
15-
"\"", "\'", "(", ")", "{", "}",
16-
";", "+", "-", "*", "/", "%"
14+
"\"", "'", "(", ")", "{", "}",
15+
";", "+", "-", "*", "/", "%"
1716
});
18-
List<String> furtherSplitList = new ArrayList<String>();
19-
List<Token> tokenList = new ArrayList<Token>();
17+
List<String> furtherSplitList = new ArrayList<String>();
18+
List<Token> tokenList = new ArrayList<Token>();
2019

21-
for (String word : splitList) {
22-
String s = "";
23-
for (int i = 0; i < word.length(); i++) {
24-
// System.out.println(word.charAt(i));
25-
String c = Character.toString(word.charAt(i));
20+
for (String word : splitList) {
21+
String s = "";
22+
for (int i = 0; i < word.length(); i++) {
23+
// System.out.println(word.charAt(i));
24+
String c = Character.toString(word.charAt(i));
2625
if (tokenCase.contains(c)) {
2726
furtherSplitList.add(s);
2827
furtherSplitList.add(c);
2928
s = "";
3029
} else {
3130
s += c;
3231
}
33-
}
34-
furtherSplitList.add(s);
35-
}
36-
// System.out.println(furtherSplitList);
32+
}
33+
furtherSplitList.add(s);
34+
}
35+
// System.out.println(furtherSplitList);
3736
for (int i = 0; i < furtherSplitList.size(); i++) {
3837
String s = furtherSplitList.get(i);
39-
if (s.length() > 0) {
40-
try {
41-
if (i > 0 && furtherSplitList.get(i-1).matches("\"")) {
38+
if (s.length() > 0) {
39+
try {
40+
if (i > 0 && furtherSplitList.get(i - 1).matches("\"")) {
4241
tokenList.add(tokenTypeStringLit(s).get());
43-
} else if (i > 0 && furtherSplitList.get(i-1).matches("\'")) {
42+
} else if (i > 0 && furtherSplitList.get(i - 1).matches("'")) {
4443
tokenList.add(tokenTypeCharLit(s).get());
45-
} else if (i < furtherSplitList.size()-1 && furtherSplitList.get(i).matches(".[=]$")) {
44+
} else if (i < furtherSplitList.size() - 1 && furtherSplitList.get(i).matches(".[=]$")) {
4645
// System.out.println(furtherSplitList.get(i));
47-
if (furtherSplitList.get(i).matches("^[>].*")) {
46+
if (furtherSplitList.get(i).matches("^[>].*")) {
4847
tokenList.add(tokenTypeGE(s).get());
49-
} else if (furtherSplitList.get(i).matches("^[<].*")) {
48+
} else if (furtherSplitList.get(i).matches("^[<].*")) {
5049
tokenList.add(tokenTypeLE(s).get());
51-
} else if (furtherSplitList.get(i).matches("^[=].*")) {
52-
tokenList.add(tokenTypeEqual(s).get());
5350
} else if (furtherSplitList.get(i).matches("^[!].*")) {
54-
tokenList.add(tokenTypeNotEqual(s).get());
55-
} else {
51+
tokenList.add(tokenTypeNE(s).get());
52+
} else if (furtherSplitList.get(i).matches("^[=].*")) {
5653
tokenList.add(tokenTypeEqual(s).get());
57-
}
58-
}
59-
else { tokenList.add(tokenFromString(s).get()); }
60-
}
61-
catch (NoSuchElementException e) {
62-
// tokenList.add(Optional.empty());
54+
} else { tokenList.add(tokenTypeEqual(s).get()); }
55+
} else {tokenList.add(tokenFromString(s).get()); }
56+
} catch (NoSuchElementException e) {
57+
// tokenList.add(Optional.empty());
6358
System.out.print("Token not found: " + e + "\n");
64-
}
65-
catch (Exception e) {
59+
} catch (Exception e) {
6660
System.out.print(e);
6761
}
68-
}
69-
}
70-
return tokenList;
71-
}
62+
}
63+
}
64+
return tokenList;
65+
}
7266

7367
private static Optional<Token> tokenFromString(String t) {
7468
Optional<Token.TokenType> type = tokenTypeOf(t);
@@ -111,8 +105,8 @@ private static Optional<Token> tokenTypeEqual(String t) {
111105
return Optional.of(new Token(type.get(), t));
112106
return Optional.empty();
113107
}
114-
115-
private static Optional<Token> tokenTypeNotEqual(String t) {
108+
109+
private static Optional<Token> tokenTypeNE(String t) {
116110
Optional<Token.TokenType> type = Optional.of(Token.TokenType.NEQUAL);
117111
if (type.isPresent())
118112
return Optional.of(new Token(type.get(), t));
@@ -185,22 +179,17 @@ private static Optional<Token.TokenType> tokenTypeOf(String t) {
185179
return Optional.of(Token.TokenType.LT);
186180
case ">":
187181
return Optional.of(Token.TokenType.GT);
188-
// case "<=":
189-
// return Optional.of(Token.TokenType.LE);
190-
// case ">=":
191-
// return Optional.of(Token.TokenType.GE);
182+
case "\"":
183+
return Optional.of(Token.TokenType.DQUOTE);
184+
case "'":
185+
return Optional.of(Token.TokenType.SQUOTE);
192186
}
193187

194-
if (t.matches("\"{1}"))
195-
return Optional.of(Token.TokenType.DQUOTE);
196-
if (t.matches("\'{1}"))
197-
return Optional.of(Token.TokenType.SQUOTE);
198188
if (t.matches("\\d+"))
199189
return Optional.of(Token.TokenType.NUM);
200190
if (Character.isAlphabetic(t.charAt(0)) && t.matches("[\\d|\\w]+")) {
201191
return Optional.of(Token.TokenType.ID);
202192
}
203-
204193
return Optional.empty();
205194
}
206195

0 commit comments

Comments
 (0)