Пример #1
0
        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);
        }
Пример #2
0
        /// <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));
        }
Пример #3
0
		/// <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;
		}
Пример #4
0
		/// <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);
			}
		}
Пример #5
0
		public void Add(State state)
		{
			list.Add(state);
		}
Пример #6
0
		/// <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;
		}
Пример #7
0
		/// <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;
		}
Пример #8
0
        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;
        }