/// <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); }
/// <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()); } }