broken parser

This commit is contained in:
nora 2021-01-16 21:11:45 +01:00
parent 313a584631
commit d7c9aeb127
4 changed files with 13 additions and 12 deletions

2
.idea/.gitignore generated vendored
View file

@ -1,5 +1,5 @@
# Default ignored files # Default ignored files
/shelf/ /shelf/
/workspace.xml /workspace.xml
out\ target/
*.class *.class

View file

@ -166,8 +166,11 @@ public class Lexer {
Leorio does say "small killua" Leorio does say "small killua"
} }
"""; """;
String hierarchy = """
killua0 hunts 3 > 3
""";
ArrayList<LexToken> tokens = l.lex(ifs); ArrayList<LexToken> tokens = l.lex(hierarchy);
Parser p = new Parser(); Parser p = new Parser();
try { try {

View file

@ -27,13 +27,13 @@ public enum WordType {
private int postArgAmount; private int postArgAmount;
private int preArgAmount; private int preArgAmount;
WordType(int postArgAmount, int preArgAmount) { WordType(int preArgAmount, int postArgAmount) {
this.postArgAmount = postArgAmount; this.postArgAmount = postArgAmount;
this.preArgAmount = preArgAmount; this.preArgAmount = preArgAmount;
} }
WordType(int postArgAmount) { WordType(int postArgAmount) {
this(postArgAmount, 0); this(0, postArgAmount);
} }
WordType() { WordType() {

View file

@ -45,7 +45,7 @@ public class Parser {
} }
//calculate the arguments of the start node, parsing the whole statement //calculate the arguments of the start node, parsing the whole statement
getArguments(startNode, true); doStatement(startNode, true);
System.out.println(prevNode); System.out.println(prevNode);
statements.add(prevNode); statements.add(prevNode);
} while (iterator.hasNext()); } while (iterator.hasNext());
@ -54,7 +54,7 @@ public class Parser {
return statements; return statements;
} }
private void getArguments(ParseTreeNode node, boolean isParent) throws ParseException { private void doStatement(ParseTreeNode node, boolean isParent) throws ParseException {
if (node.getKey() == WordType.MINUS && prevNode.getKey() != WordType.NUMBER) { if (node.getKey() == WordType.MINUS && prevNode.getKey() != WordType.NUMBER) {
node.setKey(WordType.NEGATIVE); node.setKey(WordType.NEGATIVE);
} }
@ -77,19 +77,17 @@ public class Parser {
if (isParent) { if (isParent) {
prevNode = node; prevNode = node;
} }
if (expectedArg == 0) {
nextToken();
} else {
nextToken(); nextToken();
if (expectedArg != 0) {
for (int i = 0; i < expectedArg; i++) { for (int i = 0; i < expectedArg; i++) {
ParseTreeNode child = currentToken.toNode(); ParseTreeNode child = currentToken.toNode();
getArguments(child, false); doStatement(child, false);
node.addChild(child); node.addChild(child);
} }
} }
if (currentToken != null && currentToken.expectsPreArg()) { if (currentToken != null && currentToken.expectsPreArg()) {
getArguments(currentToken.toNode(), true); doStatement(currentToken.toNode(), true);
} }
} }