package analysis;
import java.util.ArrayList;
import java.util.List;
import library.Digit;
import library.KeyWords;
import library.Symbol;
/**
*
* @author 周弘懿
*
*/
public class AnalyseWords {
private List temp = new ArrayList();
public List getTemp() {
return temp;
}
/* 关键字或者标识符的长度。 */
private static final int LENGTHOFKEYWORDS = 500;
/* 整型数据的长度。 */
private static final int LENGTHOFINT = 500;
/* 字符串常量长度。 */
private static final int LENGTHOFSTRING = 1024;
String result = "";
int position;
/**
* 处理程序
*
* @return 词法分析后的结果
*/
public void process(String aLine) {
/* 存放关键字和标识符 */
byte[] word = new byte[LENGTHOFKEYWORDS];
/* 存放数字 */
byte[] number = new byte[LENGTHOFINT];
/* 存放运算符 */
byte[] symbol = new byte[500];
/* 存放字符串常数或是字符常数 */
byte[] string = new byte[LENGTHOFSTRING];
byte temp[];
temp = aLine.getBytes();
if(new String(temp).trim().length() == 0)
this.temp.add("请输入C语言程序!");
/**
* 主要逻辑
*/
for (position = 0; position= temp.length) {
return;
}
/* 滤掉空格、制表键。 */
if (temp[position] != ' ' || temp[position] != '\t') {
/* 允许大小写字母和下划线 */
if ((temp[position] >= 65 && temp[position] = 97 && temp[position] = 48 && temp[position] = temp.length) /* 如果已经超过界限就终止循环。 */{
String checkdWord = new String(word, 0,
tempPosition + 1);
decideWord(checkdWord);
return position;
}
} while ((temp[position] >= 65 && temp[position] = 97 && temp[position] = 48 && temp[position] = 48 && temp[position] = temp.length) /* 如果已经超过界限就终止循环。 */{
String checkdNumber = new String(number, 0, tempPosition + 1);
decideNum(checkdNumber);
return position;
}
} while (temp[position] >= '0' && temp[position] <= '9'
|| temp[position] == '.');
String checkdNumber = new String(number, 0, tempPosition + 1);
decideNum(checkdNumber);
// 因为上面的已经移到末尾,因为for还要+1,所以要-1
position--;
}
return position;
}
/**
* 分析字段是实型还是整型常数
* @param checkdNumber 分析数字字段
*/
private void decideNum(String checkdNumber) {
if (Digit.hasDot(checkdNumber)) {
try {
double num = Double.parseDouble(checkdNumber);
this.temp.add("(" + num
+ ", 实型常数)\n");
} catch (Exception e) {
this.temp.add("(" + checkdNumber
+ ", 错误,发现多个.)\n");
}
} else {
try {
long num = Long.parseLong(checkdNumber);
this.temp.add("(" + num
+ ", 整型常数)\n");
} catch (Exception e) {
this.temp.add("(" + checkdNumber
+ ", 错误,不是整型常数!)\n");
}
}
}
/**
* 分析字符常数,字符串常数和边界符
* @param position 词法分析语句遍历指针
* @param string 字符,字符串和边界符的数组
* @param temp 词法分析语句数组
* @return 返回词法分析语句遍历指针的新位置
*/
public int analysisLimitSymble(int position, byte[] string, byte temp[]) {
// 过滤字符串常数
if (temp[position] == '"') {
// 存储数组的指针
int tempPosition = -1;
do {
tempPosition++;
string[tempPosition] = temp[position];
position++;
} while (temp[position] != '"');
//要把最后一个"给过滤掉,所以要向后移一个位置。
tempPosition++;
string[tempPosition] = '"';
String checkdNumber=new String(string,0,tempPosition+1);
this.temp.add("(" + checkdNumber
+ ", 字符串常数)\n");
}
// 过滤字符常数
else if (temp[position] == '\'') {
// 存储数组的指针
int tempPosition = -1;
do {
tempPosition++;
string[tempPosition] = temp[position];
position++;
} while (temp[position] != '\'');
tempPosition++;
string[tempPosition] = '\'';
//要把最后一个"给过滤掉,所以要向后移一个位置。
String checkdNumber=new String(string,0,tempPosition+1);
//c语言语法规定字符常量的字符数只能是1,再加上2个’,刚好应该是3。
if(tempPosition+1= 2) {
this.temp.add("(" + new String(symbol,0,tempPosition+1)+
", 错误,字符长度仅限2位)\n");
return position;
}
position++;
if(position >= temp.length) /* 如果已经超过界限就终止循环。 */{
String checkdWord = new String(symbol, 0, tempPosition+1);
position = decideSymble(checkdWord, temp, position);
return position;
}
} while(Symbol.isSingleSymble(new String(temp, position, 1)));
String checkdWord = new String(symbol, 0, tempPosition+1);
//单个运算符可能组成符合运算符
position = decideSymble(checkdWord, temp, position);
position--;
return position;
}
private int decideSymble(String checkdWord, byte temp[], int position) {
if(Symbol.isComboSymbol(checkdWord)) {
this.temp.add("(" + checkdWord
+ ", 复合运算符)\n");
} else if(Symbol.isSingleSymble(checkdWord)) {
this.temp.add("(" + checkdWord
+ ", 运算符)\n");
}else if(checkdWord.equals("/*")) { //过滤注释
this.temp.add("(" + checkdWord
+ ", 前注释)\n");
do {
position++;
if(position >= temp.length) {
return position;
}
} while(temp[position] != '*'); /* 直到阅读到*,表示将其间的注释都过滤 */
position++;
if(position>=temp.length) { /*如果已经超过界限就终止循环。*/
this.temp.add("错误,后注释缺少结束符'/'\n");
return position;
}
if(temp[position]!='/') {
this.temp.add("错误,后注释缺少结束符'/'\n");
} else {
this.temp.add("(" + new String(temp, position-1, 2)
+ ", 后注释 )\n");
return position-1;
}
}
return position;
}
public static void main(String[] args) {
AnalyseWords aw = new AnalyseWords();
aw.process("sd");
for (String str : aw.temp) {
System.out.println(str);
}
}
}