0% found this document useful (0 votes)
245 views2 pages

LPN 07 Exercises

The document describes DCG rules for different languages and their corresponding Prolog rules. It includes three exercises: 1. The first exercise provides a DCG and asks the user to write the equivalent Prolog rules, which are provided. It also asks for the first three responses from querying s(X,[]). 2. The second exercise asks the user to write a DCG that generates the language anbn - {ε}. The DCG is provided. 3. The third exercise asks the user to write a DCG that generates the language anb2n. The DCG is provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views2 pages

LPN 07 Exercises

The document describes DCG rules for different languages and their corresponding Prolog rules. It includes three exercises: 1. The first exercise provides a DCG and asks the user to write the equivalent Prolog rules, which are provided. It also asks for the first three responses from querying s(X,[]). 2. The second exercise asks the user to write a DCG that generates the language anbn - {ε}. The DCG is provided. 3. The third exercise asks the user to write a DCG that generates the language anb2n. The DCG is provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise 7.

1
Suppose we are working with the following DCG:
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.

Write down the ordinary Prolog rules that correspond to these DCG rules.
s(X,A) :- foo(X,Y), bar(Y,Z), wiggle(Z,A).
foo([choo|W],W).
foo(X,Z) :- foo(X,Y), foo(Y,Z).
bar(X,Z) :- mar(X,Y), zar(Y,Z).
mar(X,Z) :- me(X,Y), my(Y,Z).
me([i|W],W).
my([am|W],W).
zar(X,Z) :- blar(X,Y), car(Y,Z).
blar([a|W],W).
car([train|W],W).
wiggle([toot|W],W).
wiggle(X,Z) :- wiggle(X,Y), wiggle(Y,Z).

What are the first three responses that Prolog gives to the query 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]

Exercise 7.2
The formal language anbn - {} consists of all the strings in anbn except the empty string. Write a
DCG that generates this language.
s --> a,b.
s --> a,s,b.
a --> [a].
b --> [b].

Exercise 7.3
Let anb2n be the formal language which contains all strings of the following form: an unbroken
block of as of length n followed by an unbroken block of bs of length 2n, and nothing else. For
example, abb, aabbbb, and aaabbbbbb belong to anb2n, and so does the empty string. Write a
DCG that generates this language.
s --> [].
s --> a,s,b.
a --> [a].
b --> [b,b].

You might also like