private void button7_Click(object sender, EventArgs e) { // 获取NFA信息 string startState = startSymbol; string[] endStates = endSymbol.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); List symbolSet = new List(this.symbolSet);

// 初始化DFA
Dictionary<string, Dictionary<string, string>> dfa = new Dictionary<string, Dictionary<string, string>>();
List<string> dfaStates = new List<string>();
List<string> unmarkedStates = new List<string>();
List<List<string>> stateSets = new List<List<string>>();

// 计算开始状态集
List<string> startStateSet = E_closure(new List<string>() { startState });
stateSets.Add(startStateSet);
dfaStates.Add("{" + string.Join(",", startStateSet) + "}");
unmarkedStates.Add("{" + string.Join(",", startStateSet) + "}");

// 计算其他状态集
while (unmarkedStates.Count > 0)
{
    string currentStateSet = unmarkedStates[0];
    unmarkedStates.RemoveAt(0);

    foreach (string s in symbolSet)
    {
        List<string> moveStates = Move(currentStateSet, s);
        List<string> nextStateSet = E_closure(moveStates);

        if (nextStateSet.Count > 0)
        {
            string nextStateSetName = "{" + string.Join(",", nextStateSet) + "}";

            if (!dfaStates.Contains(nextStateSetName))
            {
                dfaStates.Add(nextStateSetName);
                unmarkedStates.Add(nextStateSetName);
                stateSets.Add(nextStateSet);
            }

            if (!dfa.ContainsKey(currentStateSet))
            {
                dfa[currentStateSet] = new Dictionary<string, string>();
            }
            dfa[currentStateSet][s] = nextStateSetName;
        }
    }
}

// 计算终止状态集
List<string> endStateSet = new List<string>();
foreach (List<string> stateSet in stateSets)
{
    foreach (string endState in endStates)
    {
        if (stateSet.Contains(endState))
        {
            endStateSet.Add("{" + string.Join(",", stateSet) + "}");
            break;
        }
    }
}

// 生成DFA文件内容
string dfaContent = "开始符:" + dfaStates[0] + "\n";
dfaContent += "终结符:" + string.Join(";", endStateSet) + "\n";
dfaContent += "状态数:" + dfaStates.Count + "\n";
dfaContent += "符号集:" + string.Join(";", symbolSet) + "\n";

foreach (string state in dfaStates)
{
    foreach (string symbol in symbolSet)
    {
        if (dfa.ContainsKey(state) && dfa[state].ContainsKey(symbol))
        {
            string nextState = dfa[state][symbol];
            dfaContent += state + "\t" + symbol + "\t" + nextState + "\n";
        }
    }
}

// 保存DFA文件
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "DFA文件(*.dfa)|*.dfa";
saveFileDialog.FileName = "DFA.txt";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
    string fileName = saveFileDialog.FileName;
    File.WriteAllText(fileName, dfaContent, Encoding.UTF8);
}

}

// 计算状态集的ε闭包 private List E_closure(List states) { List result = new List(states);

Stack<string> stack = new Stack<string>(states);
while (stack.Count > 0)
{
    string state = stack.Pop();

    foreach (ListViewItem item in listView1.Items)
    {
        if (item.SubItems[0].Text == state && item.SubItems[1].Text == "ε")
        {
            string nextState = item.SubItems[2].Text;
            if (!result.Contains(nextState))
            {
                result.Add(nextState);
                stack.Push(nextState);
            }
        }
    }
}

return result;

}

// 计算状态集在输入符号下的后继状态集 private List Move(string stateSet, string symbol) { List result = new List();

string[] states = stateSet.Substring(1, stateSet.Length - 2).Split(',');
foreach (string state in states)
{
    foreach (ListViewItem item in listView1.Items)
    {
        if (item.SubItems[0].Text == state && item.SubItems[1].Text == symbol)
        {
            string nextState = item.SubItems[2].Text;
            if (!result.Contains(nextState))
            {
                result.Add(nextState);
            }
        }
    }
}

return result;
子集构造法使NFA转为DFA使用VSC#语言的具体算法补全函数private void button7_Clickobject sender EventArgs eusing System;using System;using SystemCollectionsGeneric;using SystemComponentModel;using SystemData;using SystemDrawi

原文地址: https://www.cveoy.top/t/topic/c2Ok 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录