Beispiel #1
0
        static void test(string fname)
        {
            Console.WriteLine("Testing file: " + fname);
            Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%");
            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%");
            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            scanner.IdList = new List <string>();
            int tok = 0;

            do
            {
                tok = scanner.yylex();

                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);
            Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%");
            Console.WriteLine(String.Format("ID count:{0} ; maxlen:{1}; minlen:{2}; avglen:{3};", scanner.LexCount, scanner.LexMaxLength, scanner.LexMinLength, scanner.LexAvgLength()));
            Console.WriteLine(String.Format("Sum int: {0}; double: {1}", scanner.LexIntSum, scanner.LexDoubleSum));
            Console.WriteLine(String.Format("IDs in MLcomment: {0}", String.Join(", ", scanner.IdList.ToArray())));
            Console.WriteLine("--------------------------------------------------\n");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture)
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var fname = @"..\..\a.txt";

            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("-------------------------");

            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            int tok = 0;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);

            Console.ReadKey();
        }
Beispiel #3
0
        static void TestFile(Scanner scanner)
        {
            Console.WriteLine("-------------------------");

            int tok = 0;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);
            Console.WriteLine("-------------------------");
        }
Beispiel #4
0
        static IEnumerable <Tok> iterateThrough(Scanner scanner)
        {
            int tok = 0;

            do
            {
                tok = scanner.yylex();

                if (tok == (int)Tok.EOF)
                {
                    yield break;
                }
                else
                {
                    yield return((Tok)tok);
                }
            } while (true);
        }
Beispiel #5
0
        static void parser(string fname)
        {
            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("-------------------------");

            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            int tok = 0;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);

            if (!scanner.flag)
            {
                scanner.SynError();
            }
            else
            {
                Console.WriteLine("Count ID: {0}", scanner.cntID);
                Console.WriteLine("Average length of ID: {0}", scanner.AverageID());
                Console.WriteLine("Min length of ID: {0}", scanner.minID);
                Console.WriteLine("Max length of ID: {0}", scanner.maxID);
                Console.WriteLine("Sum of ints: {0}", scanner.sumInt);
                Console.WriteLine("Sum of doubles: {0}", scanner.sumDouble);
                if (scanner.lst.Count != 0)
                {
                    Console.Write("All ID in multiline comment: ");
                    foreach (string str in scanner.lst)
                    {
                        Console.Write(str + ' ');
                    }
                    Console.WriteLine();
                }
            }
            Console.ReadKey();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture)
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var fname = @"..\..\a.txt";

            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("-------------------------");

            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            // Задание 2
            int ID_count   = 0;
            int ID_sum_len = 0;
            int ID_max_len = 0;
            int ID_min_len = int.MaxValue;

            // Задание 3
            int    INUM_sum = 0;
            double RNUM_sum = 0;

            int tok = 0;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.EOF)
                {
                    break;
                }

                // Задание 2
                if (tok == (int)Tok.ID)
                {
                    ++ID_count;
                    int l = scanner.yytext.Length;
                    ID_sum_len += l;
                    if (l > ID_max_len)
                    {
                        ID_max_len = l;
                    }
                    if (l < ID_min_len)
                    {
                        ID_min_len = l;
                    }
                }

                // Задание 3
                else if (tok == (int)Tok.INUM)
                {
                    INUM_sum += scanner.LexValueInt;
                }
                else if (tok == (int)Tok.RNUM)
                {
                    RNUM_sum += scanner.LexValueDouble;
                }

                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);

            Console.WriteLine("\nID_count = " + ID_count.ToString());
            Console.WriteLine("ID_max_len = " + ID_max_len.ToString());
            Console.WriteLine("ID_min_len = " + ID_min_len.ToString());
            Console.WriteLine("ID_avg_len = " +
                              (ID_sum_len / (double)ID_count).ToString());
            Console.WriteLine("Integer sum = " + INUM_sum.ToString());
            Console.WriteLine("Real sum = " + RNUM_sum.ToString());

            Console.ReadKey();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture)
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var fname = @"..\..\a.txt";

            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("-------------------------");

            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            int    tok     = 0;
            int    cnt_id  = 0;
            double avg_len = 0d;
            int    max_len = Int32.MinValue;
            int    min_len = Int32.MaxValue;

            scanner.Ids = new System.Collections.Generic.List <String>();

            int    int_sum = 0;
            double d_sum   = 0d;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.ID)
                {
                    ++cnt_id;
                    int len = scanner.yytext.Length;
                    avg_len += len;
                    if (len > max_len)
                    {
                        max_len = len;
                    }
                    if (len < min_len)
                    {
                        min_len = len;
                    }
                }
                if (tok == (int)Tok.INUM)
                {
                    int_sum += Convert.ToInt32(scanner.yytext);
                }
                if (tok == (int)Tok.RNUM)
                {
                    d_sum += Convert.ToDouble(scanner.yytext);
                }
                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);

            avg_len /= cnt_id;
            Console.WriteLine(@"count of ids = {0}, average lenght = {1},
max lenght = {2}, min lenght = {3}", cnt_id, avg_len, max_len, min_len);
            Console.WriteLine("sum of inum = {0}, sum of rnum = {1}", int_sum, d_sum);

            Console.WriteLine("IDs in comments: ");
            foreach (String s in scanner.Ids)
            {
                Console.WriteLine(s);
            }

            Console.ReadKey();
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture)
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var fname = @"..\..\a.txt";

            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("-------------------------");

            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            int tok = 0;
            //Подсчитать количество, среднюю, минимальную и максимальную длину всех идентификаторов
            int cnt = 0, min = Int32.MaxValue, max = Int32.MinValue, avg = 0;

            //Найти сумму всех целых и сумму всех вещественных в файле
            int    sum_int    = 0;
            double sum_double = 0;
            int    cntID      = 0;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                if (tok == (int)Tok.ID)
                {
                    ++cnt;
                    int l = scanner.yyleng;
                    avg += l;
                    min  = Math.Min(min, l);
                    max  = Math.Max(max, l);
                }
                if (tok == (int)Tok.INUM)
                {
                    sum_int += Int32.Parse(scanner.yytext);
                }
                if (tok == (int)Tok.RNUM)
                {
                    sum_double += Double.Parse(scanner.yytext);
                }
                if (tok == (int)Tok.LONGCOMMENT)
                {
                    cntID += scanner.cntID;
                }

                Console.WriteLine(scanner.TokToString((Tok)tok));
            } while (true);

            Console.WriteLine();
            Console.WriteLine("Task 2");
            Console.WriteLine("Cnt ID: {0}", cnt);
            Console.WriteLine("Avg length ID: {0}", (avg / cnt));
            Console.WriteLine("Min length ID: {0}     Max length ID: {1}", min, max);
            Console.WriteLine();
            Console.WriteLine("Task 3");
            Console.WriteLine("Sum int: {0}      Sum double: {1}", sum_int, sum_double);
            Console.WriteLine();
            Console.WriteLine("Task 4 ad");
            Console.WriteLine("CntID {0}", cntID);



            Console.ReadKey();
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture)
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var fname = @"..\..\a.txt";

            Console.WriteLine(File.ReadAllText(fname));
            Console.WriteLine("-------------------------");

            Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open));

            int    numberOfTokens = 0;
            int    min = int.MaxValue, max = 0;
            double avg = 0;

            int    intSum  = 0;
            double realSum = 0.0;

            int tok = 0;

            do
            {
                tok = scanner.yylex();
                if (tok == (int)Tok.EOF)
                {
                    break;
                }
                Console.WriteLine(scanner.TokToString((Tok)tok));

                ++numberOfTokens;
                int len = scanner.yyleng;
                if (len < min)
                {
                    min = len;
                }
                if (max < len)
                {
                    max = len;
                }
                avg += len;

                if (tok == (int)Tok.INUM)
                {
                    intSum += scanner.LexValueInt;
                }
                else if (tok == (int)Tok.RNUM)
                {
                    realSum += scanner.LexValueDouble;
                }
            } while (true);
            avg /= numberOfTokens;

            Console.WriteLine();
            Console.WriteLine("count = " + numberOfTokens);
            Console.WriteLine("min length = " + min);
            Console.WriteLine("max length = " + max);
            Console.WriteLine("average length = " + avg);
            Console.WriteLine();
            Console.WriteLine("int sum = " + intSum);
            Console.WriteLine("real sum = " + realSum);
            Console.WriteLine();
            Console.WriteLine("ids in multiline comments: " + String.Join(", ", scanner.Ids.ToArray()));

            Console.ReadKey();
        }