public Parser(CFG grammar, Terminal syncTerm) { this.grammar = grammar; this.table = grammar.CreateLL1ParseTable(); this.start = grammar.StartSymbol; this.syncTerm = syncTerm; if (table == null) throw new Exception("GRAMMAR NOT LL(1)"); }
public Parser(CFG grammar, Terminal syncTerm) { this.grammar = grammar; this.table = grammar.CreateLL1ParseTable(); this.start = grammar.StartSymbol; this.syncTerm = syncTerm; if (table == null) { throw new Exception("GRAMMAR NOT LL(1)"); } }
// Builds an LL(1) parse table using the First and Follow sets for the grammar // returns null if CFG is not LL(1) public ParseTable CreateLL1ParseTable() { ComputeFirstSets(); ComputeFollowSets(); if (Program.debug) Console.WriteLine("========================================================="); ParseTable parseTable = new ParseTable(); // for each nonterminal in the grammar and each production rule for the nonterminal foreach (Nonterminal var in productions.Keys) { foreach (ISymbol[] varProduction in productions[var]) { if (Program.debug) Console.WriteLine("CFG: LL(1) table gen, var = " + var + " prod = " + SymbolsToString(varProduction)); ISet<Terminal> productionFirst = First(varProduction); // add an entry to the parse table for each terminal in the first set of the production foreach (Terminal term in productionFirst) { if (term != Terminal.EPSILON) { if (parseTable.Get(var, term) != null) // LL(1) violation { if (Program.debug) { Console.WriteLine("CFG: LL(1) violation at (First) [" + var + "," + term + "]"); Console.WriteLine("\tWas: " + SymbolsToString(parseTable.Get(var, term))); Console.WriteLine("\tNew: " + SymbolsToString(varProduction)); } return null; } else { parseTable.Add(var, term, varProduction); } } } // if epsilon can be derived from the production // add an entry to the parse table for each terminal in the follow set of the nonterminal as well if (productionFirst.Contains(Terminal.EPSILON)) { foreach (Terminal term in Follow(var)) { if (parseTable.Get(var, term) != null) // LL(1) violation { if (Program.debug) { Console.WriteLine("CFG: LL(1) violation at (Follow) [" + var + "," + term + "]"); Console.WriteLine("\tWas: " + SymbolsToString(parseTable.Get(var, term))); Console.WriteLine("\tNew: " + SymbolsToString(varProduction)); } return null; } else { parseTable.Add(var, term, varProduction); } } } } } if (Program.debug) foreach (Nonterminal var in nonterminals) foreach (Terminal term in terminals) if (parseTable.Get(var, term) != null) Console.WriteLine("CFG: LL(1) Table[" + var + "][" + term + "] = " + SymbolsToString(parseTable.Get(var, term))); return parseTable; }
// Builds an LL(1) parse table using the First and Follow sets for the grammar // returns null if CFG is not LL(1) public ParseTable CreateLL1ParseTable() { ComputeFirstSets(); ComputeFollowSets(); if (Program.debug) { Console.WriteLine("========================================================="); } ParseTable parseTable = new ParseTable(); // for each nonterminal in the grammar and each production rule for the nonterminal foreach (Nonterminal var in productions.Keys) { foreach (ISymbol[] varProduction in productions[var]) { if (Program.debug) { Console.WriteLine("CFG: LL(1) table gen, var = " + var + " prod = " + SymbolsToString(varProduction)); } ISet <Terminal> productionFirst = First(varProduction); // add an entry to the parse table for each terminal in the first set of the production foreach (Terminal term in productionFirst) { if (term != Terminal.EPSILON) { if (parseTable.Get(var, term) != null) // LL(1) violation { if (Program.debug) { Console.WriteLine("CFG: LL(1) violation at (First) [" + var + "," + term + "]"); Console.WriteLine("\tWas: " + SymbolsToString(parseTable.Get(var, term))); Console.WriteLine("\tNew: " + SymbolsToString(varProduction)); } return(null); } else { parseTable.Add(var, term, varProduction); } } } // if epsilon can be derived from the production // add an entry to the parse table for each terminal in the follow set of the nonterminal as well if (productionFirst.Contains(Terminal.EPSILON)) { foreach (Terminal term in Follow(var)) { if (parseTable.Get(var, term) != null) // LL(1) violation { if (Program.debug) { Console.WriteLine("CFG: LL(1) violation at (Follow) [" + var + "," + term + "]"); Console.WriteLine("\tWas: " + SymbolsToString(parseTable.Get(var, term))); Console.WriteLine("\tNew: " + SymbolsToString(varProduction)); } return(null); } else { parseTable.Add(var, term, varProduction); } } } } } if (Program.debug) { foreach (Nonterminal var in nonterminals) { foreach (Terminal term in terminals) { if (parseTable.Get(var, term) != null) { Console.WriteLine("CFG: LL(1) Table[" + var + "][" + term + "] = " + SymbolsToString(parseTable.Get(var, term))); } } } } return(parseTable); }