用于自制语法规则的Java解析器

时间:2014-10-27 20:40:58

标签: java

以下是语言规则:

myCalculus ::= expr◃▹myCalculus|expr 
expr ::= term+expr|term  
term ::= factor∗term|factor  
factor ::= (myCalculus)|num  
num ::= const  
◃▹ ::= <|>|<=|>=  
where const ∈ (−∞, ∞).

我正在为课程创建解析器。用户必须在每个元素之间用空格输入等式。然后将每个元素读入其自己的索引中的相同数组中。我的方法遇到错误java.lang.NumberFormatException。我理解错误意味着什么,但是很难解决它。我知道它位于&#34; //如果下一个元素是一个操作元素&#34;因子类,但我不确定我没有考虑的情况。

import java.util.Scanner;

public class Interpreter{
    public static void main(String []args){
    //read in the values as a string
    Scanner scan = new Scanner(System.in);
    String expressions = scan.nextLine();
    //save each token into the array
    String[] token = expressions.split("\\s+");

    //call the lexer with user input to check for syntax errors
    Lexer problem = new Lexer();
    problem.lexer(token);

    //call myCalculus function which will call the other functions
    System.out.println(factor(token, 0));
    }

    public static int myCalculus(String[] token, int i){
        if(token[i].equals("<")){
            return((expr(token, i-1) <  expr(token, i+1)) ? 1 : 0);
        }
        else if(token[i].equals(">")){
            return((expr(token, i-1) >  expr(token, i+1)) ? 1 : 0);
        }
        else if(token[i].equals(">=")){
            return((expr(token, i-1) >=  expr(token, i+1)) ? 1 : 0);
        }
        else if(token[i].equals("<=")){
            return((expr(token, i-1) <=  expr(token, i+1)) ? 1 : 0);
        }
        else{
            return(expr(token, i));
        }
    }

    public static int expr(String[] token, int j){
        if(token[j].equals("+")){
            return(term(token, j-1) + term(token, j+1));
        }
        else{
            return(term(token, j));
        }
    }

    public static int term(String[] token, int k){
        if(token[k].equals("*")){
            return(factor(token, k-1) * factor(token, k+1));
        }
        else{
            return(factor(token, k));
        }
    }

    public static int factor(String[] token, int l){
        //unlike other cases ( can never be the last element
        if(token[l].equals("(")){
            return(myCalculus(token, ++l));
        }
        //parenthesis and not the last element
        else if(token[l].equals(")") && l < (token.length-1)){
            return(myCalculus(token, ++l));
        }
        //number but not the last element
        else if(token[l].matches("[-+]?\\d*\\.?\\d+") && l < (token.length-1)){
            //there are three cases in this scenario
            //one, the next element is a ) and the last element
            //two, the next element is a ) and not the last element --
            //three, the next element is an operational element

            //if the next element is a ) and the last element
            if(token[l+1].equals(")") && l+1 == (token.length-1)){
                return(Integer.parseInt(token[l]));
            }
            //if next element is an operational element
            else{
                if(l+2 < (token.length-1)){
                    return(myCalculus(token, l+1));
                }
                else{
                    return(Integer.parseInt(token[l]));
                }
            }
        }
        //number and the last element
        else{
            return(Integer.parseInt(token[l]));
        }
    }
}

0 个答案:

没有答案