private static void AnalyseProgram(Grammar g, Dictionary <string, Dictionary <string, Rule> > parseTable) { Console.Write("Give path to program: "); string path = Console.ReadLine(); string program = File.ReadAllText(path); SymbolTable sti = new SymbolTable(); SymbolTable stc = new SymbolTable(); ProgramInternalForm pif = new ProgramInternalForm(); LexicalAnalyzer.Analyze(program, sti, stc, pif); Console.WriteLine(pif); List <int> result = ParsingAlgorithm.LL1(parseTable, g, pif); if (result != null) { Console.WriteLine("Sequence accepted!\n"); foreach (int index in result) { Console.Write(index + " "); } Console.WriteLine("\n"); } else { Console.WriteLine("Sequence NOT accepted!\n"); } }
public static void CheckWord(string word, int line, SymbolTable sti, SymbolTable stc, ProgramInternalForm pif) { if (word != "") { if (isReservedWord(word)) { //int code; //reservedWords.TryGetValue(word, out code); pif.Add(codeTable[word], -1, -1); } else if (isSeparator(word)) { pif.Add(codeTable[word], -1, -1); } else if (isOperator(word)) { pif.Add(codeTable[word], -1, -1); } else if (isIdentifier(word)) { CodePositionPair p = sti.Add(new Token(word), 0); pif.Add(p); } else if (isConstant(word)) { CodePositionPair p = stc.Add(new Token(word), 1); pif.Add(p); } else { throw new Exception("Error at line " + line + ": unknown word \"" + word + "\""); } } }
public static void Analyze(string sourceCode, SymbolTable sti, SymbolTable stc, ProgramInternalForm pif) { bool isQuoteOpen = false; string aux = ""; bool isOp = false; string code = ""; foreach (string l in sourceCode.Split('\n')) { line++; if (isQuoteOpen) { throw new Exception("Error at line " + line + ": new line not allowed here."); } CheckWord(aux, line, sti, stc, pif); aux = ""; isOp = false; code = l.Trim(); for (int i = 0; i < code.Length; i++) { if (code[i] == ' ') { if (isQuoteOpen) { throw new Exception("Error at line " + line + ": space not allowed here."); } CheckWord(aux, line, sti, stc, pif); aux = ""; isOp = false; } else if (isAlphaNumeric(code[i].ToString())) { if (!isOp) { aux = aux + code[i]; } else { CheckWord(aux, line, sti, stc, pif); aux = "" + code[i]; isOp = false; } } else if (isPossibleOperator(code[i].ToString())) { if (isQuoteOpen) { throw new Exception("Error at line " + line + ": operator not allowed here."); } if (isOp) { aux = aux + code[i]; } else { CheckWord(aux, line, sti, stc, pif); aux = "" + code[i]; isOp = true; } } else if (separators.Contains(code[i].ToString())) { if (code[i] == '\'') { if (isQuoteOpen) { throw new Exception("Error at line " + line + ": ' not allowed here."); } CheckWord(aux, line, sti, stc, pif); aux = ""; isOp = false; if (i < code.Length - 2) { if (code[i + 1] != '\'' && code[i + 2] == '\'') { CheckWord("'" + code[i + 1] + "'", line, sti, stc, pif); i = i + 2; } else { throw new Exception("Error at line " + line + ": invalid constant."); } } else { throw new Exception("Error at line " + line + ": ' not allowed here."); } } else if (code[i] == '\"') { if (isQuoteOpen) { CheckWord("\"" + aux + "\"", line, sti, stc, pif); aux = ""; isQuoteOpen = false; } else { isQuoteOpen = true; CheckWord(aux, line, sti, stc, pif); } } else { if (isQuoteOpen) { throw new Exception("Error at line " + line + ": separator not allowed here."); } CheckWord(aux, line, sti, stc, pif); CheckWord(code[i].ToString(), line, sti, stc, pif); aux = ""; } } } } }
public static List <int> LL1(Dictionary <string, Dictionary <string, Rule> > parseTable, Grammar grammar, ProgramInternalForm pif) { //try //{ Stack <string> alfa = new Stack <string>(); Stack <string> beta = new Stack <string>(); List <int> pi = new List <int>(); bool go = true, isAccepted = false; alfa.Push(DOLLAR); for (int i = pif.program.Count - 1; i >= 0; i--) { int code = pif.program[i].Code; if (code == 0) { alfa.Push("IDENTIFIER"); } else if (code == 1) { alfa.Push("CONSTANT"); } else { alfa.Push(LexicalAnalyzer.reverseCodeTable[pif.program[i].Code]); } } beta.Push(DOLLAR); beta.Push(grammar.StartingSymbol); while (go) { string row = beta.Peek() == EPSILON ? DOLLAR : beta.Peek(); string column = alfa.Peek() == EPSILON ? DOLLAR : alfa.Peek(); Rule rule = parseTable[row][column]; if (rule.ProductionIndex != 0) { beta.Pop(); if (rule.ProductionRHS != EPSILON) { string[] rhs = rule.ProductionRHS.Split(' '); for (int i = rhs.Length - 1; i >= 0; i--) { beta.Push(rhs[i].ToString()); } pi.Add(rule.ProductionIndex); } } else if (rule.ProductionRHS == "pop") { beta.Pop(); alfa.Pop(); } else if (rule.ProductionRHS == "acc") { go = false; isAccepted = true; } else { go = false; isAccepted = false; } } if (isAccepted == false) { return(null); } return(pi); //} //catch (Exception ex) //{ // return null; //} }