-1
我試圖與上#字符 這裏註釋行begining簡單的計算器是.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;
}}
.Y文件
%{
#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
所以我做錯了什麼或flex-bison在windows系統上壞了sys技術?有錯誤說,我有一些沒有關閉的大括號,但我數了他們,那不是真的
我不會說flex-bison,但是沒有';'在.l文件中爲yyerror原型丟失? – 2014-12-08 00:56:38
'char str [256]; void yyerror(char *); '它在那裏 – whd 2014-12-08 01:04:02
對不起,本來打算說.y文件:'int yylex(void); void yyerror(char * s)'。 – 2014-12-08 01:05:38