Deque<DFAState<T>> unmarked = new ArrayDeque<>(); Set<NFAState<T>> startSet = epsilonClosure(dfaScope); DFAState<T> startDfa = new DFAState<>(dfaScope, startSet); all.put(startSet, startDfa); unmarked.add(startDfa); for (Range c : dfa.possibleMoves()) Set<NFAState<T>> moveSet = dfa.nfaTransitsFor(c); if (!moveSet.isEmpty()) if (ndfa == null) ndfa = new DFAState<>(dfaScope, newSet); all.put(newSet, ndfa); unmarked.add(ndfa); dfa.addTransition(c, ndfa); dfa.removeTransitionsFromAcceptImmediatelyStates(); dfa.removeDeadEndTransitions(); dfa.optimizeTransitions();
getToken() == null || getToken().equals(ns.getToken()) || getPriority() < ns.getPriority() setToken(ns.getToken()); setPriority(ns.getPriority()); setAcceptImmediately(ns.isAcceptImmediately()); if (getPriority() == ns.getPriority()) throw new AmbiguousExpressionException("conflicting tokens ",getToken(), ns.getToken());
public Status match(int cc) state = state.transit(cc); if (state != null) if (state.isAccepting()) matched = state.getToken(); state = root; return Status.Match;
fixAddress(s.toString()); accepting(s); if (s.hasBoundaryMatches()) if (s.getTransitions().size() != 1) for (Transition tr : s.getTransitions()) DFAState to = s.transit(range); tload("reader"); iconst(range.getBoundaryMatcher()); invokevirtual(InputReader.class, "isAtBoundary", int.class); ifeq("error"); goto_n(to.toString()); Iterator<Transition<DFAState<T>>> ti = s.getTransitions().iterator(); if (ti.hasNext()) String back = s.toString()+"-repeat"; fixAddress(back); tload("reader"); iflt("eof"); Range range = first.getCondition(); DFAState to = s.transit(range); compile(range, "error", !ti.hasNext()); if (s.getTransitionSelectivity() > 2)
indexOf.put(state, index); int asl = state.matchLength(prefix); state.setAcceptStartLength(asl); if (asl > 0) for (Transition<DFAState<T>> t : state.getTransitions()) while (!s.equals(state))
for (DFAState<T> state : this) if (state.getTransitions().size() == 1) Transition<DFAState<T>> tr = state.getTransitions().iterator().next(); Range condition = tr.getCondition(); int repeat = 1; !to.isAccepting() && to.getTransitions().size() == 1 && to.inStates().size() == 1 Transition<DFAState<T>> totr = to.getTransitions().iterator().next(); if ( condition.equals(totr.getCondition()) && set.remove(state); set.remove(to); state.edges().removeAll(set); state.edges().add(to); return true;
/** * Marks all nfa states that can be accepting to end stop. */ public void analyzeEndStop() { DFA<T> dfa = constructDFA(new Scope<DFAState<T>>("analyzeEndStop")); for (DFAState<T> s : dfa) { if (s.isAccepting()) { if (s.isEndStop()) { for (NFAState<T> n : s.getNfaSet()) { if (n.isAccepting()) { n.setEndStop(true); } } } } } } /**
int index = stack.size(); indexOf.put(state, index); if (state.isAccepting()) for (Transition<DFAState<T>> t : state.getTransitions()) while (!s.equals(state))
@Override protected void accepting(DFAState<T> s) throws IOException, NoSuchMethodException { if (s.isAccepting()) { tconst(s.getToken()); tstore("accepted"); int fixedEndLength = s.getFixedEndLength(); if (fixedEndLength != 0) { tload("reader"); iconst(fixedEndLength); invokevirtual(InputReader.class, "rewind", int.class); } } else { tconst(errorToken); tstore("accepted"); } }
@Override protected void accepting(DFAState<T> s) throws IOException, NoSuchMethodException { if (s.isAccepting()) { tconst(s.getToken()); tstore("accepted"); tload("reader"); invokevirtual(InputReader.class, "findAccept"); } }
depth.put(state, 0L); for (Transition<DFAState<T>> t : state.getTransitions()) while (!s.equals(state))
for (DFAState<T> s : state.edges()) c.getState().setDistributed(true); DFA<T> ndfa = new DFA<>(c.getState(), c.getCount(), dfa); distributedDFAs.add(ndfa);
public DFACompiler(DFA<T> dfa, T errorToken, T eofToken) { this.dfa = dfa; this.errorToken = errorToken; this.eofToken = eofToken; TypeMirror errorType = Typ.getTypeFor(errorToken.getClass()); if (Typ.isSameType(Typ.String, errorType)) { this.tokenType = Typ.String; } else { this.tokenType = Typ.unboxedType(errorType); } if (!Typ.isJavaConstantType(tokenType)) { throw new IllegalArgumentException(tokenType+" is not java constant class"); } for (DFAState<T> state : dfa) { if ( eofToken.equals(state.getToken()) || errorToken.equals(state.getToken()) ) { throw new IllegalArgumentException(state.getToken()+" token is also error or eof token"); } } }
/** * Optimizes transition by merging ranges */ void optimizeTransitions() { HashMap<DFAState<T>,RangeSet> hml = new HashMap<>(); for (Transition<DFAState<T>> t : transitions.values()) { RangeSet rs = hml.get(t.getTo()); if (rs == null) { rs = new RangeSet(); hml.put(t.getTo(), rs); } rs.add(t.getCondition()); } transitions.clear(); for (DFAState<T> dfa : hml.keySet()) { RangeSet rs = RangeSet.merge(hml.get(dfa)); for (Range r : rs) { addTransition(r, dfa); } } } /**
@Override protected void afterState(DFAState<T> s) throws IOException, NoSuchMethodException { if (dfa.isAcceptStart()) { tload("reader"); iconst(s.getAcceptStartLength()); invokevirtual(InputReader.class, "setAcceptStart", int.class); } }
st = st.transit(cc); if (st != null) if (st.isAccepting()) replacer = st.getToken();
protected void accepting(DFAState<T> s) throws IOException, NoSuchMethodException { if (s.isAccepting()) { tconst(s.getToken()); tstore("accepted"); } else { tconst(errorToken); tstore("accepted"); } }