Example #1
0
        public void gen_dfa(tree t)
        {
            t.get_leaves(t.root);
            //MessageBox.Show(t.leaves.Count.ToString());
            int   ctr = 1;
            dfa   r   = new dfa();
            state beg = new state();

            beg.name = t.root.firstpos;
            r.begin  = beg;
            r.states.Add(beg);
            Stack <state> steck   = new Stack <state>();
            String        transit = "";

            steck.Push(beg);
            while (steck.Count > 0)
            {
                //state dd = new state();
                state see = steck.Pop();
                foreach (var v in new HashSet <char>(t.leaves.Values))//for all symbols
                {
                    state dd = new state();
                    foreach (int elem in see.name)
                    {
                        if (t.leaves[elem] == v)
                        {
                            dd.name.UnionWith(t.followpos[elem - 1]);
                            //richTextBox1.Text += STR(t.followpos[elem - 1]) + "|||";
                        }
                    }
                    if (v != '#')
                    {
                        richTextBox1.Text += "d(" + STR(see.name) + "," + v + ")->" + STR(dd.name) + "\n";
                    }
                    bool b = r.states.Add(dd);
                    if (b)
                    {
                        steck.Push(dd);
                    }
                }
            }
            // richTextBox1.Text += transit;
        }
Example #2
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);
        }