启天论坛软件硬件技术.net技术 → 用C#编的一段求单个文法的FIRST集合的扫描程序


  共有1348人关注过本帖树形打印

主题:用C#编的一段求单个文法的FIRST集合的扫描程序

帅哥哟,离线,有人找我吗?
whjczu
  1楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:新兵 帖子:11 积分:136 威望:0 精华:0 注册:2006-7-11 10:25:00
用C#编的一段求单个文法的FIRST集合的扫描程序  发帖心情 Post By:2006-7-12 10:31:00



public class createfileClass
 {
  
   private string  FileName;    //文件名
   
      private StreamWriter sr;
      public  int createfile( string name)
      {
    FileName=name;
    if(File.Exists(FileName))
    {
     Console.WriteLine("   ");
     Console.WriteLine("         该文件名已经存在,请重新输入文件名!");
     Console.WriteLine ("   ");
     return 0;
    }
    
    else
    {
     sr = File.CreateText(FileName);
     Console.WriteLine("   ");
     Console.WriteLine("         您的文件创建成功!");
     Console.WriteLine ("   ");
     sr.Close();
     return 1;
   
    }
      }
         
 }
class mainClass
 {
  
  [STAThread]
  static void Main(string[] args)
  { 
    string  filename;    //文件名
   string fileresult;  //文件内容
     int t;              //判断符
     int count=0;
     int m;
     int a;
     char first;
   char o_first;
   char m_first;
   char s;             //定义开始符号
   int i;
   int j=0;
   string f;
      createfileClass  cf=new createfileClass();
    methodClass  md=new methodClass();
         tt:  Console.Write("请输入您要创建的文件名:");
    filename=Console.ReadLine();
    t=cf.createfile(filename);
   if(t==0)
   {
    goto  tt;
   }
   else
   {
   ff: Console.Write("请输入要求的集合的行数:");
    try
    {
     m=Int32.Parse(Console.ReadLine());
    }
    catch
    {
     Console.WriteLine("输入格式不正确,请重新输入!");
     goto ff;
    }
    Console.Write("请输入开始符号:");
    s=char.Parse(Console.ReadLine());
    Console.WriteLine("请输入文件的内容(并保存到文件里):");
       string[] input=new string[m];
                StreamWriter sr=new StreamWriter(filename);
    for(a=0;a<m;a++)
        {
     count=count+1;
     Console.Write(count+":");
                 Console.Write("------");
     input[a]=Console.ReadLine();
                     sr.WriteLine(input[a]);
    }
    sr.Close();
     StreamReader st=new StreamReader(filename);   //创建整体文件对象
     Console.WriteLine("FIRST集合:");
    for(a=0;a<m;a++)
    {
     
     j++;
     Console.Write (j+":");
     fileresult=st.ReadLine();                //开始读取文件
     //求单个符号的FIRST
                         //(1)当X为终结符时
     if((fileresult[0]>='a'&& fileresult[0]<='z'))
     {
      first=md.first(fileresult[0]);
      Console.WriteLine("first("+fileresult[0]+")"+"="+first);
      
     }
     else if(fileresult[0]=='('||fileresult[0]==')')
     {
      first=md.first(fileresult[0]);
      Console.WriteLine("first("+fileresult[0]+")"+"="+first);
     }
     else if(fileresult[0]=='['|| fileresult[0]=='e')
     {
      first=md.first(fileresult[0]);
      Console.WriteLine("first("+fileresult[0]+")"+"="+first);
     }
      //(2)当X为非终结符,产生式为X->a...时
     else if(fileresult[3]>='a'&& fileresult[3]<='z')
     {
      if( fileresult[0]>='A'&& fileresult[0]<='Z')
      {
       first=md.first(fileresult[3]);
       Console.WriteLine("first("+fileresult[0]+")"+"="+first);
       
      }
     }
                   
      //(3)当X为非终结符,如果它有产生式X->Y...Y为非终结符时
                       
     else
     {
      if(fileresult[3]>='A'&& fileresult[3]<='Z')
      {
       
       
        o_first=fileresult[0];
        m_first=fileresult[3];
        StreamReader sn=new StreamReader(filename);       //创建搜索文件对象
        for(i=0;i<m;i++)
        {  
         
         //重新读取文件
         f=sn.ReadLine();
         if(f[0]==m_first)
         {
          
           Console.WriteLine("first("+o_first+")"+"="+f[3]);
           break;
          
         }
              
        }
        sn.Close(); 
       
        
      }
      
     }
     
         //求单个文法符号的Follow
                        //(1)若U是文法的开始识别符号   
     if(fileresult[0]==s)
     {
      Console.WriteLine("  FOLLOW"+"("+fileresult[0]+")"+"="+"$");
     }
                       st.Close();
     Console.ReadLine();

    }
  }
 }
          public class methodClass
 {
  public char first(char x)
  {

    return x;


  }

 }


启天合组-10人 15人 20人 多人合租空间 支持(0中立(0反对(0回到顶部
帅哥,在线噢!
junsan
  2楼 个性首页 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 总掌门
等级:管理员 帖子:11764 积分:7955 威望:757 精华:24 注册:2005-7-14 15:13:00
  发帖心情 Post By:2006-7-12 12:46:00

支持一下。



主页:http://www.rsky.com.cn

培训论坛:http://www.javastar.org

Blog:http://blog.csdn.net/junnef

技术交流QQ:334620162

培训咨询QQ:66883074

合租咨询QQ:631066985

MSN:junsanjin@hotmail.com

Emial:junsanjin@263.net

传真:(010)950507转747393
启天合组-专业合租空间提供商 支持(0中立(0反对(0回到顶部