我正在玩语法并试图解析一个ini风格的文件,但不知何故,Grammar.parse似乎永远循环并使用100%的CPU.任何想法在这里有什么问题?
grammar Format {
token TOP {
[
<comment>*
[
<section>
[ <line> | <comment> ]*
]*
]*
}
rule section {
'[' <identifier> <subsection>? ']'
}
rule subsection {
'"' <identifier> '"'
}
rule identifier {
<[A..Za..z]> <[A..Za..z0..9_-]>+
}
rule comment {
<[";]> .*? $$
}
rule line {
<key> '=' <value>
}
rule key {
<identifier>
}
rule value {
.*? $$
}
}
Format.parse('lol.conf'.IO.slurp)
Run Code Online (Sandbox Code Playgroud)
令牌TOP在子规则*上具有可以解析空字符串的量词(因为两者<comment>和包含的组<section>都有*自己的量词).
如果内部子规则匹配空字符串,它可以无限次地执行,而不会使光标前进.目前,Perl 6没有针对此类错误的保护.
在我看来,你可以简化你的代码
token TOP {
<comment>*
[
<section>
[ <line> | <comment> ]*
]*
}
Run Code Online (Sandbox Code Playgroud)
(不需要外部组[...]*,因为最后一个<comment>也匹配部分之前的注释.
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |