我想在Prolog中編寫gen(G,S)以生成給定語法G的有效序列S,其中G是格式語法[非終端列表],[終端列表],[規則列表],[開始序列])。規則採用格式規則(nt,[x]),其中x可以是非終端和/或終端的任何列表。 例如(規則(a,[t]),規則(a,[z]),規則(b,[y]),規則(b,[a,q])],[a,b]),X)。 返回: X = [t,y]。 X = [t,t,q]。 X =
我是新來的prolog,所以這對我來說是一個很大的挑戰。 我應該在Prolog中實現一個簡單的C語言。 the ultimate goal is to be able to execute something like this:
?- run([begin,a,:=,10,while,a,>,5,begin,write,a,a,:=,a,-,1,end,end]).
and get:
1
以從Introduction to Latin Wikiversity例如,考慮了一句: the sailor gives the girl money
我們可以在序言與DCG用這堆規則處理這個相當優雅: sentence(s(NP, VP)) --> noun_phrase(NP), verb_phrase(VP).
noun_phrase(Noun) --> det, noun(Noun