问题
I've been working a school assignment and am having difficulty figuring out which issue is causing the multiple warnings below "type clash on default action." Any help would be greatly appreciated.
Warnings received:
parser.y:62.9-23: warning: type clash on default action: <value> != <> parser.y:71.9-16: warning: type clash on default action: <value> != <> parser.y:82.5-23: warning: type clash on default action: <value> != <iden> parser.y:83.5-27: warning: type clash on default action: <value> != <iden> parser.y:104.5-55: warning: type clash on default action: <value> != <> parser.y:105.5-60: warning: type clash on default action: <value> != <> parser.y:108.5-37: warning: type clash on default action: <value> != <> parser.y:122.49-53: warning: type clash on default action: <oper> != <> parser.y:122.57-60: warning: type clash on default action: <oper> != <> parser.y:127.5-14: warning: type clash on default action: <value> != <iden> parser.y:157.9-35: warning: type clash on default action: <value> != <iden> parser.y:160.9-20: warning: type clash on default action: <value> != <> parser.y:161.9-21: warning: type clash on default action: <value> != <oper>
parser.y
%{
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
#include "values.h"
#include "listing.h"
#include "symbols.h"
int yylex();
void yyerror(const char* message);
Symbols<int> symbols;
int result;
%}
%error-verbose
%union
{
CharPtr iden;
Operators oper;
int value;
}
%token <value> INT_LITERAL REAL_LITERAL INTEGER REAL
%token <iden> IDENTIFIER
%token ARROW
%token ANDOP OROP
%token <oper> NOTOP EXPOP MULOP REMOP ADDOP RELOP
%token BOOL_LITERAL BOOLEAN
%token IF THEN ELSE ENDIF CASE OTHERS ENDCASE WHEN
%token BEGIN_ END ENDREDUCE FUNCTION IS REDUCE RETURNS
%type <value> function_header_ body variable_ parameter cases case statements statement_ statement reductions exponent expression logical relation term
factor power primary
%type <oper> operator
%%
function:
function_header_ variables body {result = $3;} ;
function_header:
FUNCTION IDENTIFIER parameters RETURNS type ';' ;
function_header_:
function_header |
error ';' {$$ = 0;} ;
statement_:
statement ';' |
error ';' {$$ = 0;} ;
variable_:
variable |
error ';' {$$ = 0;} ;
variable:
IDENTIFIER ':' type IS statement_ {symbols.insert($1, $5);} ;
variables:
variables variable_ |
;
parameter:
IDENTIFIER ':' type |
IDENTIFIER ':' type ',' ;
parameters:
parameters parameter |
;
type:
INTEGER | REAL |
BOOLEAN ;
body:
BEGIN_ statement_ END ';' {$$ = $2;} ;
statements:
statements statement_ |
statement_ ;
statement:
expression |
REDUCE operator reductions ENDREDUCE {$$ = $3;} |
IF expression THEN statements ELSE statements ENDIF |
CASE expression IS cases OTHERS ARROW statement_ ENDCASE ;
case:
WHEN INT_LITERAL ARROW statement_;
cases:
cases case |
case
;
reductions:
reductions statement_ {$$ = evaluateReduction($<oper>0, $1, $2);} |
{$$ = $<oper>0 == ADD ? 0 : 1;} ;
operator:
NOTOP | MULOP | REMOP | ADDOP | RELOP | ANDOP | OROP ;
exponent:
INT_LITERAL |
REAL_LITERAL |
IDENTIFIER ;
expression:
expression OROP logical {$$ = $1 || $3;} |
logical ;
logical:
logical ANDOP relation {$$ = $1 && $3;} |
relation ;
relation:
relation RELOP term {$$ = evaluateRelational($1, $2, $3);} |
term ;
term:
term ADDOP factor {$$ = evaluateArithmetic($1, $2, $3);} |
factor ;
factor:
factor MULOP power {$$ = evaluateArithmetic($1, $2, $3);} |
factor REMOP power {$$ = evaluateArithmetic($1, $2, $3);} |
power ;
/* right-associative */
power:
primary EXPOP exponent {$$ = evaluateArithmetic($1, $2, $3);}
;
primary:
IDENTIFIER {if (!symbols.find($1, $$)) appendError(UNDECLARED, $1);} |
IDENTIFIER '('expression')' |
INT_LITERAL |
REAL_LITERAL |
BOOL_LITERAL |
NOTOP primary |
'(' expression ')' {$$ = $2;}
;
%%
void yyerror(const char* message)
{
appendError(SYNTAX, message);
}
int main(int argc, char *argv[])
{
firstLine();
yyparse();
if (lastLine() == 0)
cout << "Result = " << result << endl;
return 0;
}
scanner.l
%{
#include <cstdio>
#include <string>
using namespace std;
#include "values.h"
#include "listing.h"
#include "tokens.h"
int l=0,r=0,cbl=0,cbr=0;
%}
%option noyywrap
ws [ \t\r]+
comment \-\-.*\n|\/\/.*\n
line [\n]
arrow =>
if if|IF
endcase endcase|ENDCASE
case case|CASE
endif endif|ENDIF
else else|ELSE
others others|OTHERS
real real|REAL
then then|THEN
when when|WHEN
begin begin|BEGIN
boolean boolean|BOOLEAN
endreduce endreduce|ENDREDUCE
end end|END
function function|FUNCTION
integer integer|INTEGER
is is|IS
reduce reduce|REDUCE
returns returns|RETURNS
and and|AND
or or|OR
not not|NOT
bool_lit true|false|TRUE|FALSE
bad_underscore [A-Za-z0-9_]*[_]{2}[A-Za-z0-9_]*|^[_][A-Za-z0-9_]*|[A-Za-z0-9_]*[_]+$
digit [0-9]
int [\-|\+]{0,1}{digit}+
punc [\(\),:;]
real_lit [\-|\+]{0,1}[0-9]+[\.][0-9]+[e|E][\-|\+][0-9]+|[\-|\+]{0,1}[0-9]*[\.][0-9]+|[\-|\+]{0,1}[0-9]+[\.][0-9]*
id [A-Za-z][A-Za-z0-9_]*
%%
{ws} { ECHO; }
{comment} { ECHO; nextLine();}
{line} { ECHO; nextLine();}
{arrow} { ECHO; return(ARROW); }
"<" { ECHO; yylval.oper = LESS; return(RELOP); }
">" { ECHO; yylval.oper = GREATER; return(RELOP); }
">=" { ECHO; yylval.oper = GREATER_OR_EQUALS; return(RELOP); }
"<=" { ECHO; yylval.oper = LESS_OR_EQUALS; return(RELOP); }
"=" { ECHO; yylval.oper = EQUALS; return(RELOP); }
"+" { ECHO; yylval.oper = ADD; return(ADDOP); }
"-" { ECHO; yylval.oper = SUBTRACT; return(ADDOP); }
"**" { ECHO; yylval.oper = EXPONENT; return(EXPOP); }
"*" { ECHO; yylval.oper = MULTIPLY; return(MULOP); }
"/" { ECHO; yylval.oper = DIVIDE; return(MULOP); }
"REM" { ECHO; yylval.oper = REMAINDER; return(REMOP); }
"rem" { ECHO; yylval.oper = REMAINDER; return(REMOP); }
{if} { ECHO; return(IF); }
{endcase} { ECHO; return(ENDCASE); }
{case} { ECHO; return(CASE); }
{endif} { ECHO; return(ENDIF); }
{else} { ECHO; return(ELSE); }
{others} { ECHO; return(OTHERS); }
{real} { ECHO; return(REAL); }
{then} { ECHO; return(THEN); }
{when} { ECHO; return(WHEN); }
{begin} { ECHO; return(BEGIN_); }
{boolean} { ECHO; return(BOOLEAN); }
{endreduce} { ECHO; return(ENDREDUCE); }
{end} { ECHO; return(END); }
{function} { ECHO; return(FUNCTION); }
{integer} { ECHO; return(INTEGER); }
{is} { ECHO; return(IS); }
{reduce} { ECHO; return(REDUCE); }
{returns} { ECHO; return(RETURNS); }
{and} { ECHO; return(ANDOP); }
{or} { ECHO; return(OROP); }
{not} { ECHO; return(NOTOP); }
{real_lit} { ECHO; return(REAL_LITERAL); }
{bool_lit} { ECHO; return(BOOL_LITERAL); }
{id} { ECHO; yylval.iden = (CharPtr)malloc(yyleng + 1);
strcpy(yylval.iden, yytext); return(IDENTIFIER);}
{int} { ECHO; yylval.value = atoi(yytext); return(INT_LITERAL); }
{punc} { ECHO; return(yytext[0]); }
. { ECHO; appendError(LEXICAL, yytext); }
%%
回答1:
These are type mismatches on the default action from a rule that has no explicit action. The first one is coming from the rule
function_header_: function_header
on line 62. Since there's no action here, it gets the action { $$ = $1; }
, but $$
has type <value>
(from the %type <value> function_header_
declaration), while $1
has no type (so <>
, as there's no %type
declaration for function_header
)
来源:https://stackoverflow.com/questions/57025022/need-help-identifying-cause-of-type-clash-on-default-action