flex 文件编译错误

发布于 2024-09-10 18:27:07 字数 1800 浏览 3 评论 0原文

我正在尝试构建一个简单的词法分析器,以及一个用于(科学)C 程序的简单输入输出库。当使用自动工具(包括 automake、libtool 和 autoconf)进行编译时,出现以下错误:

simpleio_lex.l:41: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘of’

这通常意味着我忘记了函数原型末尾的分号,但我检查了标头,没有这样的遗漏。

这是 simpleio_lex.l:

%{
int yylex(void);
#define yylex sio_lex
#include "simpleio.h"
%}

NUM [0-9]           /* a number */
FLOAT {NUM}+"."{NUM}*           /* a floating point number */
FLOATSEQ {FLOAT[[:space:]]?}+
FLOATLN ^FLOATSEQ$
SYMBOL [a-z]+           /* a symbol always comes at the
                   beginning of a line */
SYMDEF ^SYMBOL[[:space:]]*FLOAT /* define a single value for a symbol */
RANGE FLOAT":"FLOAT":"FLOAT /* a range of numbers */
SYMRANGE ^SYMBOL[[:space:]]+RANGE$ /* assign a range of values to a symbol */

%%
                /* a set of lines with just numbers
                   indicates we should parse it as data */
{FLOATLN}+ sio_read_stk_inits (yytext);
SYMDEF sio_read_parse_symdef (yytext);
SYMRANGE sio_read_parse_symrange (yytext);
%%

/* might as well define these here */
sio_symdef_t *
sio_read_parse_symdef (char * symdef)
{
  sio_symdef_t * def = malloc (sizeof (sio_symdef_t));
  /* split the string into tokens on the LHS and RHS */
  char * delim = " ";
  char * lvalue = strtok (symdef, delim);
  size_t lsize = sizeof (lvalue);

  char * rest = strtok (NULL, delim);
  double plval;         /* place holder */
  int s_ck = sscanf (rest, "%lg", &plval);
  if (s_ck == EOF)
    return NULL;
  else
    {
    def->value = plval;
    def->name = malloc (lsize);
    memcpy(def->name, lvalue, lsize);
    }
  return def;
}

Emacs 中的 *compilation* 缓冲区超链接将我引向序言末尾的 %}%。为什么我会收到此错误?我也没有名为“of”的符号。

谢谢,

乔尔

I'm trying to build a simple lexical analyzer to go along with a simple input output library for (scientific) C programs. When compiling with autotools, including automake, libtool, and autoconf, I get the following error:

simpleio_lex.l:41: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘of’

This usually means that I've forgotten a semicolon at the end of a function prototype, but I've checked my header and there's no such omission.

Here's simpleio_lex.l:

%{
int yylex(void);
#define yylex sio_lex
#include "simpleio.h"
%}

NUM [0-9]           /* a number */
FLOAT {NUM}+"."{NUM}*           /* a floating point number */
FLOATSEQ {FLOAT[[:space:]]?}+
FLOATLN ^FLOATSEQ$
SYMBOL [a-z]+           /* a symbol always comes at the
                   beginning of a line */
SYMDEF ^SYMBOL[[:space:]]*FLOAT /* define a single value for a symbol */
RANGE FLOAT":"FLOAT":"FLOAT /* a range of numbers */
SYMRANGE ^SYMBOL[[:space:]]+RANGE$ /* assign a range of values to a symbol */

%%
                /* a set of lines with just numbers
                   indicates we should parse it as data */
{FLOATLN}+ sio_read_stk_inits (yytext);
SYMDEF sio_read_parse_symdef (yytext);
SYMRANGE sio_read_parse_symrange (yytext);
%%

/* might as well define these here */
sio_symdef_t *
sio_read_parse_symdef (char * symdef)
{
  sio_symdef_t * def = malloc (sizeof (sio_symdef_t));
  /* split the string into tokens on the LHS and RHS */
  char * delim = " ";
  char * lvalue = strtok (symdef, delim);
  size_t lsize = sizeof (lvalue);

  char * rest = strtok (NULL, delim);
  double plval;         /* place holder */
  int s_ck = sscanf (rest, "%lg", &plval);
  if (s_ck == EOF)
    return NULL;
  else
    {
    def->value = plval;
    def->name = malloc (lsize);
    memcpy(def->name, lvalue, lsize);
    }
  return def;
}

The *compilation* buffer hyperlink in Emacs refers me to the %}% at the end of the preamble. Why am I getting this error? I have no symbol called "of," either.

Thanks,

Joel

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

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

发布评论

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

评论(1

空心空情空意 2024-09-17 18:27:07

问题是一个悬空注释,我将其折叠成一行,如下所示:

/* this is a comment that's going to run into a 
     new line */

第二行被直接复制到源代码中,没有注释分隔符。看来flex对注释和格式相当挑剔。错误消息中提到的“of”是注释第二行的第一个单词。

问题是我必须查看派生的 .c 文件,而不是超链接指向我的 .l 文件。这是转换后的源:

#line 38 "simpleio_lex.l"
int yylex(void);
#define yylex sio_lex
#include <simpleio.h>
beginning of a line */
#line 505 "simpleio_lex.c"

来自flex处理的文件中的这个:

%{
int yylex(void);
#define yylex sio_lex
#include <simpleio.h>
%}


NUM [0-9]           /* a number */
FLOAT {NUM}+"."{NUM}*           /* a floating point number */
FLOATSEQ {FLOAT[[:space:]]?}+
FLOATLN ^FLOATSEQ$
SYMBOL [a-z]+           /* a symbol always comes at the
                   beginning of a line */

谢谢!
乔尔

The problem was a dangling comment that I folded onto a line by itself, like this:

/* this is a comment that's going to run into a 
     new line */

The second line was copied directly into the source, without its comment delimiter. It seems flex is rather picky about comments and formatting. The "of" mentioned in the error message is the first word of the second line of the comment.

The problem is that I had to look in the derived .c file, not in the .l file where the hyperlink was directing me. This is the transformed source:

#line 38 "simpleio_lex.l"
int yylex(void);
#define yylex sio_lex
#include <simpleio.h>
beginning of a line */
#line 505 "simpleio_lex.c"

From this in the file processed by flex:

%{
int yylex(void);
#define yylex sio_lex
#include <simpleio.h>
%}


NUM [0-9]           /* a number */
FLOAT {NUM}+"."{NUM}*           /* a floating point number */
FLOATSEQ {FLOAT[[:space:]]?}+
FLOATLN ^FLOATSEQ$
SYMBOL [a-z]+           /* a symbol always comes at the
                   beginning of a line */

Thanks!
Joel

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