Beispiel #1
0
        public static void dollarPrsing(Stack <String> pda, GrammarParser grammar, string[][] table)
        {
            while (pda.Count > 0)
            {
                for (int i = 0; i < table.Length; i++)
                {
                    for (int j = 0; j < table[i].Length; j++)
                    {
                        if (table[0][j] == "$")
                        {
                            if (table[i][j] != null && table[i][0] != "")
                            {
                                if (pda.Count > 0 && table[i][0] == pda.Peek())
                                {
                                    Derivations d = new Derivations();
                                    d.input      = "$";
                                    d.topOfStack = pda.Pop();
                                    d.output     = table[i][j];

                                    List <String> temp = new List <String>();
                                    temp.Add(d.output);
                                    List <List <String> > productions = splitProduction(temp, grammar);

                                    for (int k = productions[0].Count - 1; k >= 0; k--)
                                    {
                                        if (productions[0][k] != "!")
                                        {
                                            pda.Push(productions[0][k]);
                                        }
                                    }
                                    output.Add(d);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public static List <Derivations> parser(GrammarParser grammar, List <String> input)
        {
            var            table = new ParseTable(grammar).table;
            Stack <String> pda   = new Stack <String>();
            bool           error = false;
            bool           first = true;

            for (int a = 0; a < input.Count; a++)
            {
                bool done = false;
                while (!done)
                {
                    if (error)
                    {
                        break;
                    }

                    for (int i = 0; i < table.Length; i++)
                    {
                        for (int j = 0; j < table[i].Length; j++)
                        {
                            String var = input[a];
                            if (table[0][j] == var)
                            {
                                if (!(table[i][0] == ""))
                                {
                                    if (table[i][j] != null && first)
                                    {
                                        pda.Push(table[i][0]);
                                        first = false;
                                    }

                                    if (table[i][j] != null && pda.Count > 0 && !first && !(table[i][j] == var) &&
                                        table[i][0] == pda.Peek())
                                    {
                                        Derivations d = new Derivations();
                                        d.input      = var;
                                        d.topOfStack = pda.Pop();
                                        d.output     = table[i][j];

                                        List <String> temp = new List <String>();
                                        temp.Add(d.output);
                                        List <List <String> > productions = splitProduction(temp, grammar);

                                        for (int k = productions[0].Count - 1; k >= 0; k--)
                                        {
                                            if (productions[0][k] != "!")
                                            {
                                                pda.Push(productions[0][k]);
                                            }
                                        }

                                        output.Add(d);
                                    }
                                    if (table[i][j] != null && pda.Count > 0 && (!first && table[i][j] == var) &&
                                        table[i][0] == pda.Peek())
                                    {
                                        Derivations d = new Derivations();
                                        d.input      = var;
                                        d.output     = table[i][j];
                                        d.topOfStack = pda.Pop();

                                        List <String> temp = new List <String>();
                                        temp.Add(d.output);
                                        List <List <String> > productions = splitProduction(temp, grammar);
                                        for (int k = productions[0].Count - 1; k >= 0; k--)
                                        {
                                            if (productions[0][k] != "!")
                                            {
                                                pda.Push(productions[0][k]);
                                            }
                                        }

                                        output.Add(d);
                                    }
                                    if (table[i][j] != null && grammar.terminals.Contains(pda.Peek()) &&
                                        pda.Peek() == var)
                                    {
                                        Derivations d = new Derivations();
                                        d.input      = "";
                                        d.output     = pda.Peek();
                                        d.topOfStack = pda.Pop();
                                        done         = true;
                                        output.Add(d);
                                    }
                                    if (table[i][j] == null && table[0][j] == var && table[i][0] == pda.Peek() &&
                                        !done)
                                    {
                                        error = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (!error)
            {
                dollarPrsing(pda, grammar, table);
            }
            if (error)
            {
                output = null;
            }
            return(output);
        }