java - 以下语法正确吗?

标签 java algorithm grammar

我正在尝试使用以下语法的递归下降分析来检查语法的正确性:

<FACTOR> ::= <EXPR> | i
<TERM> ::= <FACTOR> * <TERM> | <FACTOR>
<EXPR> ::= <TERM> + <EXPR> | <TERM>

问题是,语法似乎是递归的,因为 factor 可以是 expr 可以是 term 可以是 因素。所以似乎不可能使用程序来检查它的正确性。但是我不确定这是正确的,因为这是作为作业给出的。有人可以告诉我它是否正确吗?如果正确,我可以使用一种可能的算法来检查它吗?

谢谢。

不知道是否有帮助,但这是我当前的代码:

//Variable to store our current character index
static int i = 0;
//Variable to store our input string
static String input;

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter text to check for correctness: ");
    input = scan.nextLine();
    if(input.charAt(input.length() - 1) != '#'){
        input += scan.nextLine();                    
    }
    //Remove all spaces just to prevent space interference
    input = input.replaceAll(" ", "");
    if(Factor(nextChar()))
    {
        System.out.println("Your text input is correct");
    }
    else
    {
        System.out.println("Your text input does not conform with the grammar");
    }
}

public static boolean Factor(char ourChar){
    //<factor> ::= <expr> | i        
    if(ourChar == '#')
    {
        //If it's # we should bounce back if and return true since no errors yet
        return true;
    }
    if(ourChar == 'i')
    {
        //if it's i then return true
        return true;
    }
    else{
        //so the character is not i, let's check if satisfies the Expr grammar
        if(!Expr(ourChar))
        {
            //oooh, it's not return false!
            return false;
        }
        return true;
    }        
}

public static boolean Expr(char ourChar){
    //<expr> ::= <term> + <expr> | <term>
    //Before you can be an expression, you must start with a term
    if(!Term(ourChar))
        //so it doesn't start with term? Bounce back dear
        return false;
    if(nextChar() != '+'){
        //The next character is not a plus, return false to sender
        return false;
    }
    else
    {
        //So it's plus? Ok, let's check if the next character is another expr
        if(!Expr(nextChar()))
            return false;
        else
            //Everybody satisfied, return true
            return true;
    }
}

public static boolean Term(char ourChar){
    //<term> ::= <factor> * <term> | <factor>
    //If the character does not start with a factor bounce back
    if(!Factor(ourChar))
        return false;
    if(nextChar() != '*'){
        //Yekpa! The factor is not followed by * so bounce back
        return false;
    }
    else{
        //SO, it's a star. Ok, if it's followed by a term, bounce back
        if(!Term(nextChar()))
        {
            return false;
        }
        //Well, all bouncers satisfied, so return true
        return true;
    }
}

public static char nextChar(){
    i++;
    return input.charAt(i - 1);
}    

最佳答案

问题中输入的语法与正常的表达式语法不符,因为它不允许使用括号。也是模棱两可的。所以我会选择“不,下面的语法不正确。”

我建议使用以下语法(注意括号):

<FACTOR> ::= ( <EXPR> ) | i
<TERM> ::= <FACTOR> * <TERM> | <FACTOR>
<EXPR> ::= <TERM> + <EXPR> | <TERM>

关于java - 以下语法正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16966989/

相关文章:

java - 我的 maven webapp 项目在 tomcat7 上成功启动但 tomcat8 失败,为什么?

java - 使用 xml 中的 dom4j 在指定位置添加新节点

r - 我有一个可以运行的 mapply 函数 - 如何将其转换为 mlply?

javascript - 为什么匿名函数本身是 javascript 中的语法错误?

正则表达式 BNF 语法

algorithm - 上下文无关文法与上下文相关文法?

java - 将AAR指向自身而不是Maven版本

algorithm - 使用自定义函数代替素数的汉明数

c++ - 在 C++ 中用字符串中更多的字符替换一个字符,而不删除其他字母

java - 直接使用java修改XML文件