如何回到Flex中的某个位置?

时间:2012-05-15 03:19:10

标签: flex-lexer

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

//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()返回语句,但不是全部。

1 个答案:

答案 0 :(得分: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]*/\(.\)

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