【发布时间】:2012-07-03 19:52:44
【问题描述】:
在 Parse::RecDescent 中,我如何有效地忽略 C++/Java 样式的 cmets?这包括单行('//' 直到行尾)和多行(/这里的所有内容/)。
【问题讨论】:
标签: perl parsing parse-recdescent
在 Parse::RecDescent 中,我如何有效地忽略 C++/Java 样式的 cmets?这包括单行('//' 直到行尾)和多行(/这里的所有内容/)。
【问题讨论】:
标签: perl parsing parse-recdescent
<skip> 定义解析器认为什么是空白。
parse: <skip: qr{(?xs:
(?: \s+ # Whitespace
| /[*] (?:(?![*]/).)* [*]/ # Inline comment
| // [^\n]* \n? # End of line comment
)
)*}>
main_rule
/\Z/
{ $item[2] }
与 Nate Glenn 的解决方案不同,我的
注意:(?:(?!STRING).)* 对应于 (?:STRING),就像 [^CHAR] 对应于 CHAR。
【讨论】:
local 将更改限制在编译语法时。 2) 考虑到 PRD 有多慢,速度损失可以忽略不计。 3)我基本上是安全的。根据 PRD 使用正则表达式的方式,这可能是也可能不是问题。
您必须设置$Parse::RecDescent::skip 的值。默认情况下,Parse::RecDescent 会跳过所有空白。如果将此变量设置为匹配空格和 cmets 的正则表达式,则可以跳过它们。使用这个:
$Parse::RecDescent::skip =
qr{
(
\s+ #whitespace
| #or
/[*] .*? [*]/ \s* #a multiline comment
| #or
//.*?$ #a single line comment
)* #zero or more
}mxs;
# m allows '$' to match a newline, x allows regex comments/whitespace,
# s allows '.' to match newlines.
【讨论】:
\G。