You are on page 1of 6

INSTRUCTIONS FOR THE LAB PRACTICE OF THE COMPUTATIONAL COMPLEXITY

COURSE, 22/FEBRUARY - PROF. LEVIS ZERPA – CONTEXT-FREE GRAMMARS

INTRODUCTION: HOW YOU WILL DO THIS PRACTICE AND YOUR LAB REPORT &
THE CALENDAR FOR NEXT WEEKS
This Lab Practice is individual (even if you interchange ideas with your classmates you must submit
your own Lab Practice Report). You will do the exercises in Prolog and then report your results in a
text document. Do the exercises and copy and paste your answers to your report and take it with
you when you finish your practice (you can send it to yourself via email or copy it in a flash
memory), print and give it to me at the beginning of the Tuesday 27/feb class; just that day, not
before and not after.
IMPORTANT, WATCH OUT! - ACTIVITIES OF THE NEXT TWO WEEKS:
1) Quiz of next Tuesday (27/Feb): it will be based on the problems you will work today.
2) Reading for next week (Tue 27): ch. 3 of Sipser on the Church-Turing thesis, Turing machines
and the formal definition of algorithm.
3) Lab Practice of next Thursday 1: it will be focused on examples of Turing machines.
4) Quiz of Tuesday 6/March: it will be based on the problems you will solve at the Lab the Thu 1.
5) The reading for the following week (Tuesday 3, March): ch. 4 on Decidability.
In general, any quiz for a given week is based on the Lab Practice of the previous week.

Context-free grammars of CFGs like the one we are studying this week, for example,
s → np vp
np → det n
vp →v np
vp →v
det →a
det →the
n →woman
n →man
v →shoots
can be implemented in a very direct and natural way in logic programing with Prolog. Moreover,
Prolog has a built-in grammar tool to write CFGs programs called definite clause grammars. A CFG
can be considered as a finite collection of rules which tell us that certain sentences are grammatical
(that is, syntactically correct) and what their grammatical structure actually is. The rules tell us how
different grammatical categories can be built up. For example, the first rule tells us that a sentence
can consist of a noun phrase followed by a verb phrase. The third rule tells us that a verb phrase can
consist of a verb followed by a noun phrase, while the fourth rule tells us that there is another way
to build a verb phrase: simply use a verb. The last five rules tell us that a and the are determiners,
that man and woman are nouns, and that shoots is a verb (Blackburn et al., ch. 7). Is the phrase “a
woman shoots a man” grammatical? The rules can be used to calculate the result: yes. Why? Draw
this parse tree: s np det a n woman vp v shoots np det a n man (check it by yourself using the
examples and explanations discussed last Tuesday). If a parse tree of this type cannot be
constructed, then the string is ungrammatical (according to the given grammar). The language
generated by a grammar consists of all the strings that the grammar classifies as grammatical. Is
remarkable the fact that one is able to prove logically (in formal mathematics) that (1) English is a
context- free language but (2) some dialects of Swiss-German are not context-free (ibid.). CFGs
allow us to cope with the syntax of both automata (formal) languages, logico-mathematical
(formal) languages [like the language of abstract algebra] and natural languages [like in artificial
intelligence and robotics]. In this practice you will write Prolog programs implementing CFGs as
knowledge databases and you will run queries on them. In contrast with conventional programming
languages, Prolog programs are nonempty sets of facts and rules and they are executed through
queries which are questions asked to the system. We will start with an example from Learn Prolog
Now! and we will continue with other examples. The Lab computers have SWI-Prolog installed;
you can use the stored program or the online version of Prolog: Swish. My instructions are referred
to Swish but they are applicable to the installed version.

PRELIMINARY INSTRUCTIONS: WARM-UP EXAMPLE:


Type “swish prolog online” in Google; (in Ubuntu or Windows) it will take you to a screen with the
title “Limited edition!”; click in the box “Don’t show again” and close that window. Then you will
see this window:

Observe (1) the two panels (left and right) of this screen and the box with the question mark “?-” on
the right panel with the message “Your query goes here...”, (2) the button Create a Program (in
blue) on the left panel, and (3) the button Run (in blue) at the bottom of the right panel. Click the
Program button and it will take you to the work window; you will write your programs on the left
panel where it says “Your Prolog rules and facts go here...” and you will run your queries on the
box of the right panel where it says “Your query goes here...” (the query box):
To make sure that you understand the interface, let’s do a preliominary example. Write the
following facts on the left panel (WATCH OUT: don’t forget the dot “.” at the end of each line of
code; it is indispensable because it indicates the end of the line, just like “;” in C):

woman(mia).
woman(jody).
woman(yolanda).

Now asks Prolog if jody is a woman writing the following query and hitting the button Run:

?­ woman(jody).

Now, use the variable “X” in UPPERCASE format (all variables are recognized by Prolog only
when they are written with UPPERCASE letters) to ask Prolog who are women in the knowledge
database:

?­ woman(X).

When you hit Run Prolog ask you how many results you want to see showing the buttons Next, 10,
100, 1,000, Stop. If you hit the 10 button you must get the following answer:

X = mia
X = jody
X = yolanda

which is the list of all women in our very little knowledge database. Lists constitute a powerful data
structure in al programming languages (since Lisp) and they are very important in Prolog (in fact,
sets are implemented as lists in it). A common operation on list is append which concatenates lists:
append(L1,L2,L3) will hold when the list L3 is the result of concatenating the lists L1 and L2
together, so the queries
?­ append([1,2],[3,4],[1,2,3,4]).

?­ append([a,b,c],[1,2,3],[a,b,c,1,2,3]).

must give you “true” (check it now; if this is the case, you can continue with the practice;
otherwise, contact me now).

GENERAL INSTRUCTIONS:
You will write context-free recognisers today (programs which correctly tells us whether or not a
string belongs to the language generated by a context-free grammar). Notice that parse tree shows
us how the words in phrase like “a woman shoots a man” fit together, piece by piece, to form the
sentence. You will write down Prolog clauses that correspond, in an obvious way, to the grammar
rules starting with the assumption that lists (like “[a,woman,shoots,a,man]”) can be used to
represent strings (“a woman shoots a man”). For example, the rule s → np vp can be thought of as
saying: a list of words is an s list if it is the result of concatenating an np list with a vp list. As we
know how to concatenate lists in Prolog (we can use append), it should be easy to turn these kinds
of rules into Prolog. And what about the rules that tell us about individual words? Even easier: we
can simply view n → woman as saying that the list [woman] is an n list. The pre-defined append
operation is defined as ([ ] is the empty list; n is the head and m is the tail in [n | m] and “Q :- P” is
read as ← in logic, that is Q :- P is read as “Q if P” or Q←P or P→Q in logic and programming):

append([],L,L).
append([H|T],L2,[H|L3]) :­ append(T,L2,L3).

Notice the recursive nature of this definition (the base of the recursion is, of course, the fact).

SPECIFIC ACTIVITIES AND EXERCISES:


Create a text document (in LibreOffice (Ubuntu) or Microsoft Office (Windows)) and
identify (write “Complexity Course, Lab Practice 22/February/2018” and your name).
Solve in your report it the following exercises:

1) A FIRST RECOGNIZER: Write the following simple (and not very efficient)
recognizer according the code written in the left panel of the program in the screen
shot and run the query specified in the query box in the image. Your answer must
coincide with the one given in the screen shot. In your response explain or justify your
answer in terms of the rules and parse trees of the given CFG:
And also run this query: ?­ np(X).

2) (a) Write and run in the previous program ALL the 20 sentences generated by this
grammar (run all of these as queries in only one screen and verify that they are all
accepted (grammatical) and do 1 screen shot of your results (WATCH OUT: One
screen shot not twenty!). Then (b) CONSTRUCT THE PARSE TREE FOR THE
FIRST 7 SENTENCES EITHER (1) AT HAND and SCAN your results and copy and
paste it in yout report OR (2) BY COMPUTER using any of the ONLINE parse trees
generators found on the web [write “online parse tree generator context free grammar” in
Google] and copy and paste it in yout report. IMPORTANT: Use the ideas and the
format explained and exemplified in the THEORETICAL CLASS of last Tuesday 20
(remember images in pages103 and ff. of the text).
As examples, the first three sentences generated are the following:

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

COMMENT: Notice that a significative fragment of any logical, mathematical or


computational language can also be generated in this way.

3) DCG FOR OUR FIRST RECOGNIZER AND THE FORMAL LANGUAGE a nbn
(a language very similar to the one considered on the quiz of Last Tuesday): (a) Following
the explanations in the subsections “Adding recursive rules” and “A DCG for a simple
formal language” of Learn Prolog Now!, sec. 7.2, write the programs and the queries
indicated in the text and include the corresponding screen shots. WATCH OUT: In the last
query, ?- s(X,[]). of anbn only show the first 10 strings and no more like this:
USE YOUR TIME WISELY: DO AS MUCH AS YOU CAN IN THE LAB (at the Lab I
can ask your question and help). WHETHER YOU FINISH OR NOT, PRINT YOUR
LAB PRACTICE REPORT AND GIVE IT TO ME AT THE BEGINNING OF THE
TUESDAY 27 CLASS. Have a good weekend.

You might also like