用java写的编译原理课中的词法分析程序有错误不知道怎么改,求救~~
/*词法分析程序,待分析的程序(下面复出)是pascal语言,分析到"."即分析结束*/import *;
public class A
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer();
Getsym getsymbol=new Getsym();
Inout savefile=new Inout();
Init inits=new Init();
System.out.println("~~~~词法分析开始~~~~~");
inits.init();
int j=0;
do
{
j=getsymbol.getsym();
}while(j!=-1);
savefile.save(str);
System.out.println("~~分析结束,谢谢使用!~~");
}
}
enum symbol //所有符号
{
nul, ident, number, plus, minus,
times, slash, oddsym, eql, neq,
lss, leq, gtr, geq, lparen,
rparen, comma, semicolon, period, becomes,
beginsym, endsym, ifsym, thensym, whilesym,
writesym, readsym, dosym, callsym, constsym,
varsym, procsym, lbracket, rbracket
}
class Init //初始化符号表
{
private symbol sym;
private symbol ssym[]=new symbol[256];
private symbol wsym[]=new symbol[13];
private String word[]=new String[13];
public symbol getssym(char ch)
{
return ssym[ch];
}
public symbol getwsym(int i)
{
return wsym[i];
}
public String getword(int i)
{
return word[i];
}
public void init()
{
for (int i=0; i<=255; i++)
{
ssym[i] = null;
}
ssym['+'] = symbol.plus;
ssym['-'] = symbol.minus;
ssym['*'] = symbol.times;
ssym['/'] = symbol.slash;
ssym['('] = symbol.lparen;
ssym[')'] = symbol.rparen;
ssym['='] = symbol.eql;
ssym[','] =
ssym['.'] = symbol.period;
ssym['#'] = symbol.neq;
ssym[';'] = symbol.semicolon;
ssym['{'] = symbol.lbracket;
ssym['}'] = symbol.rbracket;
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 [0] = symbol.beginsym;
wsym [1] = symbol.callsym;
wsym [2] = symbol.constsym;
wsym [3] = symbol.dosym;
wsym [4] = symbol.endsym;
wsym [5] = symbol.ifsym;
wsym [6] = symbol.oddsym;
wsym [7] = symbol.procsym;
wsym [8] = symbol.readsym;
wsym [9] = symbol.thensym;
wsym [10] = symbol.varsym;
wsym [11] = symbol.whilesym;
wsym [12] = symbol.writesym;
}
}
class Getsym //取单词或符号
{
private int eof=-1;
final int al=10; // the largest length of symbol
final int norw=13; //keyword numbers
final int nmax=14; //the largest length of number
private char ch=' ';
private int j,i,k;
private int xx=0;
private String id;
symbol sym;
Inout read=new Inout();
char a[]=new char[al+1];
public int getsym() //取单词
{
while(ch==' '||ch==10||ch==9)
{
ch=read.getch();
}
if(ch>='a'&&ch<='z')
{
k=0;
do
{
if(k<al)
{
a[k]=ch;
k++;
}
ch=read.getch();
}while(ch>='a' && ch<='z' || ch>='0' && ch<='9');
id=new String(a);
for(i=0;i<=10;i++)
{
a[i]=' ';
}
for(i=0;i<13;i++)
{
if(id.trim().compareTo(inits.getword(i))==0)
{
j=i;
break;
}
else
j=13;
}
System.out.print(" ( ");
//str.append(" < ");
if(j>=0&&j<=12)
{
sym=inits.getwsym(j);
System.out.print(sym);
System.out.print(" , ");
System.out.print(id);
}
else
{
if(String.valueOf(eof)==id)
xx=-1;
else
{
sym=symbol.ident;
System.out.print(sym);
System.out.print(" , ");
System.out.print(id);
}
}
str.append(sym);
//str.append( , );
str.append(id);
//str.append( >\n);
}
else
{
System.out.print("( ");
//str.append( < );
if(ch>='0'&&ch<='9')
{
k=0;
int num=0;
sym=symbol.number;
do
{
num=10*num+ch-'0';
k++;
ch=read.getch();
}while(ch>='0' && ch<='9');
System.out.print(sym);
System.out.print(" , ");
System.out.print(num);
//str.append(sym+" , "+String.valueOf(num));
str.append(sym);
//str.append( , );
str.append(num);
}
else
{
if(ch==':')
{
ch=read.getch();
if(ch=='=')
{
sym=symbol.becomes;
System.out.print(sym);
ch=read.getch();
}
else
{
sym=symbol.nul;
System.out.print(sym);
}
}
else
{
if(ch=='<')
{
ch=read.getch();
if(ch=='=')
{
sym=symbol.leq;
System.out.print(sym);
ch=read.getch();
}
else
{
sym=symbol.lss;
System.out.print(sym);
}
}
else
{
if(ch=='>')
{
ch=read.getch();
if(ch=='=')
{
sym=symbol.geq;
System.out.print(sym);
ch=read.getch();
}
else
{
sym=symbol.gtr;
System.out.print(sym);
}
}
else
{
sym=inits.getssym(ch);
if(sym!=symbol.period)
{
ch=read.getch();
}
else
xx=-1;
System.out.print(sym);//end richard
str.append(sym);
}
}
}
System.out.print(" , null");
str.append( null);
//str.append( , null);
}
}
System.out.println(" )");
//str.append( >\n);
return xx;
}
}
class Inout //读写文件
{
private int length=0;
private long pos=0;
private char line[]=new char[81];
private int cc=0;
private char ch;
RandomAccessFile result=null;
RandomAccessFile readfile=null;
public void readline() //读行
{
File file=new File("E:\\java","yuan.txt");
try
{
readfile=new RandomAccessFile(file,"r");
String s=null;
readfile.seek(pos);
if((s=readfile.readLine().trim())!=null)
{
s.getChars(0,s.length(),line,0);
length=s.length();
pos=readfile.getFilePointer();
}
readfile.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
public char getch() //读一个字符
{
if(cc==length)
{
readline();
cc=0;
}
ch=line[cc];
cc++;
return ch;
}
public void save(String string) //保存文件
{
try
{
result=new RandomAccessFile("result.txt","rw");
result.writeChars(string);
result.close();
}catch(IOException e)
{
System.out.println(e);
}
}
}
/*待分析的程序*/
int gcd (int u, int v)
{
if (v := 0) return u ;
if(v<=0) return u;
else return gcd(v,u-u/v*v)
}
.