Esempio n. 1
0
    static bool AreProductionsSame(Grammar authority, Grammar copy)
    {
        Func <Grammar, Dictionary <string, Production> > ToDictionary;

        ToDictionary = g =>
        {
            var d = new Dictionary <string, Production>();
            foreach (var pp in g.Productions)
            {
                if (pp.ProductionName != null)
                {
                    d[pp.ProductionName] = pp;
                }
            }
            return(d);
        };
        var dauthority = ToDictionary(authority);
        var dcopy      = ToDictionary(copy);
        var ok         = true;

        foreach (var p in dauthority.Keys)
        {
            if (!dcopy.ContainsKey(p))
            {
                continue;
            }
            string pauthority = Antlr.ToString(dauthority[p]), pcopy = Antlr.ToString(dcopy[p]);
            if (pauthority == pcopy)
            {
                continue;
            }
            ok = false;
            Console.WriteLine($"MISMATCH for '{p}'\r\nAUTHORITY:\r\n{pauthority}\r\nCOPY:\r\n{pcopy}\r\n");
        }

        foreach (var p in dauthority.Keys)
        {
            if (p == "start")
            {
                continue;
            }
            if (!dcopy.ContainsKey(p))
            {
                Console.WriteLine($"Copy doesn't contain '{p}'"); ok = false;
            }
        }
        foreach (var p in dcopy.Keys)
        {
            if (p == "start")
            {
                continue;
            }
            if (!dauthority.ContainsKey(p))
            {
                Console.WriteLine($"Authority doesn't contain '{p}'"); ok = false;
            }
        }

        return(ok);
    }
Esempio n. 2
0
        static IEnumerable <ProductionDifference> CompareGrammars(EbnfGrammar authority, EbnfGrammar copy)
        {
            Func <EbnfGrammar, Dictionary <string, Production> > ToDictionary;

            ToDictionary = g =>
            {
                var d = new Dictionary <string, Production>();
                foreach (var pp in g.Productions)
                {
                    if (pp.Name != null)
                    {
                        d[pp.Name] = pp;
                    }
                }

                return(d);
            };
            var dauthority = ToDictionary(authority);
            var dcopy      = ToDictionary(copy);

            foreach (var p in dauthority.Keys)
            {
                if (!dcopy.ContainsKey(p))
                {
                    continue;
                }

                Production pauthority0 = dauthority[p], pcopy0 = dcopy[p];
                string     pauthority = Antlr.ToString(pauthority0), pcopy = Antlr.ToString(pcopy0);
                if (pauthority == pcopy)
                {
                    continue;
                }

                yield return(new ProductionDifference {
                    productionName = p, authority = pauthority, copy = pcopy
                });
            }

            foreach (var p in dauthority.Keys)
            {
                if (p == "start")
                {
                    continue;
                }

                if (!dcopy.ContainsKey(p))
                {
                    yield return(new ProductionDifference {
                        productionName = p, authority = "<defined>", copy = null
                    });
                }
            }
            foreach (var p in dcopy.Keys)
            {
                if (p == "start")
                {
                    continue;
                }

                if (!dauthority.ContainsKey(p))
                {
                    yield return(new ProductionDifference {
                        productionName = p, authority = null, copy = "<defined>"
                    });
                }
            }
        }