Академический Документы
Профессиональный Документы
Культура Документы
CMSC 331
Principles of Programming Languages
Omar Shehab
Name _________________________
Section _________________________
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:
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 ) )
2. a. Rewrite the following EBNF grammar in BNF, assuming that number and name are
already defined.
baab Yes
bbbab No
bbaaaaa No
bbaab Yes
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]]]
Hints:
ord :: Char -> Int
chr :: Int -> Char
isDigit :: Char -> Bool
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
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
4. Assume given a set of facts of the form father(name1,name2) (name1 is the father of
name2).
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)");
}
}