linux-无法编译Bison示例代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux-无法编译Bison示例代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2803字,纯文字阅读大概需要5分钟。
内容图文
![linux-无法编译Bison示例代码](/upload/InfoBanner/zyjiaocheng/946/946e56562f8247c58041281afc9c6228.jpg)
我正在阅读LFS,并且遇到过野牛,在其他地方之前我都看过它,所以我认为我应该多了解一点.我发现this page from UC Riverside CS department,示例代码不起作用.有人知道怎么了吗?为了方便起见,我粘贴了代码:
calc.lex文件:
/* Mini Calculator */
/* calc.lex */
%{
#include "heading.h"
#include "tok.h"
int yyerror(char *s);
int yylineno = 1;
%}
digit [0-9]
int_const {digit}+
%%
{int_const} { yylval.int_val = atoi(yytext); return INTEGER_LITERAL; }
"+" { yylval.op_val = new std::string(yytext); return PLUS; }
"*" { yylval.op_val = new std::string(yytext); return MULT; }
[ \t]* {}
[\n] { yylineno++; }
. { std::cerr << "SCANNER "; yyerror(""); exit(1); }
calc.y文件:
/* Mini Calculator */
/* calc.y */
%{
#include "heading.h"
int yyerror(char *s);
int yylex(void);
%}
%union{
int int_val;
string* op_val;
}
%start input
%token <int_val> INTEGER_LITERAL
%type <int_val> exp
%left PLUS
%left MULT
%%
input: /* empty */
| exp { cout << "Result: " << $1 << endl; }
;
exp: INTEGER_LITERAL { $$= $1; }
| exp PLUS exp { $$= $1 + $3; }
| exp MULT exp { $$= $1 * $3; }
;
%%
int yyerror(string s)
{
extern int yylineno; // defined and maintained in lex.c
extern char *yytext; // defined and maintained in lex.c
cerr << "ERROR: " << s << " at symbol \"" << yytext;
cerr << "\" on line " << yylineno << endl;
exit(1);
}
int yyerror(char *s)
{
return yyerror(string(s));
}
这是错误消息:
$make
bison -d -v calc.y
cp calc.tab.c bison.c
cmp -s calc.tab.h tok.h || cp calc.tab.h tok.h
g++ -g -Wall -ansi -pedantic -c bison.c -o bison.o
calc.tab.c: In function ‘int yyparse()’:
calc.tab.c:1381: warning: deprecated conversion from string constant to ‘char*’
calc.tab.c:1524: warning: deprecated conversion from string constant to ‘char*’
flex calc.lex
cp lex.yy.c lex.c
g++ -g -Wall -ansi -pedantic -c lex.c -o lex.o
calc.lex:8: error: redefinition of ‘int yylineno’
lex.yy.c:349: error: ‘int yylineno’ previously defined here
calc.lex: In function ‘int yylex()’:
calc.lex:23: warning: deprecated conversion from string constant to ‘char*’
lex.yy.c: At global scope:
lex.yy.c:1105: warning: ‘void yyunput(int, char*)’ defined but not used
make: *** [lex.o] Error 1
解决方法:
问题在于您正在使用C编译器来编译C代码.如果您希望flex生成C,则可以使用命令行选项.
扫描仪生成的代码已经为yylineno提供了定义.
在C中,可以接受以下内容:
int yylineno; /* tentative definition */
int yylineno = 1; /* definition */
在C中,它不是:
int yylineno; /* definition */
int yylineno = 1; /* another definition: duplicate, error! */
另请注意,-ansi gcc选项适用于C语言.
关于字符串常量的警告也是由于C.在C中,字符串文字(如“ abc”)的计算结果为const char *,而不是char *.
最后,请注意,Flex生成的扫描程序不会自动包含更新yylineno的代码.这是通过%option yylineno打开的.查看Flex上的GNU Info手册.
内容总结
以上是互联网集市为您收集整理的linux-无法编译Bison示例代码全部内容,希望文章能够帮你解决linux-无法编译Bison示例代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。