口译员设计功能问题(日食)

时间:2019-01-23 11:35:41

标签: java debugging

我的代码有错误,因为它不评估用户输入的句子的每个单词。

解释器必须从文件中读取所编写程序的指令。在 如果指令中有错误,解释器必须停止并显示引起错误的指令的行号。

我是否缺少该错误消息的库?我听说Maven的工具可以做到这一点,对吗?

Click here to see ideal sample output.

Click here to see program description.

公共类TokenVerifier {

public static String askInput() {
    Scanner input = new Scanner(System.in);
    return input.nextLine();
    };

public static boolean isString (String str) {
    int i = 0, last = str.length() - 1;
    if(str.isEmpty())
        return false;

        if(str.charAt(i) == '"' && (str.charAt(last)) == '"') {
        return true;
        }


        return false;
    }


public static boolean logical(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals(".OR.") || str.equals(".AND.")|| str.equals(".NOT.")) {
        result = true;
    }
    return result;

}
public static boolean rem(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals("REM")) {
        result = true;
    }
    return result;

}
public static boolean print(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals("PRINT")) {
        result = true;
    }
    return result;

}
public static boolean read(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals("READ")) {
        result = true;
    }
    return result;

}
public static boolean isIf(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals("IF")) {
        result = true;
    }
    return result;

}
public static boolean isThen(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals("THEN")) {
        result = true;
    }
    return result;

}
public static boolean isEnd(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals("END")) {
        result = true;
    }
    return result;

}
public static boolean relational(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals(".GT.") || str.equals(".LT.")|| str.equals(".EQ.")||str.equals(".GE.")||str.equals(".LE.")||str.equals(".NE.")) {
        result = true;
    }
    return result;

}
public static boolean arithmetic(String in) {
    String str = in.toUpperCase();
    boolean result = false;

    if(str.equals(".ADD.") || str.equals(".SUB.")|| str.equals(".MUL.")||str.equals(".DIV.")) {
        result = true;
    }
    return result;

}
public static boolean sign(String in) {

    boolean result = false;
    char[] chars = in.toCharArray();

     if (in.isEmpty()) 
            return false;
     if(chars[0] == '+' ||chars[0] == '-')
         result = true;


    return result;
}
public static boolean alphabet(String name) {
    char[] chars = name.toCharArray();
    if (name.isEmpty())
        return false;

    for (char c : chars) {
        if(!Character.isLetter(c)) {
            return false;
        }

    }


    return true;
}
public static boolean digit(String name) {

    char[] chars = name.toCharArray();
    if (name.isEmpty())
        return false;

    for (char c : chars) {
        if(!Character.isDigit(c)) {
            return false;
        }

    }

    return true;
}
public static boolean variable(String in) {
    int i = 0;
    String temp = in.toUpperCase();
    if(temp.equals("REM") ||temp.equals("READ")||temp.equals("PRINT")||temp.equals("IF")||temp.equals("THEN")|| temp.equals("END"))
        return false;
    if(alphabet(in)) {
        if( (in.charAt(i) >= 'a' && in.charAt(i) <= 'f') || (in.charAt(i) >= 'A' && in.charAt(i) <= 'F'))
            System.out.println(" Integer Type");
        if( (in.charAt(i) >= 'g' && in.charAt(i) <= 'n') || (in.charAt(i) >= 'G' && in.charAt(i) <= 'N'))
            System.out.println(" Real Number Variable");
        if( (in.charAt(i) >= 'o' && in.charAt(i) <= 'z') || (in.charAt(i) >= 'O' && in.charAt(i) <= 'Z'))
            System.out.println("String Variable");


        return true;
    }


    return false;
}
public static boolean unsignedInteger(String in) {
    if(digit(in))
    return true;    

    return false;
}
public static boolean signedInteger(String name) {

    if(sign(name)) {
        char[] chars = new char[name.length()-1];
        int start = 0;
        for(int i = 1;i <= name.length() - 1;i++){
            chars[start] = name.charAt(i);
            start++;

        }
        String str = new String(chars);
        if(digit(str))
            return true;
    }

    return false;
}
public static boolean decimalPoint(String in) {
    char[] chars = in.toCharArray();
    int count = 0;
    if (in.isEmpty())
        return false;
    if(chars[0] == '.'|| chars[in.length() - 1] == '.')
        return false;

    for(int i = 0; i <= in.length() - 1;i++) {
        if(chars[i] == '.') {
            count++;        
        }

    }
    if(count == 1)
        return true;


    return false;
}
public static boolean realUnsigned(String in) {

    String unsignedPart = "", decimalPart = "",intPart = in.toUpperCase(), realPart = "";
    int indexOfPoint = 0;
    if (in.isEmpty())
        return false;

    if (sign(intPart) == false){
        if(exponent(intPart)) {
            realPart = intPart.substring(0, intPart.indexOf('E'));
            if(decimalPoint(realPart)) {
                unsignedPart = realPart.substring(1, intPart.indexOf('.'));
                indexOfPoint = realPart.indexOf('.');
                decimalPart =  realPart.substring(indexOfPoint + 1);
                if(digit(unsignedPart)&&digit(decimalPart))
                    return true;
                    }
                }
            else {
                if(decimalPoint(in)) {
                    unsignedPart = in.substring(1, intPart.indexOf('.'));
                    indexOfPoint = in.indexOf('.');
                    decimalPart =  in.substring(indexOfPoint + 1);
                    if(digit(unsignedPart)&&digit(decimalPart))
                        return true;
                        }

            }

        }

    return false;

}
public static boolean realSigned(String in)
{
    String unsignedPart = "", decimalPart = "",intPart = in.toUpperCase(), realPart = "";
    int indexOfPoint = 0;
    if (in.isEmpty())
        return false;

    if (sign(intPart)){
        if(exponent(intPart)) {
            realPart = intPart.substring(0, intPart.indexOf('E'));
            if(decimalPoint(realPart)) {
                unsignedPart = realPart.substring(1, intPart.indexOf('.'));
                indexOfPoint = realPart.indexOf('.');
                decimalPart =  realPart.substring(indexOfPoint + 1);
                if(digit(unsignedPart)&&digit(decimalPart))
                    return true;
                    }
                }
        else {
            if(decimalPoint(in)) {
                unsignedPart = in.substring(1, intPart.indexOf('.'));
                indexOfPoint = in.indexOf('.');
                decimalPart =  in.substring(indexOfPoint + 1);
                if(digit(unsignedPart)&&digit(decimalPart))
                    return true;
                    }

        }

        }



    return false;

}
public static boolean exponent(String in) {

    String unsignedPart = "", decimalPart = "",str = in.toUpperCase(), afterExponent ="";
    int indexOfPoint = 0, indexOfExponent = 0;
    char[] chars = str.toCharArray();
    int count = 0;

    if (in.isEmpty())
        return false;

    for(int i = 0; i <= str.length() - 1;i++) {


        if(chars[i] == 'E') {
            count++;        
        }


        if(count == 1) {
            indexOfExponent = str.indexOf('E');
            if(str.length()-1 == indexOfExponent)
                return false;

            afterExponent = str.substring(indexOfExponent + 1);
            if(decimalPoint(afterExponent)) {
                    if(sign(afterExponent)) {
                        unsignedPart = afterExponent.substring(1, afterExponent.indexOf('.'));
                        indexOfPoint = afterExponent.indexOf('.');
                        decimalPart =  afterExponent.substring(indexOfPoint + 1);
                        if(digit(unsignedPart)&&digit(decimalPart))
                            return true;
                    }
                    else {
                        unsignedPart = afterExponent.substring(0, afterExponent.indexOf('.'));
                        indexOfPoint = afterExponent.indexOf('.');
                        decimalPart =  afterExponent.substring(indexOfPoint + 1);
                        if(digit(unsignedPart)&&digit(decimalPart))
                            return true;
                    }


            }
        }
    }

    return false;   
}

public static boolean tokenVerifier(String input) {

    if(logical(input)) 
        return true;
     if(relational(input)) 
         return true; 
     if(variable(input)) 
         return true;        
     if(isString(input)) 
         return true;
     if(unsignedInteger(input)) 
         return true;
     if(signedInteger(input)) 
         return true;
     if(realUnsigned(input)) 
         return true;
     if(realSigned(input))
         return true;
     if(arithmetic(input)) 
         return true;
     if(rem(input)) 
         return true;
     if(print(input)) 
         return true;
     if(read(input)) 
         return true;
     if(isEnd(input)) 
         return true;            
     if(isThen(input)) 
         return true;
     if(isIf(input)) 
         return true;

    return false;

}
/**********************************************************************************************************************************************************/
/**************************************************************** SENTENCE VERIFIER *****************************************************************/
public static boolean sentenceVerifier(String in) {
    String tokens[] = in.split(" ");
    int  indexOfStringArray = tokens.length;


    for(int i = 0; i <= indexOfStringArray - 1; i++) {

        if(tokenVerifier(tokens[i]))
            System.out.print("Token");
        else
            System.out.print("Not Token");      

    }

    return false;
}





 public static void main(String[] args) { 

     Scanner input = new Scanner(System.in);

     String in  = input.nextLine(); 

     sentenceVerifier(in);






     }

 }

0 个答案:

没有答案