NFA到DFA和DFA到MFA转换算法实现:C#语言代码示例
/**
-
NFA到DFA的转换 */ private void button7_Click(object sender, EventArgs e) { //获取NFA的开始状态 string[] startStates = startSymbol.Split(';').Select(x => x.Trim()).ToArray();
//获取NFA的终止状态 string[] endStates = endSymbol.Split(';').Select(x => x.Trim()).ToArray();
//获取NFA的符号集 string[] symbolSet = lines[2].Substring(lines[2].IndexOf(":") + 1).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
//创建DFA的开始状态 string startState = string.Join(",", startStates);
//创建DFA的终止状态集 List
dfaEndStates = new List (); //创建DFA的状态集 List
dfaStates = new List (); //创建DFA的转移函数 Dictionary<string, Dictionary<string, string>> dfaTransitions = new Dictionary<string, Dictionary<string, string>>();
//将NFA的开始状态加入DFA的状态集 dfaStates.Add(startState);
//创建一个队列,用于存放DFA的状态 Queue
queue = new Queue (); //将DFA的开始状态加入队列 queue.Enqueue(startState);
//循环处理队列中的状态 while (queue.Count > 0) { //取出队列中的状态 string currentState = queue.Dequeue();
//将当前状态按照逗号分隔,得到NFA的状态集 string[] nfaStates = currentState.Split(','); //创建一个字典,用于存放当前状态的转移函数 Dictionary<string, string> currentTransitions = new Dictionary<string, string>(); //循环处理NFA的符号集 foreach (string symbol in symbolSet) { //创建一个列表,用于存放当前符号下的所有状态 List<string> symbolStates = new List<string>(); //循环处理NFA的状态集 foreach (string nfaState in nfaStates) { //在NFA的转移函数中查找当前状态和当前符号下的所有状态 foreach (ListViewItem item in listView1.Items) { if (item.SubItems[0].Text == nfaState && item.SubItems[1].Text == symbol) { symbolStates.Add(item.SubItems[2].Text); } } } //去重 symbolStates = symbolStates.Distinct().ToList(); //将当前符号下的所有状态按照逗号连接,得到DFA的新状态 string newState = string.Join(",", symbolStates); //如果新状态不在DFA的状态集中,将其加入状态集和队列中 if (!dfaStates.Contains(newState) && !string.IsNullOrEmpty(newState)) { dfaStates.Add(newState); queue.Enqueue(newState); } //将当前符号下的所有状态和新状态加入当前状态的转移函数中 currentTransitions[symbol] = newState; } //将当前状态和转移函数加入DFA的转移函数中 dfaTransitions[currentState] = currentTransitions; //如果当前状态包含NFA的终止状态,将其加入DFA的终止状态集中 if (nfaStates.Intersect(endStates).Any()) { dfaEndStates.Add(currentState); }}
//创建DFA的结束状态 string endState = "终止状态集:" + string.Join(";", dfaEndStates);
//创建DFA的状态数 string stateCount = "状态数:" + dfaStates.Count;
//创建DFA的符号集 string symbols = "符号集:" + string.Join(";", symbolSet);
//创建DFA的文件内容 string dfaContent = startSymbol + "\n" + endState + "\n" + stateCount + "\n" + symbols + "\n";
//循环处理DFA的转移函数,将其加入文件内容中 foreach (string state in dfaTransitions.Keys) { foreach (string symbol in dfaTransitions[state].Keys) { string toState = dfaTransitions[state][symbol]; dfaContent += state + "\t" + symbol + "\t" + toState + "\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); }
MessageBox.Show("转换完成!"); }
/**
-
DFA到MFA的转换 */ private void button9_Click(object sender, EventArgs e) { //获取DFA的开始状态 string[] startStates = startSymbol.Split(';').Select(x => x.Trim()).ToArray();
//获取DFA的终止状态集 string[] endStates = endSymbol.Split(';').Select(x => x.Trim()).ToArray();
//获取DFA的符号集 string[] symbolSet = lines[3].Substring(lines[3].IndexOf(":") + 1).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
//创建MFA的开始状态 string startState = string.Join(",", startStates);
//创建MFA的终止状态集 List
mfaEndStates = new List (); //创建MFA的状态集 List
mfaStates = new List (); //创建MFA的转移函数 Dictionary<string, Dictionary<string, string>> mfaTransitions = new Dictionary<string, Dictionary<string, string>>();
//将DFA的开始状态加入MFA的状态集 mfaStates.Add(startState);
//创建一个队列,用于存放MFA的状态 Queue
queue = new Queue (); //将MFA的开始状态加入队列 queue.Enqueue(startState);
//循环处理队列中的状态 while (queue.Count > 0) { //取出队列中的状态 string currentState = queue.Dequeue();
//将当前状态按照逗号分隔,得到DFA的状态集 string[] dfaStates = currentState.Split(','); //创建一个字典,用于存放当前状态的转移函数 Dictionary<string, string> currentTransitions = new Dictionary<string, string>(); //循环处理DFA的符号集 foreach (string symbol in symbolSet) { //创建一个列表,用于存放当前符号下的所有状态 List<string> symbolStates = new List<string>(); //循环处理DFA的状态集 foreach (string dfaState in dfaStates) { //在DFA的转移函数中查找当前状态和当前符号下的所有状态 foreach (ListViewItem item in listView2.Items) { if (item.SubItems[0].Text == dfaState && item.SubItems[1].Text == symbol) { symbolStates.Add(item.SubItems[2].Text); } } } //去重 symbolStates = symbolStates.Distinct().ToList(); //将当前符号下的所有状态按照逗号连接,得到MFA的新状态 string newState = string.Join(",", symbolStates); //如果新状态不在MFA的状态集中,将其加入状态集和队列中 if (!mfaStates.Contains(newState) && !string.IsNullOrEmpty(newState)) { mfaStates.Add(newState); queue.Enqueue(newState); } //将当前符号下的所有状态和新状态加入当前状态的转移函数中 currentTransitions[symbol] = newState; } //将当前状态和转移函数加入MFA的转移函数中 mfaTransitions[currentState] = currentTransitions; //如果当前状态包含DFA的终止状态,将其加入MFA的终止状态集中 if (dfaStates.Intersect(endStates).Any()) { mfaEndStates.Add(currentState); }}
//创建MFA的结束状态 string endState = "终止状态集:" + string.Join(";", mfaEndStates);
//创建MFA的状态数 string stateCount = "状态数:" + mfaStates.Count;
//创建MFA的符号集 string symbols = "符号集:" + string.Join(";", symbolSet);
//创建MFA的文件内容 string mfaContent = startSymbol + "\n" + endState + "\n" + stateCount + "\n" + symbols + "\n";
//循环处理MFA的转移函数,将其加入文件内容中 foreach (string state in mfaTransitions.Keys) { foreach (string symbol in mfaTransitions[state].Keys) { string toState = mfaTransitions[state][symbol]; mfaContent += state + "\t" + symbol + "\t" + toState + "\n"; } }
//保存MFA文件 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "MFA文件(.mfa)|.mfa"; saveFileDialog.FileName = "MFA.txt"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string fileName = saveFileDialog.FileName; File.WriteAllText(fileName, mfaContent, Encoding.UTF8); }
MessageBox.Show("转换完成!");
原文地址: https://www.cveoy.top/t/topic/kp3n 著作权归作者所有。请勿转载和采集!