Ejemplo n.º 1
0
        private void button3_Click(object sender, EventArgs e)
        {
            listView2.Items.Clear();
            button2_Click(词法编译ToolStripMenuItem, null);
            listView3.Items.Clear();
            dataGridView3.Rows.Clear();
            List <ERROR> el;

            if (Lex.getTokenList() != null)
            {
                tokenList = Lex.getTokenList();

                if (!Text.TrimEnd().Equals("") && tokenList != null)
                {
                    g = new GrammarAnalysis(Text);
                    g.getGrammarFromFile();
                    g.myParser(tokenList);
                    List <parseResult> rl = g.getResultList();
                    el = g.getErrorList();
                    List <string> threel = g.getThreeList();

                    Form f = new Form3(rl, el, tokenList, threel);
                    f.Show();
                }
            }
            foreach (ERROR em in g.getErrorList())
            {
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Clear();
                lvi.SubItems[0].Text = em.getErrorKind();
                lvi.SubItems.Add(em.getStrError());
                lvi.SubItems.Add(em.getRow().ToString());
                listView2.Items.Add(lvi);
                dataGridView3.Rows.Add(em.getErrorKind(), em.getStrError(), em.getRow().ToString());
            }



            List <string> threeli = g.getThreeList();

            dataGridView1.Rows.Clear();
            foreach (string term in threeli)
            {
                dataGridView1.Rows.Add(term);
            }
            Stack <SymbolTable> sm = g.getST();

            dataGridView2.Rows.Clear();
            foreach (SymbolTable sym in sm)
            {
                dataGridView2.Rows.Add(sym.getReturnType(), sym.getFun_name(), sym.getOffset());
            }
        }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            string s = GrammarEditor.Text;
            int    i = 0;

            listView1.Items.Clear();
            listView2.Items.Clear();
            listBox1.DataSource = null;
            listBox3.DataSource = null;
            nl = new HashSet <NonTerminalSymbol>();

            if (s.Trim() != "")
            {
                g = new GrammarAnalysis(s);

                //MessageBox.Show("sdjhj");
                int count = 1;
                foreach (Grammar gm in g.getGrammarFromFile())
                {
                    // MessageBox.Show(gm.getRightPart().Count+"");

                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems[0].Text = count + "";
                    lvi.SubItems.Add(gm.getLeftPart());
                    lvi.SubItems.Add(gm.getmGrammar());
                    string selectset = "";

                    foreach (string sm in gm.getSelect())
                    {
                        selectset += sm + " ";
                    }
                    lvi.SubItems.Add(selectset);
                    string df = "";

                    foreach (string ss in g.mapGet(gm.getLeftPart()).getFollowList())
                    {
                        df += ss + " ";
                    }
                    lvi.SubItems.Add(df);
                    df = "";
                    foreach (string sr in g.mapGet(gm.getLeftPart()).getFirst())
                    {
                        df += sr + " ";
                    }

                    lvi.SubItems.Add(df);

                    count++;
                    listView1.Items.Add(lvi);
                }



                listBox1.DataSource = g.getNquene();
                listBox3.DataSource = g.getTquene();
                nl = g.getNlist();

                FileStream   fs2 = new FileStream("expression.txt", FileMode.Create);
                StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);
                foreach (NonTerminalSymbol n in nl)
                {
                    HashSet <Table> t = n.getTable();

                    foreach (Table tt in t)
                    {
                        string        item1 = tt.getName();
                        List <string> s2    = tt.getExpression();
                        string        item2 = "";
                        foreach (string ss3 in s2)
                        {
                            item2 += ss3 + " ";
                        }

                        sw2.WriteLine(n.getValue() + "#" + item1 + "#" + item2);
                    }
                }


                sw2.Flush();
                sw2.Close();
                sw2.Dispose();
            }

            else
            {
                MessageBox.Show("无法进行文法分析。文法为空");
            }
        }