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(..)ので、トークン化を開始できます。

正規表現パターンごとに、パターンごとに 1 つのトークンしか返せないため、これを行う必要があります。つまり、一致した後、またはまたはまたはreturn ステートメントの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 に答える