gpt4 book ai didi

regex - 我如何最好地使用 Perl 的 Regexp::Grammars 进行平衡引用?

转载 作者:行者123 更新时间:2023-12-04 06:51:34 25 4
gpt4 key购买 nike

使用 Damian Conway 的 Regexp::Grammars ,我正在尝试匹配不同的平衡引用( 'foo'"foo" ,但不是 'foo" )机制——例如括号、引号、双引号和双美元。这是我目前使用的代码。

<token: pair>        \'<literal>\'|\"<literal>\"|\$\$<literal>\$\$
<token: literal> [\S]+

这通常工作正常,并允许我说:
<rule: quote>            QUOTE <.as>? <pair>

我的问题是如何改革输出,以排除 pair 的针符号 token ?
{
'' => 'QUOTE AS \',\'',
'quote' => {
'' => 'QUOTE AS \',\'',
'pair' => {
'literal' => ',',
'' => '\',\''
}
}
},

这里显然没有 pair的欲望介于两者之间,报价和 literal它的值(value)。有没有更好的匹配方式 'foo' , "foo" , 和 $$foo$$ ,也许有时 ( foo )无需每次都创建不必要的 pair token ?我可以预处理该 token 或将其折叠到上面吗?或者,编写一个完全不需要它的更好的构造?

最佳答案

根据 Damian,答案实际上在 "Manual result distillation" 中部分文档

The correct answer is to tell your <pair> token
to pass the result of each <literal> subrule through as its own
result, using the MATCH=
alias (see: "Manual result distillation" in the module documentation) like so:

<token: pair> \'<MATCH=literal>\' | \"<MATCH=literal>\" |
\$\$<MATCH=literal>\$\$

这是文档所说的:

Regexp::Grammars also offers full manual control over the distillation process. If you use the reserved word MATCH as the alias for a subrule call [...] Note that, in this second case, even though and are captured to the result-hash, they are not returned, because the MATCH alias overrides the normal "return the result-hash" semantics and returns only what its associated subrule (i.e. ) produces.

关于regex - 我如何最好地使用 Perl 的 Regexp::Grammars 进行平衡引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3047155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com