in fooparser.yy :
// %define api.value.type {std::string}
%define api.value.type variant
...
// %token HELLO
// %token WORLD
%token HELLO
%token WORLD
in foolexer.hh :
// int yylex(std::string *const yylval);
int yylex(yy::parser::value_type *const yylval);
Компилятор теперь сообщает мне, что 'yy' не был объявлен (аналогично, если я заменяю yy :: parser :: value_type by yystype ). Для контекста полный файл foolexer.hh :
#pragma once
#include
#if ! defined(yyFlexLexerOnce)
#include
#endif
class FooLexer : public yyFlexLexer
{
public:
int yylex(yy::parser::value_type *const yylval);
};
Поскольку yy :: parser :: value_type определяется в сгенерированном файле заголовка fooparser.tab.hh , я затем попытался #include "fooparser.tab.hh" , но затем компилятор дает мне дополнительное сообщение
fooParser.tab.hh:666:21: error: expected ‘)’ before ‘&’ token
666 | parser (FooLexer &lexer_yyarg);
| ~ ^~
| )
fooParser.tab.hh
1099 | FooLexer &lexer;
| ^~~~~~~~
< /code>
Итак, как я могу это решить? Заранее большое спасибо
Изменить: вот полный код: < /h2>
FooLexer.hh:
#pragma once
#include
#include "fooParser.tab.hh"
#if ! defined(yyFlexLexerOnce)
#include
#endif
class FooLexer : public yyFlexLexer
{
public:
int yylex(yy::parser::value_type *const yylval);
};
< /code>
fooLexer.ll:
%{
#include "FooLexer.hh"
#include "fooParser.tab.hh"
#undef YY_DECL
#define YY_DECL int FooLexer::yylex(yy::parser::value_type *const yylval)
%}
%option c++ noyywrap
%option yyclass="FooLexer"
%%
[[:space:]] ;
Hello { return yy::parser::token::HELLO; }
[[:alpha:]]+ { *yylval = std::string(yytext, yytext + yyleng); return yy::parser::token::WORLD; }
. { return yytext[0]; }
< /code>
fooParser.yy:
%require "3.2"
%language "c++"
%code requires {
#include
#include "FooLexer.hh"
class FooLexer;
}
%define api.value.type variant
%parse-param {FooLexer &lexer}
%header
%code {
#define yylex lexer.yylex
}
%token HELLO
%token WORLD
%%
hello_world: HELLO WORLD '!' { std::cout g++ -c $< -o $@
lex.yy.cc: fooLexer.ll
> flex $<
fooParser.tab.hh fooParser.tab.cc fooParser.output: fooParser.yy
> bison $<
.PHONY: clean
clean:
> rm -f prog main.o lex.* fooParser.tab.* stack.hh
Подробнее здесь: https://stackoverflow.com/questions/796 ... h-variants
Мобильная версия