Example #1
0
File: SLR.cs Project: gu0o00/gSQL
        public void ReadGrammar(string file)
        {
            StreamReader fs = null;

            try
            {
                fs = new StreamReader(file);
            }catch (Exception) {
                Console.WriteLine("打开文件出现问题!");
                return;
            }
            while (fs.EndOfStream != true)
            {
                String    aline        = fs.ReadLine();
                String[]  part         = aline.Split("->".ToCharArray());
                String    lBufen       = part[0];
                String[]  rBufen       = part[2].Split(' ');
                TuidaoShi newTuidaoshi = new TuidaoShi();
                newTuidaoshi.Add(lBufen, rBufen);
                garmmarList.Add(newTuidaoshi);
                //add into allFuhao
                if (allFuhao.Contains(lBufen) == false)
                {
                    allFuhao.Add(lBufen);
                }
                foreach (string a in rBufen)
                {
                    if (allFuhao.Contains(a) == false)
                    {
                        allFuhao.Add(a);
                    }
                }
            }
        }
Example #2
0
 public bool Equals(TuidaoShi i)
 {
     if (i.ElementAt(0).Key.Equals(this.ElementAt(0).Key) == true &&
         i.ElementAt(0).Value.Length == this.ElementAt(0).Value.Length)
     {
         for (int k = 0; k < this.ElementAt(0).Value.Length; k++)
         {
             if (i.ElementAt(0).Value[k].Equals(this.ElementAt(0).Value[k]) == false)
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
Example #3
0
File: SLR.cs Project: gu0o00/gSQL
        public bool Drive(string file, out string output)       //file 是总符号表
        {
            output = "";
            StreamReader  fs    = new StreamReader(file);
            List <string> stack = new List <string>();

            stack.Add("0");
            fs.ReadLine();              //第一行标题,没用
            string[] aLine  = fs.ReadLine().Split('\t');
            string   aFuhao = aLine[0];

            while (true)
            {
                int      stackstatus = Convert.ToInt32(stack.ElementAt(stack.Count - 1));
                string[] status      = actionTable.Get(stackstatus, aFuhao);

                if (status == null)
                {
                    Console.WriteLine("错误。。");
                    output += "错误。。。\n";
                    fs.Close();
                    return(false);
                }
                else if (status[0].Equals("s"))
                {
                    string info = null;
                    stack.Add(aFuhao);
                    stack.Add(status[1]);

                    aLine  = fs.ReadLine().Split('\t');
                    aFuhao = aLine[0];

                    foreach (string i in stack)
                    {
                        info += i + " ";
                    }
                    info   += "移动进栈\t\t" + aFuhao + "\n";
                    output += info;
                    Console.WriteLine(info);

                    if (aFuhao == "NUM")
                    {
                        int i = Convert.ToInt16(aLine[1]);
                        shuStack.Push(numTable[i]);
                    }
                    else if (aFuhao == "ID")
                    {
                        int i = Convert.ToInt16(aLine[1]);
                        shuStack.Push(idTable[i]);
                    }
                    else if (aFuhao != "(" && aFuhao != ")" && aFuhao != ";" && aFuhao != "{" && aFuhao != "}")
                    {
                        shuStack.Push(aFuhao);
                    }
                }
                else if (status[0].Equals("r"))
                {
                    string    info          = null;
                    int       n             = Convert.ToInt32(status[1]);
                    TuidaoShi needTuidaoshi = garmmarList[n];
                    int       len           = needTuidaoshi.ElementAt(0).Value.Length;
                    for (int i = 0; i < len * 2; i++)
                    {
                        stack.RemoveAt(stack.Count - 1);
                    }
                    stackstatus = Convert.ToInt32(stack.ElementAt(stack.Count - 1));
                    stack.Add(needTuidaoshi.ElementAt(0).Key);
                    int go = gotoTable.Get(stackstatus, needTuidaoshi.ElementAt(0).Key);
                    stack.Add(go.ToString());

                    foreach (string i in stack)
                    {
                        info += i + " ";
                    }
                    info   += "用 ";
                    info   += needTuidaoshi.ToString();
                    info   += " 进行归约\t";
                    info   += aFuhao + "\n";
                    output += info;
                    //////////////////////////////////////////////////////////////
                }
                else if (status[0].Equals("acc"))
                {
                    Console.WriteLine("yes");
                    output += "yes" + "\n";
                    fs.Close();
                    return(true);
                }
                else
                {
                    Console.WriteLine("no");
                    output += "no" + "\n";
                    fs.Close();
                    return(false);
                }
            }
        }