- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 yacc 文件中添加 union 结构,但我发现了这个错误:
"error : member 'Info YYSTYPE ::info' with constructor not allowed in union "
%{
#include <cstdio>
#include <iostream>
using namespace std;
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;
struct Info{ int intval; float floatval; string stringval ;int type; }
void yyerror(const char *s);
%}
%union {
int ival;
float fval;
char *sval;
struct Info info;
}
最佳答案
不能将非 POD 结构放入 C++ 中的 union 中,因为编译器无法判断要构造或析构哪个 union 成员。
一种替代方法是在 union 中使用指针:
%union {
...
Info *info;
};
在这种情况下,如果/当不再需要指针时,您需要小心明确地删除它们。 Bison 的 %destructor
在这里很有用,可以在出现错误时避免泄漏。
或者,根本不使用 %union
-- 只需将 YYSTYPE
定义为单一类型:
%{
#define YYSTYPE struct Info
%}
在这种情况下,您的所有规则都需要使用相同的类型(没有 %type
声明让不同的规则产生不同的东西)。如果你真的需要不同的类型,像 boost::variant
这样的东西会很有用..
关于c++ - union c++ yacc 中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217643/
如何使用 yyparse() 编译、链接和调用不同的 YACC 语法在一个程序中? 最佳答案 对每个单独的 yacc 语法生成使用 -p 选项 -p prefix Use pr
我正在为类项目工作,我们必须在其中构建解析器。我们目前正处于在 yacc 中构建解析器的阶段。目前让我感到困惑的是,我读到您需要为每个非终结符分配一个类型。在某些情况下,虽然我会有类似的东西: ...
我有一个关于 yacc 编译器的问题。我不编译简单的 yacc 语法。这是代码部分: /*anbn_0.y */ %token A B %% start: anbn '\n' {printf(" is
我正在使用 Yacc 和 lex 来解析 C 类型语言, 我已经使用 C++ 构建了数据结构。 一切正常,但我无法在 main.cpp 中使用 yyin 读取输入文件。 以下是代码: 请帮忙 ! #i
每当找到一些正则表达式时,我都会使用 lex 来执行一些代码, Yacc 还能做更多的事情吗?如果是,那是什么? 最佳答案 lex 和 yacc 通常一起使用。这就是您通常使用以下两种方式构建应用程序
我是 lex 和 yacc 以及编译器设计的新手。我想知道符号表是在哪个阶段(词汇、句法或任何其他阶段)以及如何生成的? 我可以简单描述一下 y.output 文件,该文件是通过向 yacc 提供 -
我正在 yacc 中处理一个项目并且遇到了 shift/reduce 错误,但我不知道为什么我会得到它。我一直在查看 y.output 文件,但不太确定如何阅读它。我的 y.output 文件超出了
我是 yacc/lex 的新手,我正在开发一个由其他人编写的解析器。我注意到,当找到未定义的标记时,解析器返回错误并停止。有没有一种简单的方法可以让它完全忽略它无法解析的行并继续进行下一个? 最佳答案
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
嗨,我正在学习 Lex 和 yacc。我创建了以下 lex 程序。 %{ #include %} %% [0123456789]+ printf("NUMBER\n"); [a-
莱克斯部分: %% [0-9]+ { yyval = atoi (yytext); return num; } %% 雅克部分: %token num %% exp:num '+' num ; {$$
问题 我正在尝试使用 Python Lex-Yacc (PLY) 实现容错解析器,但在输入字符串末尾使用错误恢复规则时遇到问题。 如何从意外的输入结束中恢复? 示例 此示例语法生成以下形式的字符串 A
我有两个带有函数 xxlex() 和 yylex() 的 lex 文件,还有一个 yacc语法文件。我希望语法规则始终调用 xxlex()。但是 xlex() 只被调用一次,我希望它总是被调用。我该怎
我正在尝试使用 PLY (python implementation of yacc) 编写一个相当简单的语法,并且在我想要的时候让 yacc 减少标记字符串时遇到困难。 我想解释一系列采用不同类型参
notype_declarator: notype_declarator '(' parmlist_or_identifiers %prec '.' { $$ = bui
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭12 年前。 Improve th
拜托,我面临一个简单的问题..这就是问题所在,在我的 lex 文件中,我有类似的内容: char *ptr_String; "name = " { BEGIN sName; } .+ {
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
嗨,我正在尝试运行 R. Levine 所著的 lex 和 yacc 书中的 John 代码,我已经使用命令在 Linux 中编译了 lex 和 yacc 程序 lex example.l yacc
我想传递 token 的实际字符串。如果我有一个名为ID的 token ,那么我希望我的yacc文件实际知道称为什么ID。我必须将使用yylval的字符串从flex文件传递到yacc文件。我怎么做?
我是一名优秀的程序员,十分优秀!