Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
本帖最后由 xu357100870 于 2013-06-23 00:34:29 编辑
public class Test {
//符号
private final char[] symbol = {'*','+','-','/','%','=','(',')','[',']','{','}',
'!','|','&',';','.','<','>'};
//数字
private final char[] number = {'0','1','2','3','4','5','6','7','8','9','.'};
private char ch;//存放新读进的字符
private String str;//存放要分析的句子
private StringBuffer strToken;//存放构成单词符号的字符串
private int i = 0;
public Test(String str){
this.str = str;
this.strToken = new StringBuffer();
}
//将下一字符读到ch中,并将指示器前移一字符位置
public void GetChar(){
if(i < str.length()){
ch = str.charAt(i);
i++;
}
}
//判断是否为空格,若是则调用Getchar,直到ch进入下一个非空白字符
public void GetBC(){
while(i <= str.length()){
if(ch == ' '){
GetChar();
}else{
break;
}
//i++;
}
}
//将ck连接到strToken之后
public void Concat(){
strToken.append(ch);
}
//判断是ch否是数字还是字母
public boolean IsLetter(){
if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
return true;
}else {
return false;
}
}
public boolean IsDigit(){
boolean b = false;
for (int k = 0; k < number.length; k++) {
if(ch == number[k]){//超前搜索
b = true;
//i++;
}
}
return b;
}
//是否是符号
public boolean isSymbol(){
boolean b = false;
//char temp = str.charAt(i);
for (int k = 0; k < symbol.length; k++) {
if(ch == symbol[k]){
b = true;
}
}
return b;
}
//将搜索指示器回调一个字符,将ch置为空白字符
public void Retract(){
i--;
ch = ' ';
}
public static void main(String[] args) {
Test test = new Test("for (int i = 0; i < idAL.size(); i++)");
while(true){
test.GetChar();
test.GetBC();
if(test.IsLetter()){//如果是字母
test.Concat();//加到strToken后面
test.GetChar();//得到下一个字符
while(test.IsLetter() || test.IsDigit()){//如果是字母或数字
test.Concat();//加到strToken后面
test.GetChar();//得到下一个字符
}if(!test.IsLetter()){//如果不是字母
test.Retract();//指示器前移一个位置,并将ch置为空
}
System.out.println("标识符:"+test.strToken.toString());
test.strToken.delete(0, test.strToken.length());//清空StringBuffer
}else if(test.IsDigit()){
test.Concat();
test.GetChar();
while(test.IsDigit()){
test.Concat();
test.GetChar();
}
if(!test.IsDigit()){
test.Retract();
}
System.out.println("数字:"+test.strToken.toString());
test.strToken.delete(0, test.strToken.length());
}else if(test.isSymbol()){
test.Concat();
test.GetChar();
while(test.isSymbol()){
test.Concat();
test.GetChar();
}
if(!test.isSymbol()){
test.Retract();
}
System.out.println("符号:"+test.strToken.toString());
test.strToken.delete(0, test.strToken.length());
}
if(test.i == test.str.length()){
System.exit(-1);
}
}
}
}
while(i <= str.length()){ if(ch == ' '){ GetChar(); }else{ break; } //i++; }