我想解析 - 跳过Forth风格,如果从输入,Forth风格意味着每个如果以if
开头并以then
结尾,假设所有输入都是正确的,则不需要处理不匹配。红色递归解析
问题是if
的每个部分都可以递归地包含任何数量的其他if
's。
这里是一个测试案例我最好的解决办法:一个then
之间
[if a then] OK
[if a else b then] OK
[if a if b then then] OK
[if a if b then 5 then] FAIL
[if a if b then 5 if c then then] FAIL
[if a else if b then then] OK
[if a else if b then 5 then] FAIL
[if a else if b then if c then then] OK
[if a if b if c then if d then then then] OK
所以他们三人的失败,因为它们包含的东西(5
在这种情况下):
Red []
skip-nested-ifs: [skip to ['if | 'then] skip-nested-ifs-helper]
skip-nested-ifs-helper: ['then | skip-nested-ifs skip-nested-ifs-helper ]
rules: skip-nested-ifs
test-cases: [
[if a then]
[if a else b then]
[if a if b then then]
[if a if b then 5 then]
[if a if b then 5 if c then then]
[if a else if b then then]
[if a else if b then 5 then]
[if a else if b then if c then then]
[if a if b if c then if d then then then]
]
forall test-cases [
prin [mold test-cases/1 ""]
print either parse test-cases/1 rules [ "OK" ] [ "FAIL" ]
]
输出是另一个。
可能修复很简单明显,但我现在看不到它。如果可能的话,你能帮助我解决上述规则吗?还是显示出通过所有测试的不同规则?
在第一条规则中,你是否使用了'to',或者你真的想'穿过'而不是? – DocKimbel
'parse-trace'对于调试解析很有用 –
@DocKimbel我之后使用'to'来故意匹配'then',因为如果先找到'then' - 这意味着当前'if'完成,否则我们就会嵌套一。 – user3033648