From 36846733622caa4294ee75a9f4c85b442608826b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 25 Jul 2017 13:53:38 -0700 Subject: [PATCH] Implement Shunting Yard. --- src/org/nwapw/abacus/tree/TreeNode.java | 85 +++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/org/nwapw/abacus/tree/TreeNode.java diff --git a/src/org/nwapw/abacus/tree/TreeNode.java b/src/org/nwapw/abacus/tree/TreeNode.java new file mode 100644 index 0000000..f34d48d --- /dev/null +++ b/src/org/nwapw/abacus/tree/TreeNode.java @@ -0,0 +1,85 @@ +package org.nwapw.abacus.tree; + +import org.nwapw.abacus.lexing.Lexer; +import org.nwapw.abacus.lexing.pattern.Match; + +import java.util.*; + +public abstract class TreeNode { + + private static Lexer lexer = new Lexer(){{ + register(".", TokenType.ANY); + register("\\+|-|\\*|/|^", TokenType.OP); + register("[0-9]+(\\.[0-9]+)?", TokenType.NUM); + register("[a-zA-Z]+", TokenType.WORD); + register("\\(", TokenType.OPEN_PARENTH); + register("\\)", TokenType.CLOSE_PARENTH); + }}; + private static HashMap precedenceMap = new HashMap(){{ + put("+", 0); + put("-", 0); + put("*", 1); + put("/", 1); + put("^", 2); + }}; + private static HashMap associativityMap = + new HashMap() {{ + put("+", OperatorAssociativity.LEFT); + put("-", OperatorAssociativity.LEFT); + put("*", OperatorAssociativity.LEFT); + put("/", OperatorAssociativity.LEFT); + put("^", OperatorAssociativity.RIGHT); + }}; + + private static Comparator tokenSorter = Comparator.comparingInt(e -> e.priority); + + public static ArrayList> tokenize(String string){ + return lexer.lexAll(string, 0, tokenSorter); + } + + public static ArrayList> intoPostfix(String source, ArrayList> from){ + ArrayList> output = new ArrayList<>(); + Stack> tokenStack = new Stack<>(); + while(!from.isEmpty()){ + Match match = from.remove(0); + if(match.getType() == TokenType.NUM) { + output.add(match); + } else if(match.getType() == TokenType.OP){ + String tokenString = source.substring(match.getFrom(), match.getTo()); + int precedence = precedenceMap.get(tokenString); + OperatorAssociativity associativity = associativityMap.get(tokenString); + + while(!tokenStack.empty()) { + Match otherMatch = tokenStack.peek(); + if(otherMatch.getType() != TokenType.OP) break; + + int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo())); + if(otherPrecdence < precedence || + (associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) { + break; + } + output.add(tokenStack.pop()); + } + tokenStack.push(match); + } else if(match.getType() == TokenType.OPEN_PARENTH){ + tokenStack.push(match); + } else if(match.getType() == TokenType.CLOSE_PARENTH){ + while(!tokenStack.empty() && tokenStack.peek().getType() != TokenType.OPEN_PARENTH){ + output.add(tokenStack.pop()); + } + if(tokenStack.empty()) return null; + tokenStack.pop(); + } + } + while(!tokenStack.empty()){ + if(!(tokenStack.peek().getType() == TokenType.OP)) return null; + output.add(tokenStack.pop()); + } + return output; + } + + public static TreeNode fromString(String string){ + return null; + } + +}