2014-12-08 4 views
-1

Я пытаюсь сделать простой калькулятор с коментарий начала строки на символ # здесь код .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 sys tem? Есть ошибки, говорящие, что у меня есть некоторые незамкнутые фигурные скобки, но я их подсчитывал, и это не так.

+2

Я не говорю flex-bison, но не существует ';' отсутствует после прототипа yyerror в .l-файле? –

+0

'char str [256]; void yyerror (char *); 'it is – whd

+2

Извините, это значит сказать .y файл:' int yylex (void); void yyerror (char * s) '. –

ответ

0

Как было указано в комментарии, я отправляю ответ, все в комментариях при первом сообщении.

Смежные вопросы