Пример #1
0
 public void Build(string[] Input)
 {
     try
     {
         Start = Input[0][0];
         foreach (string s in Input)
         {
               if (s == string.Empty)
               {
                   continue;
               }
               string[] tp = s.Split(new String[] { "→" }, StringSplitOptions.None);
               if (tp.Length != 2)
               {
                   throw new Exception("产生式应该有且仅有一个→");
               }
               if (tp[0].Length != 1 || !isVn(tp[0][0]))
               {
                   throw new Exception("产生式左边应该是一个非终结符");
               }
               char left = tp[0][0];
               char empty= System.Configuration.ConfigurationManager.AppSettings["Empty"][0];
               Vn.Add(left);
               string[] right;
               if (tp[1][0] == '|')
               {
                   right = new String[1] { tp[1] };
               }
               else
               {
                   right = tp[1].Split('|');
               }
               for (int i = 0; i < right.Count(); ++i)
               {
                   string part = right[i];
                   foreach (char c in part)
                   {
                       if (isVn(c))
                       {
                           Vn.Add(c);
                       }
                       else
                       {
                           Vt.Add(c);
                       }
                   }
                   Production p = new Production(left,part);
                   if(isVn(part[0]) || part.Length>2 || (part.Length==2 && !isVn(part[1])))
                   {
                       throw new Exception("不是一个合格的三型文法");
                   }
                   if (!ProductionsMap.ContainsKey(p))
                   {
                       Productions.Add(p);
                       ProductionsMap.Add(p, ProductionsMap.Count() + 1);
                       _ProductionsMap.Add(_ProductionsMap.Count() + 1, p);
                   }
               }
         }
     }
     catch (Exception )
     {
         throw new Exception("输入不合法\n");
     }
 }
Пример #2
0
 public void Build(string[] Input)
 {
     try
     {
         Start = Input[0][0];
           foreach (string s in Input)
         {
             if (s == string.Empty)
             {
                 continue;
             }
             string[] tp = s.Split(new String[] { "→" }, StringSplitOptions.None);
             if (tp.Length != 2)
             {
                 throw new Exception("产生式应该有且仅有一个→");
             }
             if (tp[0].Length != 1 || !isVn(tp[0][0]))
             {
                 throw new Exception("产生式左边应该是一个非终结符");
             }
             char left  = tp[0][0];
             char empty = System.Configuration.ConfigurationManager.AppSettings["Empty"][0];
             Vn.Add(left);
             string[] right;
             if (tp[1][0] == '|')
             {
                 right = new String[1] {
                     tp[1]
                 };
             }
             else
             {
                 right = tp[1].Split('|');
             }
             for (int i = 0; i < right.Count(); ++i)
             {
                 string part = right[i];
                 foreach (char c in part)
                 {
                     if (isVn(c))
                     {
                         Vn.Add(c);
                     }
                     else
                     {
                         Vt.Add(c);
                     }
                 }
                 Production p = new Production(left, part);
                 if (isVn(part[0]) || part.Length > 2 || (part.Length == 2 && !isVn(part[1])))
                 {
                     throw new Exception("不是一个合格的三型文法");
                 }
                 if (!ProductionsMap.ContainsKey(p))
                 {
                     Productions.Add(p);
                     ProductionsMap.Add(p, ProductionsMap.Count() + 1);
                     _ProductionsMap.Add(_ProductionsMap.Count() + 1, p);
                 }
             }
         }
     }
     catch (Exception)
     {
         throw new Exception("输入不合法\n");
     }
 }