1

次の文法の再帰降下解析を使用して、文法の正確性をチェックしようとしています:

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

factor問題は、 can be exprwhich can be termwhich can be であるため、文法が再帰的であるように見えることfactorです。したがって、プログラムを使用してこれが正しいかどうかを確認することは不可能のようです。ただし、これは課題として与えられたものであるため、これが正しいかどうかはわかりません。それが正しいかどうか誰か教えてもらえますか? もし本当なら、これをチェックするために使用できるアルゴリズムを教えてもらえますか?

ありがとう。

それが役立つかどうかはわかりませんが、現在のコードは次のとおりです。

//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);
}    
4

2 に答える 2

1

質問に入力された文法は、括弧が許可されていないため、通常の式の構文に対応していません。それも曖昧です。だから私は「いいえ、次の文法は正しくありません。」

次の文法をお勧めします (括弧に注意してください)。

<FACTOR> ::= ( <EXPR> ) | i
<TERM> ::= <FACTOR> * <TERM> | <FACTOR>
<EXPR> ::= <TERM> + <EXPR> | <TERM>
于 2013-06-06T16:58:57.927 に答える
0
  1. 開始シンボルは、「<"FACTOR">」ではなく「<"EXPR">」に継ぎ目があります。これを確認する必要があります。
  2. 文法があいまいです。たとえば、"i" は "<"FACTOR">" => i
    または "<"FACTOR">" => "<"EXPR">" => "<"TERM">" => "<"で導出できます。ファクター">" => i

  3. "<"FACTOR> は "<"EXPR">" および "<"TERM">" と同等です。
    "<"FACTOR">" ::= "<"EXPR">" および "<"TERM">のためです。 " ::= "<"FACTOR">" および "<"EXPR">" ::= "<"TERM">" したがって、"<"EXPR">" ::= i + "<"EXPR を入力できます">" | i * "<"EXPR">" | i この CFG にはあいまいさも左再帰もありませんが、右因数分解する必要があるため、次を使用できます: "<"EXPR">" ::= i "<"AUX">" "<"AUX">" ::= + "<"EXPR">" | * "<"EXPR">" | | e (e は空の文字列)

この文法は LL(1) であり、同じ言語を生成するため、この文法の再帰降下解析を構築できます。

幸運を。

于 2013-06-13T02:13:16.713 に答える