Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var scheme        = new TacoSchemeGrammar();
            var schemeSymbols = new List <Symbol>(scheme.Symbols.Values);
            var schemeGrammar = new Grammar(scheme.productionRules, schemeSymbols);
            var parser        = new LR1Table(schemeGrammar, "Scheme.miniflex");
            var source        = File.ReadAllText("source.ss");
            var program       = parser.Accept(source);

            var input = "";

            do
            {
                Console.Write("Taco> ");
                input = Console.ReadLine();
                if (input == "exit")
                {
                    break;
                }
                try
                {
                    program = parser.Accept(input);
                    //Console.WriteLine(program.Result);
                }
                catch (Exception e)
                {
                    Console.WriteLine("TacoError:: " + e.Message);
                }
            } while (true);

            Console.WriteLine("TERMINATED");
            Console.Read();
        }
Ejemplo n.º 2
0
        public void TestTableAcceptor()
        {
            var symbols = new Dictionary <string, Symbol>
            {
                { "E'", new Symbol(Symbol.SymTypes.NoTerminal, "E'") },
                { "E", new Symbol(Symbol.SymTypes.NoTerminal, "E") },
                { "T", new Symbol(Symbol.SymTypes.NoTerminal, "T") },
                { "F", new Symbol(Symbol.SymTypes.NoTerminal, "F") },
                { "id", new Symbol(Symbol.SymTypes.Terminal, "id") },
                { "+", new Symbol(Symbol.SymTypes.Terminal, "+") },
                { "*", new Symbol(Symbol.SymTypes.Terminal, "*") },
                { "(", new Symbol(Symbol.SymTypes.Terminal, "(") },
                { ")", new Symbol(Symbol.SymTypes.Terminal, ")") }
            };

            var productionRules = new List <ProductionRule>();

            var productionRuleBody = new List <Symbol> {
                symbols["E"]
            };
            var productionRule = new ProductionRule(symbols["E'"], productionRuleBody);

            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["E"], symbols["+"], symbols["T"]
            };
            productionRule = new ProductionRule(symbols["E"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["T"]
            };
            productionRule = new ProductionRule(symbols["E"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["T"], symbols["*"], symbols["F"]
            };
            productionRule = new ProductionRule(symbols["T"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["F"]
            };
            productionRule = new ProductionRule(symbols["T"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["("], symbols["E"], symbols[")"]
            };
            productionRule = new ProductionRule(symbols["F"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["id"]
            };
            productionRule = new ProductionRule(symbols["F"], productionRuleBody);
            productionRules.Add(productionRule);

            var g = new Grammar(productionRules, symbols.Values.ToList());

            //build table
            var analyzer = new LR1Table(g, "Scheme.miniflex");

            //Assert.IsTrue(analyzer.Accept("id*****id*id"));
        }
Ejemplo n.º 3
0
        public void TestTableAcceptor()
        {
            var symbols = new Dictionary <string, Symbol>
            {
                { "S", new Symbol(Symbol.SymTypes.NoTerminal, "S") },
                { "E", new Symbol(Symbol.SymTypes.NoTerminal, "E") },
                { "E'", new Symbol(Symbol.SymTypes.NoTerminal, "E'") },
                { "T", new Symbol(Symbol.SymTypes.NoTerminal, "T") },
                { "T'", new Symbol(Symbol.SymTypes.NoTerminal, "T'") },
                { "F", new Symbol(Symbol.SymTypes.NoTerminal, "F") },
                { "id", new Symbol(Symbol.SymTypes.Terminal, "id") },
                { "+", new Symbol(Symbol.SymTypes.Terminal, "+") },
                { "*", new Symbol(Symbol.SymTypes.Terminal, "*") },
                { "(", new Symbol(Symbol.SymTypes.Terminal, "(") },
                { ")", new Symbol(Symbol.SymTypes.Terminal, ")") },
                { "EPSILON", new Symbol(Symbol.SymTypes.Epsilon, "EPSILON") }
            };

            var productionRules = new List <ProductionRule>();


            var productionRuleBody = new List <Symbol> {
                symbols["Program"]
            };
            var productionRule = new ProductionRule(symbols["Start"], productionRuleBody);

            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Form_list"]
            };
            productionRule = new ProductionRule(symbols["Program"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Form_list"], symbols["Form"]
            };
            productionRule = new ProductionRule(symbols["Form_list"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Definition"]
            };
            productionRule = new ProductionRule(symbols["Form"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Expression"]
            };
            productionRule = new ProductionRule(symbols["Form"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Definition_list"], symbols["Definition"]
            };
            productionRule = new ProductionRule(symbols["Definition_list"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Variable_definition"]
            };
            productionRule = new ProductionRule(symbols["Definition"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["(Paren"]
            };
            productionRule = new ProductionRule(symbols["Definition"], productionRuleBody);
            productionRules.Add(productionRule);

            productionRuleBody = new List <Symbol> {
                symbols["Variable_definition"]
            };
            productionRule = new ProductionRule(symbols["Definition"], productionRuleBody);
            productionRules.Add(productionRule);

            var g = new Grammar(productionRules, symbols.Values.ToList());

            //build table
            var analyzer = new LR1Table(g);

            analyzer.Print();

            Assert.IsTrue(analyzer.Accept("(id)"));
        }