java写的词法分析器有问题 无法判断出字符类型
import java.io.*;
public class CFfenxiqi{
public CFfenxiqi(){}
int MAX=15;
char ch =' ';
String[] key={"begin","end","if","then","else","while","write","read",
"do", "call","const","char","until","procedure","repeat"};
public int Iskey(String c){ //关键字判断
int i;
for(i=0;i<MAX;i++) {
if(key[i].equals(c))
return 1;
}
return 0;
}
public int IsLetter(char c) { //判断是否为字母
if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A'))) return 1;
else return 0;
}
public int IsDigit(char c){ //判断是否为数字
if(c>='0'&&c<='9') return 1;
else return 0;
}
public void analyse(RandomAccessFile fpin){
String arr="";
try{
while((fpin.getFilePointer()<fpin.length())) {
arr="";
ch=fpin.readChar();
if(ch==' '||ch=='\t'||ch=='\n'){}
else if(IsLetter(ch)==1){
while(IsLetter(ch)==1||IsDigit(ch)==1) {
if((ch<='Z')&&(ch>='A')) ch+=32;
arr=arr+ch;
ch=fpin.readChar();
}
long pos=fpin.getFilePointer()-1;
fpin.seek(pos);
if (Iskey(arr)==1){System.out.println(arr+"\t$关键字");}
else System.out.println(arr+"\t$普通标识符");
}
else if(IsDigit(ch)==1){
while(IsDigit(ch)==1||(ch=='.'&&IsDigit(fpin.readChar())==1)){
arr=arr+ch;
ch=(char)fpin.read();
}
long pos=fpin.getFilePointer()-1;
fpin.seek(pos);
System.out.println(arr+"\t$无符号实数");
}
else switch(ch){
case'+':
case'-' :
case'*' :
case'=' :
case'/' :System.out.println(ch+"\t$运算符");break;
case'(' :
case')' :
case'[' :
case']' :
case';' :
case'.' :
case',' :
case'{' :
case'}' :System.out.println(ch+"\t$界符");break;
case':' :{ch=fpin.readChar();
if(ch=='=') System.out.println(":="+"\t$运算符");
else {System.out.println("="+"\t$运算符");
long pos=fpin.getFilePointer()-1;
fpin.seek(pos);
}break;}
case'>' :{ch=fpin.readChar();
if(ch=='=') System.out.println(">="+"\t$运算符");
if(ch=='>') System.out.println(">>"+"\t$输入控制符");
else { System.out.println(">"+"\t$运算符");
long pos=fpin.getFilePointer()-1;
fpin.seek(pos);
}break;}
case'<' :{ch=(char)fpin.read();
if(ch=='=')System.out.println("<="+"\t$运算符");
else if(ch=='<')System.out.println("<<"+"\t$输出控制符");
else if(ch=='>') System.out.println("<>"+"\t$运算符");
else{System.out.println("<"+"\t$运算符");
long pos=fpin.getFilePointer()-1;
fpin.seek(pos);
}break;}
default : System.out.println(ch+"\t$无法识别字符");
}
}
}catch(IOException e){}
}
public static void main(String args[]){
CFfenxiqi cc=new CFfenxiqi();
RandomAccessFile fpin;
String s="";
System.out.println("请输入源文件名(包括路径和后缀名):");
for(;;){
BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
try{
s=keyin.readLine();
}catch(IOException e){}
try{
fpin=new RandomAccessFile(s,"r");
System.out.println("\n********************分析如下*********************");
cc.analyse(fpin);
fpin.close();
}catch(IOException e){
System.out.println("文件路径错误!\n请输入源文件名(包括路径和后缀名):");
}
}
}
}