private DFA.StateCollection CreateDFAStates(CGTContent content) { symbols = CreateSymbols(content); DFA.StateCollection states = new DFA.StateCollection(); foreach (DFAStateRecord stateRecord in content.DFAStateTable) { DFA.State state; if (stateRecord.AcceptState) { Symbol symbol = symbols[stateRecord.AcceptIndex]; state = new DFA.EndState(stateRecord.Index, (SymbolTerminal)symbol); //todo: type checking (exception?) } else { state = new DFA.State(stateRecord.Index); } states.Add(state); } foreach (DFAStateRecord stateRecord in content.DFAStateTable) { foreach (EdgeSubRecord edgeRecord in stateRecord.EdgeSubRecords) { DFA.State source = states[stateRecord.Index]; DFA.State target = states[edgeRecord.TargetIndex]; CharacterSetRecord charsetRec = content.CharacterSetTable[edgeRecord.CharacterSetIndex]; DFA.Transition transition = new DFA.Transition(target, charsetRec.Characters); source.Transitions.Add(transition); } } return(states); }
/// <summary> /// Creates a new tokenizer. Useful if for some reason /// you don't want a full LALR parser, but are just interested in a tokenizer. /// </summary> /// <returns></returns> public StringTokenizer CreateNewTokenizer() { State startState = dfaStates[content.InitialStates.DFA]; dfa.DFA dfa = new dfa.DFA(dfaStates, startState); return(new StringTokenizer(dfa)); }
/// <summary> /// Goto the next state depending on an input character. /// </summary> /// <param name="ch">The character that determines what state to go to next.</param> /// <returns>The new current state.</returns> public State GotoNext(char ch) { Transition transition = currentState.Transitions.Find(ch); if (transition != null) { currentState = transition.Target; return currentState; } else return null; }
/// <summary> /// Creates a new transition by specifying the target state and the criteria for /// taking a transition to another state. The source state does not need to be /// specified, because the state itself knows its transition. /// </summary> /// <param name="target">The target state.</param> /// <param name="characters">The character set criteria.</param> public Transition(State target, string characters) { this.target = target; if (characters.Length > 10) this.charset = new HashSet(); else this.charset = new ArraySet(); char[] ca = characters.ToCharArray(); foreach (Char ch in ca) { this.charset.Add(ch); } }
public void Add(State state) { list.Add(state); }
/// <summary> /// Sets the DFA back to the starting state, so it can be used to get a new token. /// </summary> public void Reset() { this.currentState = startState; }
/// <summary> /// Creates a new DFA. /// </summary> /// <param name="states">The states that are part of the DFA.</param> /// <param name="startState">The starting state</param> public DFA(StateCollection states, State startState) { this.states = states; this.startState = startState; this.currentState = startState; }
private DFA.StateCollection CreateDFAStates(CGTContent content) { symbols = CreateSymbols(content); DFA.StateCollection states = new DFA.StateCollection(); foreach (DFAStateRecord stateRecord in content.DFAStateTable) { DFA.State state; if (stateRecord.AcceptState) { Symbol symbol = symbols[stateRecord.AcceptIndex]; state = new DFA.EndState(stateRecord.Index,(SymbolTerminal)symbol); //todo: type checking (exception?) } else { state = new DFA.State(stateRecord.Index); } states.Add(state); } foreach (DFAStateRecord stateRecord in content.DFAStateTable) { foreach (EdgeSubRecord edgeRecord in stateRecord.EdgeSubRecords) { DFA.State source = states[stateRecord.Index]; DFA.State target = states[edgeRecord.TargetIndex]; CharacterSetRecord charsetRec = content.CharacterSetTable[edgeRecord.CharacterSetIndex]; DFA.Transition transition = new DFA.Transition(target,charsetRec.Characters); source.Transitions.Add(transition); } } return states; }