コード例 #1
0
ファイル: TestLexer.cs プロジェクト: ShuntaoChen/Compiler
        public void TestLexer2()
        {
            string path = @"..\..\..\TestSamples\code2.cpp";
            Assert.IsTrue(File.Exists(path));

            var tokens = new List<Token>();
            using( var reader = new StreamReader(path))
            {
                var lex = new Lexer(reader);
                while (!lex.EofReached)
                    tokens.Add(lex.Scan());
                Assert.AreEqual(8, Lexer.Line);
            }
            
            Assert.AreEqual(35, tokens.Count);
            Assert.IsNull(tokens[34]);// the last one is null
            var expect = new List<string> 
            { 
                "{", 
                "int", "i", ";",
                "float", "[", "100", "]", "a", ";",
                "while", "(", "true", ")",
                "{",
                "do", "i", "=", "i", "+", "1", ";",
                "while","(","a","[","i", "]", "<", "42",")",";",
                "}",
                "}"
            };
            for (int i = 0; i != tokens.Count - 1; ++i)
                Assert.AreEqual(expect[i], tokens[i].ToString());
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: ShuntaoChen/Compiler
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="lex">Lexer</param>
 public Parser(Lexer lex)
 {
     this.Top = null;
     this.Used = 0;
     this._lexer = lex;
     this.Move();
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: ShuntaoChen/Compiler
        static void Main(string[] args)
        {
            if (args.Count() != 1)
            {
                Console.WriteLine("Please specify code file");
                return;
            }

            var lex = new Lexer(new StreamReader(args[0]));
            var parse = new Parser(lex);
            parse.Program();
            Console.WriteLine();
        }
コード例 #4
0
ファイル: TestLexer.cs プロジェクト: ShuntaoChen/Compiler
        public void TestLexer1()
        {
            string path = @"..\..\..\TestSamples\code1.cpp";
            Assert.IsTrue(File.Exists(path));
            var reader = new StreamReader(path);

            var lex = new Lexer(reader);
            var tokens = new List<Token>();
            while (!lex.EofReached)
                tokens.Add(lex.Scan());
            reader.Close();

            Assert.AreEqual(2, Lexer.Line);
            Assert.AreEqual(3, tokens.Count);
            Assert.IsNull(tokens[2]);// the last one is null
        }