private Tree<S> parseSequence(Tree<S> currentTree) { // at this point, i have already seen at least an open parenthesis AND // an atom. If the next character is a character is a close parenthesis, // then I just return the input. NOTE: DO NOT MOVE THE TOKEN POINTER. String currentToken = tokens.get(currentTokenId); if (currentToken.equals(")")) { return currentTree; } else { // now we have something here. that should be an expression followed // by a sequence. Find the expression, attach it to the current // tree. Then give that combined tree to the sequence parser and // return whatever it gives. parseExpr(currentTree); return parseSequence(currentTree); } }
private Tree<S> parseSequence(Tree<S> currentTree) { // at this point, i have already seen at least an open parenthesis AND // an atom. If the next character is a character is a close parenthesis, // then I just return the input. NOTE: DO NOT MOVE THE TOKEN POINTER. String currentToken = tokens.get(currentTokenId); if (currentToken.equals(")")) { return currentTree; } else { // now we have something here. that should be an expression followed // by a sequence. Find the expression, attach it to the current // tree. Then give that combined tree to the sequence parser and // return whatever it gives. parseExpr(currentTree); return parseSequence(currentTree); } }
private Tree<S> parseExpr(Tree<S> tree) { String currentToken = tokens.get(currentTokenId); if (currentToken.equals("(")) { currentTokenId++; currentToken = tokens.get(currentTokenId); Tree<S> newTree = new Tree<>(nodeParser.parseNode(currentToken)); currentTokenId++; newTree = parseSequence(newTree); currentToken = tokens.get(currentTokenId); if (currentToken.equals(")")) { currentTokenId++; tree.addSubtree(newTree); return tree; } else { throw new IllegalArgumentException("Missing close parenthesis near " + currentToken); } } else { tree.addLeaf(nodeParser.parseNode(currentToken)); currentTokenId++; return tree; } }
private Tree<S> parseExpr(Tree<S> tree) { String currentToken = tokens.get(currentTokenId); if (currentToken.equals("(")) { currentTokenId++; currentToken = tokens.get(currentTokenId); Tree<S> newTree = new Tree<>(nodeParser.parseNode(currentToken)); currentTokenId++; newTree = parseSequence(newTree); currentToken = tokens.get(currentTokenId); if (currentToken.equals(")")) { currentTokenId++; tree.addSubtree(newTree); return tree; } else { throw new IllegalArgumentException("Missing close parenthesis near " + currentToken); } } else { tree.addLeaf(nodeParser.parseNode(currentToken)); currentTokenId++; return tree; } }