正文

编译原理课程设计,词法自动机。2006-05-24 19:19:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hofy/14805.html

分享到:

//编译原理课程设计,词法自动机。 import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;import java.util.*;class frame extends JFrame implements ActionListener{ JButton button1=new JButton("打开现有文件"); JButton button2=new JButton("现场输入文件"); Choice ch=new Choice(); JButton button=new JButton("执行"); TextArea ta=new TextArea(); TextArea tb=new TextArea(); JFileChooser chooser=new JFileChooser(); FileInputStream readfile=null;  int b; byte buffer[]=new byte[25000]; String str=null; String id[]={"asm","cout","main","FALSE","TRUE","include","boolean","default","float","operator","abs","static_cast","union","auto","delete","for","private","struct","unsigned","bool","do","friend","protected","switch","using","break","double","goto","public","template","virtual","case","dynamic_cast","if","register","this","void","catch","else","inline","reinterpret_cast","throw","volatile","char","enum","int","return","true","wchar_t","class","explicit","long","short","try","while","const","export","mutable","signed","typedef","const_cast","extern","namespace","sizeof","typeid","Continue","false","new","static","typename"};  FileReader file=null; BufferedReader in=null; File f=null; frame() {  super();  Container c=this.getContentPane();  ch.add("C语言常数识别");  ch.add("C语言标识符识别");  ch.add("C语言注释字母大写");  ch.add("C语言保留字大写");  c.setLayout(null);  c.add(button1);  button1.setBounds(5,5,120,25);  button1.addActionListener(this);    c.add(button2);  button2.setBounds(130,5,120,25);  button2.addActionListener(this);    c.add(ch);  ch.setBounds(260,5,120,25);    c.add(button);  button.setBounds(390,5,80,25);  button.addActionListener(this);    c.add(ta);  ta.setBounds(5,40,482,200);  ta.setEditable(false);    c.add(tb);  tb.setBounds(5,250,482,200);  tb.setEditable(false);    this.setBounds(300,100,500,500);  this.setVisible(true);  this.setResizable(false);  this.addWindowListener(new WindowAdapter()    {     public void windowClosing(WindowEvent e)     {      System.exit(0);     }    }); } public void actionPerformed(ActionEvent ae) {  //-------------输入原文件----------------  if(ae.getSource()==button1)  {   String s;   ta.setText(null);   int state=chooser.showOpenDialog(null);   f=chooser.getSelectedFile();   if(f!=null&&state==JFileChooser.APPROVE_OPTION)   {    try    {     readfile=new FileInputStream(f);    }    catch(IOException ee){}    try    {     b=readfile.read(buffer,0,25000);     str=new String(buffer,0,b);     ta.append(str);    }    catch(IOException e1)    {     System.out.println("File read error");    }   }  }  //-------------输入原文件----------------  else if(ae.getSource()==button2)  {   frame.this.ta.setEditable(true);  }  else if(ae.getSource()==button)  {   //-------------C语言常数识别---------------   if(ch.getSelectedIndex()==0)   {    tb.setText(null);    StringTokenizer sign=new StringTokenizer(str," []()={}#;\n ");    int n=sign.countTokens();    while(sign.hasMoreTokens())    {     String s=sign.nextToken();     int n1=s.length();     String s1=s.substring(0,1);     byte d[]=s1.getBytes();     if((d[0]<=57&&d[0]>=48)||d[0]==45)     {      int j=0;      for(int i=1;i<n1;i++)      {       String ss=s.substring(i,i+1);       byte f[]=ss.getBytes();       if(f[0]<=57&&f[0]>=48)       {        j+=1;       }       }      if(j==n1-1)      {       tb.append(s+"\n");      }       }     if((d[0]<=57&&d[0]>=48)||d[0]==45)     {      int j=0;      for(int i=1;i<n1;i++)      {       String ss=s.substring(i,i+1);       byte f[]=ss.getBytes();       if((f[0]<=57&&f[0]>=48)||f[0]==46)       {        j+=1;       }      }      if(j==n1-1)      {       tb.append(s+"\n");      }      }     if(d[0]==39)     {      int j=0;      for(int i=1;i<n1;i++)      {       String ss=s.substring(i,i+1);       byte f[]=ss.getBytes();       if(f[0]==39||(f[0]<=122&&f[0]>=97)||(f[0]<=90&&f[0]>=65))       {        j+=1;       }      }      if(j==n1-1)      {       tb.append(s+"\n");      }     }     else     {      String t="TRUE";      String f="FALSE";      if(s.equals(t)||s.equals(f))      {       tb.append(s+"\n");      }     }    }   }   //-------------C语言常数识别---------------   //-------------C语言标志符识别---------------   else if(ch.getSelectedIndex()==1)   {    tb.setText(null);    StringTokenizer sign=new StringTokenizer(str," []()={}#;\n ");    int n=sign.countTokens();    while(sign.hasMoreTokens())    {     String s=sign.nextToken();     int n1=s.length();     String s1=s.substring(0,1);     byte d[]=s1.getBytes();     if((d[0]<=122&&d[0]>=97)||(d[0]<=90&&d[0]>=65)||d[0]==95)     {      int j=0;      for(int i=1;i<n1;i++)      {       String ss=s.substring(i,i+1);       byte f[]=ss.getBytes();       if((f[0]<=122&&f[0]>=97)||(f[0]<=90&&f[0]>=65)||(f[0]==95)||(f[0]<=57&&f[0]>=48))       {        j+=1;       }      }      if(j==n1-1)      {       int m=0;       for(int k=0;k<id.length;k++)       {        if(s.equals(id[k]))        {         m++;        }       }       if(m==0)       {        tb.append(s+"\n");       }      }       }    }   }   //-------------C语言标志符识别---------------   //-------------C语言注释大写---------------   else if(ch.getSelectedIndex()==2)   {    tb.setText(null);    try    {     file=new FileReader(f);     in=new BufferedReader(file);    }    catch(FileNotFoundException er)    {}    catch(IOException er)    {}    String s;    try    {     while((s=in.readLine())!=null)     {      if(s.startsWith("/*"))      {       while(s.indexOf("*/")==-1)       {        s=s+in.readLine();       }       for(int i=1;i<=s.length();i++)       {        String s2=s.substring(i-1,i);        byte f[]=s2.getBytes();        if(f[0]>=97&&f[0]<=122)        {         s=s.toUpperCase();         }       }       tb.append(s+"\n");      }      else      {       if(s.startsWith("//"))       {        for(int j=1;j<=s.length();j++)        {         String s3=s.substring(j-1,j);         byte d[]=s3.getBytes();         if(d[0]>=97&&d[0]<=122)         {          s=s.toUpperCase();         }        }        tb.append(s+"\n");              }       else       {        if(!(s.indexOf("//")==-1))        {         s=s.substring(s.indexOf("//"));         for(int k=1;k<=s.length();k++)         {          String s4=s.substring(k-1,k);          byte g[]=s4.getBytes();          if(g[0]>=97&&g[0]<=122)          {           s=s.toUpperCase();          }         }         tb.append(s+"\n");        }       }       }     }    }    catch(IOException ext){}   }   //-------------C语言注释大写---------------   //-------------C语言保留字大写---------------   else if(ch.getSelectedIndex()==3)   {    tb.setText(null);    try    {     file=new FileReader(f);     in=new BufferedReader(file);    }    catch(FileNotFoundException er){}    catch(IOException er){}    String s;    try    {     while((s=in.readLine())!=null)     {      if(s.indexOf("//")==-1)      {       StringTokenizer o=new StringTokenizer(s,"  #()<>*+-/!");       while(o.hasMoreTokens())       {        String s1=o.nextToken();        int m=0;        for(int i=1;i<id.length;i++)        {         if(s1.equals(id[i]))         {          m++;         }        }        if(!(m==0))        {         s1=s1.toUpperCase();         tb.append(s1+"\n");        }       }      }      else      {       if(!(s.indexOf("//")==-1))       {        s=s.substring(0,s.indexOf("//"));        StringTokenizer o=new StringTokenizer(s,"  #()<>*+-/!");        while(o.hasMoreTokens())        {         String s1=o.nextToken();         int m=0;         for(int i=1;i<id.length;i++)         {          if(s1.equals(id[i]))          {           m++;          }         }                if(!(m==0))         {          s1=s1.toUpperCase();          tb.append(s1+"\n");         }        }       }      }     }    }    catch(IOException ext){}   }   //-------------C语言保留字大写---------------  } }}public class Design{ public static void main(String args[]) {  new frame(); }}

阅读(6287) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册