/// <summary> /// Recursive function meant to generate the push down string. /// </summary> /// <param name="list">The list with the elements.</param> /// <returns></returns> List <string> GeneratePushDown(List <string> list) { string lastElement = list.Last(); // If the last element is a number try { if (lastElement == "11" || lastElement == "10") { int ceva = 0; } int line = Convert.ToInt32(lastElement); string intrare = entryStack.Peek(); int column = UsefulStuff.GetElemetIndexInArray(intrare, terminals); // Salt if (column < 0) { column = UsefulStuff.GetElemetIndexInArray(intrare, terminaleSalt); if (salt[line, column] != "NULL") { list.Add(salt[line, column]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } // Action else { string action = actions[line, column]; // The generated string is accepted if (action == "acc") { labelResultStatus.ForeColor = System.Drawing.Color.Green; labelResultStatus.Text = "Success"; return(list); } // Deplasare if (action[0] == 'd') { list.Add(entryStack.Pop()); string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } list.Add(remaining); return(GeneratePushDown(list)); } // Reducere /* * if (action[0] == 'r') * { * string remaining = ""; * for (int index = 1; index < action.Length; index++) * { * remaining += action[index]; * } * int productionNumber = Convert.ToInt32( remaining ); * * string stringToBeReplaced = productions[productionNumber, 0]; * int stringToBeReplacedIndex = UsefulStuff.GetElementIndexInList(stringToBeReplaced, list); * * // Remove all elements starting from the string's index * list.RemoveRange(stringToBeReplacedIndex, list.Count - stringToBeReplacedIndex); * * // Add the replacement string * list.Add( productions[productionNumber, 1] ); * * return GeneratePushDown(list); * } */ if (action[0] == 'r') { string operatie123 = ""; string op123, op213; string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } int productionNumber = Convert.ToInt32(remaining); string production = productions[productionNumber, 1]; string stringToBeReplaced = productions[productionNumber, 0]; int stringToBeReplacedIndex = UsefulStuff.GetElementIndexInList(stringToBeReplaced, list); // Remove all elements starting from the string's index list.RemoveRange(stringToBeReplacedIndex, list.Count - stringToBeReplacedIndex); // Add the replacement string list.Add(productions[productionNumber, 1]); if (stringToBeReplaced == "a" || stringToBeReplaced == "id") { stack.Push(stringToBeReplaced); } for (int i = 0; i < stringToBeReplaced.Length; i++) { string ceva = stringToBeReplaced[i].ToString(); if (stringToBeReplaced[i] == '+' || stringToBeReplaced[i] == '-' || stringToBeReplaced[i] == '/' || stringToBeReplaced[i] == '*') { operatie123 = stringToBeReplaced[i].ToString(); op123 = stack.Pop(); op213 = stack.Pop(); string TheCOd = "t" + contor.ToString() + "=" + op123 + operatie123 + op213; string pushul = "t" + contor.ToString(); contor++; codIntermediar.Add(TheCOd); stack.Push(pushul); contorCOd++; } } return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } } // If the last element is not a number catch (Exception e) { string penultimulElement = list.ElementAt(list.Count - 2); int line = Convert.ToInt32(penultimulElement); int column = UsefulStuff.GetElemetIndexInArray(lastElement, terminals); // Salt if (column < 0) { column = UsefulStuff.GetElemetIndexInArray(lastElement, terminaleSalt); if (salt[line, column] != "NULL") { list.Add(salt[line, column]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } // Action else { string action = actions[line, column]; // The generated string is accepted if (action == "acc") { labelResultStatus.ForeColor = System.Drawing.Color.Green; labelResultStatus.Text = "Success"; return(list); } // Deplasare if (action[0] == 'd') { list.Add(entryStack.Pop()); string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } list.Add(remaining); return(GeneratePushDown(list)); } // Reducere if (action[0] == 'r') { string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } int productionNumber = Convert.ToInt32(remaining); string stringToBeReplaced = productions[productionNumber, 0]; int stringToBeReplacedIndex = UsefulStuff.GetElementIndexInList(stringToBeReplaced, list); // Remove all elements starting from the string's index list.RemoveRange(stringToBeReplacedIndex, list.Count - stringToBeReplacedIndex); // Add the replacement string list.Add(productions[productionNumber, 1]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } } }
/// <summary> /// Recursive function meant to generate the push down string. /// </summary> /// <param name="list">The list with the elements.</param> /// <returns></returns> List <string> GeneratePushDown(List <string> list) { string lastElement = list.Last(); // If the last element is a number try { if (lastElement == "11" || lastElement == "10") { int ceva = 0; } int line = Convert.ToInt32(lastElement); string intrare = entryStack.Peek(); int column = UsefulStuff.GetElemetIndexInArray(intrare, terminals); // Salt if (column < 0) { column = UsefulStuff.GetElemetIndexInArray(intrare, terminaleSalt); if (salt[line, column] != "NULL") { list.Add(salt[line, column]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } // Action else { string action = actions[line, column]; // The generated string is accepted if (action == "acc") { labelResultStatus.ForeColor = System.Drawing.Color.Green; labelResultStatus.Text = "Success"; return(list); } // Deplasare if (action[0] == 'd') { list.Add(entryStack.Pop()); string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } list.Add(remaining); return(GeneratePushDown(list)); } // Reducere if (action[0] == 'r') { string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } int productionNumber = Convert.ToInt32(remaining); string stringToBeReplaced = productions[productionNumber, 0]; int stringToBeReplacedIndex = UsefulStuff.GetElementIndexInList(stringToBeReplaced, list); // Remove all elements starting from the string's index list.RemoveRange(stringToBeReplacedIndex, list.Count - stringToBeReplacedIndex); // Add the replacement string list.Add(productions[productionNumber, 1]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } } // If the last element is not a number catch (Exception e) { string penultimulElement = list.ElementAt(list.Count - 2); int line = Convert.ToInt32(penultimulElement); int column = UsefulStuff.GetElemetIndexInArray(lastElement, terminals); // Salt if (column < 0) { column = UsefulStuff.GetElemetIndexInArray(lastElement, terminaleSalt); if (salt[line, column] != "NULL") { list.Add(salt[line, column]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } // Action else { string action = actions[line, column]; // The generated string is accepted if (action == "acc") { labelResultStatus.ForeColor = System.Drawing.Color.Green; labelResultStatus.Text = "Success"; return(list); } // Deplasare if (action[0] == 'd') { list.Add(entryStack.Pop()); string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } list.Add(remaining); return(GeneratePushDown(list)); } // Reducere if (action[0] == 'r') { string remaining = ""; for (int index = 1; index < action.Length; index++) { remaining += action[index]; } int productionNumber = Convert.ToInt32(remaining); string stringToBeReplaced = productions[productionNumber, 0]; int stringToBeReplacedIndex = UsefulStuff.GetElementIndexInList(stringToBeReplaced, list); // Remove all elements starting from the string's index list.RemoveRange(stringToBeReplacedIndex, list.Count - stringToBeReplacedIndex); // Add the replacement string list.Add(productions[productionNumber, 1]); return(GeneratePushDown(list)); } // If this is reached, something went wrong // End recursion with current list labelResultStatus.ForeColor = System.Drawing.Color.Red; labelResultStatus.Text = "Fail"; return(list); } } }