/** * Removes repeated transitions with the same token. After this method call * the Transition.getRepeat method must be consulted in able to detect repetitions. * @see Transition.getRepeat() */ public boolean RemoveRepeatingTransitions() { boolean removed = false; while (true) { if (!RemoveRepeatingTransition()) { return removed; } removed = true; } } private boolean RemoveRepeatingTransition()
public static void main(String... args) { try { DFA<Integer> dfa = Regex.createDFA("aaabc"); dfa.dump(System.err); dfa.calculateMaxFindSkip(); System.err.println("skip="+dfa.isAcceptStart()); System.err.println("max="+dfa.maxDepth()); System.err.println("min="+dfa.minDepth()); } catch (Exception ex) { ex.printStackTrace(); } }
public void distribute() { distribute(dfa.getRoot(), dfa.initialSize()); } private void distribute(DFAState<T> state, int rootCount)
DFA<T> ndfa = new DFA<>(c.getState(), c.getCount(), dfa); distributedDFAs.add(ndfa); if (rootCount - distCount < maxStates) dfa.subtractDistributedSize(distCount); List<DFA<T>> redistributeList = new ArrayList<>(); Iterator<DFA<T>> it = distributedDFAs.iterator(); if (ddfa.initialSize() > maxStates)
public static SubClass createSubClass(String expression, String classname, Option... options) throws IOException { //return createSubClass(expression, createDFA(expression, 1, options), classname); SubClass subClass = new SubClass(Regex.class, classname, Modifier.PUBLIC); DFA dfa = createDFA(expression, 1, options); subClass.codeDefaultConstructor( FieldInitializer.getInstance(El.getField(Regex.class, "acceptEmpty"), dfa.acceptEmpty()), FieldInitializer.getInstance(El.getField(Regex.class, "expression"), expression), FieldInitializer.getInstance(El.getField(Regex.class, "minLength"), dfa.minDepth()), FieldInitializer.getInstance(El.getField(Regex.class, "maxLength"), dfa.maxDepth()) ); MatchCompiler<Integer> matchCompiler = new MatchCompiler<>(dfa, -1, 0); if (debug) { //Method trace = Regex.class.getDeclaredMethod("trace", Integer.TYPE, String.class); //matchComp.setDebug(trace); } subClass.overrideMethod(matchCompiler, java.lang.reflect.Modifier.PUBLIC, "match", InputReader.class); dfa = createDFA(expression, 1, options); FindCompiler<Integer> findCompiler = new FindCompiler<>(dfa, -1, 0); if (debug) { //Method trace = Regex.class.getDeclaredMethod("trace", Integer.TYPE, String.class); //findComp.setDebug(trace); } subClass.overrideMethod(findCompiler, java.lang.reflect.Modifier.PUBLIC, "find", InputReader.class); return subClass; }
@Override public void implement() throws IOException { if (!Typ.isSameType(tokenType, getReturnType())) { throw new IllegalArgumentException(tokenType+" is not expected return type "+getReturnType()+" of method"); } if (dfa.initialSize() > MAXSTATES) { SubClass subClass = getSubClass(); DFADistributor<T> dd = new DFADistributor<>(dfa, MAXSTATES); dd.distribute(); for (DFA<T> ddfa : dd.getDistributedDFAs()) { String subName = jav.makeJavaIdentifier(getMethodDescription()+ddfa.name()); ExecutableElement distributedMethod = getDistributedMethod(subName); DFACompiler<T> sc = copy(ddfa); sc.setSubCompiler(true); subClass.defineMethod(sc, distributedMethod); } } try { compile(); } catch (NoSuchMethodException ex) { throw new IOException(ex); } }
protected void compile() throws IOException, NoSuchMethodException if (dfa.initialSize() > MAXSTATESUSEWIDE) tstore("accepted"); int count = 0; Iterator<DFAState<T>> si = dfa.iterator(); while (si.hasNext()) fixAddress("eof"); eof(); dfaCount += dfa.initialSize(); byteCount += position();
public Validator(String expression) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { DFA<Integer> dfa = Regex.createDFA(expression); start = dfa.getRoot(); }
/** * Calculates how many characters we can skip after failed find operation * @return */ public void calculateMaxFindSkip() { Map<DFAState<T>,Integer> indexOf = new NumMap<>(); Map<DFAState<T>,Integer> skip = new NumMap<>(); Deque<DFAState<T>> stack = new ArrayDeque<>(); findSkip(root, indexOf, stack, new LinkedList()); root.setAcceptStartLength(1); }
public FindCompiler(DFA<T> dfa, T errorToken, T eofToken) { super(dfa, errorToken, eofToken); dfa.calculateMaxFindSkip(); }
/** * Constructs a dfa from using first nfa state as starting state. * @param scope * @return */ public DFA<T> constructDFA(Scope<DFAState<T>> scope) { return new DFA<>(first.constructDFA(scope), scope.count()); } /**
@Override protected void afterState(DFAState<T> s) throws IOException, NoSuchMethodException { if (dfa.isAcceptStart()) { tload("reader"); iconst(s.getAcceptStartLength()); invokevirtual(InputReader.class, "setAcceptStart", int.class); } }
/** * @param args the command line arguments */ public static void main(String[] args) { try { DFA<Integer> dfa = Regex.createDFA("ashjkahsj(ha|kjkajdkasdj)+h|yrquiyqiwdioas|(kdajksdfh){2,3}ajkshdjkah|ajhdajsdjkahsdjkah"); DiGraphIterator<DFAState<Integer>> i = new DiGraphIterator<>(dfa.getRoot()); while (i.hasNext()) { System.err.println(i.next()); } } catch (Exception ex) { ex.printStackTrace(); } }
findSkip(t.getTo(),indexOf, stack, prefix); prefix.removeLast();
/** * Compiles expressions */ public void compile() { Scope<DFAState<T>> dfaScope = new Scope<>("org.vesalainen.regex.RegexMatcher"); if (nfa == null) { nfa = new NFA(dfaScope); } dfa = nfa.constructDFA(dfaScope); state = root = dfa.getRoot(); parser = null; nfaScope = null; nfa = null; } /**
if (dfa.getRoot().isAccepting())