PART B I. Evaluate a) + * 5 + * 4 7 2 3 b) * + 3 + * 2 4 7 9 c) + * + * 7 2 8 6 2 III. Given the BNF rules: ::= | ::= | ::= | | ::= | ::= | ::= the | a | an ::= girl | dog | man | truck ::= big | short | hairy | red ::= sadly | quickly | slowly ::= ran | fled | sank a) Draw a parse tree for a sentence in this grammar which only uses a terminal once. b) Draw a second VI. Write a LISP function COUNT that finds the number of times x occurs in a list of integers. (COUNT 7 ‘(7 5 9 7 3 6 7)) returns 3. VII.  Write a LISP function (DIFFERENCE X Y) that returns all the elements in Y that are not in X.  (DIFFERENCE ‘(3 5 7 9) ‘(1 3 5 6 8 9)) returns (1 6 8).

I. In order to evaluate the given expressions, let’s break them down one by one:

a) + * 5 + * 4 7 2 3

First, let’s evaluate the innermost expression: * 4 7. This results in 28.

Now, we have: + * 5 28 2 3.

Next, let’s evaluate * 5 28, which gives us 140.

Finally, we have: + 140 2 3. Adding these numbers together gives us the final result of 145.

b) * + 3 + * 2 4 7 9

Again, let’s start with the innermost expression: * 2 4. This gives us 8.

Now, we have: + * 3 8 7 9.

Next, let’s evaluate * 3 8, which results in 24.

Finally, we have: + 24 7 9. Adding these numbers together gives us the final result of 40.

c) + * + * 7 2 8 6 2

Let’s start with the innermost expression: * 7 2. This gives us 14.

Now, we have: + * 14 8 6 2.

Next, let’s evaluate * 14 8, which results in 112.

Finally, we have: + 112 6 2. Adding these numbers together gives us the final result of 120.

III. Given the BNF rules:

::= |
::= |
::= | |
::= |
::= |
::= the | a | an
::= girl | dog | man | truck
::= big | short | hairy | red
::= sadly | quickly | slowly
::= ran | fled | sank

a) To draw a parse tree for a sentence in this grammar which only uses a terminal once, we would need a specific sentence to work with. Without a specific sentence, it is not possible to draw a parse tree.

b) Draw a second parse tree. Similarly, without a specific sentence, it is not possible to draw a parse tree.

VI. Write a LISP function COUNT that finds the number of times x occurs in a list of integers. (COUNT 7 ‘(7 5 9 7 3 6 7)) returns 3.

Here is a LISP function that can be used to count the number of times a given element occurs in a list:

(defun count (x lst)
(if (null lst)
0
(if (equal x (car lst))
(+ 1 (count x (cdr lst)))
(count x (cdr lst)))))

This function recursively checks each element in the list. If the element is equal to x, it increments the count by 1. If not, it continues to check the rest of the list. Finally, it returns the total count.

For example, (count 7 ‘(7 5 9 7 3 6 7)) would return 3, as there are three occurrences of 7 in the list.

VII. Write a LISP function (DIFFERENCE X Y) that returns all the elements in Y that are not in X. (DIFFERENCE ‘(3 5 7 9) ‘(1 3 5 6 8 9)) returns (1 6 8).

Here is a LISP function that can be used to find the difference between two lists:

(defun difference (x y)
(cond ((null x) y)
((member (car x) y)
(difference (cdr x) (remove (car x) y)))
(t (difference (cdr x) y))))

This function uses recursion and conditional statements to compare each element in list x with the elements in list y. If an element in x is found in y, it is removed from y. Finally, the function returns the updated y list.

For example, (difference ‘(3 5 7 9) ‘(1 3 5 6 8 9)) would return (1 6 8), as these are the elements in y that are not in x.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer