确定子句文法 上机操作

转载自:https://mp.weixin.qq.com/s/UYedzZIXz0JRYrYTNjItbw

?- [user].

|: s(Z):- np(X), vp(Y), append(X,Y,Z).

|: np(Z):- det(X), n(Y), append(X,Y,Z).

|: vp(Z):- v(X), np(Y), append(X,Y,Z).

|: vp(Z):- v(Z).

|: det([the]).

|: det([a]).

|: n([woman]).

|: n([man]).

|: v([shoots]).

|: ^D% user://2 compiled 0.01 sec, 11 clauses

true.

 

?- s([a,woman,shoots,a,man]).

 

true ;

true ;

true ;

false.

 

?- s(X).

X = [the, woman, shoots, the, woman] ;

X = [the, woman, shoots, the, man] ;

X = [the, woman, shoots, a, woman] ;

X = [the, woman, shoots, a, man] ;

X = [the, woman, shoots] ;

X = [the, man, shoots, the, woman] ;

X = [the, man, shoots, the, man] ;

X = [the, man, shoots, a, woman] ;

X = [the, man, shoots, a, man] ;

X = [the, man, shoots] ;

X = [a, woman, shoots, the, woman] ;

X = [a, woman, shoots, the, man] ;

X = [a, woman, shoots, a, woman] ;

X = [a, woman, shoots, a, man] ;

X = [a, woman, shoots] ;

X = [a, man, shoots, the, woman] ;

X = [a, man, shoots, the, man] ;

X = [a, man, shoots, a, woman] ;

X = [a, man, shoots, a, man] ;

X = [a, man, shoots] ;

X = [a, woman, shoots, a, man] ;

X = [the, woman, shoots, the, woman] ;

X = [the, woman, shoots, the, man] ;

X = [the, woman, shoots, a, woman] ;

X = [the, woman, shoots, a, man] ;

X = [the, woman, shoots] ;

X = [the, man, shoots, the, woman] ;

X = [the, man, shoots, the, man] ;

X = [the, man, shoots, a, woman] ;

X = [the, man, shoots, a, man] ;

X = [the, man, shoots] ;

X = [a, woman, shoots, the, woman] ;

X = [a, woman, shoots, the, man] ;

X = [a, woman, shoots, a, woman] ;

X = [a, woman, shoots, a, man] ;

X = [a, woman, shoots] ;

X = [a, man, shoots, the, woman] ;

X = [a, man, shoots, the, man] ;

X = [a, man, shoots, a, woman] ;

X = [a, man, shoots, a, man] ;

X = [a, man, shoots].

 

?- np([a,woman]).

true ;

false.

 

?- np(X).

X = [the, woman] ;

X = [the, man] ;

X = [a, woman] ;

X = [a, man].

 

?- halt.

 

 

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s(Z):- append(X,Y,Z), np(X), vp(Y).

|: np(Z):- append(X,Y,Z), det(X), n(Y).

|: vp(Z):- append(X,Y,Z), v(X), np(Y).

|: vp(Z):- v(Z).

|: det([the]).

|: det([a]).

|: n([woman]).

|: n([man]).

|: v([shoots]).

|: 

|: ^D% user://1 compiled 0.00 sec, 9 clauses

true.

 

?- s([a,woman,shoots,a,man]).

true ;

false.

 

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s(X,Z):- np(X,Y), vp(Y,Z).

|: np(X,Z):- det(X,Y), n(Y,Z).

|: vp(X,Z):- v(X,Y), np(Y,Z).

|: vp(X,Z):- v(X,Z).

|: det([the|W],W).

|: det([a|W],W).

|: n([woman|W],W).

|: n([man|W],W).

|: v([shoots|W],W).

|: ^D% user://1 compiled 0.00 sec, 9 clauses

true.

 

?- n([man|W],[W]).

W = [W].

 

?-  s([a,woman,shoots,a,man],[]).

true ;

false.

 

?- s([a,woman,shoots,a,man],[]).

true .

 

?-  s(X,[]).

X = [the, woman, shoots, the, woman] ;

X = [the, woman, shoots, the, man] ;

X = [the, woman, shoots, a, woman] ;

X = [the, woman, shoots, a, man] ;

X = [the, woman, shoots] ;

X = [the, man, shoots, the, woman] ;

X = [the, man, shoots, the, man] ;

X = [the, man, shoots, a, woman] ;

X = [the, man, shoots, a, man] ;

X = [the, man, shoots] ;

X = [a, woman, shoots, the, woman] ;

X = [a, woman, shoots, the, man] ;

X = [a, woman, shoots, a, woman] ;

X = [a, woman, shoots, a, man] ;

X = [a, woman, shoots] ;

X = [a, man, shoots, the, woman] ;

X = [a, man, shoots, the, man] ;

X = [a, man, shoots, a, woman] ;

X = [a, man, shoots, a, man] ;

X = [a, man, shoots].

 

?- np([a,woman],[]).

true.

 

?- np(X,[]).

X = [the, woman] ;

X = [the, man] ;

X = [a, woman] ;

X = [a, man].

 

?- halt.

 

$ swipl

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s-->np,vp.

|: np-->det,n.

|: vp-->v,np.

|: vp-->v.

|: det-->[the].

|: det-->[a].

|: n-->[woman].

|: n-->[man].

|: v-->[shoots].

|: ^D% user://1 compiled 0.01 sec, 9 clauses

true.

 

?- s([a,woman,shoots,a,man],[]).

true ;

false.

 

?- s([a,woman,shoots,a,man],[]).

true .

 

?- s(X,[]).

X = [the, woman, shoots, the, woman] ;

X = [the, woman, shoots, the, man] ;

X = [the, woman, shoots, a, woman] ;

X = [the, woman, shoots, a, man] ;

X = [the, woman, shoots] ;

X = [the, man, shoots, the, woman] ;

X = [the, man, shoots, the, man] ;

X = [the, man, shoots, a, woman] ;

X = [the, man, shoots, a, man] ;

X = [the, man, shoots] ;

X = [a, woman, shoots, the, woman] ;

X = [a, woman, shoots, the, man] ;

X = [a, woman, shoots, a, woman] ;

X = [a, woman, shoots, a, man] ;

X = [a, woman, shoots] ;

X = [a, man, shoots, the, woman] ;

X = [a, man, shoots, the, man] ;

X = [a, man, shoots, a, woman] ;

X = [a, man, shoots, a, man] ;

X = [a, man, shoots].

 

?- np([a,woman],[]).

true.

 

?- np(X,[]).

X = [the, woman] ;

X = [the, man] ;

X = [a, woman] ;

X = [a, man].

 

?- listing(s).

s(A, C) :-

    np(A, B),

    vp(B, C).

 

true.

 

?- listing(np).

np(A, C) :-

    det(A, B),

    n(B, C).

 

true.

 

?- listing(det).

det([the|A], A).

det([a|A], A).

 

true.

 

?- [user].

|: s --> s,conj,s.

 

Warning: user://2:79:

Warning:    Redefined static procedure s/2

Warning:    Previously defined at user://1:8

|: conj --> [and].

|: conj --> [or].

|: conj --> [but].

|: ^D% user://2 compiled 0.00 sec, 4 clauses

true.

 

?- listing(s).

s(A, D) :-

    s(A, B),

    conj(B, C),

    s(C, D).

 

true.

 

?- s([a,woman,shoots],[]).

ERROR: Stack limit (1.0Gb) exceeded

ERROR:   Stack sizes: local: 1.0Gb, global: 66Kb, trail: 4Kb

ERROR:   Stack depth: 11,182,627, last-call: 0%, Choice points: 3

ERROR:   Probable infinite recursion (cycle):

ERROR:     [11,182,627] user:s([length:3], _17166)

ERROR:     [11,182,626] user:s([length:3], _17192)

?- halt.

 

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s --> simple_s.

|: s --> simple_s,conj,s.

|: simple_s --> np,vp.

|: np --> det,n.

|: vp --> v,np.

|: vp --> v.

|: det --> [the].

|: det --> [a].

|: n --> [woman].

|: n --> [man].

|: v --> [shoots].

|: conj --> [and].

|: conj --> [or].

|: conj --> [but].

|: ^D% user://1 compiled 0.00 sec, 14 clauses

true.

 

?- s([a,woman,shoots],[]).

true ;

false.

 

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s --> [].

|: s --> l,s,r.

|: l --> [a].

|: r --> [b].

|: ^D% user://1 compiled 0.00 sec, 4 clauses

true.

 

?- s([a,a,a,b,b,b],[]).

true .

 

?- s([a,a,a,b,b,b,b],[]).

false.

 

?- s(X,[]).

X = [] ;

X = [a, b] ;

X = [a, a, b, b] ;

X = [a, a, a, b, b, b] ;

X = [a, a, a, a, b, b, b, b] ;

X = [a, a, a, a, a, b, b, b, b|...] ;

X = [a, a, a, a, a, a, b, b, b|...] ;

X = [a, a, a, a, a, a, a, b, b|...] ;

X = [a, a, a, a, a, a, a, a, b|...] .

 

?- 

 

 

练习 

?- [user].

|: s --> foo,bar,wiggle.

|: foo --> [choo].

|: foo --> foo,foo.

|: bar --> mar,zar.

|: mar --> me,my.

|: me --> [i].

|: my --> [am].

|: zar --> blar,car.

|: blar --> [a].

|: car --> [train].

|: wiggle --> [toot].

|: wiggle --> wiggle,wiggle.

|: ^D% user://1 compiled 0.00 sec, 12 clauses

true.

 

?- s(X,[]).

X = [choo, i, am, a, train, toot] ;

X = [choo, i, am, a, train, toot, toot] ;

X = [choo, i, am, a, train, toot, toot, toot] ;

X = [choo, i, am, a, train, toot, toot, toot, toot] ;

X = [choo, i, am, a, train, toot, toot, toot, toot|...] ;

X = [choo, i, am, a, train, toot, toot, toot, toot|...] .

 

?- listing(s).

s(A, D) :-

    foo(A, B),

    bar(B, C),

    wiggle(C, D).

 

true.

 

?- listing(foo).

foo([choo|A], A).

foo(A, C) :-

    foo(A, B),

    foo(B, C).

 

true.

 

?- listing(bar).

bar(A, C) :-

    mar(A, B),

    zar(B, C).

 

true.

 

?- listing(mar).

mar(A, C) :-

    me(A, B),

    my(B, C).

 

true.

 

?- listing(me).

me([i|A], A).

 

true.

 

?- listing(my).

my([am|A], A).

 

true.

 

?- listing(zar).

zar(A, C) :-

    blar(A, B),

    car(B, C).

 

true.

 

?- listing(blar).

blar([a|A], A).

 

true.

 

?- listing(car).

car([train|A], A).

 

true.

 

?- listing(wiggle).

wiggle([toot|A], A).

wiggle(A, C) :-

    wiggle(A, B),

    wiggle(B, C).

 

true.

 

 

?- halt.

 

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s --> [a,b].

|: s --> a, s, b.

|: a --> [a].

|: b --> [b].

|: ^D% user://1 compiled 0.00 sec, 4 clauses

true.

 

?- halt.

 

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- [user].

|: s --> [].

|: s --> a, s, b.

|: a --> [a].

|: b --> [b,b].

|: ^D% user://1 compiled 0.00 sec, 4 clauses

true.

 

?- halt.

 

后续可以根据实践环节的要求参考本上机操作熟练掌握本章内容!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值