0

例如,我的词法分析器识别函数调用模式:

//i.e. hello(...), foo(...), bar(...)
FUNCALL     [a-zA-Z0-9]*[-_]*[a-zA-Z0-9]+[-_*][a-zA-Z0-9]*\(.\)

现在 flex 识别了模式,但是它传递了模式中的最后一个字符(即存储foo(...)在里面之后yytext,词法分析器将指向之后的下一个字符foo(...)

如何将词法分析器指针重置回函数模式的开头?即在识别之后foo(..),我想让词法分析器指向的开始foo(..),所以我可以开始标记它。

我需要这样做,因为对于每个正则表达式模式,每个模式只能返回一个标记。即匹配后foo(...),我只能返回foo或返回语句,但不能全部返回()

4

1 回答 1

1

Flex 有一个尾随上下文模式匹配(下面的手动摘录) 在使用它之前阅读并理解限制。

`r/s'

 an `r' but only if it is followed by an `s'.  The text matched by
 `s' is included when determining whether this rule is the longest
 match, but is then returned to the input before the action is
 executed.  So the action only sees the text matched by `r'.  This
 type of pattern is called "trailing context".  (There are some
 combinations of `r/s' that flex cannot match correctly. *Note
 Limitations::, regarding dangerous trailing context.)

大概是这样的:

FUNCALL     [a-zA-Z0-9]*[-_]*[a-zA-Z0-9]+[-_*][a-zA-Z0-9]*/\(.\)

您可能会发现更改解析器更有意义,因此您不需要这样做。

于 2012-05-15T16:35:24.793 回答