YACC Rules not reduced

北战南征 提交于 2019-12-13 04:48:44

问题


This is my code calc.y. I keep getting the error: yacc: 1 rule never reduced yacc: 3 reduce/reduce conflicts

not really sure what this means

Ive done some research in other places but I am now lost. Im guessing the rules being referred to is program and statement but even so... what does the reduce rule mean?

%{
  #include <stdio.h>
  FILE *outfile;
  int yyline = 1;
  int yycolumn = 1;
%}

%union{
  int nw;
  struct{
    int v;
    char s[1000];
  }attr;
}

%token   SEMInumber
%token   LPARENnumber
%token   <nw> ICONSTnumber
%token   BEGINnumber
%token   PROGRAMnumber
%token   MINUSnumber
%token   TIMESnumber
%token   <nw> VARnumber
%token   INTnumber
%token   EOFnumber
%token   COMMAnumber
%token   RPARENnumber
%token   <nw>IDnumber
%token   ENDnumber
%token   ISnumber
%token   PLUSnumber
%token   DIVnumber
%token   PRINTnumber
%token   EQnumber

%type <attr> exp
%type <attr> term
%type <attr> factor
%%


program: PROGRAMnumber IDnumber ISnumber compstate
         ;

compstate: BEGINnumber {print_header();} statement ENDnumber{print_end();}
           | BEGINnumber {print_header();} statement SEMInumber statement ENDnumber{print_end();}
           ;

statement: IDnumber EQnumber exp
           | PRINTnumber exp
 | declaration
           ;

declaration: VARnumber IDnumber
             | VARnumber IDnumber COMMAnumber IDnumber
             ;

exp: term {$$.v = $1.v; strcpy($$.s, $1.s);}
     | exp PLUSnumber term {$$.v = $1.v + $3.v; sprintf($$.s, "(%s) + (%s)", $1. s, $3.s);}
     | exp MINUSnumber term {$$.v = $1.v - $3.v; sprintf($$.s, "(%s) - (%s)", $1. s, $3.s);}
     ;

term: factor {$$.v = $1.v; strcpy($$.s, $1.s);}
      | term TIMESnumber factor {$$.v = $1.v * $3.v; sprintf($$.s, "(%s) * (%s)", $1.s, $3.s);}
      | term DIVnumber factor {$$.v = $1.v / $3.v; sprintf($$.s, "(%s) / (%s)", $1.s, $3.s);}
      ;

 factor: ICONSTnumber {$$.v = $1; sprintf($$.s, "%d", $1);}
          | IDnumber {$$.v = $1.v; strcpy($$.s,  $1.s);}
          | LPARENnumber exp RPARENnumber {$$.v = $2.v; strcpy($$.s,  $2.s);}
;


%%


int main()
{
  if(!yyparse())
    {
      printf("accept\n");
    }
  else
    printf("reject\n");
}

void print_header() {}
void print_end(){}

void yyerror(const char *str)
{
  printf("yyerror: %s at line %d\n", str, yyline);
}

回答1:


When compstate shifts the BEGINnumber token, two inner rules for the mid rule action {print_header();} can be both reduced resulting in a R/R conflict. Youe can replace

compstate: BEGINnumber {print_header();} statement ENDnumber{print_end();}
           | BEGINnumber {print_header();} statement SEMInumber statement 
ENDnumber{print_end();}
           ;

with, for example

begin_number:
    BEGINnumber { print_header(); }

compstate: begin_number statement ENDnumber{print_end();}
           | begin_number statement SEMInumber statement 
ENDnumber{print_end();}
           ;

to solve the conflict.




回答2:


Informative Messages %s: %d rules never reduced
Some rules are never used, either because they weren't used in the grammar or because they were on the losing end of shift/reduce or reduce/reduce conflicts. Either change the grammar to use the rules or remove them.



来源:https://stackoverflow.com/questions/29135724/yacc-rules-not-reduced

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!