Skip to content

Commit 5d2295f

Browse files
committed
merged into my branch
2 parents 1af15ed + 8c82b4c commit 5d2295f

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed

Assignment2/src/com/company/LexicalAnalyser.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ public class LexicalAnalyser {
1111

1212
public static List<Token> analyse(String sourceCode) throws LexicalException {
1313
// Turn the input String into a list of Tokens!
14-
// split source code by spaces into a list and initialize all other necessary lists
1514
String[] splitList = sourceCode.split(" ");
16-
List<String> tokenCase = Arrays.asList(new String[]{ // list to check characters against
17-
"\"", "\'", "(", ")", "{", "}",
15+
List<String> tokenCase = Arrays.asList(new String[]{
16+
"\"", "'", "(", ")", "{", "}",
1817
";", "+", "-", "*", "/", "%"
1918
});
2019
List<String> furtherSplitList = new ArrayList<String>();
2120
List<Token> tokenList = new ArrayList<Token>();
2221

2322
for (String word : splitList) {
2423
String s = "";
25-
for (int i = 0; i < word.length(); i++) { // iterate through the word
24+
for (int i = 0; i < word.length(); i++) {
2625
// System.out.println(word.charAt(i));
2726
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
27+
if (tokenCase.contains(c)) {
28+
furtherSplitList.add(s);
29+
furtherSplitList.add(c);
3130
s = "";
3231
} else {
33-
s += c; // if character wasn't a separator keep adding to s
32+
s += c;
3433
}
3534
}
3635
furtherSplitList.add(s);
3736
}
38-
// start adding tokens to the token list based on what we have in further split list
37+
// System.out.println(furtherSplitList);
3938
for (int i = 0; i < furtherSplitList.size(); i++) {
4039
String s = furtherSplitList.get(i);
4140
if (s.length() > 0) {
4241
try {
42+
// System.out.println(s);
4343
if (i > 0 && furtherSplitList.get(i - 1).matches("\"")) {
4444
tokenList.add(tokenTypeStringLit(s).get());
45-
} else if (i > 0 && furtherSplitList.get(i - 1).matches("\'")) {
45+
} else if (i > 0 && furtherSplitList.get(i - 1).matches("'")) {
4646
tokenList.add(tokenTypeCharLit(s).get());
4747
} else if (i < furtherSplitList.size() - 1 && furtherSplitList.get(i).matches(".[=]$")) {
4848
// System.out.println(furtherSplitList.get(i));
@@ -54,14 +54,12 @@ public static List<Token> analyse(String sourceCode) throws LexicalException {
5454
tokenList.add(tokenTypeNE(s).get());
5555
} else if (furtherSplitList.get(i).matches("^[=].*")) {
5656
tokenList.add(tokenTypeEqual(s).get());
57-
} else {
58-
tokenList.add(tokenTypeEqual(s).get());
59-
}
60-
} else {
61-
tokenList.add(tokenFromString(s).get());
62-
}
57+
} else { tokenList.add(tokenTypeEqual(s).get()); }
58+
} else {tokenList.add(tokenFromString(s).get()); }
6359
} catch (NoSuchElementException e) {
64-
System.out.print("Token not found: " + e + "\n");
60+
// tokenList.add(Optional.empty());
61+
// System.out.print("Token not found: " + e + "\n");
62+
throw new LexicalException("Token not found: " + e);
6563
} catch (Exception e) {
6664
System.out.print(e);
6765
}
@@ -185,22 +183,17 @@ private static Optional<Token.TokenType> tokenTypeOf(String t) {
185183
return Optional.of(Token.TokenType.LT);
186184
case ">":
187185
return Optional.of(Token.TokenType.GT);
188-
// case "<=":
189-
// return Optional.of(Token.TokenType.LE);
190-
// case ">=":
191-
// return Optional.of(Token.TokenType.GE);
186+
case "\"":
187+
return Optional.of(Token.TokenType.DQUOTE);
188+
case "'":
189+
return Optional.of(Token.TokenType.SQUOTE);
192190
}
193191

194-
if (t.matches("\"{1}"))
195-
return Optional.of(Token.TokenType.DQUOTE);
196-
if (t.matches("\'{1}"))
197-
return Optional.of(Token.TokenType.SQUOTE);
198192
if (t.matches("\\d+"))
199193
return Optional.of(Token.TokenType.NUM);
200194
if (Character.isAlphabetic(t.charAt(0)) && t.matches("[\\d|\\w]+")) {
201195
return Optional.of(Token.TokenType.ID);
202196
}
203-
204197
return Optional.empty();
205198
}
206199

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.company;
2+
3+
4+
5+
public class NodeWrapper {
6+
private TreeNode.Label label;
7+
private TreeNode node;
8+
private TreeNode parent;
9+
private String value;
10+
11+
public NodeWrapper(TreeNode parent, TreeNode.Label label, String value) {
12+
this.parent = parent;
13+
this.label = label;
14+
this.value = value;
15+
}
16+
17+
public TreeNode getParent() {
18+
return this.parent;
19+
}
20+
21+
public TreeNode.Label getLabel() {
22+
return this.label;
23+
}
24+
25+
public TreeNode getNode() {
26+
return this.node;
27+
}
28+
29+
public void setNode(TreeNode node) {
30+
this.node = node;
31+
}
32+
33+
public String getValue() {
34+
return this.value;
35+
}
36+
37+
public TreeNode createNode(Token token) {
38+
this.node = new TreeNode(this.label, token, this.parent);
39+
this.parent.addChild(this.node);
40+
return this.node;
41+
}
42+
}

Assignment2/src/com/company/Runner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class Runner {
99

1010
public static void main(String[] args) {
1111
try {
12-
List<Token> results = LexicalAnalyser.analyse("public class foo { public static void main(String[] args){ int i = 0; if (i != 2) { i = i + 1; System.out.println(\"Hi\"); } else { i = i * 2; } } }");
12+
// List<Token> results = LexicalAnalyser.analyse("public class foo { public static void main(String[] args){ int i = 0; if (i != 2) { i = i + 1; System.out.println(\"Hi\"); } else { i = i * 2; } } }");
13+
List<Token> results = LexicalAnalyser.analyse("public class Test { public static void main(String[] args){ for ( ; 5 ;) {; } }}");
14+
// List<Token> results = LexicalAnalyser.analyse("public class Test { public static void main(String[] args){ int i; }}");
1315
System.out.println(results);
1416
ParseTree tree = SyntacticAnalyser.parse(results);
1517
System.out.println(tree);

Assignment2/src/com/company/SyntacticalAnalysisTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public void testSimplestWorkingProgram() throws LexicalException, SyntaxExceptio
6060
assertEquals(children.get(i).getLabel(), TreeNode.Label.terminal);
6161
assertEquals(children.get(i++).getToken().get().getType(), Token.TokenType.PUBLIC);
6262
assertEquals(children.get(i).getLabel(), TreeNode.Label.terminal);
63+
assertEquals(children.get(i++).getToken().get().getType(), Token.TokenType.STATIC);
64+
assertEquals(children.get(i).getLabel(), TreeNode.Label.terminal);
6365
assertEquals(children.get(i++).getToken().get().getType(), Token.TokenType.VOID);
6466
assertEquals(children.get(i).getLabel(), TreeNode.Label.terminal);
6567
assertEquals(children.get(i++).getToken().get().getType(), Token.TokenType.MAIN);

Assignment2/src/com/company/TreeNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public Optional<Token> getToken() {
4444
return this.token;
4545
}
4646

47+
public void setToken(Token token) {
48+
this.token = Optional.of(token);
49+
}
50+
4751
public TreeNode getParent() {
4852
return this.parent;
4953
}

0 commit comments

Comments
 (0)