📄 wordanalyse.java
字号:
import java.io.*;
class WordAnalyse
{
int cc=0,ll,kk=0,sym=0,num=0,row=0,err=0;
char ch;
int wsym[]=new int[15];
int ssym[]=new int[65536];
String KeyWord[]=new String[15];
char line[]=new char[81];
int flag=0;
StringBuffer strid=new StringBuffer();
String inputstr=new String();
InputStreamReader in;
RandomAccessFile infile;
OutputStreamWriter write;
String str=new String();
//内部码对照
final static int PROGRAM=1;
final static int CONST=2;
final static int VAR=3;
final static int INTEGER=4;
final static int LONG=5;
final static int PROCEDURE=6;
final static int IF=7;
final static int THEN=8;
final static int WHILE=9;
final static int DO=10;
final static int READ=11;
final static int WRITE=12;
final static int BEGIN=13;
final static int END=14;
final static int ODD=15;
final static int PLUS=16;
final static int MINUS=17;
final static int TIMES=18;
final static int SLASH=19;
final static int EQL=20;
final static int NLEQ=21;
final static int LSS=22;
final static int LEQ=23;
final static int GTR=24;
final static int GEQ=25;
final static int PERIOD=26;
final static int COMMA=27;
final static int SEMICOLON=28;
final static int COLON=29;
final static int BECOMES=30;
final static int LPAREN=31;
final static int RPAREN=32;
final static int USNUMBER=33;
final static int IDENT=34;
final static int NEQ=35;
//关键字的个数
final static int NORW=15;
//符号的最大长度
final static int AL=20;
//number的最大位数
final static int NMAX=14;
//构造函数
public WordAnalyse()
{
SetStringToInt();
SetSym();
SetKeyWord();
}
public void SetStringToInt()
{
wsym[0]=BEGIN;
wsym[1]=CONST;
wsym[2]=DO;
wsym[3]=END;
wsym[4]=IF;
wsym[5]=INTEGER;
wsym[6]=LONG;
wsym[7]=ODD;
wsym[8]=PROCEDURE;
wsym[9]=PROGRAM;
wsym[10]=READ;
wsym[11]=THEN;
wsym[12]=VAR;
wsym[13]=WHILE;
wsym[14]=WRITE;
}
//设置单字符符号
public void SetSym()
{
//初始化单符号数组
//for(int i=0;i<=255;i++) ssym[i]=0;
ssym['+']=PLUS;
ssym['-']=MINUS;
ssym['*']=TIMES;
ssym['/']=SLASH;
ssym['(']=LPAREN;
ssym[')']=RPAREN;
ssym['=']=EQL;
ssym[',']=COMMA;
ssym['.']=PERIOD;
ssym['#']=NEQ;
ssym[';']=SEMICOLON;
}
//设置要进行读的文件路径
public boolean OpenFile(String path)
{
try
{
in=new InputStreamReader(new FileInputStream(path));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e1)
{
e1.printStackTrace();
}
return true;
}
public int FileIsEnd()
{
return flag;
}
//设置要将文件写入文件
public boolean WriteFile(String path)
{
try
{
FileOutputStream output=new FileOutputStream(new File(path));
//infile=new RandomAccessFile(new File("F//java",path),"rw");
write=new OutputStreamWriter(output);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
return true;
}
//设置关键字
public void SetKeyWord()
{
KeyWord[0]="BEGIN";
KeyWord[1]="CONST";
KeyWord[2]="DO";
KeyWord[3]="END";
KeyWord[4]="IF";
KeyWord[5]="INTEGER";
KeyWord[6]="LONG";
KeyWord[7]="ODD";
KeyWord[8]="PROCEDURE";
KeyWord[9]="PROGRAM";
KeyWord[10]="READ";
KeyWord[11]="THEN";
KeyWord[12]="VAR";
KeyWord[13]="WHILE";
KeyWord[14]="WRITE";
}
//供GetSym取一个字符,每次读一行,存入line缓冲区,line被getsym取空时 再读一行
public int GetChar()
{
if(cc==ll)
{
if(flag==-1)
{
System.out.println("program incomplete");
return -1;
}
ll=0;
cc=0;
ch=' ';
while(ch!='\n'&&ch!='\r')
{
try
{
flag=in.read();
if(flag==-1)
{
System.out.println("program incomplete");
return -1;
}
ch=(char)flag;
line[ll]=ch;
ll++;
}
catch(IOException e)
{
e.printStackTrace();
}
}
try
{
in.skip(1);
}
catch(IOException e)
{
e.printStackTrace();
}
row++;
}
ch=line[cc];
cc++;
return 0;
}
//测试
public String Test()
{
return (strid.toString());
}
//取一个词
public int GetSym()
{
int i,j,k;
strid=new StringBuffer();
while(ch==' '||ch==10||ch==9) // 忽略空格、换行和TAB
{
if(-1==GetChar()) return -1;
}
if(ch>='A'&&ch<='Z')
{ // 名字或保留字以a..z开头
k=0;
do
{
if(k<AL)
{
strid.append(ch);
k++;
}
if(-1==GetChar()) return -1;
}
while(ch>='A'&&ch<='Z'||ch>='0'&&ch<='9');
i=0;
j=NORW-1;
do // 搜索当前符号是否为保留字
{
k=(i+j)/2;
if(strid.toString().compareTo(KeyWord[k])<=0)j=k-1;
if(strid.toString().compareTo(KeyWord[k])>=0)i=k+1;
}
while(i<=j);
if(i-1>j) sym=wsym[k]; else sym=IDENT; // 搜索失败则,是名字或数字
}
else
{
if(ch>='0'&&ch<='9')
{ // 检测是否为数字:以0..9开头
k=0;
num=0;
sym=USNUMBER;
do
{
num=10*num+ch-'0';
k++;
if(-1==GetChar()) return -1;
}
while(ch>='0'&&ch<='9'); //获取数字的值
k--;
if(k>NMAX) error();
}
else
{
if(ch==':') //检测赋值符号
{
if(-1==GetChar()) return -1;
if(ch=='=')
{
sym=BECOMES;
if(-1==GetChar()) return -1;
}
else
{
sym=COLON;
}
}
else
{
if(ch=='<') //检测小于或小于等于符号
{
if(-1==GetChar()) return -1;
if(ch=='=')
{
sym=LEQ;
if(-1==GetChar()) return -1;
}
else if(ch=='>')
{
sym=NLEQ;
if(-1==GetChar()) return -1;
}
else
{
sym=LSS;
}
}
else
{
if(ch=='>') // 检测大于或大于等于符号
{
if(-1==GetChar()) return -1;
if(ch=='=')
{
sym=GEQ;
if(-1==GetChar()) return -1;
}
else
{
sym=GTR;
}
}
else
{
sym=ssym[ch]; //当符号不满足上述条件时,全部按照单字符符号处理
if(-1==GetChar()) return -1;
}
}
}
}
}
return 0;
}
//错误输出
public void error()
{
System.out.println("****Error on row="+row+"****Error on column="+cc);
err++;
}
public String ReadLineFormConsole()
{
String cstr=new String();
try{
String temp=new String();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
temp=in.readLine();
cstr=temp;
}
catch(IOException e)
{
e.printStackTrace();
}
return cstr;
}
//取得错误的总数
public int GetErrorNum()
{
return err;
}
public void PrintResult()
{
if(sym==IDENT)
{
inputstr="("+IDENT+","+strid.toString()+")";
System.out.println(inputstr);
try
{
write.write(row+inputstr+"\r\n");
write.flush();
}
catch(IOException e)
{
e.printStackTrace();
}
}
else if(sym==USNUMBER)
{
inputstr="("+USNUMBER+","+num+")";
System.out.println(inputstr);
try
{
write.write(row+inputstr+"\r\n");
write.flush();
}
catch(IOException e)
{
e.printStackTrace();
}
}
else
{
if(0!=sym)
{
inputstr="("+sym+","+0+")";
System.out.println(inputstr);
try
{
write.write(row+inputstr+"\r\n");
write.flush();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
//主要函数
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -