作业系列 - 尝试编写SQL数据库(二)

语法分析:

我仔细的检查了SQL语言的语法,他的格式基本上是固定的,因此我初步把语句分离划分了以下几种情况:

1.获得一个单词

这个很有必要,在确定关键字等方面都用得到。

2.获得括号中的内容

在insert语句里和处理where判断的时候都会用的到。

3.获得关键字之间的内容

比如select * from中的select 和 from 之间的语句,目前该算法实现了取最远和最近的两个关键字之间的内容。

代码如下,欢迎参考指正。里面的一些算法不是用的最新算法,是我为了写代码的速度和熟悉程度来写。

package com.watermooon.gram_about;

import java.util.Stack;
import java.util.concurrent.Exchanger;

/**
 * Created by popp on 2016/10/23.
 */
public class SentenceBroken {
    public static  boolean isLetter(char c){
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }
    public static boolean isSpace(char c){
        return  c == '\t' || c =='\r' || c == '\n' || c == ' ' || c =='\b' || c == '\f';
    }
    public static String getWord(String sentence, int beginIndex){
        int endIndex = 0;
        for(; beginIndex < sentence.length(); ++beginIndex){
            char c = sentence.charAt(beginIndex);
            if(isSpace(c))
                continue;
            else
                break;
        }
        for(int i = beginIndex; i < sentence.length(); endIndex = ++i){
            char c = sentence.charAt(i);
            if( isLetter(c) )
                continue;
            else break;
        }
        return sentence.substring(beginIndex, endIndex);
    }
    public static String getBrackets(String sentence,int beginIndex) throws Exception {
        int endIndex = 0;
        for(; beginIndex < sentence.length(); ++beginIndex){//head space
            char c = sentence.charAt(beginIndex);
            if(isSpace(c))
                continue;
            else
                break;
        }
        Stack<Integer> s = new Stack<>();
        if(sentence.charAt(beginIndex) != '('){//if the first !space char not '(  throw exception
            throw  new Exception("Error: not begin with '(");
        }
        for(int i = beginIndex; i < sentence.length(); endIndex = ++i){
            char c = sentence.charAt(i);
            if(c == '('){
                s.push(1);
            }
            if(c == ')'){
                s.pop();
            }
            if(s.size() == 0){
                ++endIndex;
                break;
            }
        }
        if(s.size() != 0)
            throw new Exception("Error:the number of left bracket not equals right bracket");
        return sentence.substring(beginIndex, endIndex);
    }
    public static  boolean sameLetter(char a, char b){
        if(a == b)return true;
        if( (a - b) == ('A' - 'a') )return true;
        if( (b - a) == ('A' - 'a') )return true;
        return  false;
    }
    public static int getWordPos(String sentence, String contWord){
        int ct = sentence.length() - contWord.length() + 1;
        for(int i = 0; i < ct; ++i){
            boolean flag = (i == 0 || !isLetter(sentence.charAt(i-1)));
            for(int j = 0; j < contWord.length(); ++j){
                if(!sameLetter(sentence.charAt(i + j), contWord.charAt(j)))
                    flag = false;
            }
            if(i != ct-1 && isLetter(sentence.charAt(i + contWord.length())))
                flag = false;
            if(flag)return  i;
        }
        return -1;
    }
    public static String getContent_S(String sentence, String beginWord, String endWord){
        String t1 = getContent_S(sentence, beginWord);
        if(t1 == null)return  null;
        String t2 = getContent_S(t1, endWord);
        if(t2 == null)return null;
        return  t1.substring(0, t1.length() - t2.length() - endWord.length());
    }
    public static String getContent_L(String sentence, String beginWord, String endWord){
        String t1 = getContent_S(sentence, beginWord);
        if(t1 == null)return  null;
        String t2 = getContent_L(t1, endWord);
        if(t2 == null)return null;
        return  t1.substring(0, t1.length() - t2.length() - endWord.length());
    }
    public static String getContent_S(String sentence, String beginWord){
        int beginIndex = getWordPos(sentence, beginWord);
        if(beginIndex == -1)
            return  null;
        beginIndex += beginWord.length();
        return sentence.substring(beginIndex);
    }
    public static String getContent_L(String sentence, String beginWord){
        String res = getContent_S(sentence, beginWord), tmp = res;
        while(tmp != null){
            tmp = getContent_S(sentence.substring(sentence.length() - res.length()), beginWord);
            if(tmp != null)res = tmp;
        }
        return  res;
    }
}

4.各个部分的执行顺序。

第二部分到此结束。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值