Моего yacc
файла производят неожиданные предупреждения:Yacc относится к значимым правилам, бесполезного
%{
#include<stdio.h>
#include<stdlib.h>
#define YYSTYPE char*
#define YYSTYPE int
#define YYSTYPE char
extern char *yytext;
%}
%token INT STR FLOAT INTEGER CHARACTER STRING IDENTIFIER UNARYOPR OPR
%token NOT LEFT_BKT RIGHT_BKT RELATIONOPR ASSIGNMENT IF ELIF
%token ELSE UNTIL WHILE ENDWHILE FOR RAW_INPUT INPUT PRINT
%token TERMINATOR DO SET SETATTRIBUTE FUNCDEC CLASSDEC IMPORT
%token FROM TYPE RETURN BREAK AND OR SQBR_L SQBR_R CUR_L CUR_R IN
%%
stmt
: IMPORT STRING stmt {printf("Parsed import.\n");}
| number
| def {printf("def");}
| initialization stmt {printf("init");}
| statement {printf("somestmt");}
def
: FUNCDEC STRING LEFT_BKT RIGHT_BKT stmt {printf("Defined function.");}
statement
: initialization statement {printf("Statement"); }
| print statement {printf("Statement"); }
| inp statement
| loop
| condition
;
int
: literal "=" number {printf("Integer");}
assign
: literal "=" literal {printf("Assign");}
condition
: if
| else
| elif
;
loop
: forloop
| whileloop
;
inp
: IDENTIFIER "=" INPUT LEFT_BKT literal RIGHT_BKT {printf("Input");}
| IDENTIFIER "=" RAW_INPUT LEFT_BKT literal RIGHT_BKT {printf("Raw Input");}
print
: PRINT "\"" literal "\""
| PRINT expression
| expression
;
expression
: "+" "*" number "+" "*" number {printf("Expression");}
| bktexpression {printf("Expression");}
| "+" "*" number "+" "*" number expression {printf("Expression");}
bktexpression
: expression LEFT_BKT expression RIGHT_BKT expression
;
forloop
: FOR IDENTIFIER IN "xrange" LEFT_BKT number","number","number RIGHT_BKT ":" statement {printf("For loop");}
whileloop
: WHILE IDENTIFIER comparison IDENTIFIER statement {printf("While loop");}
| WHILE IDENTIFIER comparison number statement {printf("While loop");}
if
: IF IDENTIFIER comparison IDENTIFIER ":" statement {printf("If condition");}
| IF number comparison number ":" statement {printf("If condition");}
| IF IDENTIFIER comparison number ":" statement {printf("If condition");}
| IF number comparison IDENTIFIER ":" statement {printf("If condition");}
else
: if ELSE ":" statement {printf("Else");}
elif
: if ELIF IDENTIFIER comparison IDENTIFIER ":" statement {printf("Elif condition");}
| if ELIF number comparison number ":" statement {printf("Elif condition");}
| if ELIF IDENTIFIER comparison number ":" statement {printf("Elif condition");}
| if ELIF number comparison IDENTIFIER ":" statement {printf("Elif condition");}
identifier
: IDENTIFIER {printf("Identifier");}
comparison
: RELATIONOPR {printf("Comparison");}
initialization
: string {printf("Initialization");}
string
: STRING "=" "\""STRING"\"" {printf("string");}
literal
: STRING INTEGER {printf("Literal");}
| STRING "." STRING {printf("Literal");}
| "_"STRING"_" {printf("Literal");}
number
: INTEGER {$$ = printf("Number");}
class
: CLASSDEC literal "():" initialization
| CLASSDEC literal "():" initialization def;
%%
main()
{
return (yyparse());
}
yyerror (s) char *s;
{
fprintf (stderr, "%s\n", s);
}
yywrap()
{
return(1);
}
Я получаю эти предупреждения от Yacc о бесполезных правилах в моей грамматике:
grammarwoindentation.y: warning: 18 nonterminals useless in grammar [-Wother]
grammarwoindentation.y: warning: 41 rules useless in grammar [-Wother]
grammarwoindentation.y:18.131-139: warning: nonterminal useless in grammar: statement [-Wother]
stmt : IMPORT STRING stmt {printf("Parsed import.\n");} | number | def {printf("def");} | initialization stmt {printf("init");} | statement {printf("somestmt");}
^^^^^^^^^
grammarwoindentation.y:21.1-3: warning: nonterminal useless in grammar: int [-Wother]
int : literal "=" number {printf("Integer");}
^^^
grammarwoindentation.y:22.1-6: warning: nonterminal useless in grammar: assign [-Wother]
assign : literal "=" literal {printf("Assign");}
^^^^^^
grammarwoindentation.y:20.124-132: warning: nonterminal useless in grammar: condition [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^^^^^^
grammarwoindentation.y:20.119-122: warning: nonterminal useless in grammar: loop [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^
grammarwoindentation.y:20.105-107: warning: nonterminal useless in grammar: inp [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^
grammarwoindentation.y:20.62-66: warning: nonterminal useless in grammar: print [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^^
grammarwoindentation.y:26.41-50: warning: nonterminal useless in grammar: expression [-Wother]
print : PRINT "\"" literal "\"" | PRINT expression | expression;
^^^^^^^^^^
grammarwoindentation.y:28.70-82: warning: nonterminal useless in grammar: bktexpression [-Wother]
expression : "+" "*" number "+" "*" number {printf("Expression");} | bktexpression {printf("Expression");}
^^^^^^^^^^^^^
grammarwoindentation.y:24.6-12: warning: nonterminal useless in grammar: forloop [-Wother]
loop:forloop|whileloop;
^^^^^^^
grammarwoindentation.y:24.14-22: warning: nonterminal useless in grammar: whileloop [-Wother]
loop:forloop|whileloop;
^^^^^^^^^
grammarwoindentation.y:23.11-12: warning: nonterminal useless in grammar: if [-Wother]
condition:if|else|elif;
^^
grammarwoindentation.y:23.14-17: warning: nonterminal useless in grammar: else [-Wother]
condition:if|else|elif;
^^^^
grammarwoindentation.y:23.19-22: warning: nonterminal useless in grammar: elif [-Wother]
condition:if|else|elif;
^^^^
grammarwoindentation.y:47.1-10: warning: nonterminal useless in grammar: identifier [-Wother]
identifier : IDENTIFIER {printf("Identifier");}
^^^^^^^^^^
grammarwoindentation.y:34.30-39: warning: nonterminal useless in grammar: comparison [-Wother]
whileloop : WHILE IDENTIFIER comparison IDENTIFIER statement {printf("While loop");} | WHILE IDENTIFIER comparison number statement {printf("While loop");}
^^^^^^^^^^
grammarwoindentation.y:21.7-13: warning: nonterminal useless in grammar: literal [-Wother]
int : literal "=" number {printf("Integer");}
^^^^^^^
grammarwoindentation.y:55.1-5: warning: nonterminal useless in grammar: class [-Wother]
class: CLASSDEC literal "():" initialization | CLASSDEC literal "():" initialization def;
^^^^^
grammarwoindentation.y:18.131-161: warning: rule useless in grammar [-Wother]
stmt : IMPORT STRING stmt {printf("Parsed import.\n");} | number | def {printf("def");} | initialization stmt {printf("init");} | statement {printf("somestmt");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:20.12-59: warning: rule useless in grammar [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:20.62-100: warning: rule useless in grammar [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:20.105-117: warning: rule useless in grammar [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^^^^^^^^^^
grammarwoindentation.y:20.119-122: warning: rule useless in grammar [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^
grammarwoindentation.y:20.124-132: warning: rule useless in grammar [-Wother]
statement: initialization statement {printf("Statement"); }| print statement {printf("Statement"); } | inp statement|loop|condition;
^^^^^^^^^
grammarwoindentation.y:21.7-45: warning: rule useless in grammar [-Wother]
int : literal "=" number {printf("Integer");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:22.10-48: warning: rule useless in grammar [-Wother]
assign : literal "=" literal {printf("Assign");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:23.11-12: warning: rule useless in grammar [-Wother]
condition:if|else|elif;
^^
grammarwoindentation.y:23.14-17: warning: rule useless in grammar [-Wother]
condition:if|else|elif;
^^^^
grammarwoindentation.y:23.19-22: warning: rule useless in grammar [-Wother]
condition:if|else|elif;
^^^^
grammarwoindentation.y:24.6-12: warning: rule useless in grammar [-Wother]
loop:forloop|whileloop;
^^^^^^^
grammarwoindentation.y:24.14-22: warning: rule useless in grammar [-Wother]
loop:forloop|whileloop;
^^^^^^^^^
grammarwoindentation.y:25.6-71: warning: rule useless in grammar [-Wother]
inp: IDENTIFIER "=" INPUT LEFT_BKT literal RIGHT_BKT {printf("Input");} | IDENTIFIER "=" RAW_INPUT LEFT_BKT literal RIGHT_BKT {printf("Raw Input");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:25.75-148: warning: rule useless in grammar [-Wother]
inp: IDENTIFIER "=" INPUT LEFT_BKT literal RIGHT_BKT {printf("Input");} | IDENTIFIER "=" RAW_INPUT LEFT_BKT literal RIGHT_BKT {printf("Raw Input");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:26.9-31: warning: rule useless in grammar [-Wother]
print : PRINT "\"" literal "\"" | PRINT expression | expression;
^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:26.35-50: warning: rule useless in grammar [-Wother]
print : PRINT "\"" literal "\"" | PRINT expression | expression;
^^^^^^^^^^^^^^^^
grammarwoindentation.y:26.54-63: warning: rule useless in grammar [-Wother]
print : PRINT "\"" literal "\"" | PRINT expression | expression;
^^^^^^^^^^
grammarwoindentation.y:28.14-66: warning: rule useless in grammar [-Wother]
expression : "+" "*" number "+" "*" number {printf("Expression");} | bktexpression {printf("Expression");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:28.70-106: warning: rule useless in grammar [-Wother]
expression : "+" "*" number "+" "*" number {printf("Expression");} | bktexpression {printf("Expression");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:29.22-85: warning: rule useless in grammar [-Wother]
| "+" "*" number "+" "*" number expression {printf("Expression");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:32.17-67: warning: rule useless in grammar [-Wother]
bktexpression : expression LEFT_BKT expression RIGHT_BKT expression;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:33.11-116: warning: rule useless in grammar [-Wother]
forloop : FOR IDENTIFIER IN "xrange" LEFT_BKT number","number","number RIGHT_BKT ":" statement {printf("For loop");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:34.13-84: warning: rule useless in grammar [-Wother]
whileloop : WHILE IDENTIFIER comparison IDENTIFIER statement {printf("While loop");} | WHILE IDENTIFIER comparison number statement {printf("While loop");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:34.88-155: warning: rule useless in grammar [-Wother]
whileloop : WHILE IDENTIFIER comparison IDENTIFIER statement {printf("While loop");} | WHILE IDENTIFIER comparison number statement {printf("While loop");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:36.8-82: warning: rule useless in grammar [-Wother]
if : IF IDENTIFIER comparison IDENTIFIER ":" statement {printf("If condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:37.11-77: warning: rule useless in grammar [-Wother]
| IF number comparison number ":" statement {printf("If condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:38.11-81: warning: rule useless in grammar [-Wother]
| IF IDENTIFIER comparison number ":" statement {printf("If condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:39.11-81: warning: rule useless in grammar [-Wother]
| IF number comparison IDENTIFIER ":" statement {printf("If condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:41.8-46: warning: rule useless in grammar [-Wother]
else : if ELSE ":" statement {printf("Else");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:42.8-89: warning: rule useless in grammar [-Wother]
elif : if ELIF IDENTIFIER comparison IDENTIFIER ":" statement {printf("Elif condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:43.12-85: warning: rule useless in grammar [-Wother]
| if ELIF number comparison number ":" statement {printf("Elif condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:44.12-89: warning: rule useless in grammar [-Wother]
| if ELIF IDENTIFIER comparison number ":" statement {printf("Elif condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:45.12-89: warning: rule useless in grammar [-Wother]
| if ELIF number comparison IDENTIFIER ":" statement {printf("Elif condition");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:47.14-47: warning: rule useless in grammar [-Wother]
identifier : IDENTIFIER {printf("Identifier");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:49.14-48: warning: rule useless in grammar [-Wother]
comparison : RELATIONOPR {printf("Comparison");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:53.11-45: warning: rule useless in grammar [-Wother]
literal : STRING INTEGER {printf("Literal");} | STRING "." STRING {printf("Literal");} | "_"STRING"_" {printf("Literal");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:53.49-86: warning: rule useless in grammar [-Wother]
literal : STRING INTEGER {printf("Literal");} | STRING "." STRING {printf("Literal");} | "_"STRING"_" {printf("Literal");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:53.90-122: warning: rule useless in grammar [-Wother]
literal : STRING INTEGER {printf("Literal");} | STRING "." STRING {printf("Literal");} | "_"STRING"_" {printf("Literal");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:55.8-44: warning: rule useless in grammar [-Wother]
class: CLASSDEC literal "():" initialization | CLASSDEC literal "():" initialization def;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grammarwoindentation.y:55.48-88: warning: rule useless in grammar [-Wother]
class: CLASSDEC literal "():" initialization | CLASSDEC literal "():" initialization def;
Я работая над созданием базового компилятора для Python. Какие-либо решения или проблемы, которые любой может идентифицировать здесь?
В ваших правилах отсутствует большое количество конечных точек с запятой. – EJP
можете ли вы упомянуть несколько мест в коде в качестве примера, если вы не возражаете? –
точки с запятой до конечных правил являются необязательными в yacc, поэтому это не должно иметь значения. –