gpt4 book ai didi

c++ - 从 ‘SymbolInfo*’ 转换为 ‘YYSTYPE {aka int}’ 失去精度

转载 作者:行者123 更新时间:2023-11-28 01:32:59 25 4
gpt4 key购买 nike

在我的 .l 文件中,我有以下内容:

<some include files....>
#include "SymbolInfo.h"
#include "y.tab.h"

using namespace std;

int line_count = 1;
int TOTAL_ERROR = 0;

extern SymbolTable symbolTable;
extern FILE *yyin,*yyout;
extern YYSTYPE yylval;

<further declarations...>

%%

"int" {
SymbolInfo* s = new SymbolInfo(string(yytext),"INT");
yylval = (YYSTYPE)s;
return INT;
}

<more patterns....>

%%

在我的 .y 文件中,我定义了 YYSTYPE:

#include "SymbolInfo.h"

int SymbolTable::id = 0;

#define YYSTYPE SymbolInfo*

但是当我尝试编译它时,它给出了以下错误:

Lexical Analyzer.l: In function ‘int yylex()’:
Lexical Analyzer.l:162:27: error: cast from ‘SymbolInfo*’ to ‘YYSTYPE {aka int}’ loses precision [-fpermissive]
yylval = (YYSTYPE)res;
^

我的问题是,为什么即使在将 YYSTYPE 定义为 SymbolInfo* 之后仍会出现编译错误?我该如何处理这个错误?

  1. SymbolInfo Code
  2. Parser Code
  3. Lexer Code

最佳答案

why is it giving a compilation error even after defining YYSTYPE as SymbolInfo*?

因为正如您所说,您在解析器定义中定义了 YYSTYPE。错误发生在扫描仪定义中。这些是单独的文件,其中定义的宏在另一个文件中不可见。

您可以将 #define YYSTYPE 行放在 .l 文件中,包含由 bison 生成的头文件之前。或者你可以把它放在 %code requires 中bison 文件中的部分,以便将其插入到 bison 生成的 header 中。

但是你应该做的是避免宏并使用 bison declaration :

%define api.value.type { SymbolInfo* }

这不仅会正确定义语义类型,它还会被放入 bison 生成的头文件中,这样您就不必担心在其他源文件中定义 YYSTYPE(如只要他们#include Bison 头文件)。

请不要使用 bison 的 -y 选项。它只适用于遗留代码。应该将新代码写入 bison 接口(interface)。除非另有说明,否则 bison(没有 -y 标志)会将生成的代码放在 .tab.c 中,将头文件放在 .tab.h 中。如果您正在生成 C++,您可能希望使用 --output(或 -o)指定输出文件名,并在必要时使用 - 指定头文件名-defines(而不是 -d--defines 允许您指定文件名)。参见 Bison options了解详情。

关于c++ - 从 ‘SymbolInfo*’ 转换为 ‘YYSTYPE {aka int}’ 失去精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50768162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com