1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-06-25 20:26:24 -07:00

Merge tree construction feature into master.

This commit is contained in:
Danila Fedorin 2017-07-25 14:26:31 -07:00
commit 38255b1219
15 changed files with 597 additions and 0 deletions

View File

@ -0,0 +1,68 @@
package org.nwapw.abacus.lexing;
import org.nwapw.abacus.lexing.pattern.EndNode;
import org.nwapw.abacus.lexing.pattern.Match;
import org.nwapw.abacus.lexing.pattern.Pattern;
import org.nwapw.abacus.lexing.pattern.PatternNode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
public class Lexer<T> {
private ArrayList<Pattern<T>> patterns;
public Lexer(){
patterns = new ArrayList<>();
}
public void register(String pattern, T id){
Pattern<T> compiledPattern = new Pattern<>(pattern, id);
if(compiledPattern.getHead() != null) patterns.add(compiledPattern);
}
public Match<T> lexOne(String from, int startAt, Comparator<T> compare){
ArrayList<Match<T>> matches = new ArrayList<>();
HashSet<PatternNode<T>> currentSet = new HashSet<>();
HashSet<PatternNode<T>> futureSet = new HashSet<>();
int index = startAt;
for(Pattern<T> pattern : patterns){
pattern.getHead().addInto(currentSet);
}
while(!currentSet.isEmpty()){
for(PatternNode<T> node : currentSet){
if(index < from.length() && node.matches(from.charAt(index))) {
node.addOutputsInto(futureSet);
} else if(node instanceof EndNode){
matches.add(new Match<>(startAt, index, ((EndNode<T>) node).getPatternId()));
}
}
HashSet<PatternNode<T>> tmp = currentSet;
currentSet = futureSet;
futureSet = tmp;
futureSet.clear();
index++;
}
matches.sort((a, b) -> compare.compare(a.getType(), b.getType()));
if(compare != null) {
matches.sort(Comparator.comparingInt(a -> a.getTo() - a.getFrom()));
}
return matches.isEmpty() ? null : matches.get(matches.size() - 1);
}
public ArrayList<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){
int index = startAt;
ArrayList<Match<T>> matches = new ArrayList<>();
Match<T> lastMatch = null;
while((lastMatch = lexOne(from, index, compare)) != null && index < from.length()){
if(lastMatch.getTo() == lastMatch.getFrom()) return null;
matches.add(lastMatch);
index += lastMatch.getTo() - lastMatch.getFrom();
}
return matches;
}
}

View File

@ -0,0 +1,10 @@
package org.nwapw.abacus.lexing.pattern;
public class AnyNode<T> extends PatternNode<T> {
@Override
public boolean matches(char other) {
return true;
}
}

View File

@ -0,0 +1,15 @@
package org.nwapw.abacus.lexing.pattern;
public class EndNode<T> extends PatternNode<T> {
private T patternId;
public EndNode(T patternId){
this.patternId = patternId;
}
public T getPatternId(){
return patternId;
}
}

View File

@ -0,0 +1,13 @@
package org.nwapw.abacus.lexing.pattern;
import java.util.ArrayList;
import java.util.Collection;
public class LinkNode<T> extends PatternNode<T> {
@Override
public void addInto(Collection<PatternNode<T>> into) {
addOutputsInto(into);
}
}

View File

@ -0,0 +1,26 @@
package org.nwapw.abacus.lexing.pattern;
public class Match<T> {
private int from;
private int to;
private T type;
public Match(int from, int to, T type){
this.from = from;
this.to = to;
this.type = type;
}
public int getFrom() {
return from;
}
public int getTo() {
return to;
}
public T getType() {
return type;
}
}

View File

@ -0,0 +1,172 @@
package org.nwapw.abacus.lexing.pattern;
import java.util.Collection;
import java.util.HashMap;
import java.util.Stack;
import java.util.function.Function;
public class Pattern<T> {
private T id;
private PatternNode<T> head;
private String source;
private int index;
private HashMap<Character, Function<PatternChain<T>, PatternChain<T>>> operations =
new HashMap<Character, Function<PatternChain<T>, PatternChain<T>>>() {{
put('+', Pattern.this::transformPlus);
put('*', Pattern.this::transformStar);
put('?', Pattern.this::transformQuestion);
}};
private PatternChain<T> transformPlus(PatternChain<T> chain){
chain.tail.outputStates.add(chain.head);
return chain;
}
private PatternChain<T> transformStar(PatternChain<T> chain){
LinkNode<T> newTail = new LinkNode<>();
LinkNode<T> newHead = new LinkNode<>();
newHead.outputStates.add(chain.head);
newHead.outputStates.add(newTail);
chain.tail.outputStates.add(newTail);
newTail.outputStates.add(newHead);
chain.head = newHead;
chain.tail = newTail;
return chain;
}
private PatternChain<T> transformQuestion(PatternChain<T> chain){
LinkNode<T> newTail = new LinkNode<>();
LinkNode<T> newHead = new LinkNode<>();
newHead.outputStates.add(chain.head);
newHead.outputStates.add(newTail);
chain.tail.outputStates.add(newTail);
chain.head = newHead;
chain.tail = newTail;
return chain;
}
private PatternChain<T> combineChains(Collection<PatternChain<T>> collection){
LinkNode<T> head = new LinkNode<>();
LinkNode<T> tail = new LinkNode<>();
PatternChain<T> newChain = new PatternChain<>(head, tail);
for(PatternChain<T> chain : collection){
head.outputStates.add(chain.head);
chain.tail.outputStates.add(tail);
}
return newChain;
}
private PatternChain<T> parseValue(){
if(index >= source.length()) return null;
if(source.charAt(index) == '\\'){
if(++index >= source.length()) return null;
}
return new PatternChain<>(new ValueNode<>(source.charAt(index++)));
}
private PatternChain<T> parseOr(){
Stack<PatternChain<T>> orStack = new Stack<>();
index++;
while(index < source.length() && source.charAt(index) != ']'){
if(source.charAt(index) == '-'){
index++;
if(orStack.empty() || orStack.peek().tail.range() == '\0') return null;
PatternChain<T> bottomRange = orStack.pop();
PatternChain<T> topRange = parseValue();
if(topRange == null || topRange.tail.range() == '\0') return null;
orStack.push(new PatternChain<>(new RangeNode<>(bottomRange.tail.range(), topRange.tail.range())));
} else {
PatternChain<T> newChain = parseValue();
if(newChain == null) return null;
orStack.push(newChain);
}
}
if(index++ >= source.length()) return null;
return (orStack.size() == 1) ? orStack.pop() : combineChains(orStack);
}
private PatternChain<T> parseSegment(boolean isSubsegment){
if(index >= source.length() || ((source.charAt(index) != '(') && isSubsegment)) return null;
if(isSubsegment) index++;
Stack<PatternChain<T>> orChain = new Stack<>();
PatternChain<T> fullChain = new PatternChain<>();
PatternChain<T> currentChain = null;
while (index < source.length() && source.charAt(index) != ')'){
char currentChar = source.charAt(index);
if(operations.containsKey(currentChar)){
if(currentChain == null) return null;
currentChain = operations.get(currentChar).apply(currentChain);
fullChain.append(currentChain);
currentChain = null;
index++;
} else if(currentChar == '|'){
if(currentChain == null) return null;
fullChain.append(currentChain);
orChain.push(fullChain);
currentChain = null;
fullChain = new PatternChain<>();
if(++index >= source.length()) return null;
} else if(currentChar == '('){
if(currentChain != null) {
fullChain.append(currentChain);
}
currentChain = parseSegment(true);
if(currentChain == null) return null;
} else if(currentChar == '['){
if(currentChain != null){
fullChain.append(currentChain);
}
currentChain = parseOr();
if(currentChain == null) return null;
} else if(currentChar == '.'){
if(currentChain != null){
fullChain.append(currentChain);
}
currentChain = new PatternChain<>(new AnyNode<>());
index++;
} else {
if(currentChain != null){
fullChain.append(currentChain);
}
currentChain = parseValue();
if(currentChain == null) return null;
}
}
if(!(!isSubsegment || (index < source.length() && source.charAt(index) == ')'))) return null;
if(isSubsegment) index++;
if(currentChain != null) fullChain.append(currentChain);
if(!orChain.empty()){
orChain.push(fullChain);
fullChain = combineChains(orChain);
}
return fullChain;
}
public Pattern(String from, T id){
this.id = id;
index = 0;
source = from;
PatternChain<T> chain = parseSegment(false);
if(chain == null) {
head = null;
} else {
chain.append(new EndNode<>(id));
head = chain.head;
}
}
public PatternNode<T> getHead() {
return head;
}
}

View File

@ -0,0 +1,40 @@
package org.nwapw.abacus.lexing.pattern;
public class PatternChain<T> {
public PatternNode<T> head;
public PatternNode<T> tail;
public PatternChain(PatternNode<T> head, PatternNode<T> tail){
this.head = head;
this.tail = tail;
}
public PatternChain(PatternNode<T> node){
this(node, node);
}
public PatternChain(){
this(null);
}
public void append(PatternChain<T> other){
if(other.head == null || tail == null) {
this.head = other.head;
this.tail = other.tail;
} else {
tail.outputStates.add(other.head);
tail = other.tail;
}
}
public void append(PatternNode<T> node){
if(tail == null){
head = tail = node;
} else {
tail.outputStates.add(node);
tail = node;
}
}
}

View File

@ -0,0 +1,31 @@
package org.nwapw.abacus.lexing.pattern;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class PatternNode<T> {
protected HashSet<PatternNode<T>> outputStates;
public PatternNode(){
outputStates = new HashSet<>();
}
public boolean matches(char other){
return false;
}
public char range(){
return '\0';
}
public void addInto(Collection<PatternNode<T>> into){
into.add(this);
}
public void addOutputsInto(Collection<PatternNode<T>> into){
outputStates.forEach(e -> e.addInto(into));
}
}

View File

@ -0,0 +1,18 @@
package org.nwapw.abacus.lexing.pattern;
public class RangeNode<T> extends PatternNode<T> {
private char from;
private char to;
public RangeNode(char from, char to){
this.from = from;
this.to = to;
}
@Override
public boolean matches(char other) {
return other >= from && other <= to;
}
}

View File

@ -0,0 +1,20 @@
package org.nwapw.abacus.lexing.pattern;
public class ValueNode<T> extends PatternNode<T> {
private char value;
public ValueNode(char value){
this.value = value;
}
@Override
public boolean matches(char other) {
return other == value;
}
@Override
public char range() {
return value;
}
}

View File

@ -0,0 +1,25 @@
package org.nwapw.abacus.tree;
import org.nwapw.abacus.number.NaiveNumber;
import org.nwapw.abacus.number.NumberInterface;
public class NumberNode extends TreeNode {
private NumberInterface number;
public NumberNode(){
number = null;
}
public NumberNode(double value){
number = new NaiveNumber(value);
}
public NumberNode(String value){
this(Double.parseDouble(value));
}
public NumberInterface getNumber() {
return number;
}
}

View File

@ -0,0 +1,38 @@
package org.nwapw.abacus.tree;
public class OpNode extends TreeNode {
private String operation;
private TreeNode left;
private TreeNode right;
public OpNode(String operation){
this(operation, null, null);
}
public OpNode(String operation, TreeNode left, TreeNode right){
this.operation = operation;
this.left = left;
this.right = right;
}
public String getOperation() {
return operation;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
}

View File

@ -0,0 +1,5 @@
package org.nwapw.abacus.tree;
public enum OperatorAssociativity {
LEFT, RIGHT
}

View File

@ -0,0 +1,13 @@
package org.nwapw.abacus.tree;
public enum TokenType {
ANY(0), OP(1), NUM(2), WORD(3), OPEN_PARENTH(4), CLOSE_PARENTH(5);
public final int priority;
TokenType(int priority){
this.priority = priority;
}
}

View File

@ -0,0 +1,103 @@
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<TokenType> lexer = new Lexer<TokenType>(){{
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<String, Integer> precedenceMap = new HashMap<String, Integer>(){{
put("+", 0);
put("-", 0);
put("*", 1);
put("/", 1);
put("^", 2);
}};
private static HashMap<String, OperatorAssociativity> associativityMap =
new HashMap<String, OperatorAssociativity>() {{
put("+", OperatorAssociativity.LEFT);
put("-", OperatorAssociativity.LEFT);
put("*", OperatorAssociativity.LEFT);
put("/", OperatorAssociativity.LEFT);
put("^", OperatorAssociativity.RIGHT);
}};
private static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
public static ArrayList<Match<TokenType>> tokenize(String string){
return lexer.lexAll(string, 0, tokenSorter);
}
public static ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){
ArrayList<Match<TokenType>> output = new ArrayList<>();
Stack<Match<TokenType>> tokenStack = new Stack<>();
while(!from.isEmpty()){
Match<TokenType> 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<TokenType> 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 fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){
if(matches.size() == 0) return null;
Match<TokenType> match = matches.remove(0);
if(match.getType() == TokenType.OP){
TreeNode right = fromStringRecursive(source, matches);
TreeNode left = fromStringRecursive(source, matches);
if(left == null || right == null) return null;
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
} else if(match.getType() == TokenType.NUM){
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
}
return null;
}
public static TreeNode fromString(String string){
ArrayList<Match<TokenType>> matches = intoPostfix(string, tokenize(string));
if(matches == null) return null;
Collections.reverse(matches);
return fromStringRecursive(string, matches);
}
}