static void Main(string[] args) { operations = StandOpers(); brackets = StandBrackets(); terms = StandTerms(); brake = new Element("$"); Table tab = new Table(operations, terms, brackets, brake); tab.PrintTable(); //string input = "(1) & 0 & ~1 ! a & 1 $"; //string input = "(1) & 0 & ~1 ! a & 1 & 0 ! 1 & (1 ! ~0)$"; //string input = "~1 & ~0$"; string input = "[1 | 0] & (1 | [~a | 0]) & ~[0 | (1 & 0)]$"; //string input = "(1 | 0) & (1 | (~a | 0)) & ~(0 | (1 & 0))$"; //string input = "a & a & ~(0 | (1 & 0))$"; //string input = "a & ~[0 | (1 & 0)]$"; //string input = "1 & a & 0$"; //string input = "~(a | 0) & (~1 | (0))$"; Console.WriteLine(input); GrammProcessor GP = new GrammProcessor(operations, terms, brackets, brake, tab); string res = GP.Process(input); if (res == "") { Console.WriteLine("Error during processing!"); } else { Console.WriteLine($"Result: {res}"); } }
static void Main(string[] args) { GramFileProcessor.WriteGramm(CreateTestGramm(), "TestGramm.json"); Gramm input = GramFileProcessor.ReadGramm("TestGramm.json"); var reachGr = UnreachableProcessor.RemoveUnreachable(input); var newGr = GrammProcessor.RemoveLR(reachGr); var factGr = FactProcessor.Fact(newGr); GramFileProcessor.WriteGramm(factGr, "ResultGramm.json"); }
public static Gramm Fact(Gramm gr) { var symbRules = GrammProcessor.GetAllSymbRules(gr); var newSymbRules = new Dictionary <string, List <List <string> > >(symbRules); foreach (var rule in symbRules) { var left = rule.Key; bool flag = true; while (flag) { var rights = newSymbRules[left]; var fullRights = GetFullRights(rights); var msub = GetMSub(fullRights); if (msub.Length == 0) { flag = false; break; } var subRights = GetSubRights(fullRights, msub); var nonSubRights = fullRights.Except(subRights).ToList(); var cutRights = GetCutRights(subRights, msub); List <List <string> > newRights = new List <List <string> >(); var newTerm = FindNewTerm(left, gr); newRights.Add(GetRight(msub + newTerm)); newRights.AddRange(GetRights(nonSubRights)); newSymbRules[left] = newRights; newSymbRules.Add(newTerm, GetRights(cutRights)); gr.NonTerms.Add(newTerm); } } gr.Rules = new List <Rule>(); foreach (var rule in newSymbRules) { foreach (var right in rule.Value) { gr.Rules.Add(new Rule(rule.Key, right)); } } return(gr); }
public static Gramm RemoveUnreachable(Gramm gr) { var symbRules = GrammProcessor.GetAllSymbRules(gr); bool changed = true; List <string> UsedNT = new List <string>() { gr.St }; while (changed) { int len = UsedNT.Count; foreach (var rule in symbRules) { if (UsedNT.Contains(rule.Key)) { UsedNT = UsedNT.Union(FindNTInRules(rule.Value, gr)).ToList(); } } if (UsedNT.Count == len) { changed = false; } } List <string> unreachable = gr.NonTerms.Except(UsedNT).ToList(); gr.NonTerms = UsedNT; foreach (var un in unreachable) { symbRules.Remove(un); } gr.Rules = new List <Rule>(); foreach (var rule in symbRules) { foreach (var right in rule.Value) { gr.Rules.Add(new Rule(rule.Key, right)); } } return(gr); }
static void Main(string[] args) { //string input = "15 & true & ~ 15 & true ! 19 & 10 & ~5 ! true"; string input = "begin" + "15 = true;" + "21 = true & 15;" + "8 = 21 ! true & ~false;" + "15 = ~8 & 15 ! false" + "end"; bool res = GrammProcessor.CheckInput(input); if (!res) { Console.WriteLine("Incorrect input"); } else { Console.WriteLine("Correct input"); } }