弹性动作在什么范围内执行?

What scope do flex actions execute in?

我正在学习使用 flex,但我想出了一个我还没有找到答案的问题(甚至在 reference 中也找不到)。假设我有这个代码:

patt1   { do_foo(42); }
patt2   { do_bar(); }

这可能会奏效。问题是,do_foo 可能需要通过引用接收一个参数(例如,一个 int)并用它做一些事情(实际上是 foo)。我能想到 do_foo 到达该变量的唯一方法是将其声明为全局变量,但根据代码运行的范围,可能会有另一个(更清洁、更好)的解决方案。

有什么想法吗?任何帮助将不胜感激。

提前致谢。

您可以创建一个宏,而不是声明一个全局变量。由于是预处理指令,所以大概是"cleaner".

#define FOO 42

patt1   { do_foo(FOO); }

What scope do flex actions execute in?

它们在生成的词法分析器的 yylex() 函数中的巨大 switch 语句范围内执行。

The problem is, do_foo might need to receive an argument by reference (say, an int) and do something (foo, actually) with it.

可能需要从什么地方接收参数?调用动作时唯一 运行 的是解析器和词法分析器。不是你的代码。因此,您甚至无法传递参数,更不用说接收参数了。

The only way I can think of do_foo reaching that variable is by declaring it as a global variable, but depending on the scope that code runs there may be another (cleaner, better) solution.

没有。它可能是生成的词法分析器文件范围内的 static 变量,您可以在第二个 %% 之后的代码部分中声明它,以及 getter 和 setter.但它不能是 yylex() 方法中的任何类型的局部变量,因为您无法控制它何时被调用。

或者它可以存储在解析器的 %union 中,如果这有意义的话。

实际上,生成的扫描器看起来像这样,遗漏了很多主要与缓冲区管理有关的细节:

int yylex() {
  /* A bit of setup */
  while (1) {
    do {
      yy_current_state = next_state(yy_current_state, get_next_char());
    } while (has_no_action(yy_current_state));
    yy_act = yy_accept[yy_current_state];
    switch (yy_act) {
      case 1: /* First action block */
              break;
      case 2: /* Second action block */
              break;
      /* etc. */
    }
  }
}

因此很容易看出操作的去向以及它们所处的范围。为了使该信息有用,您需要查看可以插入的挂钩,所以让我们用一些明确的挂钩再次写出来:

YY_DECL {
  /* Some declarations */

  /******** Prelude block *********/

  /* A bit of setup */
  while (1) {
    do {
      yy_current_state = next_state(yy_current_state, get_next_char());
    } while (has_no_action(yy_current_state));
    yy_act = yy_accept[yy_current_state];
    switch (yy_act) {
      case 1: YY_USER_ACTION   /**** User defined macro ****/
              /******** First action block *********/
              YY_BREAK
      case 2: YY_USER_ACTION
              /******** Second action block *********/
              YY_BREAK
      /* etc. */
    }
  }
}

最有趣的功能之一是 "prelude block"。在您的 (f)lex 输入文件中,它看起来像这样:

%option ...

%%
  /* Prelude block: indented lines before the first pattern */
  int locvar = 0;

patt1   { /* first action block */ }
patt2   { /* second action block */ }

宏都有合理的默认值:

/* The default definition of YY_DECL will be different if you've
 * asked for a reentrant lexer
 */
#ifndef YY_DECL
extern int yylex(void);
#define YY_DECL int yylex(void);
#endif

/* Code executed at the beginning of each rule, after yytext and yyleng
 * have been set up.
 */
#ifndef YY_USER_ACTION
#define YY_USER_ACTION
#endif

/* Code executed at the end of each rule. */
#ifndef YY_BREAK
#define YY_BREAK break;
#endif

为了您的目的,其中最有趣的是 YY_DECL。如果你想给yylex传递参数,你可以通过定义这个宏来修改原型。如果在 yylex 调用期间还需要局部变量,则可以在前奏块中声明它们。 (这对 "push" 词法分析器更有用,但即使对普通词法分析器也有它的用处。)

YY_USER_ACTIONYY_BREAK 宏更加专业化。虽然它们看起来都可能对调试有用,但您通常最好使用 flex 的内置跟踪工具。如果您想跟踪列位置而不仅仅是行号,则 YY_USER_ACTION 宏很有用;您可能会找到为此目的使用它的示例。如果您的编译器在 return 语句之后抱怨 break,则 YY_BREAK 宏可以设置为空(而不是 break)。

另外一个宏,在上面的代码中没有指出,是YY_USER_INIT,它将被合并到一次性初始化代码中(上面也没有显示,抱歉)。

其中大部分功能都记录在 flex 手册中。 YY_DECLSection 9 ("The Generated Scanner"); YY_USER_ACTION and YY_USER_INIT are in Section 13 ("Miscellaneous Macros") 中(以及其他一些功能)。 (YY_BREAK 在该部分的最后描述。)

prelude 块是一个 Posix 功能,因此它在 lex 中也可用,并记录在 Posix(以及 Section 5.2 of the Flex manual)中:

Any such input (beginning with a <blank> or within %{ and %} delimiter lines) appearing at the beginning of the Rules section before any rules are specified shall be written to lex.yy.c after the declarations of variables for the yylex() function and before the first line of code in yylex(). Thus, user variables local to yylex() can be declared here, as well as application code to execute upon entry to yylex().