Ejemplo n.º 1
0
        private void button5_Click(object sender, EventArgs e)
        {
            Boolean jieshu = false;
            //int serial = 0;
            int lie = 0;

            if (d_serial == 0)
            {
                listView3.Items.Clear();
                d_source = textBox2.Text.Trim() + '#';
                d_sysk.Push('#');
                d_state_stack.Add(0);
                write_hang(d_serial, d_state_stack, d_sysk, "", d_source, "初始状态");
                d_serial++;
            }
            else
            {
                string shuoming  = "";
                string css       = "";
                char   shou_char = d_source[0];
                for (int a = 0; a < char_form.Count; a++)
                {
                    if (shou_char == char_form[a])
                    {
                        lie = a;
                        break;
                    }
                }

                string ff = erwei[d_state_stack[d_state_stack.Count - 1]][lie];
                if (ff == null)
                {
                    d_flag_wan = true;
                }
                else if (ff[0] == 'S')
                {
                    ff = ff.Remove(0, 1);
                    d_state_stack.Add(Convert.ToInt16(ff));
                    d_sysk.Push(shou_char);
                    d_source = d_source.Remove(0, 1);

                    shuoming = ff + "移进状态栈" + shou_char.ToString() + "移进符号栈";
                }
                else if (ff[0] == 'a')
                {
                    shuoming = "接受";
                    jieshu   = true;
                }
                else if (ff[0] == 'r')
                {
                    ff = ff.Remove(0, 1);
                    int ba  = Convert.ToInt16(ff);
                    int vtl = ItemC[ba].VT.Length;

                    css = ItemC[ba].VN + "->" + ItemC[ba].VT;
                    for (int ide = 0; ide < vtl; ide++)
                    {
                        d_sysk.Pop();
                        d_state_stack.RemoveAt(d_state_stack.Count - 1);
                    }

                    d_sysk.Push(ItemC[ba].VN);

                    for (int a = 0; a < char_form.Count; a++)
                    {
                        if (ItemC[ba].VN == char_form[a])
                        {
                            lie = a;
                            break;
                        }
                    }
                    d_state_stack.Add(Convert.ToInt16(erwei[d_state_stack[d_state_stack.Count - 1]][lie]));
                    shuoming = "规约";
                }
                else
                {
                    d_flag_wan = true;
                }

                write_hang(d_serial, d_state_stack, d_sysk, css, d_source, shuoming);
                d_serial++;
            }


            if (d_flag_wan)
            {
                MessageBox.Show("分析失败,不是该文法的句子!");
                d_flag_wan = false;
                d_serial   = 0;
                d_source   = "";
                d_sysk.out_clear();
                d_state_stack.Clear();
            }
            else
            {
                if (jieshu)
                {
                    MessageBox.Show("分析完成");
                    d_serial = 0;
                    d_source = "";
                    d_sysk.out_clear();
                    d_state_stack.Clear();
                }
            }
        }
Ejemplo n.º 2
0
        private void button6_Click(object sender, EventArgs e)
        {
            SymbolStack sysk        = new SymbolStack();
            List <int>  state_stack = new List <int>();

            listView3.Items.Clear();
            string  source_text = textBox2.Text.Trim() + '#';
            Boolean jieshu      = false;
            Boolean flag_wan    = false;
            int     serial      = 0;
            int     lie         = 0;

            sysk.Push('#');
            state_stack.Add(0);
            write_hang(serial, state_stack, sysk, "", source_text, "初始状态");
            while (!(jieshu))
            {
                serial++;
                string shuoming  = "";
                string css       = "";
                char   shou_char = source_text[0];
                for (int a = 0; a < char_form.Count; a++)
                {
                    if (shou_char == char_form[a])
                    {
                        lie = a;
                        break;
                    }
                }

                string ff = erwei[state_stack[state_stack.Count - 1]][lie];
                if (ff == null)
                {
                    flag_wan = true;
                    break;
                }
                else if (ff[0] == 'S')
                {
                    ff = ff.Remove(0, 1);
                    state_stack.Add(Convert.ToInt16(ff));
                    sysk.Push(shou_char);
                    source_text = source_text.Remove(0, 1);

                    shuoming = ff + "移进状态栈" + shou_char.ToString() + "移进符号栈";
                }
                else if (ff[0] == 'a')
                {
                    shuoming = "接受";
                    jieshu   = true;
                }
                else if (ff[0] == 'r')
                {
                    ff = ff.Remove(0, 1);
                    int ba  = Convert.ToInt16(ff);
                    int vtl = ItemC[ba].VT.Length;

                    css = ItemC[ba].VN + "->" + ItemC[ba].VT;
                    for (int ide = 0; ide < vtl; ide++)
                    {
                        sysk.Pop();
                        state_stack.RemoveAt(state_stack.Count - 1);
                    }

                    sysk.Push(ItemC[ba].VN);

                    for (int a = 0; a < char_form.Count; a++)
                    {
                        if (ItemC[ba].VN == char_form[a])
                        {
                            lie = a;
                            break;
                        }
                    }
                    state_stack.Add(Convert.ToInt16(erwei[state_stack[state_stack.Count - 1]][lie]));
                    shuoming = "规约";
                }
                else
                {
                    flag_wan = true;
                    break;
                    //MessageBox.Show("分析失败,不是该文法的句子!");
                }

                write_hang(serial, state_stack, sysk, css, source_text, shuoming);
            }
            if (flag_wan)
            {
                MessageBox.Show("分析失败,不是该文法的句子!");
            }
        }