using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection.Emit; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace byyljxfzxt { public partial class Form3 : Form { public Form3() { InitializeComponent(); }

    string[] lines;
    string startSymbol;
    string endSymbol;
    string[] symbolSet;
    string[] endStates;

    private void button5_Click(object sender, EventArgs e)
    {
        // 获取开始符和终结符
        string startSym = '开始符:' + startSymbol;
        string endSym = '终结符:' + endSymbol;

        // 初始化NFA文件内容
        string nfaContent = startSym + "\n" + endSym + "\n";

        // 遍历listView1中的数据,生成NFA文件内容
        for (int i = 0; i < listView1.Items.Count; i++)
        {
            string startState = listView1.Items[i].SubItems[0].Text;
            string symbol = listView1.Items[i].SubItems[1].Text;
            string endState = listView1.Items[i].SubItems[2].Text;

            // 拼接NFA文件内容
            nfaContent += startState + "\t" + symbol + "\t" + endState + "\n";
        }

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

    private void label7_Click(object sender, EventArgs e)
    {

    }


    private void button3_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "NFA Files|*.nfa";
        openFileDialog1.Title = "Select an NFA File";

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            lines = File.ReadAllLines(openFileDialog1.FileName);
            startSymbol = lines[0].Substring(lines[0].IndexOf(':') + 1).Trim();
            endSymbol = lines[1].Substring(lines[1].IndexOf(':') + 1).Trim();
            symbolSet = lines[2].Substring(lines[2].IndexOf(':') + 1).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 3; i < lines.Length; i++)
            {
                string[] tokens = lines[i].Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                string startState = tokens[0];
                string inputSymbol = tokens[1];
                string nextState = tokens[2];

                ListViewItem item = new ListViewItem(startState);
                item.SubItems.Add(inputSymbol);
                item.SubItems.Add(nextState);

                listView1.Items.Add(item);
            }
        }
        label3.Text = "初始状态集:" + startSymbol;
        label4.Text = "终止状态集:" + endSymbol;
        button6.Enabled = false;
        button7.Enabled = true;
    }

    private void button6_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "DFA文件|*.dfa";
        openFileDialog1.Title = "选择DFA文件";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            lines = File.ReadAllLines(openFileDialog1.FileName);
            startSymbol = lines[0].Split(':').Last().Trim();
            endStates = lines[1].Split(':').Last().Split(';').Select(x => x.Trim()).ToArray();
            int maxState = int.Parse(lines[2].Split(':').Last().Trim());
            string[] symbols = lines[3].Split(':').Last().Split(';').Select(x => x.Trim()).ToArray();
            Dictionary<string, Dictionary<string, string>> transitions = new Dictionary<string, Dictionary<string, string>>();
            for (int i = 4; i < lines.Length; i++)
            {
                string[] parts = lines[i].Split('\t');
                string fromState = parts[0].Trim();
                string symbol = parts[1].Trim();
                string toState = parts[2].Trim();
                if (!transitions.ContainsKey(fromState))
                {
                    transitions[fromState] = new Dictionary<string, string>();
                }
                transitions[fromState][symbol] = toState;
            }
            listView2.Items.Clear();
            foreach (string state in transitions.Keys)
            {
                foreach (string symbol in transitions[state].Keys)
                {
                    string toState = transitions[state][symbol];
                    ListViewItem item = new ListViewItem(new[] { state, symbol, toState });
                    listView2.Items.Add(item);
                }
            }
        }
        label5.Text = "初始状态集:" + startSymbol;
        label7.Text = "终止状态集:";
        for (int i = 0; i < endStates.Length - 1 && !string.IsNullOrEmpty(endStates[i]); i++)
             label7.Text = label7.Text + endStates[i] + ";";
        button7.Enabled = false;
        button9.Enabled = true;
    }

    private void button8_Click(object sender, EventArgs e)
    {
        // 获取开始符和终结符
        string startSym = "开始符:" + startSymbol + ";";
        string endSym = "终结符:";
        for (int i = 0; i < endStates.Length - 1; i++)
            endSym = endSym + endStates[i] + ";";

        // 初始化DFA文件内容
        string nfaContent = startSym + "\n" + endSym + "\n";

        // 遍历listView1中的数据,生成DFA文件内容
        for (int i = 0; i < listView2.Items.Count; i++)
        {
            string startState = listView2.Items[i].SubItems[0].Text;
            string symbol = listView2.Items[i].SubItems[1].Text;
            string endState = listView2.Items[i].SubItems[2].Text;

            // 拼接DFA文件内容
            nfaContent += startState + "\t" + symbol + "\t" + endState + "\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, nfaContent, Encoding.UTF8);
        }
    }


    private void button7_Click(object sender, EventArgs e)//NFA->DFA
    {
        // 1.确定字母表
        // 2.对初始状态进行 e-closure 运算,得到 DFA 起始状
        // 3.开始子集构造
        // 标记已经加入到 DFA 中的状态
        // 待处理的状态集合队列

        //判断markedSet中是否已经存在U
        // 生成新状态
        // 判断一个状态集合是否为终止状态
    
        // 获取某个状态集合通过 symbol 转移的结果
    
        // e-closure 运算
         // e-closure 运算
    }


}

} 补全函数private void button7_Click(object sender, EventArgs e)//NFA->DFA内容:private void button7_Click(object sender, EventArgs e)//NFA->DFA { // 1.确定字母表 List alphabet = new List(); foreach (string symbol in symbolSet) { if (symbol != "ε") { alphabet.Add(symbol); } }

// 2.对初始状态进行 e-closure 运算,得到 DFA 起始状态
string[] startStates = startSymbol.Split(';');
List<string> dfaStartStateList = new List<string>();
foreach (string state in startStates)
{
    dfaStartStateList.AddRange(EClosure(state));
}
string dfaStartState = string.Join(",", dfaStartStateList);

// 3.开始子集构造
List<string> markedSet = new List<string>();
Dictionary<string, string> dfaTransitions = new Dictionary<string, string>();
Queue<string> stateQueue = new Queue<string>();
stateQueue.Enqueue(dfaStartState);
markedSet.Add(dfaStartState);

while (stateQueue.Count > 0)
{
    string currentState = stateQueue.Dequeue();
    foreach (string symbol in alphabet)
    {
        string[] currentStates = currentState.Split(',');
        List<string> nextStates = new List<string>();
        foreach (string state in currentStates)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.SubItems[0].Text == state && item.SubItems[1].Text == symbol)
                {
                    string nextState = item.SubItems[2].Text;
                    nextStates.AddRange(EClosure(nextState));
                }
            }
        }
        string nextStateStr = string.Join(",", nextStates);
        if (!markedSet.Contains(nextStateStr))
        {
            markedSet.Add(nextStateStr);
            stateQueue.Enqueue(nextStateStr);
        }
        dfaTransitions[currentState + "," + symbol] = nextStateStr;
    }
}

// 4.生成 DFA 终止状态
List<string> dfaEndStates = new List<string>();
foreach (string state in markedSet)
{
    string[] states = state.Split(',');
    foreach (string endState in endStates)
    {
        if (states.Contains(endState))
        {
            dfaEndStates.Add(state);
            break;
        }
    }
}

// 5.将 DFA 转换结果显示在 listView2 中
listView2.Items.Clear();
foreach (string state in markedSet)
{
    foreach (string symbol in alphabet)
    {
        string nextState = dfaTransitions[state + "," + symbol];
        ListViewItem item = new ListViewItem(new[] { state, symbol, nextState });
        listView2.Items.Add(item);
    }
}

// 显示 DFA 的起始状态和终止状态
label5.Text = "初始状态集:" + dfaStartState;
label7.Text = "终止状态集:";
for (int i = 0; i < dfaEndStates.Count; i++)
{
    label7.Text += dfaEndStates[i];
    if (i < dfaEndStates.Count - 1)
    {
        label7.Text += ";";
    }
}
button9.Enabled = true;

}

// e-closure 运算 private List EClosure(string state) { List closure = new List(); closure.Add(state); foreach (ListViewItem item in listView1.Items) { if (item.SubItems[0].Text == state && item.SubItems[1].Text == "ε") { string nextState = item.SubItems[2].Text; closure.AddRange(EClosure(nextState)); } } return closure.Distinct().ToList();

NFA to DFA Converter - Convert Your Finite Automata Easily

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

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