Exemple #1
0
        /// <summary>
        /// 填充 DFA 的数据。
        /// </summary>
        /// <param name="grammar">词法分析器使用的语法。</param>
        /// <param name="useTrailing">是否用到了向前看。</param>
        private void FillDfa(Grammar <T> grammar, out bool useTrailing)
        {
            // 构造 DFA。
            Nfa nfa     = BuildNfa(grammar, out useTrailing);
            int headCnt = this.contextCount * 2;
            Dfa dfa     = nfa.BuildDfa(headCnt);

            dfa.Minimize(headCnt);
            // 获取 DFA 的数据。
            this.charClass      = dfa.CharClass.GetCharClassMap();
            this.stateCount     = dfa.Count;
            this.states         = new StateData[stateCount];
            this.charClassCount = dfa.CharClass.Count;
            for (int i = 0; i < this.stateCount; i++)
            {
                DfaState state       = dfa[i];
                int[]    transitions = new int[charClassCount];
                for (int j = 0; j < charClassCount; j++)
                {
                    DfaState target = state[j];
                    if (target == null)
                    {
                        transitions[j] = Constants.DeadState;
                    }
                    else
                    {
                        transitions[j] = target.Index;
                    }
                }
                states[i] = new StateData(transitions, state.SymbolIndex);
            }
        }