flex-lexer 相关问题

Flex(快速词法分析器生成器)是一个自由软件,可生成词法分析器(“扫描仪”或“词法分析器”)。

Flex/C++:'yypanic'未在范围内声明,错误:重新定义'class yFlexLexer','yyFlexLexer'未在此范围内声明

您好,我正在尝试将 Flex 与 C++ 结合使用。阅读 Flex 手册后,我开始编写一个小示例来构建我的项目。 我最初想出了这个小例子。 #包括 您好,我正在尝试将 Flex 与 C++ 结合使用。阅读 Flex 手册后,我开始编写一个小示例来构建我的项目。 我最初想出了这个小例子。 #include <string.h> #include <iostream> #include <sstream> #include <string> #include <streambuf> #include <fstream> int main() { std::string stringvalues = "TOKEN TEST 123"; std::istringstream inputStringStream(stringvalues); std::istream& inputStream = inputStringStream; std::ofstream outputStream("test.txt"); yyFlexLexer myTest(inputStream, outputStream); } 在构建文件夹内,当我执行 cmake .. 然后点击 make 时,我收到以下错误: alfredo@alfredo-ThinkPad-T440p:~/repos/FlexCpp/build$ make [ 20%] Linking CXX static library libkeywords.a [ 20%] Built target keywords [ 40%] Building CXX object src/back-end/CMakeFiles/Foo.dir/main.cpp.o /home/alfredo/repos/FlexCpp/src/back-end/main.cpp: In function ‘int main()’: /home/alfredo/repos/FlexCpp/src/back-end/main.cpp:17:5: error: ‘yyFlexLexer’ was not declared in this scope 17 | yyFlexLexer myTest(inputStream, outputStream); | ^~~~~~~~~~~ make[2]: *** [src/back-end/CMakeFiles/Foo.dir/build.make:80: src/back-end/CMakeFiles/Foo.dir/main.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:126: src/back-end/CMakeFiles/Foo.dir/all] Error 2 make: *** [Makefile:91: all] Error 2 奇怪的是,如果我注释掉main.cpp中的最后一行 //yyFlexLexer myTest(inputStream, outputStream); 我收到以下错误: lexer.l: In member function ‘virtual int yyFlexLexer::yylex()’: lexer.l:172:1: error: ‘yypanic’ was not declared in this scope /usr/local/flex/include/FlexLexer.h: At global scope: /home/alfredo/repos/FlexCpp/src/back-end/lexer.yy.cc:49:25: error: redefinition of ‘class yyFlexLexer’ 49 | #define yyFlexLexer yyFlexLexer | ^~~~~~~~~~~ /home/alfredo/repos/FlexCpp/src/back-end/lexer.yy.cc:49:25: note: previous definition of ‘class yyFlexLexer’ 49 | #define yyFlexLexer yyFlexLexer | ^~~~~~~~~~~ make[2]: *** [src/back-end/CMakeFiles/Foo.dir/build.make:94: src/back-end/CMakeFiles/Foo.dir/lexer.yy.cc.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:126: src/back-end/CMakeFiles/Foo.dir/all] Error 2 make: *** [Makefile:91: all] Error 2 我的项目目录如下所示。 backend/ |- main.cpp (what you saw ealier) |- CMakeLists.txt |- keywords.hxx (contains enum tokens and struct) |- lexer.l |- lexer.yy.cc (this will be generated by lexer.l when we compile with c++ we set %option c++ in our lexer.l file ) 这是我的 lexer.l 的样子 /* we dont have an interactive session. */ %option never-interactive /* we will handle the nodefault rule. */ %option nodefault /* we dont want yyinput() and yyunput(). */ %option noinput nounput /* don't include <unistd.h> we dont need it. */ %option nounistd /* not needed for now, in the future when users can work with multiple files we can start using it. */ %option noyywrap /* these two options are doubled to get more detailed reports. write performance report to stderr. */ %option perf-report perf-report /* write statistics summary to stderr. */ %option verbose verbose /* generate warning messages for mistakes. */ %option warn /* maintain current line number in yylineno. */ %option yylineno %option c++ /* c items that can be used in the rules section */ %{ #include <stdio.h> #include <keywords.hxx> int iskeyword(char * keyword); %} digit [0-9] exponent [ee][-+]?{digit}+ id_start [_a-za-z] id_after {id_start}|{digit} /*===================rules===================*/ %% /* a bunch of rules */ %% /*=================user-code=================*/ #include <keywords.hxx> /*As mentioned by Flex manual we include this if our main() is not in this file */ #include <FlexLexer.h> int iskeyword(char * keyword){ for(int index = 0; index < NUM_KEYWORDS; index++){ if(strcmp(keywords[index].keyWord, keyword)){ return keywords[index].token; } } return 0; } CMakeLists.cmake cmake_minimum_required(VERSION 3.5) project( TestingProj ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /usr/local/flex/include") find_package(FLEX REQUIRED) add_library(keywords keywords.hxx) set_target_properties(keywords PROPERTIES LINKER_LANGUAGE CXX) target_include_directories(keywords PUBLIC ./) FLEX_TARGET( gen_lexer lexer.l lexer.yy.cc ) add_executable( Foo main.cpp ${FLEX_gen_lexer_OUTPUTS} ) target_link_libraries(Foo PUBLIC keywords) target_link_libraries(Foo PUBLIC ${FLEX_LIBRARIES}) 如果我记录以下内容 message("FLEX_FOUND ${FLEX_FOUND}") message("FLEX_EXECUTABLE ${FLEX_EXECUTABLE}") message("FLEX_VERSION ${FLEX_VERSION}") message("FLEX_LIBRARIES ${FLEX_LIBRARIES}") message("FLEX_INCLUDE_DIRS ${FLEX_INCLUDE_DIRS}") 我明白了 FLEX_FOUND TRUE FLEX_EXECUTABLE /usr/local/flex/bin/flex FLEX_VERSION 2.6.4 FLEX_LIBRARIES /usr/local/flex/lib/libfl.so FLEX_INCLUDE_DIRS /usr/local/flex/include 我也厌倦了遵循 Flex 手册。没有帮助。 如果您想创建多个(不同的)词法分析器类,您可以使用“-P”标志(或 prefix= option) 将每个 yyFlexLexer 重命名为其他“xxFlexLexer”。然后是你 每个词法分析器类可以在其他源中包含 一次,首先重命名 yyFlexLexer如下: #undef yyFlexLexer #define yyFlexLexer xxFlexLexer #包括 #undef yyFlexLexer #define yyFlexLexer zzFlexLexer #包括 我不确定我做错了什么。我花了很多时间试图让 Flex 和 C++ 工作。我能够让 Flex 和纯 C 工作,但只是 C++ 我遇到了麻烦。截至发布时,我的最佳猜测是 FlexLexer.h 文件存在问题。出于某种原因,我从源代码构建的其他项目在其 FlexLexer.h 文件夹中构建了 /include 文件。据我所知,它现在指向 FlexLexer.h 的正确位置,我的情况是在 /usr/local/flex/include/FlexLexer.h: 或者我的 .cmake 文件可能有问题。 任何帮助将不胜感激。 我首先按照 Tysvarev 留下的评论解决了这个问题,我引用了他们。 “不需要显式包含 lexer.l 中的该文件。但是在 main.cpp 中,你需要包含 FlexLexer.h,否则编译器没有有关 yyFlexLexer 类的信息” 然后在%{ ... %}块内,我像这样定义了yypanic void yypanic( char* errorMessage); 最后在我写的用户代码中 void yypanic(char * errorMessage) { std::cout << errorMessage << std::endl; }

回答 1 投票 0

词法分析时如何检测字符串?

我正在使用一些语法在词法分析期间检测字符串 "".*"" 返回 TOK_STRING; 但这不起作用。

回答 2 投票 0

Bison 解析器总是打印语法错误而不指定错误所在

大家好,我正在尝试为这个简单的java简单文件编写一个解析器,基本上我希望这个解析器能够识别和验证指令块。找到正确的指令块后,该值...

回答 1 投票 0

Flex 词法分析器中的定义 - 如何定义科学记数法

我刚开始使用flex,需要定义真正的文字数字,即。 3.1 或 3.0e-10 为可接受的数字。这是我到目前为止所拥有的: 数字 [0-9] 整数 {数字}+ real_literal ({digit}+)(&quo...

回答 2 投票 0

我怎样才能获得当前flex/lex正在词法分析的行以在yyerror中使用它?

我正在使用 flex+bison 作为工具包开发简单的 aql 解析器。现在,我正在尝试在发生语法错误时提供有用的反馈。我想向用户展示这样的想法: 语法错误...

回答 1 投票 0

Flex Scanner 中的“input(yytext, yyleng)”函数从何而来?

我正在做斯坦福大学的CS143:edx上的编译器 我从这个仓库中看到了这段代码 /* 字符串结束,我们需要处理一些转义字符 */ \" { std::字符串输入(yytext, yyle...

回答 1 投票 0

Flex+ Bison 集成错误:杂散的 '\' 和预期的 ';'在“{”标记之前

我在 Flex 和 Bison 集成方面遇到问题,特别是与“程序中的杂散`\`”和“预期的 ';' 等错误相关在‘{’标记之前。”我检查了我的代码...

回答 1 投票 0

我在 Flex/Bison 的 C 程序中遇到语法错误

我正在为我的编译器课程做我的项目,我终于完成了它和它的编译器,但我不断收到语法错误,我不知道为什么!我尝试使用差异来源来看看我能做什么......

回答 1 投票 0

Makefile自动调用`yacc`

嗨我有一个像这样的makefile: # 变量================================================== ======================================= 假货 = 项目= ALL_FILES := $(过滤 $(pr...

回答 2 投票 0

为什么我在我的 Lexer 程序中收到此警告“规则无法匹配”

我在flex程序“.l”中有这段代码 “if”{return IF;}“else”{return ELSE;}“For”{return FOR;} 。 {printf("语法错误..退出..");退出(0);} 一个...

回答 1 投票 0

Bison-Flex extern FILE *yyin 不起作用(C 语言)

我知道在flex中你只需要做 yyin = fopen(filename, "r");读取文件,但如果你想从 bison 读取文件,这怎么可能呢?我正在尝试将 Flex 和 Bison 结合起来以达到我的目的(阅读...

回答 1 投票 0

Flex 会在调用 YY_FATAL_ERROR 之前自行清理吗?

Flex 生成的词法分析器调用用户可定义的 YY_FATAL_ERROR()。在调用此宏/函数之前,Flex 是否会清理它分配给自己的任何资源?它会释放任何分配的内存吗? ...

回答 1 投票 0

生成的头文件不包含用户定义的文件

目前正在尝试使用 Flex 创建词法分析器。运行命令 flex pl0_lexer.l 时,我的 pl0_lexer.h 中的 include 语句与我的 pl0_lexer.c 不匹配。 pl0_lexer.l 片段 %选项标头过滤...

回答 1 投票 0

YACC 解析器中 YYEOF 的正确用法

我一直在尝试编写一个解析器来评估单个文件中的多个算术运算。每个表达式都会计算并且工作正常,除了最后我打印出这个错误(yyeerr...

回答 1 投票 0

用于多行注释的 Unix Flex 正则表达式

我正在 Unix 上使用 Flex 制作词法分析器。如果您曾经使用过它,那么您知道您主要只是为您正在编写词法分析的任何语言的标记定义正则表达式......

回答 3 投票 0

如何使用 Flex 强制执行空格分隔?

如何在 Flex 中强制关键字以空格分隔? 例如,如果猫和狗是关键字,那么猫狗应该被接受,但猫狗不应该被接受。 使用尾随 con...

回答 1 投票 0

如何在windows上编译lex文件?

我已在 C:\ 中正确下载并安装了 flex、bison 和 Dev C++。然后我尝试在命令提示符下编译 myfile.l,但出现错误: Flex:无法打开 myfile.l。 什么是专业...

回答 2 投票 0

yyparse() 未在 Bison/Flex C++ 项目中声明,仅适用于某些版本的 gcc/bison/flex

首先,我对 Bison 和 Flex 还很陌生。我知道这些工具是为在 C 中使用而设计的,而且我感觉我的所有问题都来自于在 C++ 中使用它们。我不确定我是否已经做到了...

回答 2 投票 0

在 C++ 项目中使用 flex/bison 时避免“警告 C4005: 'INT8_MIN': 宏重新定义”

我有一个使用flex/bison 的C++ 项目。 词法分析器生成的代码包括以下部分: /* C99 系统有 。非 C99 系统可能会也可能不会。 */ #如果已定义(

回答 1 投票 0

如何使用我的 Bison 解析器“链接”或读取输入?

我知道解析器读取词法分析器生成的标记。我有一个使用 Flex++ 创建的工作词法分析器,它读取文本文件并将标记输出到标准输出。就解析器而言,我......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.