获取此错误。我粘贴了一部分错误和语法规则。
parser.y:警告:6个非终结者在语法中没用[-Wother]
parser.y:警告:15条规则在语法中没用[-Wother]
parser.y:95.1-16:警告:语法中的非终结无用: func_declaration [-Wother] func_declaration: ^^^^^^^^^^^^^^^^
parser.y:113.1-15:警告:语法中的非终结无用:func_definition [-Wother] func_definition:type_specifier ID LPAREN parameter_list RPAREN compound_statement { ^^^^^^^^^^^^^^^
parser.y:96.44-57:警告:语法中的非终结无用:parameter_list [-Wother] | type_specifier ID LPAREN parameter_list RPAREN SEMICOLON { ^^^^^^^^^^^^^^
parser.y:113.66-83:警告:语法中的非终结无用:compound_statement [-Wother] func_definition:type_specifier ID LPAREN parameter_list RPAREN compound_statement { ^^^^^^^^^^^^^^^^^^
parser.y:143.28-37:警告:非终结语法无用:语句[-Wother] compound_statement:LCURL语句RCURL ^^^^^^^^^^
parser.y:217.14-22:警告:语法中的非终结无效:声明[-Wother] 声明:声明 ^^^^^^^^^
parser.y:95.17:警告:规则在语法中没用[-Wother] func_declaration: ^
parser.y:96.19-105.17:警告:规则在语法中没用[-Wother] | type_specifier ID LPAREN parameter_list RPAREN SEMICOLON { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parser.y:106.19-110.17:警告:规则在语法中没用[-Wother] | type_specifier ID LPAREN RPAREN SEMICOLON { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parser.y:113.19-116.17:警告:规则在语法中没用[-Wother] func_definition:type_specifier ID LPAREN parameter_list RPAREN compound_statement { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
parser.y:117.19-120.17:警告:规则在语法中没用[-Wother] | type_specifier ID LPAREN RPAREN compound_statement { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parser.y:124.19-127.17:警告:规则在语法中无用[-Wother] parameter_list:parameter_list COMMA type_specifier ID { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parser.y:128.19-131.17:警告:规则在语法中没用[-Wother] | parameter_list COMMA type_specifier { ^^^^^^^^^^^^^^^^^^^^^^^
parser.y:132.19-135.17:警告:规则在语法中无用[-Wother] | type_specifier ID { ^^^^^^
parser.y:136.19-139.17:警告:规则在语法中无用[-Wother] | type_specifier { ^^
parser.y:143.22-43:警告:规则在语法中没用[-Wother] compound_statement:LCURL语句RCURL ^^^^^^^^^^^^^^^^^^^^^^
parser.y:144.23-33:警告:规则在语法中没用[-Wother] | LCURL RCURL ^^^^^^^^^^^
parser.y:217.14-22:警告:规则在语法中没用[-Wother] 声明:声明 ^^^^^^^^^
parser.y:218.14-33:警告:规则在语法中没用[-Wother] |陈述声明 ^^^^^^^^^^^^^^^^^^^^
parser.y:221.13-18:警告:规则在语法中无用[-Wother] 声明:DOUBLE ^^^^^^
parser.y:222.19-34:警告:规则在语法中无用[-Wother] |声明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;
}
答案 0 :(得分:2)
在您的“简化”语法中,您已将unit
定义为:
unit : var_declaration
| func_declaration
| func_definition
;
但是在你真正的语法中,你已经将它定义为:
unit : var_dec {
//...
}
;
此处var_dec
是unit
中的唯一替代方案。永远不会提及func_declaration
和func_definition
因此无法访问。这就是警告试图告诉你的。
如果您将实际代码更改为与“简化”代码具有相同的三个备选方案,那么应该可以解决问题。