我正在尝试从 yylex 返回符号对象,如本文档http://www.gnu.org/software/bison/manual/html_node/Complete-Symbols.html所示
但是,当我编译时,我发现它return yy::parser::make_PLUS();被放入int yyFlexLexer::yylex(),所以我收到此错误消息(并且许多类似的消息形成其他规则):
lexer.ll:22:10: error: no viable conversion from 'parser::symbol_type' (aka 'basic_symbol<yy::parser::by_type>') to 'int'
{ return yy::parser::make_PLUS(); }
Run Code Online (Sandbox Code Playgroud)
解决这个问题的正确方法是什么?
词法分析器
%{
#include "ASTNode.hpp"
// why isn't this in parser.tab.hh?
# ifndef YY_NULLPTR
# if defined __cplusplus && 201103L <= __cplusplus
# define YY_NULLPTR nullptr
# else
# define YY_NULLPTR 0
# endif
# endif
#include "parser.tab.hh"
#define yyterminate() return yy::parser::make_END()
%}
%option nodefault c++ noyywrap
%%
"+" { return yy::parser::make_PLUS(); }
"-" …Run Code Online (Sandbox Code Playgroud) 当 Flex 和 Bison 一起使用时,
为什么 Flex 文件需要#includebison 创建的 C 头文件?
编译需要 bison 和 flex 创建的 C 源文件。bison 和 flex 创建的 C 源文件相互需要什么?
现在我正在接受其他事情.当我这样做时,我bison -d calc.y在控制台中获得了许多源代码(有很多m4_define),但它不会生成任何文件.现在我的代码是这样的:
%{
#define YYSTYPE double
#include <math.h>
%}
%token NUM
%%
input: /* empty */
| input line
;
line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
;
exp: NUM { $$ = $1; }
| exp exp '+' { $$ = $1 + $2; }
| exp exp '-' { $$ = $1 - $2; }
| exp exp '*' { $$ = $1 * $2; }
| exp exp '/' …Run Code Online (Sandbox Code Playgroud) 我是Bison的新手,但是在C/C++中没有,在这个开发和正则表达式的时候我从来没有听过这样的东西,只有\n那个用于换行,但我想知道是什么解释\t%.10g,那个在代码中是这样的:
line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
;
Run Code Online (Sandbox Code Playgroud)
最好的祝福.
我正在使用Lex和Yacc构建计算器编译器.该想法基于以下资源:http://epaperpress.com/lexandyacc/index.html.
对于给定的输入文件,我需要识别所有注释:
//.TEST -- JWJ
//.Step final -- testing all requirements
//.source: test-1m.cal
//.expected output: test-1m_expected.out
/**
* This program will use Newton's method to estimate the roots of
This should be a comment as well, but does not get picked up
* f(x) = x^3 - 3*x
*/
float xn;
float xo;
// int num_iterations;
xo = 3.0;
xn = 3.0;
num_iterations = 1;
/* A do-while loop */
do {
print xo;
xo = …Run Code Online (Sandbox Code Playgroud) 我正在使用以下命令
bison.exe -v -d -l -pTPZyy -bTpz -o %ParserDir%\parser.cpp %ParserDir%\parser.y
它放出“ Parser.cpp”和“ Parser.hpp”。在我们公司,我们不具备约定cpp/hpp的cpp/h文件。如何告诉野牛生成parser.h和parser.cpp文件?
它厌倦了其文档,但未找到任何有关此的信息。
我想在我的应用程序中实现脚本语言.仅用于控制某些行为,定义规则等.
我没有找到使用Lua over bison/yacc的理由,反之亦然.这些工具有哪些优缺点?
从实现点来看,Lua似乎更容易实现,而yacc/bison需要学习编写解析器标记,但后来我有一个独立的解析器.除此之外有什么区别?
我写了一个简单的语法:
operations :
/* empty */
| operations operation ';'
| operations operation_id ';'
;
operation :
NUM operator NUM
{
printf("%d\n%d\n",$1, $3);
}
;
operation_id :
WORD operator WORD
{
printf("%s\n%s\n%s\n",$1, $3, $<string>2);
}
;
operator :
'+' | '-' | '*' | '/'
{
$<string>$ = strdup(yytext);
}
;
Run Code Online (Sandbox Code Playgroud)
如您所见,我定义了operator可以识别4个符号之一的。现在,我要在中打印该符号operation_id。问题是,逻辑operator仅适用于替代中的最后一个符号。所以如果我写a / b; 它显示ab /,这很酷。但是对于其他操作,例如。a + b; 它打印aba。我究竟做错了什么?
*我在示例输出中省略了换行符号。
我正在尝试从BNF Grammar编写一个Flex/Bison文件.但是,当我尝试编译时出现错误,我不确定如何调试它们.
BNF Grammer:
<exp>::=<list> | head(<list>)
<list>::=<num1>::<list> | <list>@<list> | tail(<list>) | [<numlist>]
<numlist>::=<empty> | <num1><num2>
<num2>::=<empty> | ,<num1><num2>
<num1>::=<D1><N> | <N> | head(<list>)
<D1>::=<D1><N> | <D2>
<D2>::=[1-9]
<N>::=[0-9]
Run Code Online (Sandbox Code Playgroud)
我的Flex文件
%option noyywrap
%{
#include <stdlib.h>
#include <list>
using namespace std;
#define YYSTYPE list<int>
#include "listop.tab.h"
%}
hd head
tl tail
ap [@()\[\]]
con ::
n [0-9]
d2 [1-9]
d1 ({d2}{n}+)|{d2}
ws[ \t\n]+
%%
{d1} yylval.clear(); yylval.push_front(atoi(yytext)); return D1;
{n} yylval.clear(); yylval.push_front(atoi(yytext)); return N;
{hd} return HEAD;
{tl} return TAIL; …Run Code Online (Sandbox Code Playgroud) 我有一段Jison的代码,看起来像这样:
%lex
%options flex
%{
if (!('regions' in yy)) {
yy.regions = [];
}
%}
text [a-zA-Z][a-zA-Z0-9]*
%%
\s+ /* skip whitespace */
\n+ return 'NL';
"," return ',';
"-" return '-';
"[" return '[';
"]" return ']';
{text} return 'TEXT';
<<EOF>> return 'EOF';
/lex
%start expressions
%%
expressions
: content EOF
{
console.log(yy.regions);
return yy.regions;
}
| EOF
{
console.log("empty file");
return yy.regions;
}
;
content
: line NL content
{ console.log("NL"); }
| line content
{ console.log("no NL"); …Run Code Online (Sandbox Code Playgroud)