flex/bison计算器-编译错误

flex/bison计算器-编译错误,bison,yacc,flex-lexer,Bison,Yacc,Flex Lexer,我试图制作一个简单的计算器,注释行以#字符开头 这是.l文件代码 %{ #include <stdlib.h> #include <stdio.h> #include <string.h> #include "y.tab.h" char str[256]; void yyerror(char *); %} %x COM %% <INITIAL>^# BEGIN(COM); <COM>[\t\x20-\x7E]*\\\n ; <

我试图制作一个简单的计算器,注释行以#字符开头 这是.l文件代码

%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
char str[256];
void yyerror(char *);
%}

%x COM

%%
<INITIAL>^# BEGIN(COM);
<COM>[\t\x20-\x7E]*\\\n ;
<COM>[\t\x20-\x7E]* ; BEGIN(INITIAL);
<COM>[\t\x20-\x7E]*\\* ; BEGIN(INITIAL);
[ \t]+ ;
[0-9]+ { yylval = atoi(yytext); strcat(str,yytext); strcat(str, " ") ; return INTEGER;}
\\\n ;
[-\+\*\/\^()%] {strcat(str, yytext); strcat(str," ") ;return *yytext;}
\n { str[0] = '\0'; return *yytext;}
. { char msg[25]; sprintf(msg, "niedozwolony znak '%s'", yytext); yyerror(msg);}

%%


int yywrap(void)
{
    return 1;
}}

那么我是做错了什么,还是flex bison在windows系统上坏了?有错误说我有一些未闭合的大括号,但我正在计算它们,这不是真的

如评论中所要求的,我正在发布答案,所有内容都在第一篇文章的评论中。

我不会说flex bison,但不是有一个
字符str[256];无效错误(字符*)很抱歉,意思是.y文件:
intyylex(void);void yyerror(char*s)
。您是否看到
yywrap
方法末尾的第二个
}
!我还发现我在第39行和第40行缺少两个分号
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int yylex(void);
void yyerror(char *s)

%}

%token INTEGER

%%

program: 
 program line
| line
;

line:
 expr '\n' {printf("%d\n",$1);}
| '\n'
;

expr:
 expr '-' mulex { $$ = $1 - $3; }
| mulex { $$ = $1; }
; 

mulex:
 mulex '*' term { $$ = $1 * $3; }
| mulex '/' term { $$ = $1 / $3; }
| mulex '^' term { $$ = pow($1,$3); }
| mulex '%' term { $$ = $1 % $3; }
| term { $$ = $1; }
;

term:
 '(' expr ')' { $$ = $2 }
| INTEGER { $$ = $1 }
;

%%

void yyerror(char *s)
{
    fprintf(stderr, "%s\n", s);
}

int main(void)
{
    /* yydebug = 1; */
    yyparse();
    return 0;
}
C:\Users\User\Desktop>gcc lex.yy.c y.tab.c -o calc
calc.l:31:2: error: expected identifier or '(' before '}' token
y.tab.c: In function 'yyerror':
y.tab.c:109:4: warning: empty declaration [enabled by default]
y.tab.c:120:13: error: storage class specified for parameter 'YYSTYPE'
y.tab.c:140:23: error: storage class specified for parameter 'yytype_uint8'
y.tab.c:147:21: error: storage class specified for parameter 'yytype_int8'
y.tab.c:155:28: error: storage class specified for parameter 'yytype_uint16'
y.tab.c:161:19: error: storage class specified for parameter 'yytype_int16'
y.tab.c:294:3: error: expected specifier-qualifier-list before 'yytype_int16'
y.tab.c:292:1: warning: empty declaration [enabled by default]
y.tab.c:365:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yytranslate'
y.tab.c:393:2: error: expected declaration specifiers before ';' token
y.tab.c:444:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yyr1'
y.tab.c:448:2: error: expected declaration specifiers before ';' token
y.tab.c:451:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yyr2'
y.tab.c:455:2: error: expected declaration specifiers before ';' token
y.tab.c:460:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yydefact'
y.tab.c:465:2: error: expected declaration specifiers before ';' token
y.tab.c:468:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yydefgoto'
y.tab.c:471:2: error: expected declaration specifiers before ';' token
y.tab.c:476:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yypact'
y.tab.c:481:2: error: expected declaration specifiers before ';' token
y.tab.c:484:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yypgoto'
y.tab.c:487:2: error: expected declaration specifiers before ';' token
y.tab.c:494:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yytable'
y.tab.c:499:2: error: expected declaration specifiers before ';' token
y.tab.c:501:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yycheck'
y.tab.c:506:2: error: expected declaration specifiers before ';' token
y.tab.c:510:27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
yystos'
y.tab.c:515:2: error: expected declaration specifiers before ';' token
y.tab.c:1008:44: error: expected declaration specifiers or '...' before 'YYSTYPE
'
y.tab.c:1051:1: error: expected declaration specifiers before 'YYSTYPE'
y.tab.c:1083:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '
{' token
calc.y:46:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{'
token
calc.y:51:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{'
token
calc.y:7:6: error: old-style parameter declarations in prototyped function defin
ition
calc.y:55:1: error: expected '{' at end of input