我正在尝试使用#字符开头的注释行来制作简单的计算器 这是.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
所以我做错了什么,或者在Windows系统上打破了野牛?有错误说我有一些未闭合的大括号,但我在计算它们,那不是真的
答案 0 :(得分:0)
根据评论中的要求,我发布了答案,所有内容都在第一篇帖子的评论中。