/** * Set the start state for a specific precedence value. * * @param precedence The current precedence. * @param startState The start state corresponding to the specified * precedence. * * @throws IllegalStateException if this is not a precedence DFA. * @see #isPrecedenceDfa() */ public void SetPrecedenceStartState(int precedence, DFAState startState) { if (!IsPrecedenceDfa) { throw new Exception("Only precedence DFAs may contain a precedence start state."); } if (precedence < 0) { return; } // synchronization on s0 here is ok. when the DFA is turned into a // precedence DFA, s0 will be initialized once and not updated again lock (s0) { // s0.edges is never null for a precedence DFA if (precedence >= s0.edges.Length) { s0.edges = Arrays.CopyOf(s0.edges, precedence + 1); } s0.edges[precedence] = startState; } }
internal virtual string GetStateString(DFAState s) { if (s == ATNSimulator.ERROR) { return("ERROR"); } int n = s.stateNumber; string baseStateStr = (s.isAcceptState ? ":" : "") + "s" + n + (s.requiresFullContext ? "^" : ""); if (s.isAcceptState) { if (s.predicates != null) { return(baseStateStr + "=>" + Arrays.ToString(s.predicates)); } else { return(baseStateStr + "=>" + s.prediction); } } else { return(baseStateStr); } }
public DFA(DecisionState atnStartState, int decision) { this.atnStartState = atnStartState; this.decision = decision; this.precedenceDfa = false; if (atnStartState is StarLoopEntryState && ((StarLoopEntryState)atnStartState).isPrecedenceDecision) { this.precedenceDfa = true; DFAState precedenceState = new DFAState(new ATNConfigSet()); precedenceState.edges = new DFAState[0]; precedenceState.isAcceptState = false; precedenceState.requiresFullContext = false; this.s0 = precedenceState; } }
/** * Two {@link DFAState} instances are equal if their ATN configuration sets * are the same. This method is used to see if a state already exists. * * <p>Because the number of alternatives and number of ATN configurations are * finite, there is a finite number of DFA states that can be processed. * This is necessary to show that the algorithm terminates.</p> * * <p>Cannot test the DFA state numbers here because in * {@link ParserATNSimulator#addDFAState} we need to know if any other state * exists that has this exact set of ATN configurations. The * {@link #stateNumber} is irrelevant.</p> */ public override bool Equals(Object o) { // compare set of ATN configurations in this set with other if (this == o) { return(true); } if (!(o is DFAState)) { return(false); } DFAState other = (DFAState)o; // TODO (sam): what to do when configs==null? bool sameSet = this.configSet.Equals(other.configSet); // System.out.println("DFAState.equals: "+configs+(sameSet?"==":"!=")+other.configs); return(sameSet); }
public override string ToString() { if (dfa.s0 == null) { return(null); } StringBuilder buf = new StringBuilder(); if (dfa.states != null) { List <DFAState> states = new List <DFAState>(dfa.states.Values); states.Sort((x, y) => x.stateNumber - y.stateNumber); foreach (DFAState s in states) { int n = s.edges != null ? s.edges.Length : 0; for (int i = 0; i < n; i++) { DFAState t = s.edges[i]; if (t != null && t.stateNumber != int.MaxValue) { buf.Append(GetStateString(s)); String label = GetEdgeLabel(i); buf.Append("-"); buf.Append(label); buf.Append("->"); buf.Append(GetStateString(t)); buf.Append('\n'); } } } } string output = buf.ToString(); if (output.Length == 0) { return(null); } return(output); }