这个语法有什么问题?

What is wrong with this grammar?

收到此错误。我贴了一部分错误和语法规则。

parser.y: warning: 6 nonterminals useless in grammar [-Wother]

parser.y: warning: 15 rules useless in grammar [-Wother]

parser.y:95.1-16: warning: nonterminal useless in grammar: func_declaration [-Wother] func_declaration : ^^^^^^^^^^^^^^^^

parser.y:113.1-15: warning: nonterminal useless in grammar: func_definition [-Wother] func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement { ^^^^^^^^^^^^^^^

parser.y:96.44-57: warning: nonterminal useless in grammar: parameter_list [-Wother] | type_specifier ID LPAREN parameter_list RPAREN SEMICOLON { ^^^^^^^^^^^^^^

parser.y:113.66-83: warning: nonterminal useless in grammar: compound_statement [-Wother] func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement { ^^^^^^^^^^^^^^^^^^

parser.y:143.28-37: warning: nonterminal useless in grammar: statements [-Wother] compound_statement : LCURL statements RCURL ^^^^^^^^^^

parser.y:217.14-22: warning: nonterminal useless in grammar: statement [-Wother] statements : statement ^^^^^^^^^

parser.y:95.17: warning: rule useless in grammar [-Wother] func_declaration : ^

parser.y:96.19-105.17: warning: rule useless in grammar [-Wother] | type_specifier ID LPAREN parameter_list RPAREN SEMICOLON { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

parser.y:106.19-110.17: warning: rule useless in grammar [-Wother] | type_specifier ID LPAREN RPAREN SEMICOLON { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

parser.y:113.19-116.17: warning: rule useless in grammar [-Wother] func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

parser.y:117.19-120.17: warning: rule useless in grammar [-Wother] | type_specifier ID LPAREN RPAREN compound_statement { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

parser.y:124.19-127.17: warning: rule useless in grammar [-Wother] parameter_list : parameter_list COMMA type_specifier ID { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

parser.y:128.19-131.17: warning: rule useless in grammar [-Wother] | parameter_list COMMA type_specifier { ^^^^^^^^^^^^^^^^^^^^^^^

parser.y:132.19-135.17: warning: rule useless in grammar [-Wother] | type_specifier ID { ^^^^^^

parser.y:136.19-139.17: warning: rule useless in grammar [-Wother] | type_specifier { ^^

parser.y:143.22-43: warning: rule useless in grammar [-Wother] compound_statement : LCURL statements RCURL ^^^^^^^^^^^^^^^^^^^^^^

parser.y:144.23-33: warning: rule useless in grammar [-Wother] | LCURL RCURL ^^^^^^^^^^^

parser.y:217.14-22: warning: rule useless in grammar [-Wother] statements : statement ^^^^^^^^^

parser.y:218.14-33: warning: rule useless in grammar [-Wother] | statements statement ^^^^^^^^^^^^^^^^^^^^

parser.y:221.13-18: warning: rule useless in grammar [-Wother] statement : DOUBLE ^^^^^^

parser.y:222.19-34: warning: rule useless in grammar [-Wother] | statement DOUBLE ^^^^^^^^^^^^^^^^

语法规则:

    start : program;

    program : program unit 
        | unit
        ;

    unit : var_declaration
         | func_declaration
         | func_definition
         ;

    func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON
            | type_specifier ID LPAREN RPAREN SEMICOLON
            ;

    func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement
            | type_specifier ID LPAREN RPAREN compound_statement
            ;               


    parameter_list  : parameter_list COMMA type_specifier ID
            | parameter_list COMMA type_specifier
            | type_specifier ID
            | type_specifier
            ;


    compound_statement : LCURL statements RCURL
                | LCURL RCURL
                ;

    var_declaration : type_specifier declaration_list SEMICOLON
             ;

    type_specifier  : INT
            | FLOAT
            | VOID
            ;

    declaration_list : declaration_list COMMA ID
              | declaration_list COMMA ID LTHIRD CONST_INT RTHIRD
              | ID
              | ID LTHIRD CONST_INT RTHIRD
              ;

    statements : statement
           | statements statement
           ;
    statement : DOUBLE
            | statement DOUBLE
            ;

parser.y

%{
#include<stdio.h>
#include<stdlib.h>  
#include<string.h>
#include "SymbolTable.h"
#include "SymbolInfo.h"
#include "ScopeTable.h"

int yyparse(void);
int yylex(void);
extern char* yytext;
extern FILE * yyin;
extern int tableSize;

FILE *logout;
extern int line_count;
extern char *arr[100];
extern char *final_arr[100];

SymbolTable *table;

void yyerror (const char *s)
{
    fprintf(stderr,"%s\n",s);
    return;
}

%}

%union {
    class SymbolInfo* sym;
    char *s;
    float f;
}

%error-verbose
%verbose
%token COMMA INT SEMICOLON FLOAT VOID LCURL RCURL RETURN RELOP LOGICOP ADDOP INCOP DECOP CONST_FLOAT NOT IF FOR WHILE ASSIGNOP MULOP PRINTLN LPAREN RPAREN
%token CONST_INT LTHIRD RTHIRD 
%token <s> ID

%token <f> DOUBLE
//%expect 1

%precedence THEN
%precedence ELSE

%left "<" ">" "<=" ">=" "=" "!="
%left "+" "-"
%left "*" "/"
%left UMINUS 


%%

start : program     {   //printf("start -> program\n");
                        fprintf(logout,"%d : start ->  program\n",line_count);
                    }
      ;

program : program unit {
                            //printf("program -> program unit\n");
                            fprintf(logout,"%d : program -> program unit\n\n",line_count);
                            for(int j = 0; final_arr[j] != NULL; j++)
                            {
                                fprintf(logout,"%s",final_arr[j]);
                            }
                                fprintf(logout,"\n\n");
                        }
        | unit          {
                            //printf("program -> unit\n");
                            fprintf(logout,"%d : program -> unit\n\n",line_count);
                            for(int j = 0; final_arr[j] != NULL; j++)
                            {
                                fprintf(logout,"%s",final_arr[j]);
                            }
                                fprintf(logout,"\n\n");

                        }
        ;

unit : var_dec  {
                    //printf("unit -> var_dec\n");
                    fprintf(logout,"%d : unit -> var_dec\n\n",line_count);
                    for(int j = 0; arr[j] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                    fprintf(logout,"\n\n");

                }

     ;

func_declaration : 
        | type_specifier ID LPAREN parameter_list RPAREN SEMICOLON  {
                fprintf(logout,"%d : func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON\n\n", line_count);

                for(int j = 0; arr[j] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                    fprintf(logout,"\n\n");

        }
        | type_specifier ID LPAREN RPAREN SEMICOLON {
                fprintf(logout,"%d : func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON\n\n", line_count); 


        }
        ;

func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement {
                fprintf(logout,"%d : func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement\n\n", line_count); 

        }
        | type_specifier ID LPAREN RPAREN compound_statement {
                fprintf(logout,"%d : func_definition : type_specifier ID LPAREN RPAREN compound_statement\n\n", line_count);    

        }
        ;               


parameter_list  : parameter_list COMMA type_specifier ID {
                fprintf(logout,"%d : parameter_list  : parameter_list COMMA type_specifier ID\n\n", line_count);    

        }
        | parameter_list COMMA type_specifier {
                fprintf(logout,"%d : parameter_list  : parameter_list COMMA type_specifier\n\n", line_count);   

        }
        | type_specifier ID {
                fprintf(logout,"%d : parameter_list : type_specifier ID\n\n", line_count);  

        }
        | type_specifier {
                fprintf(logout,"%d :  parameter_list : type_specifier \n\n", line_count);   

        }
        ;


compound_statement : LCURL statements RCURL
            | LCURL RCURL
            ;

var_dec: type_specifier declaration_list SEMICOLON {

                    //printf("var_dec -> type_specifier declaration_list SEMICOLON \n");
                    fprintf(logout,"%d : var_dec: type_specifier declaration_list SEMICOLON \n\n", line_count);

                    for(int j = 0; arr[j] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                    fprintf(logout,"\n\n");

            }
        ;            

type_specifier : INT    {//printf("type_specifier -> INT\n");
                            fprintf(logout,"%d : type_specifier-> INT\n\n%s\n\n", line_count,yytext);
                        }
               | FLOAT  {//printf("type_specifier ->FLOAT\n");
                            fprintf(logout,"%d : type_specifier-> FLOAT\n\n%s\n\n",line_count, yytext);

                        }
               | VOID   {printf("type_specifier -> VOID\n");
                            fprintf(logout,"%d : type_specifier-> VOID\n\n%s\n\n",line_count, yytext);

                         }
               ;        

declaration_list : declaration_list COMMA ID {

                        printf("declaration_list -> declaration_list COMMA ID\n");  
                        fprintf(logout,"%d : declaration_list -> declaration_list COMMA ID\n\n",line_count);
                        for(int j = 1; arr[j+1] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                            fprintf(logout,"\n\n");
                       }
                 | declaration_list COMMA ID LTHIRD CONST_INT RTHIRD {

                        printf("declaration_list -> declaration_list COMMA ID LTHIRD CONST_INT RTHIRD\n");      
                        fprintf(logout,"%d : declaration_list -> declaration_list COMMA ID LTHIRD CONST_INT RTHIRD\n",line_count);
                        for(int j = 1; arr[j+1] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                            fprintf(logout,"\n\n");

                        }
                 |ID    {
                        printf("declaration_list -> ID\n");
                        fprintf(logout,"%d : declaration_list -> ID\n\n",line_count);
                        for(int j = 1; arr[j+1] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                            fprintf(logout,"\n\n");
                        }
                 |ID LTHIRD CONST_INT RTHIRD {

                        printf("declaration_list -> ID LTHIRD CONST_INT RTHIRD\n"); 
                        fprintf(logout,"%d : declaration_list -> ID LTHIRD CONST_INT RTHIRD\n",line_count);
                        for(int j = 1; arr[j+1] != NULL; j++)
                        {
                            fprintf(logout,"%s",arr[j]);
                        }
                            fprintf(logout,"\n\n");

                        }
                 ;  

statements : statement
       | statements statement
       ;

statement : DOUBLE
        | statement DOUBLE
        ;



%%

int main(int argc, char *argv[])
{

    FILE *fp  ;
    int token = 0;
    if((fp = fopen(argv[1],"r")) == NULL)
    {
        fprintf(logout,"cannot open file");
        exit(1);
    }


    logout = fopen("log.txt","w");

    yyin = fp;
    yyparse();

    fclose(fp);
    fclose(logout);
    return 0;

}

在您的 "simplified" 语法中,您将 unit 定义为:

unit : var_declaration
     | func_declaration
     | func_definition
     ;

但在您的实际语法中,您将其定义为:

unit : var_dec  {
                //...
            }
;

这里var_decunit中唯一的选择。 func_declarationfunc_definition 从未被提及,因此无法访问。这就是警告试图告诉您的内容。

如果您将实际代码更改为具有与 "simplified" 相同的三个备选方案,那应该可以解决问题。