package wordanalyse;
import wordanalyse.*;
import java.util.Vector;
public class MainControl {
private InputFile inputFile;
private OutPutFile outputFile;
private Vector temp=new Vector();
private static final int LENGTHOFKEYWORDS=32;/*关键字或者标识符的长度。*/
private static final int LENGTHOFINT=32; /*整型数据的长度。*/
private static final int LENGTHOFSTRING=1024;/*字符串常量长度。*/
/**
* @param args
*/
public MainControl()
{
inputFile=new InputFile();
inputFile.getPathFromKeys();
if(inputFile.getPath()==null) /*如果没有获得路径就使用默认路径。*/
inputFile.setPath("D:\\Documents and Settings\\Administrator\\workspace\\WordAnalyse\\wordanalyse\\demo.c");
outputFile=new OutPutFile(inputFile.getPath());
}
public void control()
{
String content=inputFile.getContentFromFile();
String result[]=new String[2];
result[1]=content;
int counter=0;
do{
result=inputFile.getOneLineString(result[1]);
if(result!=null&&result[0]!=null)
{
counter++;
System.out.println("读入的第"+counter+"行是:"+result[0]);
process(result[0]);
}
}while(result!=null);
}
public void process(String oneLineString)
{
byte temp[];
byte[] word=new byte[LENGTHOFKEYWORDS],number=new byte[LENGTHOFINT],symbol=new byte[32],string=new byte[LENGTHOFSTRING];
temp=oneLineString.getBytes();
for(int i=0;i {
if(temp[i]!=' '&&temp[i]!='\t') /*滤掉空格、制表键。*/
{
if((temp[i]>=65&&temp[i]<=90)
||(temp[i]>=97&&temp[i]<=122)
||temp[i]=='_')
{
int j=-1;
do{
if(j {
j++;
word[j]=temp[i];
}
i++;
if(i>=temp.length) /*如果已经超过界限就终止循环。*/
return;
}while((temp[i]>=65&&temp[i]<=90)
||(temp[i]>=97&&temp[i]<=122)
||temp[i]=='_'
||(temp[i]>=48&&temp[i]<=57));
String checkdWord=new String(word,0,j+1);
if(KeyWord.isKeyWord(checkdWord))
this.temp.addElement(checkdWord+" 关键字\n");
else this.temp.addElement(checkdWord+" 标识符\n");
i--; //将指针向前移一个,因为上面是已经移动到后面一个才结束的。而for循环还要++,否则将无法检察当前字符。
}
else
{
if(temp[i]>=48&&temp[i]<=57) //如果是数字。
{
int k=-1;
do{
k++;
number[k]=temp[i];
i++;
if(i>=temp.length) //如果已经超过界限就终止循环。
return;
}while(temp[i]>='0'&&temp[i]<='9'||temp[i]=='.');
String checkdNumber=new String(number,0,k+1);
if(hasDot(checkdNumber))
{
try{
double num=Double.parseDouble(checkdNumber);
this.temp.addElement(num+" 实型常数\n");
}
catch(Exception e)
{
this.temp.addElement(checkdNumber+" 一个错误\n");
}
}
else
{
try{
long num=Long.parseLong(checkdNumber);
this.temp.addElement(num+" 整型常数\n");
}
catch(Exception e)
{
this.temp.addElement(checkdNumber+" 一个错误\n");
}
}
i--; //将指针向前移一个,因为上面是已经移动到后面一个才结束的。而for循环还要++,否则将无法检察当前字符。
}
else if(Symbol.isLimitedSymble(new String(temp,i,1)))
{
if(temp[i]=='"') //过滤掉字符串常数。
{
int n=-1;
do{
n++;
string[n]=temp[i];
i++;
if(i>=temp.length) //如果已经超过界限就终止循环。
{
this.temp.addElement(new String(string,0,n+1)+" 一个错误\n");
return;
}
}while(temp[i]!='"');
n++;
string[n]='"'; //要把最后一个"给过滤掉,所以n要向后移一个位置。
String checkdNumber=new String(string,0,n+1);
this.temp.addElement(checkdNumber+" 字符串常数\n");
}
else if(temp[i]=='\'') /*过滤掉字符常数。*/
{
int n=-1;
do{
n++;
string[n]=temp[i];
i++;
if(i>=temp.length) /*如果已经超过界限就终止循环。*/
{
this.temp.addElement(new String(string,0,n+1)+" 一个错误\n");
return;
}
}while(temp[i]!='\'');
n++;
string[n]='\''; /*要把最后一个"给过滤掉,所以n要向后移一个位置。*/
String checkdNumber=new String(string,0,n+1);
if(n+1<=3) /*c语言语法规定字符常量的字符数只能是1,再加上2个’,刚好应该是3。*/
this.temp.addElement(checkdNumber+" 字符常数\n");
else
this.temp.addElement(checkdNumber+" 一个错误!\n");
}
else{
this.temp.addElement(new String(temp,i,1)+" 界符\n"); /*过滤掉界符。*/
}
}
else if(Symbol.isSingleSymble(new String(temp,i,1)))
{
int m=-1;
do{
m++;
if(m>=2)
{
this.temp.addElement(new String(symbol,0,m+1)+" 一个错误\n");
break;
}
symbol[m]=temp[i];
i++;
if(i>=temp.length) /*如果已经超过界限就终止循环。*/
return;
}while(Symbol.isSingleSymble(new String(temp,i,1)));
String checkdWord=new String(symbol,0,m+1);
if(Symbol.isCalSymbol(checkdWord))
this.temp.addElement(checkdWord+" 运算符\n");
else if(checkdWord.equals("/*")) /*将注释给滤掉*/
{
do
{
i++;
if(i>=temp.length) /*如果已经超过界限就终止循环*/
return;
}while(temp[i]!='*');
i++;
if(i>=temp.length) /*如果已经超过界限就终止循环。*/
return;
if(temp[i]=='/');
else this.temp.addElement("注释 缺少结束符'/'\n");
}
else
this.temp.addElement(checkdWord+" 一个错误\n");
i--; /*将指针向前移一个,因为上面是已经移动到后面一个才结束的。而for循环还要++,否则将无法检察当前字符。*/
}
else {
this.temp.addElement(temp[i]+" 非法字符\n");
}
}
}
}
}
/*
* 检查字符串中是否有小数点,以便于判断是否是浮点数。
*/
public boolean hasDot(String checkdString)
{
byte []temp=checkdString.getBytes();
for(int i=0;i if(temp[i]==46)
return true;
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String result[];
MainControl mainControl=new MainControl();
//mainControl.inputFile.getPathFromKeys();
mainControl.control();
result=new String[mainControl.temp.size()];
for(int i=0;i {
result[i]=(String)mainControl.temp.elementAt(i);
System.out.print(result[i]);
}
mainControl.outputFile.writeToFile(result);
}
}