【发布时间】:2019-05-15 21:25:32
【问题描述】:
perl6 如何决定首先匹配哪个 proto token?
下面的代码按预期工作,它匹配字符串1234,而Grammar::Tracer 表明匹配的第一个令牌是s:sym<d>,这是有道理的,因为它是最长的令牌。
但是,如果我将文字更改为令牌,例如,将 token three 从 '3' 更改为 <digit>,则匹配失败,Grammar::Tracer 显示正在匹配 s:sym<b>首先。
将s:sym<d> 移到顶部,在这两种情况下都匹配字符串,但这种行为的解释是什么?
#!/usr/bin/env perl6
no precompilation;
use Grammar::Tracer;
grammar G {
token TOP { <s> }
proto token s { * }
token s:sym<a> { <one> }
token s:sym<b> { <one> <two> }
token s:sym<c> { <one> <two> <three> }
token s:sym<d> { <one> <two> <three> <four> }
token one { '1' }
token two { '2' }
token three { '3' }
token four { '4' }
}
my $g = G.new;
say $g.parse: '1234';
# Output: Match
# token three { '3' }
TOP
| s
| | s:sym<d>
| | | one
# Output No Match
# token three { <digit> }
TOP
| s
| | s:sym<b>
| | | one
【问题讨论】: