我想在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