⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dos_words.java

📁 主要是编译原理的实验,实验内容是词法分析 用java写的
💻 JAVA
字号:
import java.io.*;
public class dos_words
{
       static  StringBuffer  string=new   StringBuffer("");//定义一个空字符串
        public static  void main(String  args[])   throws  java.io.IOException//抛出输入输出异常
        {
             char  ch;
             int     k;
             System.out.println("C语言词法分析:");
             System.out.println("分析的对象为program.txt,");
             System.out.println("*****分析结果*****");
             try
             {
                BufferedReader  program_file=new BufferedReader(new FileReader("program.txt"));
                //读取所要分析的源程序,名为program.txt
                while(program_file.ready())
                {
                    String  line=program_file.readLine();//读取文件的一行
                    for(int  i=0;i<line.length();i++)
                    {
                       ch=line.charAt(i);//每次读其一个字符,保存在ch中
                       if(ch==' ')//若是空格,则重新读取
                           continue;
                       if(ch=='/')//判断有没有注释,即/*  */
                       {
                          i++;
                          if(line.charAt(i)=='*')//若有注释,后都不用读,直到遇到*/
                          {
                             do
                             {
                            	 i++;
                                 if(i>=line.length()-1)//假如注释不只一行,要在下一行找*/
                                 {
                                    line=new String(program_file.readLine());
                                    i=0;//下标重0开始
                                 }
                              }while(line.charAt(i)!='*'||line.charAt(i+1)!='/');//直到遇到*/在一块
                              i++;
                              continue; //不做下面的就直接进入i++循环
                           }
                           else i--; //发现不是解释下标就退回原处
                        }
                        if(Ischar(ch))//处理遇到的是变量名或关键字
                        {
                           char  word[]=new char[20];//保存一单词
                           int j=0;                                  //单词数组的下标
                           while(Ischar(ch)||Isdigit(ch))
                           //判断字符下一个还是不是这个单词里的,是则保存进去
                           {
                              word[j]=ch;            
                              i++;
                              j++;
                              ch=line.charAt(i);
                           }
                           word[j]='\0';
                           dowith_word(word);
                           i--;
                           j=0;
                         }                                                                     
                      else if(Isdigit(ch))      //处理遇到的是数字
                           {
                             char   digit[]=new char[20];   
                             int  j=0;
                             while(Isdigit(ch))
                             {
                                digit[j]=ch;
                                i++;
                                j++;
                               ch=line.charAt(i);
                             }
                             digit[j]='\0';  
                             dowith_word(digit);
                             i--;
                             j=0;
                            }        
                        else//处理遇到的是符号等
                        {
                           char zifu[]=new char[4];
                           int j=0;
                           zifu[j]=ch;
                           j++;
                           zifu[j]='\0';
                           dowith_word(zifu);
                           j=0;
                        }
                    }
                }
             }
             catch(Exception e)
             {    System.out.println("读取program.txt文件有误:"+e.getMessage());   }
             try
             {
                   RandomAccessFile             Random_file=new   RandomAccessFile ("result","rw");
                   //创建一个输入输出RandomAccessFile 流,文件名为result的txt文档
                   Random_file.seek(Random_file.length());//定位文件指针在文件的最后
                   Random_file.writeBytes(string.toString());//向文件写入分析的数据
                   Random_file.close();
               }
              catch(Exception  e)
              {     e.printStackTrace();}    //打印出错的地方  
           System.out.println("*****结束******");   
           System.out.println("分析结果已存入result.txt文件中");
        }
        public   static  boolean  Ischar(char  ch)  //判断是不是字符(这包括大写和小写的字符)
        {  
            if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
               return true;
            else  return false;
        }
        public static boolean Isdigit(char ch)   //判断是不是数字
        {  
           if(ch>='0'&&ch<='9')
               return true;
           else    return false;
        }
        public static void dowith_word(char a[])
        {
            int   count=1;
            try
            {
                 BufferedReader Symbols_file = new BufferedReader(new FileReader("Symbols.txt")); 
                 //从关键字和运算符存储的Symbols.txt文件中读取数据
                 String s=new String(a);    //把传下来的数组转化为字符串
                 s=s.trim();         //把转化的字符串去除空格
                 while(Symbols_file.ready())  //判断br流是否已准备好被读取
                 {
                          String  line=Symbols_file.readLine();//从Symbols.txt文件中读取一个文本行
                           if(line.equals(s))    //与转化的字符串进行比对,找到了就退出并打印
                                break;
                           else count++;
               }
               if(count<=58)
               { 
                   System.out.println("("+count+",-)\t"+"# "+s+" #\t");
                   string.append("("+count+",-)\t"+"# "+s+" #\t"+"\r\n");  
                   //把相应的字符串添加到定义的空字符串中
               }
               else
               { 
                   System.out.println("(100,"+s+")");
                   string.append("(100,"+s+")"+"\r\n");
               }
              Symbols_file.close();     //关闭该流并释放与之关联的所有资源
           }
           catch(Exception e)
  	       {   System.out.println("读取Symbols.txt文件发生错误,代号为:"+e.getMessage());   }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -