Beispiel #1
0
        public void TestLetterDigitFail()
        {
            LetterDigitLexer l = new LetterDigitLexer("a25j3");

            Assert.Throws <LexerException>(() => { l.Parse(); }, "Пропускает a25j3");

            l = new LetterDigitLexer("a2jb3");
            Assert.Throws <LexerException>(() => { l.Parse(); }, "Пропускает a2jb3");

            l = new LetterDigitLexer("2a3b");
            Assert.Throws <LexerException>(() => { l.Parse(); }, "Пропускает 2a3b");

            l = new LetterDigitLexer("a2f 3b");
            Assert.Throws <LexerException>(() => { l.Parse(); }, "Пропускает a2f 3b");
        }
Beispiel #2
0
        public void TestLetterDigitParse()
        {
            LetterDigitLexer l = new LetterDigitLexer("a2f5j3");

            Assert.IsTrue(l.Parse(), "Не разбирает a2f5j3");

            l = new LetterDigitLexer("a");
            Assert.IsTrue(l.Parse(), "Не разбирает a");

            l = new LetterDigitLexer("a2");
            Assert.IsTrue(l.Parse(), "Не разбирает a2");

            l = new LetterDigitLexer("a2B");
            Assert.IsTrue(l.Parse(), "Не разбирает a2B");
        }
Beispiel #3
0
    public static void Main()
    {
        string input = "154216";
        Lexer  L     = new IntLexer(input);

        try
        {
            L.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        //Задание 1
        System.Console.WriteLine("\nTask 1:");

        input = "0";
        Lexer L1 = new NewIntLexer(input);

        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "365";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "-22";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4-2";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        //Задание 2
        System.Console.WriteLine("\nTask 2:");

        input = "";
        Lexer L2 = new IDLexer(input);

        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "d";
        L2    = new IDLexer(input);
        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "da236E0";
        L2    = new IDLexer(input);
        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "2abcd";
        L2    = new IDLexer(input);
        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        //Задание 3
        System.Console.WriteLine("\nTask 3:");

        input = "";
        Lexer L3 = new NewIntLexer2(input);

        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "25";
        L3    = new NewIntLexer2(input);
        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "-73060";
        L3    = new NewIntLexer2(input);
        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "012";
        L3    = new NewIntLexer2(input);
        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        // Задание 4
        System.Console.WriteLine("\nTask 4:");

        input = "a";
        Lexer L4 = new LetterDigitLexer(input);

        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a1b2c3";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "aa2b3c";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "r4c2n";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "0a9u5";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Задание 5
        System.Console.WriteLine("\nTask 5:");

        input = "c";
        Lexer L5 = new LettersLexer(input);

        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "c,a,b";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "c,a;b,5;k";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "ca,g,h";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a;b,c;d;e,";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "e,f,g;h";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Дополнительное задание 1
        System.Console.WriteLine("\nExtra Task 1:");

        input = "4";
        Lexer LL1 = new DigitsLexer(input);

        try
        {
            LL1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4 5   2";
        LL1   = new DigitsLexer(input);
        try
        {
            LL1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4,5   2";
        LL1   = new DigitsLexer(input);
        try
        {
            LL1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "1 2  3   4    5";
        LL1   = new DigitsLexer(input);
        try
        {
            LL1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "1 2 34  5  6";
        LL1   = new DigitsLexer(input);
        try
        {
            LL1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Дополнительное задание 2
        System.Console.WriteLine("\nExtra Task 2:");

        input = "aa12c23dd1";
        Lexer LL2 = new DigitsLettersGroupsLexer(input);

        try
        {
            LL2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4";
        LL2   = new DigitsLettersGroupsLexer(input);
        try
        {
            LL2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4sd35r5d5t03ab";
        LL2   = new DigitsLettersGroupsLexer(input);
        try
        {
            LL2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "ab0k";
        LL2   = new DigitsLettersGroupsLexer(input);
        try
        {
            LL2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a4bcd5e";
        LL2   = new DigitsLettersGroupsLexer(input);
        try
        {
            LL2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "ab4c,d5e";
        LL2   = new DigitsLettersGroupsLexer(input);
        try
        {
            LL2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        // Дополнительное задание 3
        System.Console.WriteLine("\nExtra Task 3:");

        input = "1.0";
        Lexer LL3 = new DoubleLexer(input);

        try
        {
            LL3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4";
        LL3   = new DoubleLexer(input);
        try
        {
            LL3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "55.";
        LL3   = new DoubleLexer(input);
        try
        {
            LL3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = ".3";
        LL3   = new DoubleLexer(input);
        try
        {
            LL3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "12.345";
        LL3   = new DoubleLexer(input);
        try
        {
            LL3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Дополнительное задание 4
        System.Console.WriteLine("\nExtra Task 4:");

        input = "a'bcd'";
        Lexer LL4 = new StringLexer(input);

        try
        {
            LL4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "'a'";
        LL4   = new StringLexer(input);
        try
        {
            LL4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "'a'bcd";
        LL4   = new StringLexer(input);
        try
        {
            LL4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "'a123bcd0'";
        LL4   = new StringLexer(input);
        try
        {
            LL4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "'123";
        LL4   = new StringLexer(input);
        try
        {
            LL4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Дополнительное задание 5
        System.Console.WriteLine("\nExtra Task 5:");

        input = "/*12345";
        Lexer LL5 = new CommentLexer(input);

        try
        {
            LL5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "12/*345*/";
        LL5   = new CommentLexer(input);
        try
        {
            LL5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "/*1234*/5";
        LL5   = new CommentLexer(input);
        try
        {
            LL5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "/*12345*/";
        LL5   = new CommentLexer(input);
        try
        {
            LL5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Дополнительное сложное задание
        System.Console.WriteLine("\nHard Extra Task:");

        input = "a225";
        Lexer LLL = new IDListLexer(input);

        try
        {
            LLL.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a225.abcd.e00e00.k";
        LLL   = new IDListLexer(input);
        try
        {
            LLL.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a225.abcd e00e00.k";
        LLL   = new IDListLexer(input);
        try
        {
            LLL.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a225..abcd.e00e00.k";
        LLL   = new IDListLexer(input);
        try
        {
            LLL.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }
    }
Beispiel #4
0
    public static void Main()
    {
        string input = "154216";
        Lexer  L     = new IntLexer(input);

        try
        {
            L.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        //Задание 1
        System.Console.WriteLine("\nTask 1:");

        input = "0";
        Lexer L1 = new NewIntLexer(input);

        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "365";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "-22";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "4-2";
        L1    = new NewIntLexer(input);
        try
        {
            L1.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        //Задание 2
        System.Console.WriteLine("\nTask 2:");

        input = "";
        Lexer L2 = new IDLexer(input);

        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "d";
        L2    = new IDLexer(input);
        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "da236E0";
        L2    = new IDLexer(input);
        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "2abcd";
        L2    = new IDLexer(input);
        try
        {
            L2.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        //Задание 3
        System.Console.WriteLine("\nTask 3:");

        input = "";
        Lexer L3 = new NewIntLexer2(input);

        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "25";
        L3    = new NewIntLexer2(input);
        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "-73060";
        L3    = new NewIntLexer2(input);
        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "012";
        L3    = new NewIntLexer2(input);
        try
        {
            L3.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }


        // Задание 4
        System.Console.WriteLine("\nTask 4:");

        input = "a";
        Lexer L4 = new LetterDigitLexer(input);

        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a1b2c3";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "aa2b3c";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "r4c2n";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "0a9u5";
        L4    = new LetterDigitLexer(input);
        try
        {
            L4.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        // Задание 5
        System.Console.WriteLine("\nTask 5:");

        input = "c";
        Lexer L5 = new LettersLexer(input);

        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "c,a,b";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "c,a;b,5;k";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "ca,g,h";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "a;b,c;d;e,";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }

        input = "e,f,g;h";
        L5    = new LettersLexer(input);
        try
        {
            L5.Parse();
        }
        catch (LexerException e)
        {
            System.Console.WriteLine(e.Message);
        }
    }
Beispiel #5
0
    public static void Main()
    {
        System.Console.WriteLine("Testing IntLexer:");
        List <string> test_int = new List <string> {
            "1", "123", "+123", "-123", "+a", "+", "+1233f", " "
        };

        foreach (var str in test_int)
        {
            IntLexer L = new IntLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing IDLexer:");
        List <string> test_id = new List <string> {
            "a", "a1ad", "a___", "1", "", " "
        };

        foreach (var str in test_id)
        {
            IdLexer L = new IdLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing NoZeroIntLexer:");
        List <string> test_nozeroint = new List <string> {
            "1", "123", "+123", "-123", "+0", "0", "-0", " "
        };

        foreach (var str in test_nozeroint)
        {
            NoZeroIntLexer L = new NoZeroIntLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }


        System.Console.WriteLine("Testing LetterDigitLexer:");
        List <string> test_LetterDigitLexer = new List <string> {
            "a", "a1", "a1a", "1", "a11", "a1aa", "", " "
        };

        foreach (var str in test_LetterDigitLexer)
        {
            LetterDigitLexer L = new LetterDigitLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing ListLexer:");
        List <string> test_ListLexer = new List <string> {
            "a", "a,", "a;b", "a,b;", "ab", "", ",", " "
        };

        foreach (var str in test_ListLexer)
        {
            ListLexer L = new ListLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1} : {2}", str, L.Parse(), string.Join(",", L.chrs)));
        }


        System.Console.WriteLine("Testing DigitListLexer:");
        List <string> test_DigitListLexer = new List <string> {
            "1", "  1 2    3", "    ", "", "1 2 3 b"
        };

        foreach (var str in test_DigitListLexer)
        {
            DigitListLexer L = new DigitListLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1} : {2}", str, L.Parse(), string.Join(",", L.ints)));
        }

        System.Console.WriteLine("Testing GroupLexer:");
        List <string> test_GroupLexer = new List <string> {
            "aa12c23dd1", "aaa12c23dd1", "aa12c232dd1", " ", ""
        };

        foreach (var str in test_GroupLexer)
        {
            GroupLexer L = new GroupLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1} : {2}", str, L.Parse(), L.result));
        }


        System.Console.WriteLine("Testing RealLexer:");
        List <string> test_RealLexer = new List <string> {
            "+12", "12", "+0.1", "123.b", " ", ""
        };

        foreach (var str in test_RealLexer)
        {
            RealLexer L = new RealLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }

        System.Console.WriteLine("Testing StringLexer:");
        List <string> test_StringLexer = new List <string> {
            "'asd'", "''", "a", "'aaa'a", "'aaa", " ", ""
        };

        foreach (var str in test_StringLexer)
        {
            StringLexer L = new StringLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }


        System.Console.WriteLine("Testing CommentLexer:");
        List <string> test_CommentLexer = new List <string> {
            "/*ddd*/", "/**/", "/*fff", " ", ""
        };

        foreach (var str in test_CommentLexer)
        {
            CommentLexer L = new CommentLexer(str);
            System.Console.WriteLine(System.String.Format("{0} : {1}", str, L.Parse()));
        }
    }