public static void resetAllVariebles() { //实例化全局XFA对象 globalNFA = new NFA(); globalDFA = new DFA(); globalMFA = new MFA(); }
//计算 a* 的NFA public static NFA NFAofA_Clouse(NFA A) { NFA newNFA = new NFA(); List <String> startState = new List <String>(); List <String> endState = new List <String>(); List <String> inputChar = new List <String>(); List <transformFunction> TF = new List <transformFunction>(); inputChar = A.NFA_INPUT.ToList();//输入符号与输入的NFA A相同 startState.Add(stateID.ToString()); stateID++; endState.Add(stateID.ToString()); stateID++; TF = A.NFA_Transform.ToList();//保留原NFA A的转换函数 //在原NFA A的转换函数基础上添加转换函数 TF.Add(new transformFunction(startState[0], "$", endState[0])); TF.Add(new transformFunction(startState[0], "$", A.NFA_START[0])); TF.Add(new transformFunction(A.NFA_END[0], "$", A.NFA_START[0])); TF.Add(new transformFunction(A.NFA_END[0], "$", endState[0])); //更新NFA newNFA.NFA_START = startState.ToArray(); newNFA.NFA_END = endState.ToArray(); newNFA.NFA_INPUT = inputChar.ToArray(); newNFA.NFA_Transform = TF.ToArray(); return(newNFA); }
//正规式转NFA //测试样例:r=(a|b)*(aa|bb)(a|b)* public static NFA REtoNFA(String Expression) { int i = 0; stateID = 0;//初始化stateID为0 Expression = addJoinOperator(Expression.Trim()); Expression = postfixExpression(Expression); //Expression = "aa|*aa+b|b++ab|*+"; //MessageBox.Show("生成的后缀表达式:\n"+Expression); Stack <NFA> NFAStack = new Stack <NFA>(); NFA A = null, B = null;//用于临时保存NFA while (i < Expression.Length) { char currentChar = Expression[i]; //如果当前字符为字母,则构造该字母的NFA,并将其入NFA栈 if (isLetter(currentChar)) { NFAStack.Push(NFAofA(currentChar)); } else { switch (currentChar) { case '*': A = NFAStack.Peek(); //MessageBox.Show(A.NFAInfo()); NFAStack.Pop(); NFAStack.Push(NFAofA_Clouse(A)); break; case '|': B = NFAStack.Peek(); //MessageBox.Show(B.NFAInfo()); NFAStack.Pop(); A = NFAStack.Peek(); //MessageBox.Show(A.NFAInfo()); NFAStack.Pop(); NFAStack.Push(NFAofA_Union_B(A, B)); break; case '+': B = NFAStack.Peek(); //MessageBox.Show(B.NFAInfo()); NFAStack.Pop(); A = NFAStack.Peek(); //MessageBox.Show(A.NFAInfo()); NFAStack.Pop(); NFAStack.Push(NFAofA_Join_B(A, B)); break; default: break; } } i++; } //栈顶NFA即为构造的最终NFA return(NFAStack.Peek()); }
//NFA运算测试(返回NFA信息) public static String NFATest() { stateID = 0; NFA A = NFAofA('a'); NFA B = NFAofA('b'); //NFA C = NFAofA_Union_B(A,B); NFA C = NFAofA_Join_B(A, B); return(C.NFAInfo()); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm mForm = new mainForm(); mForm.StartPosition = FormStartPosition.CenterScreen; //实例化全局XFA对象 globalNFA = new NFA(); globalDFA = new DFA(); globalMFA = new MFA(); Application.Run(mForm); }
//函数功能:将正规式r(RegularExpression)转化为等价的NFA private void REtoNFA() { //去除输入的空格 String Expression = editBox.Text.Replace(" ", ""); if (isRegularExpression(Expression) != null) { MessageBox.Show("正规式非法,请检查后重新输入!", "提示"); return; } if (Expression == "") { MessageBox.Show("无法生成NFA,请先输入正规式", "提示"); return; } NFA myNFA = REtoNFA_Helper.REtoNFA(Expression); //MessageBox.Show(myNFA.NFAInfo()); //赋值Program.cs相关变量 Program.globalNFA.NFA_INPUT = myNFA.NFA_INPUT; Program.globalNFA.NFA_START = myNFA.NFA_START; Program.globalNFA.NFA_END = myNFA.NFA_END; Program.globalNFA.NFA_Transform = myNFA.NFA_Transform; //NFA转化函数显示 listViewNFA.Items.Clear();//清除当前项目 foreach (transformFunction TFItem in myNFA.NFA_Transform) { ListViewItem item = new ListViewItem(); item.Text = TFItem.from; item.SubItems.Add(TFItem.by); item.SubItems.Add(TFItem.to); listViewNFA.Items.Add(item); } //显示初态集 String startState = ""; foreach (String item in myNFA.NFA_START) { startState = startState + item + ";"; } textBoxNFAStart.Text = startState; //显示终态集 String endState = ""; foreach (String item in myNFA.NFA_END) { endState = endState + item + ";"; } textBoxNFAEnd.Text = endState; }
//计算 a 的NFA public static NFA NFAofA(char a) { NFA newNFA = new NFA(); List <String> startState = new List <String>(); List <String> endState = new List <String>(); List <String> inputChar = new List <String>(); List <transformFunction> TF = new List <transformFunction>(); inputChar.Add(a.ToString()); startState.Add(stateID.ToString()); stateID++; endState.Add(stateID.ToString()); stateID++; TF.Add(new transformFunction(startState[0], a.ToString(), endState[0])); //更新NFA newNFA.NFA_START = startState.ToArray(); newNFA.NFA_END = endState.ToArray(); newNFA.NFA_INPUT = inputChar.ToArray(); newNFA.NFA_Transform = TF.ToArray(); return(newNFA); }
//计算 a|b 的NFA public static NFA NFAofA_Union_B(NFA A, NFA B) { NFA newNFA = new NFA(); List <String> startState = new List <String>(); List <String> endState = new List <String>(); List <String> inputChar = new List <String>(); List <transformFunction> TF = new List <transformFunction>(); inputChar = A.NFA_INPUT.ToList();//先把A的输入符号加入新的NFA输入符号集合 foreach (String item in B.NFA_INPUT) { if (!inputChar.Contains(item)) //若B的输入符号不在A中,则加入新的NFA输入符号集合 { inputChar.Add(item); } } startState.Add(stateID.ToString()); stateID++; endState.Add(stateID.ToString()); stateID++; TF = A.NFA_Transform.ToList();//先把A的转换函数加入新的NFA转换函数集合 foreach (transformFunction item in B.NFA_Transform) { if (!TF.Contains(item)) //若B的转换函数不在A中,则加入新的NFA转换函数集合 { TF.Add(item); } } //在原NFA A的转换函数基础上添加转换函数 TF.Add(new transformFunction(startState[0], "$", A.NFA_START[0])); TF.Add(new transformFunction(startState[0], "$", B.NFA_START[0])); TF.Add(new transformFunction(A.NFA_END[0], "$", endState[0])); TF.Add(new transformFunction(B.NFA_END[0], "$", endState[0])); //更新NFA newNFA.NFA_START = startState.ToArray(); newNFA.NFA_END = endState.ToArray(); newNFA.NFA_INPUT = inputChar.ToArray(); newNFA.NFA_Transform = TF.ToArray(); return(newNFA); }