如何访问flexlexer的输入功能而不来自yyflexlexer?

发布于 2025-01-26 16:56:58 字数 2025 浏览 3 评论 0原文

我正在为简单的C语言编写一个简单的编译器。为此,我想使用Flex和Bison,但在现代C ++的背景下使用它们 现在,我的设置由一个main.cpp组成,cpp仅实例化扫描仪和解析器,然后开始解析

// some includes here

#include <FlexLexer.h>
#include "parser.tab.hh"

FlexLexer* scanner;

int main(int argc, char **argv) {
    yy::parser p;
    scanner = new yyFlexLexer;
    // boilerplate
    if (!p()) std::cout << "No errors detected\n";
}

scanner.l

%option c++ noyywrap

D           [0-9]
L           [a-zA-Z_]
H           [a-fA-F0-9]
E           [Ee][+-]?{D}+
FS          (f|F|l|L)
IS          (u|U|l|L)*

%{
// Several includes

#include "parser.tab.hh"

using token = yy::parser::token;

void count();
void comment();
int check_type();
void add_symbol(void);

void yyerror(const char *);
extern std::map<std::string, std::vector<int>> symbol_table;
extern FlexLexer* scanner;
%}
%%
"/*"            { comment(); }

// skipped for brevity

%%
void comment(void)
{
    int c;
    while ((c = scanner->input()) != 0)
        if (c == '*')
        {
            while ((c = scanner->input()) == '*');

            if (c == '/')
                return;

            if (c == 0)
                break;
        }
    yyerror("unterminated comment");
}

和最后一个parser.yy,看起来像是ansi c yacc语法,并在此设置中添加了此

%require "3.2"
%language "c++"
%skeleton "lalr1.cc"

%{
extern int yylex();
%}

%define api.token.constructor
%define api.value.type variant
%define parse.assert

...

设置并不编译。课程为Flexlexer没有名为input()的成员,但是,实例化yyflexlexer也不是一个选项,因为input()班级受到保护,而不是可用的。 flex手册状态必须使用yyinput()在flex中生成C ++代码时,并将其用作免费的常规功能,这是我的设置中由于某些原因而无法使用的。 https://ftp.gnu.org/ old-gnu/手册/flex-2.5.4/html_node/flex_19.html

我的问题是这样的:是否有可能,如果是这样,则如何访问默认input()函数当还使用flex的C ++选项时,不诉诸创建来自Yyflexlexer的单独的Lexer类?

I am writing a simple compiler for a simple C like language. To do that I would like to use Flex and Bison but use them both in the context of modern C++
Right now my setup consists of a main.cpp which just instantiates the scanner and parser and starts parsing

// some includes here

#include <FlexLexer.h>
#include "parser.tab.hh"

FlexLexer* scanner;

int main(int argc, char **argv) {
    yy::parser p;
    scanner = new yyFlexLexer;
    // boilerplate
    if (!p()) std::cout << "No errors detected\n";
}

scanner.l

%option c++ noyywrap

D           [0-9]
L           [a-zA-Z_]
H           [a-fA-F0-9]
E           [Ee][+-]?{D}+
FS          (f|F|l|L)
IS          (u|U|l|L)*

%{
// Several includes

#include "parser.tab.hh"

using token = yy::parser::token;

void count();
void comment();
int check_type();
void add_symbol(void);

void yyerror(const char *);
extern std::map<std::string, std::vector<int>> symbol_table;
extern FlexLexer* scanner;
%}
%%
"/*"            { comment(); }

// skipped for brevity

%%
void comment(void)
{
    int c;
    while ((c = scanner->input()) != 0)
        if (c == '*')
        {
            while ((c = scanner->input()) == '*');

            if (c == '/')
                return;

            if (c == 0)
                break;
        }
    yyerror("unterminated comment");
}

and finally parser.yy which looks like the Ansi C yacc grammar with the addition of this at the top

%require "3.2"
%language "c++"
%skeleton "lalr1.cc"

%{
extern int yylex();
%}

%define api.token.constructor
%define api.value.type variant
%define parse.assert

...

This setup does not compile of course as FlexLexer does not have a member called input(), however instantiating yyFlexLexer is also not an option as the input() function of that class is protected and not usable.
The Flex manual states one must use yyinput() when generating C++ code in flex and uses it as a free standing function which is not available for some reason on my setup.
https://ftp.gnu.org/old-gnu/Manuals/flex-2.5.4/html_node/flex_19.html

My question is thus: Is it possible, and if so how, to access the default input() functionality when also using the C++ option of Flex, without resorting to creating a separate Lexer class that derives from yyFlexLexer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文