-1

Getting this error. I'm pasting a part of the error and grammar rules.

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 ^^^^^^^^^^^^^^^^

grammar rules:

    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;

}
afsara_ben
  • 542
  • 1
  • 11
  • 30
  • Which of those rules is bison calling useless (how do the line numbers in the bison output map to the snippet you posted)? And which one is the start rule? – sepp2k Jun 11 '18 at 17:08
  • wait i'm editing it – afsara_ben Jun 11 '18 at 17:12
  • @sepp2k edited it. i cannot copy the text along with line numbers sorry. is this information enough? – afsara_ben Jun 11 '18 at 17:23
  • 1
    Please don't paste errors from one version of your program and text from another version. Error messages (and debugging output if applicable) sjould come from *precisely* the program pasted. Also, please paste *complete* compilable programs. It's entirely appropriate to remove actions from a grammar file, as you have done, but you must create the modified grammar on your machine and collect the error messages from the result of running bison on that file. Otherwise, we have make guesses about the incomplete files, and that makes it much harder for us to help you. – rici Jun 11 '18 at 17:23
  • Also, you should paste output such as error messages as code, not blockquotes. It's unreadable when spacing and newlines are removed from the messages. – rici Jun 11 '18 at 17:26
  • sorry editing it – afsara_ben Jun 11 '18 at 17:28
  • See, this is why you should always ensure that errors come from the actual code pasted. What you've done here is a massive waste of everybody's time, including yours. – rici Jun 11 '18 at 17:42

1 Answers1

2

In your "simplified" grammar you've defined unit as:

unit : var_declaration
     | func_declaration
     | func_definition
     ;

But in your real grammar, you've defined it as:

unit : var_dec  {
                //...
            }
;

Here var_dec is the only alternative in unit. func_declaration and func_definition are never mentioned and thus not reachable. That's what the warnings are trying to tell you.

If you change your real code to have the same three alternatives as the "simplified" one, that should fix the problem.

sepp2k
  • 363,768
  • 54
  • 674
  • 675