Ejemplo n.º 1
0
        void create_parse_tree(String s)
        {
            int          idctr = 1;
            Stack <node> st    = new Stack <node>();

            foreach (char c in s)
            {
                if (Char.IsLetterOrDigit(c) || c == '#')
                {
                    node tmp = new node(c);
                    tmp.ID = idctr;
                    tmp.lastpos.Add(idctr);
                    tmp.firstpos.Add(idctr++);
                    //tmp.left = null; tmp.right = null;
                    st.Push(tmp);
                }
                else
                {
                    if (c == '*')
                    {
                        node d   = st.Pop();
                        node now = new node(c);
                        now.firstpos = d.firstpos;
                        now.lastpos  = d.lastpos;
                        now.left     = d;
                        now.nullable = true;
                        //richTextBox1.Text += "\n*:"+now.lastpos.Count.ToString() + "_" + now.firstpos.Count.ToString();
                        st.Push(now);
                    }
                    if (c == '|' || c == '+')
                    {
                        node n1  = st.Pop();
                        node n2  = st.Pop();
                        node now = new node(c);
                        now.left  = n2;
                        now.right = n1;
                        now.firstpos.UnionWith(n1.firstpos);
                        now.firstpos.UnionWith(n2.firstpos);
                        now.lastpos.UnionWith(n1.lastpos);
                        now.lastpos.UnionWith(n2.lastpos);
                        now.nullable = n1.nullable || n2.nullable;
                        //richTextBox1.Text += "\n|:" + now.lastpos.Count.ToString() + "_" + now.firstpos.Count.ToString();
                        st.Push(now);
                    }
                    if (c == '.')
                    {
                        node n1  = st.Pop();
                        node n2  = st.Pop();
                        node now = new node(c);
                        now.left     = n2;
                        now.right    = n1;
                        now.nullable = n1.nullable && n2.nullable;
                        if (n2.nullable)
                        {
                            now.firstpos.UnionWith(n1.firstpos);
                            now.firstpos.UnionWith(n2.firstpos);
                        }
                        else
                        {
                            now.firstpos.UnionWith(n2.firstpos);
                        }
                        if (n1.nullable)
                        {
                            now.lastpos.UnionWith(n1.lastpos);
                            now.lastpos.UnionWith(n2.lastpos);
                        }
                        else
                        {
                            now.lastpos.UnionWith(n1.lastpos);
                        }
                        //richTextBox1.Text += "\n.:" + now.lastpos.Count.ToString() + "_" + now.firstpos.Count.ToString();
                        st.Push(now);
                    }
                }
            }
            tree tr = new tree(st.Pop());

            tr.inorder(tr.root);
            tr.followpos = new HashSet <int> [idctr - 1];
            for (int p = 0; p < idctr - 1; p++)
            {
                tr.followpos[p] = new HashSet <int>();
            }
            tr.follow_pos(tr.root);
            // MessageBox.Show((idctr - 1).ToString());
            Console.WriteLine();
            richTextBox1.Text += "\nfirstpos of root:  ";
            foreach (int x in tr.root.firstpos)
            {
                richTextBox1.Text += x.ToString() + "_";
            }
            richTextBox1.Text += "\nendpos of root:  ";
            foreach (int x in tr.root.lastpos)
            {
                richTextBox1.Text += x.ToString() + "_";
            }
            String fol = "";

            for (int h = 0; h < tr.followpos.Length; h++)
            {
                fol += "\nfollowpos(" + (h + 1) + ")=";
                foreach (var set in tr.followpos[h])
                {
                    fol += set + ",";
                }
            }
            richTextBox1.Text += fol + "\n";
            gen_dfa(tr);
        }
Ejemplo n.º 2
0
 public tree(node n)
 {
     root = n;
 }