Exemple #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);
        }
Exemple #2
0
        /// <summary>
        ///  打印所有的Token
        /// </summary>
        /// <param name="tokenReader">tokenReader</param>
        public static void Dump(SimpleTokenReader tokenReader)
        {
            Console.WriteLine("text\ttype");
            Token token = null;

            while ((token = tokenReader.read()) != null)
            {
                Console.WriteLine(token.getText() + "\t\t" + token.getType());
            }
        }