扫描,解析

时间:2013-11-16 05:53:50

标签: c# compiler-construction

我正在编写简单的编译器而不使用语法生成器。 Here 。我编写了读取file.txt的扫描代码,并将其分类为Identifier或Revers单词,Number,Variable ..等。 我得到了我想要的结果但是可以用这个代码来完成我的意思是应用语法分析吗?和解析? 。因为我尝试用java编写,我用错误的方式编写它。我想看看我做得对,特别是我不擅长C#codding。

阵列:

 string[] Rw = new string[8];
        Rw[0] = "for";
        Rw[1] = "while";
        Rw[2] = "end";
        Rw[3] = "contiune";
        Rw[4] = "if";
        Rw[5] = "break";
        Rw[6] = "do";
        Rw[7] = "return";
        string[] id = new string[6];
        id[0] = "int";
        id[1] = "float";
        id[2] = "string";
        id[3] = "double";
        id[4] = "bool";
        id[5] = "char";
        string[] op = new string[6];
        op[0] = "+";
        op[1] = "-";
        op[2] = "/";
        op[3] = "*";
        op[4] = "%";
        string[] Num = new string[10];
        Num[0] = "0";
        Num[1] = "1";
        Num[2] = "2";
        Num[3] = "3";
        Num[4] = "4";
        Num[5] = "5";
        Num[6] = "6";
        Num[7] = "7";
        Num[8] = "8";
        Num[9] = "9";

        string[] var = new string[17];
     var[0] = "a";
     var[1] = "b";
     var[2] = "A";
     var[3] = "B";
     var[4] = "C";
     var[5] = "d";
     var[6] = "D";
     var[7] = "X";
     var[8] = "x";
     var[9] = "Y";
     var[10] = "y";
     var[11] = "z";
     var[12] = "Z";
     var[13] = "v";
     var[14] = "V";
     var[15] = "f";
     var[16] = "F";

阅读和分裂:

        char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
        using (System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\caza\Desktop\1.txt"))
    {
        while ((line = file.ReadLine()) != null)
        {


            string[] token = line.Split(delimiterChars);


            foreach (string s in token)
            {
             // Console.WriteLine(s);
                foreach (string z in Rw)
                {
                    if (s == z)
                    {

                        Console.WriteLine(s + " is a Reserved Word ");

                    }

                }
                foreach (string y in id)
                {
                    if (s == y)
                    {
                        Console.WriteLine(s + " is a identifier ");

                    }
                }
                    foreach (string o in op)
                    {
                        if (s == o)
                        {
                            Console.WriteLine(s + " is a Operation ");
                        }
                    }

                    foreach (string n in Num)
                    {
                        if (s == n)
                        {
                            Console.WriteLine(s + " is  a Number ");
                        }
                    }
                    foreach (string v in var)
                    {
                        if (s == v)
                        {



                            Console.WriteLine(s + " is a Variable");
                        }
                    }
            }


            }
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

0 个答案:

没有答案