入力からのForth形式のスキップを希望します.Forth形式は、入力がすべてif
で始まり、then
で終わる場合、すべての入力が正しい不一致処理を必要としないと仮定します。赤色の再帰的な構文解析
問題は、if
の各部分は、他の任意の数を再帰的に含むことができますif
のです。ここで
は、テストケースで私の最高のソリューションです:
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" ]
]
出力は次のとおりです。
[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
彼らは1 then
との間で何か(この場合は5
)が含まれているためので、それらの3が失敗します別の
おそらく、修正は非常に簡単で明白ですが、今は表示されません。可能であれば、上記のルールを修正したり、すべてのテストに合格する別のルールを表示したりできますか?
最初のルールで 'to'を使用していますか、本当に' thru'を本当に使いたいですか? – DocKimbel
'parse-trace'は解析をデバッグするのに便利です –
@DocKimbel私は' then'を最初に見つけたので 'to'を意図的に使用しています - これは現在の' if'が終了したことを意味します。 1。 – user3033648