linux - Bison 示例代码未编译

标签 linux compiler-errors bison

我正在阅读 LFS 并遇到了 bison,我在其他地方见过它,所以我认为我应该更多地了解它。我找到了 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 示例代码未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19256161/

相关文章:

在 yacc 中连接字符串

sqlplus - 定期将结果写入文件

c# - 自定义属性 : An attribute argument must be a constant expression

c - 如何在 GCC -> C 中将 flex 和 bison 文件输出链接在一起

static_cast< > 中的 C++ 编译错误

linux - 如何将 8086 emu 汇编程序转换为 linux 汇编兼容

c - flex 'yyval' 未声明

c++ - 来自 Linux Mint 17.3 repo 的 Eigen3,SparseMatrix 未命名

c - 为什么在 struct file_operation 中没有 munmap 回调?

linux - 如何使用 altgr+space 禁用不间断空格