?? dos_words.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 + -