?? wordanalysis.java
字號:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class wordanalysis
{
char []array; //用來把每一行的字符傳傳進數組
char c;
int i=0;
boolean iserror=false; //如果出錯就不執行
String info=""; //信息
String temp=new String(""); //用做臨時存入單詞的值
public word whead;
String keyword[]={"createtable","select","where","from","into","update","delete","insert","values","and","set"};
//
public static void main(String args[]) //main函數
{
new wordanalysis();
}
public String analyse(String command)
{
whead=new word("head",0);
word w1=whead,w2=null;
//核心部分,對文件的詞法分析構件按鈕
array=command.toCharArray(); //把命令轉換成字符串
i=0;
int state=-1;
while(i<array.length) //把命令讀完或者出錯退出
{
c=array[i];
if(state==-1)
{
if( (Character.isUpperCase(c)) || (Character.isLowerCase(c)) )
{
temp=temp+c;
state=0;
i++;
continue;
}
if( (Character.isDigit(c)) )
{
temp=temp+c;
state=1;
i++;
continue;
}
/*標號是3類詞語*/
if( (c==';')||(c=='(')||(c==')')||(c==',') )
{
temp+=c;
w2=new word(temp,3);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
temp="";
i++;
continue;
}
/*如果讀到了"則認為是表中的值,則在這里就一直讀完,
*讀完就保存,不另外做處理
*如沒有配對的"則記錄出錯,跳出循環*/
if( c=='\"')
{
i++;
while(i<array.length && array[i]!='\"')
{
temp=temp+array[i];
i++;
}
if( i==array.length)
{
info+="The string is not closed\n";
iserror=true;
break;
}
if(array[i]=='\"')
{
w2=new word(temp,2);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
temp="";
i++;
continue;
}
}
if( c==' ' || c==10 || c==13 )
{
i++;
continue;
}
info+="存在非法字符"+c+"\n"; //存在其他字符判錯
temp="";
i++;
iserror=true;
continue;
}
if(state==0)//進入詞語狀態,這里可能是關鍵字,或者是標示名
{
if( (Character.isUpperCase(c)) || (Character.isLowerCase(c)) )
{
temp=temp+c;
state=0;
i++;
continue;
}
if( (Character.isDigit(c)) )
{
temp=temp+c;
state=1;
i++;
continue;
}
/*標號是3類詞語*/
if( (c==';')||(c=='(')||(c==')')||(c==',')||(c=='<')||(c=='=')||(c=='>') )
{
int temp_type=1;
for(int j=0;j<keyword.length;j++)
{
if(temp.equals(keyword[j]))
{
temp_type=0;
break;
}
}
w2=new word(temp,temp_type);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
temp="";
temp+=c;
w2=new word(temp,3);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
temp="";
i++;
state=-1;
continue;
}
if( c==' ' || c==10 || c==13 )
{
int temp_type=1;
for(int j=0;j<keyword.length;j++)
{
if(temp.equals(keyword[j]))
{
temp_type=0;
break;
}
}
w2=new word(temp,temp_type);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
state=-1;
temp="";
i++;
continue;
}
/*如果讀到了"則認為是表中的值,則在這里就一直讀完,
*讀完就保存,不另外做處理
*如沒有配對的"則記錄出錯,跳出循環*/
if( c=='\"')
{
info+="標示符后存在\"\n"; //存在其他字符判錯
temp="";
i++;
state=-1;
iserror=true;
continue;
}
info+="存在非法字符\n"; //存在其他字符判錯
temp="";
state=-1;
i++;
iserror=true;
continue;
}
if(state==1)
{
if( (Character.isUpperCase(c)) || (Character.isLowerCase(c)) )
{
temp=temp+c;
state=1;
i++;
continue;
}
if( (Character.isDigit(c)) )
{
temp=temp+c;
state=1;
i++;
continue;
}
/*標號是3類詞語*/
if( (c==';')||(c=='(')||(c==')')||(c==',')||(c=='<')||(c=='=')||(c=='>') )
{
w2=new word(temp,1);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
temp="";
temp+=c;
w2=new word(temp,3);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
temp="";
state=-1;
i++;
continue;
}
if( c==' ' || c==10 || c==13 )
{
w2=new word(temp,1);w2.ago=w1;w1.next=w2;w1=w2;w2=w2.next;
state=0;
temp="";
state=-1;
i++;
continue;
}
if( c=='\"')
{
info+="標示符后存在\"\n"; //存在其他字符判錯
temp="";
i++;
state=-1;
iserror=true;
continue;
}
info+="存在非法字符\n"; //存在其他字符判錯
temp="";
state=-1;
i++;
iserror=true;
continue;
}
}
if(iserror==false)
{
info="沒有文字錯誤";
}
return info;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -