private bool FixEmpties(GrammarRule grammarRule) { if (!ContainsEmptyString(grammarRule)) { return(false); } else { // Consists { Eps } alternative. foreach (List <string> alternative in grammarRule.Alternatives) { if (AllowsEmptyString(alternative, new List <string>())) { } if ((!ListsAreEqual(alternative, _emptyStringAlternative)) && (AllowsEmptyString(alternative, new List <string>()))) { // Found alternative, which is not { Eps }, but allows empty string. StopConsistingEmptyString(grammarRule); return(true); } } return(false); } }
private string UnfactorisingSymbol(GrammarRule grammarRule) { List <string> firsts = new List <string>(); for (int i = 0; i < grammarRule.Alternatives.Count; i++) { List <string> alternative = grammarRule.Alternatives[i]; foreach (string first in firsts) { if (CanStartWith(alternative, first)) { if (AlternativesStartingWith(grammarRule, alternative[0]) > 1) { return(alternative[0]); } else { return(first); } } } // Current alternative is OK. // Add firsts of current alternative to storage. firsts.AddRange(GetFirst(alternative)); } return(null); }
private int GetAlternativeIndex(GrammarRule grammarRule, List <string> alternative) { for (int i = 0; i < grammarRule.Alternatives.Count; i++) { if (ListsAreEqual(alternative, grammarRule.Alternatives[i])) { return(i); } } return(-1); }
/// <summary> /// /// </summary> /// <param name="grammarRule"></param> /// <param name="symbol"></param> /// <returns>How many alternatives in grammarRule start with symbol.</returns> private static int AlternativesStartingWith(GrammarRule grammarRule, string symbol) { int resultCount = 0; foreach (List <string> alternative in grammarRule.Alternatives) { if ((alternative.Count > 0) && (alternative[0].Equals(symbol))) { resultCount++; } } return(resultCount); }
private bool StopConsistingEmptyString(GrammarRule grammarRule) { //int index = grammarRule.Alternatives.IndexOf(_emptyStringAlternative); int index = GetAlternativeIndex(grammarRule, _emptyStringAlternative); if (index == -1) { return(false); } while (index > -1) { grammarRule.Alternatives.RemoveAt(index); index = GetAlternativeIndex(grammarRule, _emptyStringAlternative); } return(true); }
private bool RenameGrammarRule(GrammarRule rule, string oldName, string newName) { bool result = false; foreach (List <string> alternative in rule.Alternatives) { for (int i = 0; i < alternative.Count; i++) { if (alternative[i].Equals(oldName)) { alternative[i] = newName; result = true; } } } return(result); }
private bool ContainsEmptyString(GrammarRule grammarRule) { if (grammarRule == null) { return(false); } foreach (List <string> alternative in grammarRule.Alternatives) { if ((alternative.Count == 1) && (alternative[0].Equals(_emptyStringType))) { // Can be just emptyString. return(true); } } return(false); }
private bool ClearEqualAlternatives(GrammarRule rule) { bool result = false; for (int i = 0; i < rule.Alternatives.Count; i++) { for (int j = i + 1; j < rule.Alternatives.Count; j++) { if (ListsAreEqual(rule.Alternatives[i], rule.Alternatives[j])) { rule.Alternatives.RemoveAt(j); result = true; j--; } } } return(result); }
private bool AllowsEmptyString(GrammarRule grammarRule, List <string> callStack) { if (grammarRule == null) { return(false); } if (_allowingEmpty.Contains(grammarRule.ProductTypeName)) { return(true); } if (callStack.Contains(grammarRule.ProductTypeName)) { return(false); } callStack.Add(grammarRule.ProductTypeName); if (ContainsEmptyString(grammarRule)) { //DeleteFromStack(callStack, grammarRule.ProductTypeName); MakeAllowingEmpty(grammarRule.ProductTypeName); return(true); } foreach (List <string> alternative in grammarRule.Alternatives) { ///// IF Alternative is on the top of the stack! (New method) //if(StackStartsWith(callStack, alternative)) //{ // return true; //} if (AllowsEmptyString(alternative, callStack)) { //DeleteFromStack(callStack, grammarRule.ProductTypeName); MakeAllowingEmpty(grammarRule.ProductTypeName); return(true); } } return(false); }
private Tuple <List <string>, double> WhichAlternative(GrammarRule grammarRule, string startingTerminalOrEmpty) { int count = 0; foreach (List <string> alternative in grammarRule.Alternatives) { if (CanStartWith(alternative, startingTerminalOrEmpty)) { return(new Tuple <List <string>, double>(alternative, GetGrammarRuleIndex(grammarRule) + ShiftComaLeft(count))); } count++; } count = 0; if (AllowsEmptyString(grammarRule, new List <string>())) { // Determine, which alternative ALLOWS _emptyString. foreach (List <string> alternative in grammarRule.Alternatives) { if (AllowsEmptyString(alternative, new List <string>())) { return(new Tuple <List <string>, double>(alternative, GetGrammarRuleIndex(grammarRule) + ShiftComaLeft(count))); } count++; } } //if (AllowsEmptyString(grammarRule)) //{ // return new Tuple<List<string>, double>(_emptyStringAlternative, // GetGrammarRuleIndex(grammarRule) + ShiftComaLeft(count)); //} return(null); }
private bool ContainsEmptyString(string nonTerminalType) { GrammarRule grammarRule = GetGrammarRule(nonTerminalType); return(ContainsEmptyString(grammarRule)); }
private void InsertNewGrammarRule(int index, GrammarRule grammarRule) { _grammarRules.Insert(index, grammarRule); UpdateNonTerminals(); }
public Gramatics(List <string> grammarRuleStrings, List <string> terminalSymbolsTypes, string emptyStringType) : this(GrammarRule.GrammarRules(grammarRuleStrings), terminalSymbolsTypes, emptyStringType) { }
private int GetGrammarRuleIndex(GrammarRule grammarRule) { return(GrammarRules.IndexOf(grammarRule)); }
private bool AllowsEmptyString(string nonTerminalType, List <string> callStack) { GrammarRule grammarRule = GetGrammarRule(nonTerminalType); return(AllowsEmptyString(grammarRule, callStack)); }
private Tuple <GrammarRule, GrammarRule> InsteadOfLeftRecursive(GrammarRule grammarRule) { if (!grammarRule.IsLeftRecursive) { return(null); } GrammarRule newRule = new GrammarRule(NewLeftRuleName(), new List <List <string> >()); List <List <string> > leftRecursiveAlternatives = new List <List <string> >(); // Delete leftRecursive rules, fill leftRecursiveAlternatives. for (int i = 0; i < grammarRule.Alternatives.Count; i++) { if ((grammarRule.Alternatives[i].Count > 0) && (grammarRule.Alternatives[i][0].Equals(grammarRule.ProductTypeName))) { // Alternative is leftRecursive. grammarRule.Alternatives[i].RemoveAt(0); leftRecursiveAlternatives.Add(new List <string>(grammarRule.Alternatives[i])); grammarRule.Alternatives.RemoveAt(i); i--; } } if (grammarRule.Alternatives.Count == 0) { grammarRule.Alternatives.Add(new List <string> { newRule.ProductTypeName }); } // Fill newRule. int alternativesCount = leftRecursiveAlternatives.Count; for (int i = 0; i < alternativesCount; i++) { List <string> newAlternative = new List <string>(leftRecursiveAlternatives[i]); //newAlternative.Add(newRule.ProductTypeName); newRule.Alternatives.Add(newAlternative); newAlternative = new List <string>(newAlternative); newAlternative.Add(newRule.ProductTypeName); newRule.Alternatives.Add(newAlternative); } // Fill grammarRule. alternativesCount = grammarRule.Alternatives.Count; for (int i = 0; i < alternativesCount; i++) { List <string> newAlternative = new List <string>(grammarRule.Alternatives[i]); newAlternative.Add(newRule.ProductTypeName); grammarRule.Alternatives.Add(newAlternative); } string oldName = grammarRule.ProductTypeName.ToString(); grammarRule.ProductTypeName = NewLeftRuleName(grammarRule.ProductTypeName); RenameGrammarRule(newRule, oldName, grammarRule.ProductTypeName); RenameGrammarRules(oldName, grammarRule.ProductTypeName); ClearEqualAlternatives(grammarRule); ClearEqualAlternatives(newRule); return(new Tuple <GrammarRule, GrammarRule>(grammarRule, newRule)); }
private Tuple <GrammarRule, GrammarRule> InsteadOfUnfactorized(GrammarRule grammarRule, string startingSymbol) { if (grammarRule.ProductTypeName == "Bool_Plural_Statement_Eps") { } if (UnfactorisingSymbol(grammarRule) == null) { return(null); } GrammarRule newRule = new GrammarRule(NewUnfactorizedRuleName(), new List <List <string> >()); List <List <string> > unfactorizedAlternatives = new List <List <string> >(); for (int i = 0; i < grammarRule.Alternatives.Count; i++) { if ((grammarRule.Alternatives[i].Count > 0) && (!grammarRule.Alternatives[i][0].Equals(startingSymbol)) && (CanStartWith(grammarRule.Alternatives[i], startingSymbol))) { // Doesn't start with the particular terminal, but this terminal is digged in the starting nonterminal. //List<string> newAlternative = new List<string>(grammarRule.Alternatives[i]); //newAlternative = UnpackAlternative(newAlternative, startingSymbol); //grammarRule.Alternatives[i] = newAlternative; /////////////////////////////////////////////////////// List <List <string> > newAlternatives = UnpackAlternative(grammarRule.Alternatives[i], startingSymbol); if ((newAlternatives != null) && (newAlternatives.Count > 0)) { grammarRule.Alternatives[i] = newAlternatives[0]; int j = 1; for (; j < newAlternatives.Count; j++) { grammarRule.Alternatives.Insert(i + j, newAlternatives[j]); } } } if ((grammarRule.Alternatives[i].Count > 0) && (grammarRule.Alternatives[i][0].Equals(startingSymbol))) { // Alternative starts with startingSymbol. grammarRule.Alternatives[i].RemoveAt(0); if (grammarRule.Alternatives[i].Count > 0) { unfactorizedAlternatives.Add(new List <string>(grammarRule.Alternatives[i])); } else { unfactorizedAlternatives.Add(_emptyStringAlternative); } grammarRule.Alternatives.RemoveAt(i); i--; } } grammarRule.Alternatives.Insert(0, new List <string>() { startingSymbol, newRule.ProductTypeName }); newRule.Alternatives = new List <List <string> >(unfactorizedAlternatives); string oldName = grammarRule.ProductTypeName.ToString(); grammarRule.ProductTypeName = NewUnfactorizedRuleName(grammarRule.ProductTypeName); RenameGrammarRule(newRule, oldName, grammarRule.ProductTypeName); RenameGrammarRules(oldName, grammarRule.ProductTypeName); ClearEqualAlternatives(grammarRule); ClearEqualAlternatives(newRule); return(new Tuple <GrammarRule, GrammarRule>(grammarRule, newRule)); }