Вы находитесь на странице: 1из 11

Final Exam

CMSC 331
Principles of Programming Languages

Omar Shehab

Name _________________________

Section _________________________

Section - 2 - FRI, MAY 19 1:00-3:00 PM, Fine Arts 303

This exam is a closed-book-open-mind exam. It will be graded on the basis of correctness,


clarity, and neatness. Please show your work for partial credit. Use of electronic device is not
permitted without the permission of the instructor.

Academic Honesty
By enrolling in this course, each student assumes the responsibilities of an active participant in
UMBCs scholarly community in which everyones academic work and behavior are held to the
highest standards of honesty. Cheating, fabrication, plagiarism, and helping others to commit
these acts are all forms of academic dishonesty, and they are wrong. Academic misconduct
could result in disciplinary action that may include, but is not limited to, suspension or dismissal.
2

1. Using the following grammar, show a parse tree and a leftmost derivation for the
statement
A = A * ( B + ( C * A ) ).

Grammar:
<assign> <id> = <expr>
<id> A | B | C
<expr> <id> + <expr> | <id> * <expr> | (<expr>) | <id>

Parse tree:

Final Exam CMSC 331 NAME ___________________


3

Leftmost derivation:
(a) <assign> => <id> = <expr>
=> A = <expr>
=> A = <id> * <expr>
=> A = A * <expr>
=> A = A * ( <expr> )
=> A = A * ( <id> + <expr> )
=> A = A * ( B + <expr> )
=> A = A * ( B + ( <expr> ) )
=> A = A * ( B + ( <id> * <expr> ) )
=> A = A * ( B + ( C * <expr> ) )
=> A = A * ( B + ( C * <id> ) )
=> A = A * ( B + ( C * A ) )

Final Exam CMSC 331 NAME ___________________


4

2. a. Rewrite the following EBNF grammar in BNF, assuming that number and name are
already defined.

<expr> <term> {(+|-) <term>}


<term> <factor> {(*|/) <factor>}
<factor> '(' <expr> ')' | ident | number

2. b. Which of the following sentences are in the following grammar:

<S> <A> a <B> b


<A> <A> b | b
<B> a <B> | a

Sentences Valid against given grammar

baab Yes

bbbab No

bbaaaaa No

bbaab Yes

Final Exam CMSC 331 NAME ___________________


5

3.a. What is the type of the following values? If the expression is invalid, briefly state
why. (Assume numbers have the type Int. Remember that the type of True is Bool.)

Expression Type

"x" [Char] or String

(1, 'x', [True]) (Int, Char, [Bool])

["x" : [ ] ] [[[Char]]]

head [a] -> a

not . not . isLetter Char -> Bool

map not [Bool] -> [Bool]

(+1) . (0<) Invalid, since (0<) produces a Bool, and


Bools can't be added.

\x -> x + 1 Int -> Int

[1,'2',"3"] Invalid, due to multiple types.

isDigit . chr . head . (:[]) . (+3) Int -> Bool


. ord . chr

Hints:
ord :: Char -> Int
chr :: Int -> Char
isDigit :: Char -> Bool

Final Exam CMSC 331 NAME ___________________


6

3.b. Using no functions other than helper functions you write yourself, implement the
following Prelude functions. You must use the wildcard pattern (the underscore) where it
would be appropriate.

head
head [] = error "empty"
head (h:_) = h

tail
tail [] = error "empty"
tail (_:t) = t

length
length [] = 0
length (_:t) = 1 + length t

sum ( computes a sum of all elements in the list)


sum [] = 0
sum (h:t) = h + sum t

last (Returns the last item of a list. Return error "empty" if the list is empty.)
last [] = error "shortList"
last [x] = x
last (_:t) = last t

snd (Returns second element of a 2-tuple)


snd (_,x) = x

Final Exam CMSC 331 NAME ___________________


7

4. Assume given a set of facts of the form father(name1,name2) (name1 is the father of
name2).

Define a predicate brother(X,Y) which holds iff X and Y are brothers.


brother(X,Y) :- father(Z,X), father(Z,Y), not(X=Y).

Define a predicate cousin(X,Y) which holds iff X and Y are cousins.


cousin(X,Y) :- father(Z,X), father(W,Y), brother(Z,W).

Define a predicate grandson(X,Y) which holds iff X is a grandson of Y.


grandson(X,Y) :- father(Z,X), father(Y,Z).

Final Exam CMSC 331 NAME ___________________


8

5. Write next to each method call in main() the output that it prints.

class A {
public void f(A a) {
System.out.println("fa(A)");
}
public void f(B b) {
System.out.println("fa(B)");
}
}

class B extends A {
public void f(A a) {
System.out.println("fb(A)");
}
public void f(B b) {
System.out.println("fb(B)");
}
}

public class TypeMeister {


public static void main(String[] args) {
A a = new A();
B b = new B();
A ba=(A)b;
// Write output next to each of the following:
a.f(a); fa(A) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
a.f(b); fa(B) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
b.f(a); fb(A) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
b.f(b); fb(B) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
a.f(ba); fa(A)_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
b.f(ba); fb(A)_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
ba.f(a); fb(A)_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
ba.f(b); fb(B)_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
ba.f(ba); fb(A) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}
}

Final Exam CMSC 331 NAME ___________________


9

[This page is intentionally kept blank.]

Final Exam CMSC 331 NAME ___________________


10

[This page is intentionally kept blank.]

Final Exam CMSC 331 NAME ___________________


11

[This page is intentionally kept blank.]

Final Exam CMSC 331 NAME ___________________

Вам также может понравиться