Beispiel #1
0
        /// <summary>
        /// 词法解析
        /// </summary>
        void TestSimpleLexer()
        {
            SimpleLexer lexer = new SimpleLexer();

            String script = "int age = 45;";

            Console.WriteLine("parse: " + script);
            SimpleTokenReader tokenReader = lexer.Tokenize(script);

            SimpleLexer.Dump(tokenReader);

            //测试inta的解析
            script = "inta age == 45;";
            Console.WriteLine("\nparse :" + script);
            tokenReader = lexer.Tokenize(script);
            SimpleLexer.Dump(tokenReader);

            //测试in的解析
            script = "in age = 45;";
            Console.WriteLine("\nparse :" + script);
            tokenReader = lexer.Tokenize(script);
            SimpleLexer.Dump(tokenReader);

            //测试>=的解析
            script = "age >= 45;";
            Console.WriteLine("\nparse :" + script);
            tokenReader = lexer.Tokenize(script);
            SimpleLexer.Dump(tokenReader);

            //测试>的解析
            script = "age > 45;";
            Console.WriteLine("\nparse :" + script);
            tokenReader = lexer.Tokenize(script);
            SimpleLexer.Dump(tokenReader);
        }
Beispiel #2
0
        /**
         * 解析脚本
         * @param script
         * @return
         * @throws Exception
         */
        public ASTNode parse(String script)
        {
            SimpleLexer lexer    = new SimpleLexer();
            TokenReader tokens   = lexer.Tokenize(script);
            ASTNode     rootNode = Prog(tokens);

            return(rootNode);
        }
Beispiel #3
0
        /**
         * 执行脚本,并打印输出AST和求值过程。
         * @param script
         */
        public void evaluate(String script)
        {
            try
            {
                SimpleLexer lexer  = new SimpleLexer();
                TokenReader tokens = lexer.Tokenize(script);

                ASTNode tree = Prog(tokens);

                DumpAST(tree, "");
                evaluate(tree, "");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }