?? cifa.java
字號:
//ronghong 2005.5.12
import java.io.*;
class CiFa
{
private String[] word;//保留字表
private String[] wsym;
private char[] sword;//運算符表
private String[] ssym;
private int CC;
private char CH;
private char CalMark;
public String BuffStr;//緩沖區(qū)
private int BuffLength;
private String SYM;
private String ID;
private String NUM;
private String SourceLocat; //源文件位置
private String Sources[]; //源文件,以行為單位存儲
private String inLine;
private String TargetLocat; //
private String WrTwoFormule; //二元式
CiFa(){
word=new String[13];
word[0]="begin";
word[1]="call";
word[2]="const";
word[3]="do";
word[4]="end";
word[5]="if";
word[6]="odd";
word[7]="procedure";
word[8]="read";
word[9]="then";
word[10]="var";
word[11]="while";
word[12]="write";
wsym=new String[13];
wsym[0]="beginsym";
wsym[1]="callsym";
wsym[2]="constsym";
wsym[3]="dosym";
wsym[4]="endsym";
wsym[5]="ifsym";
wsym[6]="oddsym";
wsym[7]="proceduresym";
wsym[8]="readsym";
wsym[9]="thensym";
wsym[10]="varsym";
wsym[11]="whilesym";
wsym[12]="writesym";
sword=new char[8];
sword[0]='+';
sword[1]='-';
sword[2]='*';
sword[3]='/';
sword[4]='(';
sword[5]=')';
//sword[6]=',';
sword[6]='=';
//sword[8]=';';
sword[7]='#';
ssym=new String[8];
ssym[0]="plus";
ssym[1]="minus";
ssym[2]="times";
ssym[3]="slash";
ssym[4]="lparen";
ssym[5]="rparen";
//ssym[6]="comma";
ssym[6]="eql";
//ssym[8]="semicolon";
ssym[7]="end";
CC=0;
//BuffStr=TempStr;//緩沖區(qū)
//BuffLength=BuffStr.length();
SYM="";
ID="";
NUM="";
SourceLocat="Source.txt";
Sources=new String[10]; //源文件不超過10行
File f=new File("Target.txt");
f.delete();
TargetLocat="Target.txt";
WrTwoFormule="";
}
public void Getsym()
{
String A="";
boolean condition=false;
if(CC==0){
do{
Getch();
}
while(IsBorder());
};
if(!IsOver()){
condition=( ( CH>=65 && CH<=90 )||( CH>=97 && CH<=122 ) );//CH是字符
if(condition){
int k=0;
do{
A=A+CH;
k++;
Getch();
condition=(CH>=65 && CH<=90)||(CH>=97 && CH<=122)||(CH>=65 && CH<=90)||(CH>=97 && CH<=122);
}
while(condition);
while(IsBorder()){
Getch();
}
ID=A;
SYM="";
for(int temp2=0;temp2<word.length;temp2++){
if(word[temp2].equals(ID)){ //是保留字
SYM=wsym[temp2];
break;
};
}
if(SYM.equals("")){
SYM="ident";//不是保留字
};
//System.out.println("("+ID+","+SYM+")");
WrTwoFormule="("+ID+","+SYM+")";
try{WriteFile(WrTwoFormule);}catch(IOException ioe){};
}
else{
condition=(CH>=48 && CH<=57);
if(condition){ //CH是數(shù)字
NUM="";
SYM="";
while(CH>=48 && CH<=57){
NUM=NUM+CH;
Getch();
}
while(IsBorder()){
Getch();
}
SYM="number";
//System.out.println("("+NUM+","+SYM+")");
WrTwoFormule="("+NUM+","+SYM+")";
try{WriteFile(WrTwoFormule);}catch(IOException ioe){};
}
else{
if(!IsOver()){ //CH是算符或界符
for(int temp3=0;temp3<sword.length;temp3++){
if(sword[temp3]==CH){
SYM=ssym[temp3];
CalMark=sword[temp3];
break;
}
} //off for
//System.out.println("("+CalMark+","+SYM+")");
WrTwoFormule="("+CalMark+","+SYM+")";
try{WriteFile(WrTwoFormule);}catch(IOException ioe){};
do{
Getch();
}
while(IsBorder());
};
};
}; //off else
}; //off if
} //off Getsym()
public char Getch(){
String TempStr2="";
if(CC+1<=BuffStr.length()){
TempStr2=BuffStr.substring(CC,CC+1);
CC++;
CH=TempStr2.charAt(0);
return CH;
};
CH='#';
return CH;
} //off Getch()
private boolean IsOver(){ //判斷是否結(jié)束
if(CH=='#'){
return true;
}
return false;
}
private boolean IsBorder(){ //判斷是否為界符
boolean IsBorder=false;
IsBorder=(CH==',') || (CH=='.') || (CH==';') || (CH==' ');
return IsBorder;
}
private void ReadFile() //讀入文件
throws java.io.IOException{
int counter=0;
FileReader fr= new FileReader(SourceLocat);
BufferedReader br=new BufferedReader(fr);
while( (inLine=br.readLine())!=null){
Sources[counter]=inLine+"#";
counter++;
}
br.close();
}
private void WriteFile(String TempTarg) //寫文件
throws java.io.IOException{
FileWriter fw=new FileWriter(TargetLocat,true);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw,true);
pw.println(TempTarg);
pw.close();
}
public void TempWriteOut(){ //暫時用來輸出二元式
try{ReadFile();}catch(IOException ioe){};
int i=0;
//String TempStr3="";
while(Sources[i]!=null){
BuffStr=Sources[i];
CC=0;
CH=' ';
while(CH!='#'){
Getsym();
}
i++;
}
}
public String getNextSymOut(){
return "";
}
}//off class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -