public static bool IsSimplePrecedence(this GrammarPrecedence grammarPrecedence) { if (grammarPrecedence.Grammar.GetCyclicNonTerminals().Any()) { return(false); } if (!grammarPrecedence.Grammar.IsReversibleGrammar()) { return(false); } foreach (var a in grammarPrecedence.Grammar.Symbols) { foreach (var b in grammarPrecedence.Grammar.Symbols) { var pair = (a, b); var isLess = grammarPrecedence.IsLess(pair); var isEqual = grammarPrecedence.IsEqual(pair); var isGreater = grammarPrecedence.IsGreater(pair); if (isLess && isEqual || isEqual && isGreater || isLess && isGreater) { return(false); } } } return(true); }
public GrammarAnalyzer(GrammarPrecedence grammar) { this.grammar = grammar; IsSimplePrecedence = grammar.IsSimplePrecedence(); IsWeakPrecedence = IsSimplePrecedence || grammar.IsWeakPrecedence(); foreach (var rule in grammar.Grammar.GetRules()) { rules.Add(rule.Right.Reverse(), rule); } }
private static bool HasAmbiguousEndedRules(this GrammarPrecedence grammar) { foreach (var sRule in grammar.Grammar.GetRules()) { foreach (var aRule in grammar.Grammar.GetRules()) { if (sRule.Right.Count > aRule.Right.Count && aRule.Right.Any() && sRule.Right.EndsWith(aRule.Right)) { var x = sRule.Right[sRule.Right.Count - aRule.Right.Count - 1]; var a = aRule.Left; if (grammar.IsEqual((x, a)) || grammar.IsLess((x, a))) { return(true); } } } } return(false); }
public static bool IsWeakPrecedence(this GrammarPrecedence grammarPrecedence) { if (grammarPrecedence.Grammar.GetCyclicNonTerminals().Any()) { return(false); } if (!grammarPrecedence.Grammar.IsReversibleGrammar()) { return(false); } if (grammarPrecedence.Equal.Any(grammarPrecedence.IsGreater)) { return(false); } if (grammarPrecedence.Less.Any(grammarPrecedence.IsGreater)) { return(false); } if (grammarPrecedence.HasAmbiguousEndedRules()) { return(false); } return(true); }