2016-10-15 29 views
0

當我用yacc編譯我的程序時,它給了我錯誤。 我不能理解,如果我使用間接遞歸或其他。 我不熟悉yacc。 我的代碼的規則部分是:語法中無用的非終結者[-Wother]

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

你的文法中沒有任何地方使用stmtwh。它只出現在它自己的定義中,沒有其他地方。

+0

No.I'hv used stmtwh! – Omniverse10

+0

@ Omniverse10哪裏? – sepp2k

+0

具體來說,這就是錯誤告訴你的 - 從開始符號到'stmtwh'或'exprwh'沒有路徑,所以他們所有的規則都是「無用的」,因爲它們永遠不可能達到。 –