柔性++不良字符错误等等。刚接触弹性

发布于 2024-12-14 02:00:39 字数 1082 浏览 5 评论 0原文

我们刚刚开始使用 flex 为项目构建词法分析器,但我们不知道如何让它工作。我复制教程中给出的示例代码,并尝试使用 tut 文件作为参数运行 flex++,但每次都收到错误。例如

输入文件 (calc.l)

%name Scanner
%define IOSTREAM

DIGIT   [0-9]
DIGIT1  [1-9]

%%

"+"               { cout << "operator <" << yytext[0] << ">" << endl; }
"-"               { cout << "operator <" << yytext[0] << ">" << endl; }
"="               { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}*  { cout << "  number <" << yytext    << ">" << endl; }
.                 { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }

%%

int main(int argc, char ** argv)
{
    Scanner scanner;
    scanner.yylex();
    return 0;
}

与我得到的代码

flex++ calc.l
calc.l:1: 错误字符: % calc.l:1: 未知错误处理部分 1
calc.l:1: 未知错误处理部分 1
calc.l:1: 未知错误处理部分 1
calc.l:2: 无法识别的“%”指令

任何人都可以帮助我理解我在这里做错了什么吗?干杯

we are just getting started using flex to build a lexer for a project, but we cant figure out how to get it to work. I copy the example code given in tutorials and try to run flex++ with the tut file as its argument however I just receive an error each time. e.g.

input file (calc.l)

%name Scanner
%define IOSTREAM

DIGIT   [0-9]
DIGIT1  [1-9]

%%

"+"               { cout << "operator <" << yytext[0] << ">" << endl; }
"-"               { cout << "operator <" << yytext[0] << ">" << endl; }
"="               { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}*  { cout << "  number <" << yytext    << ">" << endl; }
.                 { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }

%%

int main(int argc, char ** argv)
{
    Scanner scanner;
    scanner.yylex();
    return 0;
}

with this code i get

flex++ calc.l
calc.l:1: bad character: %
calc.l:1: unknown error processing section 1
calc.l:1: unknown error processing section 1
calc.l:1: unknown error processing section 1
calc.l:2: unrecognised '%' directive

could anyone help me understand what im doing wrong here? cheers

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄司 2024-12-21 02:00:39

您可以尝试以下操作:

  • %{ ... %} 添加到文件的前几行,
  • 添加 #includeusing namespace std; (而不是尝试定义扫描仪)
  • 使用 yylex( ) (而不是尝试调用不存在的扫描仪的方法)

对于您的示例,它可能看起来像这样:

%{
#include <iostream>
using namespace std;
%}

DIGIT   [0-9]
DIGIT1  [1-9]

/* read only one input file */
%option noyywrap

%%
"+"               { cout << "operator <" << yytext[0] << ">" << endl; }
"-"               { cout << "operator <" << yytext[0] << ">" << endl; }
"="               { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}*  { cout << "  number <" << yytext    << ">" << endl; }
.                 { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }
%%

int main(int argc, char** argv)
{
    yylex();
    return 0;
}

You might try something like:

  • adding %{ ... %} to the first couple of lines in your file
  • adding #include <iostream> and using namespace std; (instead of trying to define Scanner)
  • adding %option noyywrap above the rules section
  • using just yylex() (instead of trying to call the method of a non-existant Scanner)

With your example, it could look something like this:

%{
#include <iostream>
using namespace std;
%}

DIGIT   [0-9]
DIGIT1  [1-9]

/* read only one input file */
%option noyywrap

%%
"+"               { cout << "operator <" << yytext[0] << ">" << endl; }
"-"               { cout << "operator <" << yytext[0] << ">" << endl; }
"="               { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}*  { cout << "  number <" << yytext    << ">" << endl; }
.                 { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }
%%

int main(int argc, char** argv)
{
    yylex();
    return 0;
}
夜司空 2024-12-21 02:00:39

你使用的flex++版本是什么?
我使用 '功能:快速词法分析器生成器 C/C++ V2.3.8-7 (flex++),基于 2.3.8 并由 [email protected] for c++' (-? 选项) 并且您的 cacl.c 已完美处理..

对于 Win32,此版本的 Flex++/Bison++ 是 此处

What is the version of flex++ you using?
I uses 'Function: fast lexical analyzer generator C/C++ V2.3.8-7 (flex++), based on 2.3.8 and modified by [email protected] for c++' (-? option) and your cacl.c is processed perfectly..

For Win32, this version of Flex++/Bison++ is here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文