Example #1
0
        public TreeNode parse(List <Token> tokens)
        {
            Stack <LR0State> stateStack = new Stack <LR0State>();
            Stack <TreeNode> nodeStack  = new Stack <TreeNode>();

            stateStack.Push(g.LR0DFAStartState);
            nodeStack.Push(new TreeNode("S'"));

            int ti = 0;

            while (true)
            {
                LR0State s = stateStack.Peek();
                string   t = tokens[ti].symbol;
                if (!table.ContainsKey(s))
                {
                    throw new SystemException("Table does not contain item.");
                }
                if (!table[s].ContainsKey(t))
                {
                    throw new SystemException("Table does not contain item.");
                }
                List <string> action = table[s][t];
                if (action[0] == "S")
                {
                    stateStack.Push(g.states[Int32.Parse(action[1])]);
                    nodeStack.Push(new TreeNode(t, tokens[ti]));
                    ti++;
                }
                else
                {
                    int      numpop   = Int32.Parse(action[1]);
                    string   reduceTo = action[2];
                    TreeNode n        = new TreeNode(reduceTo);
                    for (int d = 0; d < numpop; d++)
                    {
                        s = stateStack.Pop();
                        n.Children.Insert(0, nodeStack.Peek());
                        nodeStack.Pop();
                    }
                    if (reduceTo == "S'")
                    {
                        if (t == "$")
                        {
                            return(n);
                        }
                        else
                        {
                            throw new SystemException("Yo Grammar is Wrong...");
                        }
                    }
                    s = stateStack.Peek();
                    stateStack.Push(g.states[Int32.Parse(table[s][reduceTo][1])]);
                    nodeStack.Push(n);
                }
            }
            throw new Exception("Internal Compiler Error");
        }
Example #2
0
        private void CalculateLR0DFA()
        {
            List <string> l = new List <string>();

            l.Add(this.nonterminals[0].identifier);

            LR0State Q = new LR0State();

            Q.Items.Add(new LR0Item(this.nonterminals[0].identifier, l, 0));

            HashSet <LR0Item> S = new HashSet <LR0Item>();

            S.Add(new LR0Item("S'", l, 0));
            LR0State        startState = new LR0State(this.ComputeClosure(S));
            List <LR0State> todo       = new List <LR0State>();

            todo.Add(startState);

            Dictionary <HashSet <LR0Item>, LR0State> seen = new Dictionary <HashSet <LR0Item>, LR0State>(new EQ());

            seen[startState.Items] = startState;

            states = new List <LR0State>();
            while (todo.Count > 0)
            {
                Q = todo[0];
                todo.RemoveAt(0);
                if (states.Contains(Q))
                {
                    continue;
                }
                states.Add(Q);

                Dictionary <string, HashSet <LR0Item> > transitions = this.ComputeTransitions(Q);

                foreach (string sym in transitions.Keys)
                {
                    HashSet <LR0Item> I2 = ComputeClosure(transitions[sym]);
                    if (!seen.ContainsKey(I2))
                    {
                        LR0State Q2 = new LR0State(I2);
                        seen[I2] = Q2;
                        todo.Add(Q2);
                    }
                    Q.Transitions[sym] = seen[I2];
                }
            }

            for (int j = 0; j < states.Count; j++)
            {
                states[j].index = j;
            }

            this.LR0DFAStartState = startState;
        }
Example #3
0
        private Dictionary <string, HashSet <LR0Item> > ComputeTransitions(LR0State Q)
        {
            Dictionary <string, HashSet <LR0Item> > result = new Dictionary <string, HashSet <LR0Item> >();

            foreach (LR0Item item in Q.Items)
            {
                if (!item.DposAtEnd())
                {
                    string sym = item.Rhs[item.Dpos];
                    if (!result.ContainsKey(sym))
                    {
                        result[sym] = new HashSet <LR0Item>();
                    }

                    result[sym].Add(new LR0Item(item.Lhs, item.Rhs, item.Dpos + 1));
                }
            }

            return(result);
        }
Example #4
0
        void computeLRTable()
        {
            HashSet <LR0State> visitedStates = new HashSet <LR0State>();
            List <LR0State>    states        = new List <LR0State>();

            states.Add(g.LR0DFAStartState);

            while (states.Count != 0)
            {
                LR0State s = states[0];
                states.Remove(s);

                Dictionary <string, List <string> > row = new Dictionary <string, List <string> >();
                foreach (string sym in s.Transitions.Keys)
                {
                    bool IsTerminal = false;
                    foreach (Terminal t in g.terminals)
                    {
                        if (sym == t.symbol)
                        {
                            IsTerminal = true;
                            break;
                        }
                    }

                    if (IsTerminal)
                    {
                        List <String> contents = new List <string>();
                        contents.Add("S");
                        contents.Add(s.Transitions[sym].index.ToString());
                        row[sym] = contents;
                    }
                    else
                    {
                        List <String> contents = new List <string>();
                        contents.Add("T");
                        contents.Add(s.Transitions[sym].index.ToString());
                        row[sym] = contents;
                    }
                }

                foreach (var i in s.Items)
                {
                    if (i.DposAtEnd() && g.follows.ContainsKey(i.Lhs))
                    {
                        foreach (string w in g.follows[i.Lhs])
                        {
                            List <String> contents = new List <string>();
                            contents.Add("R");
                            contents.Add(i.Rhs.Count.ToString());
                            contents.Add(i.Lhs);
                            row[w] = contents;
                        }
                    }
                }

                table.Add(s, row);

                foreach (var a in s.Transitions)
                {
                    if (!visitedStates.Contains(a.Value))
                    {
                        visitedStates.Add(a.Value);
                        states.Add(a.Value);
                    }
                }
            }
        }