在我添加“浮动”规则之前,以下代码正在正确编译,然后它给了我在下面列出的错误,任何帮助将不胜感激。
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define YYSTYPE double
int yylex(void);
static
void yyerror(char *s)
{
printf("yyerror: %s\n", s);
}
%}
%union{
int int_val;
string* op_val;
}
%token PLUS
%token MINUS
%token MULT
%token DIVIDE
%token LPAREN
%token RPAREN
%token Unsigned_float
%token UNSIGNEDINTEGER
%left PLUS MINUS
%left MULT DIVIDE
%%
lines : lines expr '\n' {printf("%g\n", $2);}
| lines '\n'
| /*empty*/
;
expr : expr PLUS expr {$$ = $1 + $3;}
| expr MINUS expr {$$ = $1 - $3;}
| expr MULT expr {$$ = $1 * $3;}
| expr DIVIDE expr {$$ = $1 / $3;}
| LPAREN expr RPAREN {$$ = $2;}
| UNSIGNEDINTEGER
;
float : Unsigned_float PLUS Unsigned_float {$$ = $1 + $3;}
| Unsigned_float MINUS Unsigned_float {$$ = $1 - $3;}
| Unsigned_float MULT Unsigned_float {$$ = $1 * $3;}
| Unsigned_float DIVIDE Unsigned_float {$$ = $1 / $3;}
| LPAREN Unsigned_float RPAREN {$$ = $2;}
;
%%
#include "lex.yy.c"
int yylex(void);
int yyparse(void);
int main(void)
{
return yyparse();
}
以下是错误:
stojk_3_4.y:40.63-64: $2 of `lines' has no declared type
stojk_3_4.y:45.49-50: $$ of `expr' has no declared type
stojk_3_4.y:45.56-57: $1 of `expr' has no declared type
stojk_3_4.y:45.63-64: $3 of `expr' has no declared type
stojk_3_4.y:46.61-62: $$ of `expr' has no declared type
stojk_3_4.y:46.68-69: $1 of `expr' has no declared type
stojk_3_4.y:46.75-76: $3 of `expr' has no declared type
stojk_3_4.y:47.60-61: $$ of `expr' has no declared type
stojk_3_4.y:47.67-68: $1 of `expr' has no declared type
stojk_3_4.y:47.74-75: $3 of `expr' has no declared type
stojk_3_4.y:48.63-64: $$ of `expr' has no declared type
stojk_3_4.y:48.70-71: $1 of `expr' has no declared type
stojk_3_4.y:48.77-78: $3 of `expr' has no declared type
stojk_3_4.y:49.62-63: $$ of `expr' has no declared type
stojk_3_4.y:49.68-69: $2 of `expr' has no declared type
stojk_3_4.y:53.60-61: $$ of `float' has no declared type
stojk_3_4.y:53.67-68: $1 of `float' has no declared type
stojk_3_4.y:53.74-75: $3 of `float' has no declared type
stojk_3_4.y:54.82-83: $$ of `float' has no declared type
stojk_3_4.y:54.89-90: $1 of `float' has no declared type
stojk_3_4.y:54.96-97: $3 of `float' has no declared type
stojk_3_4.y:55.81-82: $$ of `float' has no declared type
stojk_3_4.y:55.88-89: $1 of `float' has no declared type
stojk_3_4.y:55.95-96: $3 of `float' has no declared type
stojk_3_4.y:56.83-84: $$ of `float' has no declared type
stojk_3_4.y:56.90-91: $1 of `float' has no declared type
stojk_3_4.y:56.97-98: $3 of `float' has no declared type
stojk_3_4.y:57.73-74: $$ of `float' has no declared type
stojk_3_4.y:57.79-80: $2 of `float' has no declared type