コード例 #1
0
ファイル: Helpers.cs プロジェクト: RaduPantelimon/LR1
        public static List <ElementFirst> getFirstsForGIC(GIC gic)
        {
            //initializare variabile
            List <ElementFirst> rezultat = new List <ElementFirst>();

            //adaugare rezultate pt terminale
            foreach (string term in gic.terminale)
            {
                rezultat.Add(new ElementFirst(term, new List <string> {
                    term
                }));
            }
            //adaugare rezultate pt neterminale
            foreach (string neterm in gic.neterminale)
            {
                rezultat.Add(new ElementFirst(neterm, new List <string>()));
            }

            //adaugam rez pt lambda
            rezultat.Add(new ElementFirst(Constants.Lambda, new List <string> {
                Constants.Lambda
            }));

            bool actualizare = true;

            //cat timp inca actualizam matricea
            while (actualizare)
            {
                actualizare = false;
                foreach (Regula r in gic.reguli_productie)
                {
                    ElementFirst first_neterminal_curent = rezultat.Where(x => x.x_string == r.neterminal).FirstOrDefault();
                    if (first_neterminal_curent != null)
                    {
                        ElementFirst newResults = new ElementFirst(r.neterminal, new List <string>());
                        getFirstsForString(r.productie, rezultat, newResults);
                        first_neterminal_curent.rezultat.Distinct();


                        //we check if we added any new values

                        bool anythingNew = newResults.rezultat.Except(first_neterminal_curent.rezultat).Any();
                        if (anythingNew)
                        {
                            actualizare = true;
                        }
                        first_neterminal_curent.rezultat.AddRange(newResults.rezultat);
                        first_neterminal_curent.rezultat = first_neterminal_curent.rezultat.Distinct().ToList();;
                    }
                }
            }

            return(rezultat);
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: RaduPantelimon/LR1
        public Parser(GIC _gram)
        {
            gram = _gram;

            //numerotam regulile
            for (int i = 0; i < gram.reguli_productie.Count; i++)
            {
                gram.reguli_productie[i].parserIndex = i;
            }
            configuratii_first = Helpers.getFirstsForGIC(_gram);
        }
コード例 #3
0
    private static void Main(string[] args)
    {
        var sw = Stopwatch.StartNew();
        //cronometram cat a durat toata sandramaua

        /*
         * GIC gram = new LR1.Utils.GIC(new List<string>{ "a", "b" },
         *  new List<string> {"S"}, "S", new List<Regula> { new Regula("S","aSbS"), new Regula("S",Constants.Lambda) });
         *  //First(S) imi da 'a'   - E OK!
         */
        /*GIC gram = new LR1.Utils.GIC(new List<string> { "a", "b" },
         * new List<string> { "S" }, "S", new List<Regula> { new Regula("S", "SbS"), new Regula("S", "a"), new Regula("S", Constants.Lambda) });
         *  //First(S) imi da 'a' si 'b'  - E OK!
         *
         */

        GIC gram = new LR1.Utils.GIC(new List <string> {
            "a", "b", "c", "d"
        },
                                     new List <string> {
            "S", "A"
        }, "S",
                                     new List <Regula> {
            new Regula("S", "SbS"),
            new Regula("S", "a"),
            new Regula("S", Constants.Lambda),
            new Regula("S", "A"),
            new Regula("A", "Sc")
        });


        GIC extinsa = GIC.extinde(gram);



        Parser lr1Parser = new Parser(extinsa);


        Console.WriteLine(sw.ElapsedMilliseconds);

        List <string> firstA = lr1Parser.getFirst("A");
    }
コード例 #4
0
ファイル: GIC.cs プロジェクト: RaduPantelimon/LR1
        public static GIC extinde(GIC original)
        {
            GIC gramExtinsa = new Utils.GIC();



            gramExtinsa.Start = Constants.Start;

            gramExtinsa.neterminale = new List <String>(original.neterminale);
            gramExtinsa.neterminale.Add(Constants.Start);

            gramExtinsa.reguli_productie = new List <Regula>(original.reguli_productie);
            gramExtinsa.reguli_productie.Add(new Regula(Constants.Start, original.Start + Constants.Finish));

            gramExtinsa.terminale = new List <String>(original.terminale);
            gramExtinsa.terminale.Add(Constants.Finish);



            return(gramExtinsa);
        }