LINUX.ORG.RU

bison & C++


0

0

Здравствуйте!

Не могу компилятором c++ собрать простейший калькулятор. Хотя аналог С функциями без проблем собрался.

Логи ниже

# cat scan.l %{ #include <sstream> %} DIGITS [0-9]+ %% [ ] {} {DIGITS} { std::istringstream str(yytext); str>>yylval; return NUM; } \n|. {return yytext[0];} %%

# cat calc.yy %{ #include <iostream> int yylex(); int yyerror(char const *m){std::cout<<m;} int yywrap(); int yyparse(void); %} %token NUM %left '+' '-' %left '*' '/' %% p : | p s ;

s : e '\n' { std::cout << $1 << std::endl; } | error '\n' ; e : e '+' e { $$ = $1 + $3; } | e '-' e { $$ = $1 - $3; } | NUM ; %%

#include "lex.yy.cc"

# flex -+ scan.l # bison calc.yy #c++ calc.tab.cc -lfl -ly /tmp/cceihXfg.o(.text+0x27d): In function `yyparse()': : undefined reference to `yylex()' /tmp/cceihXfg.o(.text+0xc9f): In function `yyFlexLexer::yylex()': : undefined reference to `yywrap()' /tmp/cceihXfg.o(.text+0x1b0a): In function `yyFlexLexer::yyinput()': : undefined reference to `yywrap()' /usr/lib/gcc-lib/i486-linux/3.3.5/../../../libfl.a(libmain.o)(.text+0x11): In function `main': : undefined reference to `yylex'

anonymous

Извиняюсь. Впервые отсылаю

Это scan.l
%{
#include <sstream>
%}
DIGITS [0-9]+
%%
[ ] {}
{DIGITS} {
std::istringstream str(yytext);
str>>yylval;
return NUM;
}
\n|. {return yytext[0];}
%%

calc.yy:
%{
#include <iostream>
int yylex();
int yyerror(char const *m){std::cout<<m;}
int yywrap();
int yyparse(void);
%}
%token NUM
%left '+' '-'
%left '*' '/'
%%
p : | p s ;

s : e '\n' { std::cout << $1 << std::endl; }
| error '\n'
;
e : e '+' e { $$ = $1 + $3; }
| e '-' e { $$ = $1 - $3; }
| NUM
;
%%

#include "lex.yy.cc"


Далее попытки сборки
flex -+ scan.l
bison calc.yy
c++ calc.tab.cc -lfl -ly

И собственно ошибки
tmp/cceihXfg.o(.text+0x27d): In function `yyparse()': : undefined reference to `yylex()' /tmp/cceihXfg.o(.text+0xc9f): In function `yyFlexLexer::yylex()': : undefined reference to `yywrap()' /tmp/cceihXfg.o(.text+0x1b0a): In function `yyFlexLexer::yyinput()': : undefined reference to `yywrap()' /usr/lib/gcc-lib/i486-linux/3.3.5/../../../libfl.a(libmain.o)(.text+0x11): In function `main': : undefined reference to `yylex'

anonymous
()
Ответ на: комментарий от MKuznetsov

Спасибо! Собралось!!! Но ;( не работает.( Похоже сканер работает а действия бизона нет. ) Причем отмечу что в аналоге на C проблем нет.

scan.l
%{
#include <sstream>
%}
DIGITS [0-9]+
%%
[ ] {}
{DIGITS} {
std::istringstream str(yytext);
str>>yylval;
return NUM;
}
\n|. {return yytext[0];}
%%

// В С так:
// {DIGITS} {sscanf(yytext,"%i",&yylval);return NUM;}

calc.yy
%{
#include <iostream> // В C этого нет
extern "C" int yylex(); // В C этого нет
int yyerror(char const *m){std::cout<<m;} // В С этого нет
%}
%token NUM
%left '+' '-'
%left '*' '/'
%%
p :
| p s
;
s : e '\n' { std::cout << $1 << std::endl; }
// В С так { printf("%d\n",$1); }
| error '\n'
;
e : e '+' e { $$ = $1 + $3; }
| e '-' e { $$ = $1 - $3; }
| NUM
;
%%

#include "lex.yy.c"

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.