Esempio n. 1
0
        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());
        }
Esempio n. 2
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="lex">Lexer</param>
 public Parser(Lexer lex)
 {
     this.Top = null;
     this.Used = 0;
     this._lexer = lex;
     this.Move();
 }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        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
        }