非终结者在语法中没用[-Wother]

时间:2016-10-15 16:17:35

标签: bison yacc

当我用yacc编译我的程序时,它给了我错误。 我无法理解我是否使用间接递归或其他东西。 我不是很熟悉yacc。 我的代码规则部分是:

 stmtfor    :   exprfor OPENB CLOSEB    {   printf("\nValid For statement!\n"); }           |
        exprfor OPENB stmtfor CLOSEB    {   printf("\nValid For statement!\n"); }   |
        exprfor     {   printf("\nValid For statement!\n"); }
        ;

 exprfor    :   FOR '(' ID '=' DIGIT DELIMITER ID COND DIGIT DELIMITER ID INC ')'   |
        FOR '(' ID '=' ID DELIMITER ID COND DIGIT DELIMITER ID INC ')'      |
        FOR '(' ID '=' DIGIT DELIMITER ID COND ID DELIMITER ID INC ')'      |
        FOR '(' ID '=' ID DELIMITER ID COND ID DELIMITER ID INC ')'         |
        FOR '(' DELIMITER DELIMITER ')'                                     |
        FOR '(' DELIMITER ID COND DELIMITER INC')'                          |
        FOR '(' DELIMITER DELIMITER INC ')'
        ;

stmtif  :   exprif OPENB CLOSEB ELSE OPENB CLOSEB   {   printf("\nValid if statement!\n");  }   |
        exprif OPENB CLOSEB {   printf("\nValid if statement!\n");  }   |
        exprif OPENB stmtif CLOSEB {    printf("\nValid if statement!\n");  }   |
        exprif OPENB stmtif CLOSEB ELSE OPENB stmtif CLOSEB {   printf("\nValid if statement!\n");  }
        ;

exprif  :   IF '(' ID COND DIGIT ')'    |
        IF '(' ID COND ID ')'       |
        IF '(' DIGIT COND DIGIT ')' |
        IF '(' ID ')'               |
        IF '(' DIGIT COND ID ')'
        ;

 stmtwh :   exprwh OPENB CLOSEB     {   printf("\nValid while statement");  }       |
        exprwh OPENB stmtwh CLOSEB  {   printf("\nValid while statement");  }   |
        exprwh  {   printf("\nValid while statement");  }
        ;

exprwh  :   WHILE '(' ID COND DIGIT ')' |
        WHILE '(' ID COND ID ')'    |
        WHILE '(' DIGIT COND DIGIT ')'  |
        WHILE '(' DIGIT COND ID ')' |
        WHILE '(' ID ')'            |
        WHILE '(' DIGIT ')'
        ;
%%

我得到的错误是:

    ond_rec.y: warning: 2 nonterminals useless in grammar [-Wother]
    cond_rec.y: warning: 9 rules useless in grammar [-Wother]
    cond_rec.y:21.1-6: warning: nonterminal useless in grammar: stmtwh       [-Wother]
    stmtwh  :   exprwh OPENB CLOSEB     {   printf("\nValid while            statement");   }       |
    ^^^^^^
    cond_rec.y:21.17-22: warning: nonterminal useless in grammar: exprwh     [-Wother]
    stmtwh  :   exprwh OPENB CLOSEB     {   printf("\nValid while       statement");    }       |
             ^^^^^^
    cond_rec.y:21.17-97: warning: rule useless in grammar [-Wother]
    stmtwh  :   exprwh OPENB CLOSEB     {   printf("\nValid while statement");  }       |
             ^^^^^^^^^^
    cond_rec.y:22.25-105: warning: rule useless in grammar [-Wother]
        exprwh OPENB stmtwh CLOSEB  {   printf("\nValid while statement");  }   |
                     ^^^
    cond_rec.y:23.25-81: warning: rule useless in grammar [-Wother]
        exprwh  {   printf("\nValid while statement");  }
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cond_rec.y:26.17-43: warning: rule useless in grammar [-Wother]
    exprwh  :   WHILE '(' ID COND DIGIT ')' |
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cond_rec.y:27.25-48: warning: rule useless in grammar [-Wother]
        WHILE '(' ID COND ID ')'    |
                     ^^^^^^^^^^^^^^^^^^^^^^^^
    cond_rec.y:28.25-54: warning: rule useless in grammar [-Wother]
        WHILE '(' DIGIT COND DIGIT ')'  |
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cond_rec.y:29.25-51: warning: rule useless in grammar [-Wother]
        WHILE '(' DIGIT COND ID ')' |
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cond_rec.y:30.25-40: warning: rule useless in grammar [-Wother]
        WHILE '(' ID ')'            |
                     ^^^^^^^^^^^^^^^^
    cond_rec.y:31.25-43: warning: rule useless in grammar [-Wother]
        WHILE '(' DIGIT ')'
                     ^^^^^^^^^^^^^^^^^^^

我之前看过关于这个话题的帖子,但似乎没有一个对我有用,因为我无法理解这个问题。

1 个答案:

答案 0 :(得分:1)

你似乎没有在语法的任何地方使用stmtwh。它只出现在它自己的定义中,而不是其他地方。