Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Assignment2/src/com/company/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class Runner {

public static void main(String[] args) {
try {
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; } } }");
// 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; } } }");
List<Token> results = LexicalAnalyser.analyse("public class Test { public static void main(String[] args){ ; }}");
// List<Token> results = LexicalAnalyser.analyse("public class Test { public static void main(String[] args){ int i; }}");
System.out.println(results);
ParseTree tree = SyntacticAnalyser.parse(results);
Expand Down
13 changes: 10 additions & 3 deletions Assignment2/src/com/company/SyntacticAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class SyntacticAnalyser {

public static ParseTree parse(List<Token> tokens) throws SyntaxException {

if (tokens.size() == 0)
throw new SyntaxException("Syntactical exception.");

HashMap<TreeNode.Label, HashMap<String, Integer>> parseTable = createTable();
Deque<NodeWrapper> stack = new ArrayDeque<>();
Expand Down Expand Up @@ -43,7 +45,12 @@ public static ParseTree parse(List<Token> tokens) throws SyntaxException {
stack.getFirst().createNode(token);
currentNode = stack.getFirst();
}
int ruleNum = (int) parseTable.get(currentNode.getNode().getLabel()).get(tokenToString(token)); // ----------- checks if a rule goes with the variable
int ruleNum = 0;
try {
ruleNum = (int) parseTable.get(currentNode.getNode().getLabel()).get(tokenToString(token)); // ----------- checks if a rule goes with the variable
} catch (NullPointerException e) {
throw new SyntaxException("Syntactical Exception.");
}
switch (ruleNum) {
case 1:
stack.pop();
Expand Down Expand Up @@ -102,7 +109,7 @@ public static ParseTree parse(List<Token> tokens) throws SyntaxException {
break;
case 10:
stack.pop();
stack.push(new NodeWrapper(currentNode.getNode(), TreeNode.Label.epsilon, ""));
stack.push(new NodeWrapper(currentNode.getNode(), TreeNode.Label.terminal, ";"));
break;
case 11:
stack.pop();
Expand Down Expand Up @@ -392,7 +399,7 @@ public static ParseTree parse(List<Token> tokens) throws SyntaxException {
stack.push(new NodeWrapper(currentNode.getNode(), TreeNode.Label.terminal, "\""));
break;
}
} else if (stack.getFirst().getLabel().equals(TreeNode.Label.terminal) && stack.getFirst().getValue().equals(tokenToString(token))) {
} else if (stack.getFirst().getLabel().equals(TreeNode.Label.terminal) && stack.getFirst().getValue().equals(tokenToString(token))) {
stack.getFirst().createNode(token);
stack.pop();
terminalAvailable = true;
Expand Down