Exemple #1
0
 public void printTable()
 {
     foreach (String[] row in table)
     {
         ParseTable.printRow(row);
     }
 }
Exemple #2
0
        static void Main(string[] args)
        {
            GrammarParser grammar = new GrammarParser(@"C:\Users\darkshot\source\repos\LL1Parser\ParserTests\Sample5.in");

            grammar.parse();
            var table = new ParseTable(grammar);

            table.printTable();


            InputLexer n = new InputLexer(@"C:\Users\darkshot\source\repos\LL1\FirstAndFollow\Input2.in", grammar);

            Console.WriteLine("__________________________________________________________");
            Console.WriteLine(string.Join(",", n.elements));
            var output = Parser.parser(grammar, n.elements);

            Parser.printOutput(output);
            Console.WriteLine();
        }
Exemple #3
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);
        }