⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordanalysis.java

📁 基于xml的数据库管理系统
💻 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 + -