-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdcg1_2.pl
72 lines (58 loc) · 1.38 KB
/
dcg1_2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% double quoted strings are just lists of UTF-8 codes,
% so they're terminals too
cliche -->
thing,
" is a ",
type_of_thing,
" trapped in a ",
opposite_type_of_thing,
" body.".
thing --> "Cygwin".
type_of_thing --> "Unix OS".
opposite_type_of_thing --> "Windows'".
% query try_literals. to see cliches
try_literals :- phrase(cliche, X),format('~s~n', [X]).
% you can unify arguments, just like normal prolog predicates
fizz_buzz(Msg) --> anything, fizz(Msg), anything, buzz, anything.
anything --> [].
anything --> [_], anything.
fizz(Msg) -->
"fizz",
{
format('At fizz we have Msg=~w~n', [Msg])
}.
buzz -->
"buzz".
try_fizz_buzz :- phrase(fizz_buzz('howdy'), X),format('~s~n', [X]).
% The ; operator allows alternatives. This matches a book,
% an book, a car, or an car
%
article_phrase --> ("a" ; "an"),
" ",
noun.
noun --> "book".
noun --> "car".
try_article_phrase :-
phrase(article_phrase, X),
format('~s~n', [X]),
fail.
try_article_phrase.
something(X) -->
({ is_wobbly(X) } ->
"a wobbly ",
thing
;
"a stable ",
thing
).
is_wobbly(X) :-
X = wobbly.
try_something :-
phrase(something(wobbly), Generated),
format('~s~n', [Generated]),
fail.
try_something :-
phrase(something(not_wobbly), Generated),
format('~s~n', [Generated]),
fail.
try_something.