java写的词法分析器有问题 无法判断出字符类型

maxuechao1213 2011-11-02 04:26:12

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请输入源文件名(包括路径和后缀名):");

}

}

}

}
...全文
60 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
maxuechao1213 2011-11-04
帮帮忙啊,大神们,谢谢啊!!!!
回复
maxuechao1213 2011-11-03
begin

x:=9

if x>0 then x:=x+1...111a;

while a:=0 do

b:2*x/3;

end;
回复
ytu2006123 2011-11-02
看下取文件内容
回复
相关推荐
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2011-11-02 04:26
社区公告
暂无公告