📄 byylscanner.java
字号:
*根据不同的字符,调用不同的程序分析
*/
public void beginScanner()
{
scannerInit(); //初始化
try{
if(!currentFile.exists())
{
fileSaver= new FileDialog(frame);
fileSaver.setMode(1);
fileSaver.setTitle("保存原文件为:");
fileSaver.setVisible(true);
try{
currentFile=new File(fileSaver.getFile());
}catch(Exception oe){return ;}
}
fos=new FileOutputStream(currentFile);
saveText=new String(inputTextArea.getText());
buffer=saveText.getBytes();
fos.write(buffer);
fileFlag=0;
}catch(IOException Ioe) {};
frame.setTitle("Simple 语言编译器"+currentFile.toString()+"-已保存");
//开始扫描
ch=getNextChar();
while(ch!=1000)
{
if((ch>47)&&(ch<58)) //是数字
isNumber();
else //是保留字或标志符
{if(((ch>64)&&(ch<90))||((ch>96)&&(ch<123))||ch=='_')
isAlpha();
else
{if(ch=='/') isAnotation();//是否为注释
else
if(ch=='\'') isChar(); //字符常数
else isOther(); //其他情况
}
}
}
if(!(beginCode==endCode)) //检验程序是否正常结束
error(4);
if(errorCount==0)
resultTextArea.append("恭喜,已成功通过词法分析");
}
/**
*获得下一个字符
*@return 返回下一个字符
*/
public char getNextChar()
{
try{
charId++;
columnCount++;
if(saveText.charAt(charId)==13)
{rowCount++; //行计数器加一并把列计数器置零
columnCount=0;
}
if(charId<saveText.length())
return saveText.charAt(charId);
}catch(Exception oo){}
return 1000; //1000为程序结束标志
}
/**
*处理保留字和字符串
*/
public void isAlpha()
{
int keyFlag=0,i,alpaFlag;
while(((ch>64)&&(ch<90))||((ch>96)&&(ch<123))||ch=='_')
{
currentToken.name.append(ch);
ch=getNextChar();
} //读完所有的字母并读入下一个非字母的字符
for(i=0;i<LENGTH;i++) //检测是否为保留字
{
if(keyWord[i].toString().equals(currentToken.name.toString()))
{keyFlag=1;break;}
}
if(keyFlag==1) //是保留字
{
currentToken.code=i;
currentToken.address=-1;
if(currentToken.code==3)
varEnd=1; //表示声明变量已经结束
}
else //是标志符
{ currentToken.code=34;
alpaFlag=wordExist(); // 确定入口地址需要检测是否在符号表中
if(alpaFlag==-1) //不在符号表中
{
if(varEnd==1)
{error(5);
currentToken.name.delete(0,currentToken.name.length());
return;}
currentToken.address=addrCount++;
}
else
currentToken.address=alpaFlag;
}
currentToken.label=labelCount++;
outPut();
}
/**
*处理注释和除号
*/
public void isAnotation()
{
char ch1;
ch1=ch; //ch1存放'/' 判断下一个字符是不是*
ch=getNextChar();
if(ch=='*')
{ //如果是注释
while(true)
{
ch=getNextChar();
if(ch==1000)
{
error(3);
break;
}
if(ch=='*')
{
ch1=ch;
ch=getNextChar();
if(ch=='/') //注释正常结束
{ ch=getNextChar();
break;
}
else
error(3);
}
}
}
else //除号
{
currentToken.name.append('/');
currentToken.code=48;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
}
}
/**
*处理字符串
*/
public void isChar() //字符串常量
{
int alpaFlag;
while(true)
{
ch=getNextChar();
if(ch!='\'')
currentToken.name.append(ch);
else
break;
}
currentToken.code=37;
alpaFlag=wordExist(); // 确定入口地址需要检测是否在符号表中
if(alpaFlag==-1) //不在符号表中
currentToken.address=addrCount++;
else
currentToken.address=alpaFlag;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
/**
*处理数字
*/
public void isNumber()
{
int k=0;
int flag=0,numberFlag;
char ch1;
while((ch>47)&&(ch<58))
{
currentToken.name.append(ch);
ch=getNextChar();
if(ch=='.')
{ flag=1; //是实数
break;
}
}
if(((ch>64)&&(ch<90))||((ch>96)&&(ch<123))) //整数后又字母
{
error(0);
while(((ch>64)&&(ch<90))||((ch>96)&&(ch<123)))
{
ch=getNextChar();
while((ch>47)&&(ch<58)) ch=getNextChar();
}
}
if(flag==1)
{
ch1=getNextChar();
if((ch1>47)&&(ch1<58))
currentToken.name.append(ch);//小数点后是数字则将小数点加入 name 中
else
error(2); //小数点后是字母
ch=ch1;
while((ch>47)&&(ch<58))
{
currentToken.name.append(ch);
ch=getNextChar();
}
currentToken.code=36; //是实数 编码36
if(ch=='.') //又出现小数点
{
error(2);
ch=getNextChar();
while((ch>47)&&(ch<58)) ch=getNextChar();//读完剩下的数字
}
if(((ch>64)&&(ch<90))||((ch>96)&&(ch<123))) //又出现字母
{
error(2);
while(((ch>64)&&(ch<90))||((ch>96)&&(ch<123))) //去掉后面的数字和字母
{
ch=getNextChar();
while((ch>47)&&(ch<58)) ch=getNextChar();
}
}
}
else
currentToken.code=35; //是整数
numberFlag=numberExist(); // 确定入口地址需要检测是否在符号表中
if(numberFlag==-1) //不在符号表中
currentToken.address=addrCount++;
else
currentToken.address=numberFlag;
currentToken.label=labelCount++;
outPut();
}
/**
*处理其他字符
*/
public void isOther()
{
char ch1;
switch(ch)
{ case '\'':
currentToken.name.append(ch);
currentToken.code=38;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '(':
currentToken.name.append(ch);
currentToken.code=39;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case ')':
currentToken.name.append(ch);
currentToken.code=40;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '*': ch1=ch;
ch=getNextChar();
if(ch!='/')
{currentToken.name.append(ch1);
currentToken.code=41;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
}
else
{currentToken.name.append(ch1);
currentToken.name.append(ch);
currentToken.code=42;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
break;
case '+':
currentToken.name.append(ch);
currentToken.code=43;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case ',':
currentToken.name.append(ch);
currentToken.code=44;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '-':
currentToken.name.append(ch);
currentToken.code=45;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '、': //这就是顿号
currentToken.name.append(ch);
currentToken.code=46;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '.':
currentToken.name.append(ch);
currentToken.code=47;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '/': ch1=ch;
ch=getNextChar();
if(ch!='*')
{currentToken.name.append(ch1);
currentToken.code=48;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
}
else
{currentToken.name.append(ch1);
currentToken.name.append(ch);
currentToken.code=49;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
break;
case ':': ch1=ch;
ch=getNextChar();
if(ch!='=')
{currentToken.name.append(ch1);
currentToken.code=50;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
}
else
{currentToken.name.append(ch1);
currentToken.name.append(ch);
currentToken.code=51;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
break;
case ';':
currentToken.name.append(ch);
currentToken.code=52;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '<': ch1=ch;
ch=getNextChar();
if(ch=='=')
{
currentToken.name.append(ch1);
currentToken.name.append(ch);
currentToken.code=54;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
else
if(ch=='>')
{currentToken.name.append(ch1);
currentToken.name.append(ch);
currentToken.code=55;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
else
{currentToken.name.append(ch1);
currentToken.code=53;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
}
break;
case '=':
currentToken.name.append(ch);
currentToken.code=56;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
break;
case '>': ch1=ch;
ch=getNextChar();
if(ch!='=')
{currentToken.name.append(ch1);
currentToken.code=57;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
}
else
{
currentToken.name.append(ch1);
currentToken.name.append(ch);
currentToken.code=58;
currentToken.address=-1;
currentToken.label=labelCount++;
outPut();
ch=getNextChar();
}
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -