Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Midterm 330-002 Fall 2019

1. For the following KB, what are the queries for a,b, and c. It is fine if your query “returns” (sets) variables; that does not change the success or failure of a query if that is all that is requested.

%book( stock# , title, edition, year).   – stock # is a unique identfier

book( id1942, 'The C Programming Language', 1, 1978 ).

book( id2491, 'The C Programming Language', 2, 1988 ).

book( id9791, 'Pride and Prejudice'  ,1 , 1813 ).

%... [remember, “…” means “many more like this”]

%author( stock#, fname, lname).

author( id1942, brian , kernighan).

author( id1942, dennis, ritchie).

author( id9791, jane, austen).

%...

%in_stock(stock#, yes/no)

in_stock( id1942 , no ).

in_stock( id2491, yes).

In_stock( id9791, no).

%...

a. (5) Query for “what is the title of book with stock#  id9791”?

?-

b.  (5) Query for “what books are in stock and published in the year 1900?”.

?-

c. (5) Query for “is there any book where the same edition was published in multiple years?”

?-

2. Describe how to represent a list of historic events such as the following.

· First Continental Congress: September 5, 1774

· Signing of Articles of Confederation: November 15, 1777

· […]

· Gulf of Tonkin Incident, August 2, 1964

· […] 

a. (5) Represent the layout in a manner appropriate for solving b and c. Ellipses, “%…” are fine, obviously, if your pattern is obvious. Show the above three, at least.

b. (5) Define a rule, before(EventA,EventB) that succeeds if EventA occurred before Event B chronologically (assume events on the same day occur simultaneously, not before).

c. (5) Define a rule for between(A,B,C), such that event B happens between events A and C. You cannot assume A happened before C. You may assume a working solution to b, above, i.e. that you have a working before(A,B) rule.

3. (15) Map Coloring: Solve the map coloring problem for the following Hexagonal map.

a. The top and bottom rows of hexagons should be sea tile.

b. The middle eight hexagons can be anything but sea.

c. No two towns can be adjacent.

 

tile(town). tile (sea). tile (mountain). tile (forest). tile(desert).

4. Trees, in this case binary trees, are recursive structures, so naturally it’s easy to work with them in Prolog. (these are not binary search trees). Write the descendant rule for binary trees:

 

%EXAMPLE tree

node(a,b,c).

node(b,d,e).

node(c,f,nil).

node(d,nil,nil).

node(e,nil,g).

node(f,nil,nil).

node(g,nil,nil).

a. (5) Define a root(R) rule, where R is the root of the tree.

b. (10) Define a leaf_of(Leaf,Node) that returns true if Leaf is a leaf node in one of the Node’s subtrees.

5. (10) [Extra Credit] Write a begins_and_ends rule that holds if a list begins and ends with the same item.

6. (3) A language with high orthogonality is going to have a ______________ number of similar/redundant features, e.g. to add numbers, perform iteration, etc.  

7. (3) Prolog is an example of a(n) ________________ language.

8. (3) ________________ is an example of an imperative language.

9. (3) Define “binding”?

10. (3 ) What is the difference between late and early binding?

11. (3) The syntax of most programming languages are specified with a __________________ grammar.

12. (3) Checking that the types of variables and the values being assigned the them are correct, for a statically typed language, is done by the ________________ stage of compilation.

13. (3) The output of the lexer, the program that perfoms lexical analysis, is …

14. (6) Give the parse tree for bbdxxggxx from A, as the start symbol. The alphabet is  .

A->BG

B->bB | C

C->cC | D

D-> dD | ε            // ε is “nothing”, the empty string

G->xGx | gGg | // ε is “nothing”, the empty string

15. (5) Give EBNF for a string having any number of as and bs  but having ab  as a substring.

a. Positive Examples: ab, aaabbbb, ababababababba

b. Negative Examples: ba, bbbaaaa, ε 

16. (5) Match a the EBNF on the left to the strings they match on the right.

xyzzzyx

[y]{x|y|z}x

zyxxxyz

(y|z) {x|y|z}

yxzyxzyx

{x|y}y{y|z}

xxxx