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

}

}

}

}
...全文
133 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
看下取文件内容

23,409

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧