Skip to content

Commit d70cbb4

Browse files
committed
updated lexical analyser to fix merge problems
1 parent 887e60d commit d70cbb4

File tree

1 file changed

+113
-63
lines changed

1 file changed

+113
-63
lines changed

Assignment2/src/com/company/LexicalAnalyser.java

Lines changed: 113 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,116 @@
44
import java.util.Optional;
55
import java.util.Collections;
66
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.NoSuchElementException;
79

810
public class LexicalAnalyser {
911

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("\\s+");
13-
List<String> furtherSplitList = new ArrayList<String>();
14-
List<Token> tokenList = new ArrayList<Token>();
12+
public static List<Token> analyse(String sourceCode) throws LexicalException {
13+
// Turn the input String into a list of Tokens!
14+
// split source code by spaces into a list and initialize all other necessary lists
15+
String[] splitList = sourceCode.split(" ");
16+
List<String> tokenCase = Arrays.asList(new String[]{ // list to check characters against
17+
"\"", "\'", "(", ")", "{", "}",
18+
";", "+", "-", "*", "/", "%"
19+
});
20+
List<String> furtherSplitList = new ArrayList<String>();
21+
List<Token> tokenList = new ArrayList<Token>();
1522

16-
for (String word : splitList) {
17-
String s = "";
18-
for (int i = 0; i < word.length(); i++) {
19-
// System.out.println(word.charAt(i));
20-
String c = Character.toString(word.charAt(i));
21-
switch (c) {
22-
case "(":
23-
case ")":
24-
case "{":
25-
case "}":
26-
case ";":
27-
// System.out.println(s);
28-
// System.out.println(c);
29-
// if (s != null || s.length() > 0 || s != "") {
30-
// System.out.println(s);
31-
// furtherSplitList.add(s);
32-
// }
33-
furtherSplitList.add(s);
34-
furtherSplitList.add(c);
35-
s = "";
36-
break;
37-
case " ":
38-
i = word.length();
39-
break;
40-
default:
41-
s += c;
42-
}
43-
// if token exists, add s to split list, then add token
44-
// clear string var
45-
}
46-
// if (s != null || s.length() > 0 || s != "") {
47-
// System.out.println(s);
48-
// furtherSplitList.add(s);
49-
// }
50-
furtherSplitList.add(s);
51-
}
23+
for (String word : splitList) {
24+
String s = "";
25+
for (int i = 0; i < word.length(); i++) { // iterate through the word
26+
// System.out.println(word.charAt(i));
27+
String c = Character.toString(word.charAt(i));
28+
if (tokenCase.contains(c)) { // check against list of tokens
29+
furtherSplitList.add(s); // add whatever we have in the list
30+
furtherSplitList.add(c); // add the last character as a separate token
31+
s = "";
32+
} else {
33+
s += c; // if character wasn't a separator keep adding to s
34+
}
35+
}
36+
furtherSplitList.add(s);
37+
}
38+
// start adding tokens to the token list based on what we have in further split list
39+
for (int i = 0; i < furtherSplitList.size(); i++) {
40+
String s = furtherSplitList.get(i);
41+
if (s.length() > 0) {
42+
try {
43+
if (i > 0 && furtherSplitList.get(i - 1).matches("\"")) {
44+
tokenList.add(tokenTypeStringLit(s).get());
45+
} else if (i > 0 && furtherSplitList.get(i - 1).matches("\'")) {
46+
tokenList.add(tokenTypeCharLit(s).get());
47+
} else if (i < furtherSplitList.size() - 1 && furtherSplitList.get(i).matches(".[=]$")) {
48+
// System.out.println(furtherSplitList.get(i));
49+
if (furtherSplitList.get(i).matches("^[>].*")) {
50+
tokenList.add(tokenTypeGE(s).get());
51+
} else if (furtherSplitList.get(i).matches("^[<].*")) {
52+
tokenList.add(tokenTypeLE(s).get());
53+
} else if (furtherSplitList.get(i).matches("^[!].*")) {
54+
tokenList.add(tokenTypeNE(s).get());
55+
} else if (furtherSplitList.get(i).matches("^[=].*")) {
56+
tokenList.add(tokenTypeEqual(s).get());
57+
} else {
58+
tokenList.add(tokenTypeEqual(s).get());
59+
}
60+
} else {
61+
tokenList.add(tokenFromString(s).get());
62+
}
63+
} catch (NoSuchElementException e) {
64+
System.out.print("Token not found: " + e + "\n");
65+
} catch (Exception e) {
66+
System.out.print(e);
67+
}
68+
}
69+
}
70+
return tokenList;
71+
}
5272

53-
System.out.println(furtherSplitList);
73+
private static Optional<Token> tokenFromString(String t) {
74+
Optional<Token.TokenType> type = tokenTypeOf(t);
75+
if (type.isPresent())
76+
return Optional.of(new Token(type.get(), t));
77+
return Optional.empty();
78+
}
5479

80+
private static Optional<Token> tokenTypeStringLit(String t) {
81+
Optional<Token.TokenType> type = Optional.of(Token.TokenType.STRINGLIT);
82+
if (type.isPresent())
83+
return Optional.of(new Token(type.get(), t));
84+
return Optional.empty();
85+
}
5586

56-
for(int j = 0; j < furtherSplitList.size(); j++){
87+
private static Optional<Token> tokenTypeCharLit(String t) {
88+
Optional<Token.TokenType> type = Optional.of(Token.TokenType.CHARLIT);
89+
if (type.isPresent())
90+
return Optional.of(new Token(type.get(), t));
91+
return Optional.empty();
92+
}
5793

58-
}
94+
private static Optional<Token> tokenTypeGE(String t) {
95+
Optional<Token.TokenType> type = Optional.of(Token.TokenType.GE);
96+
if (type.isPresent())
97+
return Optional.of(new Token(type.get(), t));
98+
return Optional.empty();
99+
}
59100

60-
for (String s : furtherSplitList) {
101+
private static Optional<Token> tokenTypeLE(String t) {
102+
Optional<Token.TokenType> type = Optional.of(Token.TokenType.LE);
103+
if (type.isPresent())
104+
return Optional.of(new Token(type.get(), t));
105+
return Optional.empty();
106+
}
61107

62-
if(s.length() > 0){
63-
try {
64-
tokenList.add(tokenFromString(s).get());
65-
}
66-
catch (NoSuchElementException e) {
67-
// tokenList.add(Optional.empty());
68-
}
69-
}
70-
//try {
71-
// tokenList.add(LexicalAnalyser.tokenFromString().get())
72-
//}
73-
}
74-
return tokenList;
75-
// return Collections.emptyList();
76-
}
108+
private static Optional<Token> tokenTypeEqual(String t) {
109+
Optional<Token.TokenType> type = Optional.of(Token.TokenType.EQUAL);
110+
if (type.isPresent())
111+
return Optional.of(new Token(type.get(), t));
112+
return Optional.empty();
113+
}
77114

78-
private static Optional<Token> tokenFromString(String t) {
79-
Optional<Token.TokenType> type = tokenTypeOf(t);
115+
private static Optional<Token> tokenTypeNE(String t) {
116+
Optional<Token.TokenType> type = Optional.of(Token.TokenType.NEQUAL);
80117
if (type.isPresent())
81118
return Optional.of(new Token(type.get(), t));
82119
return Optional.empty();
@@ -144,14 +181,27 @@ private static Optional<Token.TokenType> tokenTypeOf(String t) {
144181
return Optional.of(Token.TokenType.TRUE);
145182
case "false":
146183
return Optional.of(Token.TokenType.FALSE);
184+
case "<":
185+
return Optional.of(Token.TokenType.LT);
186+
case ">":
187+
return Optional.of(Token.TokenType.GT);
188+
// case "<=":
189+
// return Optional.of(Token.TokenType.LE);
190+
// case ">=":
191+
// return Optional.of(Token.TokenType.GE);
147192
}
148193

194+
if (t.matches("\"{1}"))
195+
return Optional.of(Token.TokenType.DQUOTE);
196+
if (t.matches("\'{1}"))
197+
return Optional.of(Token.TokenType.SQUOTE);
149198
if (t.matches("\\d+"))
150199
return Optional.of(Token.TokenType.NUM);
151200
if (Character.isAlphabetic(t.charAt(0)) && t.matches("[\\d|\\w]+")) {
152201
return Optional.of(Token.TokenType.ID);
153202
}
203+
154204
return Optional.empty();
155205
}
156206

157-
}
207+
}

0 commit comments

Comments
 (0)