Anda di halaman 1dari 10

ECS 140A Programming Languages

Midterm

Midterm
Please read all instructions (including these) carefully.
There are six questions on the exam, some with multiple parts. You have 1:20 to work on the
exam.
The exam is open book, open notes.
Please write your answers in the space provided on the exam, and clearly mark your solutions.
You may use the backs of the exam pages as scratch paper. Please do not use any additional
scratch paper.
Solutions will be graded on correctness and clarity. Each problem has a relatively simple and
straightforward solution. You may get as few as 0 points for a question if your solution is far
more complicated than necessary. Partial solutions will be graded for partial credit.

NAME:

(Last, First)

Problem
1
2
3
4
5
6
TOTAL

Spring 2007

Max points
30
15
12
10
10
23
100

Points

1 of 10

ECS 140A Programming Languages

Midterm

1. Syntax Analysis and Predicative Parsing (30 points)


Consider the following grammar:
S
T
L
F

::=
::=
::=
::=

S ++ T | T
F :: T | L
(S) | nil
0|1

In this grammar, S, T , L, and F are nonterminals, whereas ++, ::, (, ), nil, 0, and 1 are
terminals.
This grammar generates bit string expressions. For example, it can generate
0 :: 1 :: nil
This question asks you to construct a predicative parser for the given grammar.
(a) (6 points) Does each of the following strings belong to the language of the given grammar?
Circle YES or NO.
i.

YES

NO

1 :: 0 ++ 1 :: nil

ii.

YES NO

0 :: 1 :: (1 :: nil ++ 0 :: nil)

iii.

YES NO

(1 :: nil ++ nil) ++ 0 :: 1 :: nil

(b) (5 points) Notice that there are left recursions in the grammar. Convert the grammar to
EBNF to get rid of the left recursions.

(c) (5 points) Compute the FIRST sets for the nonterminals S, T , L, and F .

Spring 2007

2 of 10

ECS 140A Programming Languages

Midterm

(d) (9 points) Use the FIRST sets that you have computed in part (c) to construct a predicative
parser based on the EBNF grammar in part (b). You can write your parser in Java or
pseudo-code. Explicitly state any assumptions that you make.

Spring 2007

3 of 10

ECS 140A Programming Languages

Midterm

(e) (5 points) Extend your parser to evaluate the bit string expressions. For instance, given
the input on the left it should evaluate to the output on the right:
Expression
0 :: nil
0 :: 1 :: nil
0 :: 1 :: nil ++ 0 :: nil
1 :: (0 :: nil ++ 1 :: nil)

Output
[0]
[01]
[010]
[101]

You can use functions concat, which takes to bit strings and returns the concatenated
string, and prepend, which takes a bit and a string, and returns the prepended string.

Spring 2007

4 of 10

ECS 140A Programming Languages

Midterm

2. Subtyping (15 points)


This question deals with the concepts of subtyping. For this problem, assume that the following
classes and their subclasses are defined:
public class A {
public void ma1(F f) {}
}

public class D extends B {


public void md1(B b) {}
}

public class B extends A {


public void mb1(E e) {}
}

public class E extends C {


public void me1(A a) {}
}

public class C extends B {


public void mc1(C c) {}
}

public class F extends E {


public void mf1(C c) {}
}

(a) (5 points) Draw the inheritance graph for the above classes.

(b) (10 points) Assume that the following variables are defined: A a; B b; C c; D d; E e; F f.
For each of the following statements write Y if the statement is type-correct or N if
the statement is not type correct:
Statement
b = d
f = c
e.me1(e)
f.mc1(a)
f.me1(f)
a.ma1(f)
b.mb1(f)
c.mc1(d)
a = e = f
d.md1(e)

Spring 2007

Is it type correct?

5 of 10

ECS 140A Programming Languages

Midterm

3. Dynamic Dispatch (12 points) The program below shows both the class definitions and the
main program that uses the class definitions. Show the output of running the main program.
public interface I {
public void m4(I i);
public void m5();
}
public abstract class A {
public void m1()
{
System.out.println("A m1");
m2();
}
public abstract void m2();
}
public class B extends A {
public static void m3()
{
System.out.println("B m3");
}
public void m2() {
System.out.println("B m2");
m3();
}
}
public class C extends A implements I{
public void m2() {
System.out.println("C m2");
}
public void m4(I i) {
System.out.println("C m4");
i.m5();
}
public void m5() {
System.out.println("C m5");
}
}

Statement 1

Spring 2007

Statement 2

Statement 3

public class D extends C implements I {


public void m2() {
System.out.println("D m2");
super.m2();
}
public void m4(I i) {
System.out.println("D m4");
i.m5();
}
public void m5() {
System.out.println("D m5");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
C c = new C();
D d = new D();
b.m1(); //Statement 1
d.m1(); //Statement 2
c.m4(c); //Statement 3
d.m4(c); //Statement 4
I i = c;
c = d;
i.m4(c); //Statement 5
A a = c;
a.m1(); //Statement 6
}
}

Statement 4

Statement 5

Statement 6

6 of 10

ECS 140A Programming Languages

Midterm

4. Scoping (10 points) This question tests your understanding of the difference between static and
dynamic scoping. Consider the following program (using Cs syntax):
#include <stdio.h>
int x = 1;
char c = e;
int h(int d)
{
return x*d;
}
void g(){
printf("%d,%c\n",x,c);
}
void f(){
printf("%d,%c\n",x,c);
char c = r;
g();
x = h(x);
g();
}
int main (int argc, char * const argv[]) {
int x = 6;
f();
printf("%d,%c\n",x,c);
return 0;
}
(a) (5 points) What does the program print under static scoping?

(b) (5 points) What does the program print under dynamic scoping?

Spring 2007

7 of 10

ECS 140A Programming Languages

Midterm

5. Object Layout (10 points)


This problem concerns the implementation of inheritance and dynamic binding. Assume the
following classes are defined:
class A {
int i;
void f();
static int m();
String p();
int g();
}
class B extends A {
String s;
int j;
int g();
void h();
static void n();
}
class C extends B {
int k;
void f();
void h();
}
Show the object layout for an object of C. In your layout, you must also show the virtual method
table for C.

Spring 2007

8 of 10

ECS 140A Programming Languages

Midterm

6. Miscellaneous Questions on Types and Subtyping (23 points)


(a) (6 points) In class, we have discussed the difference between subtyping and inheritance.
Give example code to illustrate the notion subtyping without inheritance and another
example code to illustrate inheritance without subtyping (Hint: think about C++).

(b) (9 points) Java allows covariant array subtyping: if A is a subtype of B, then A[] is a
subtype of B[]. Use a concrete example to illustrate the potential problem. How does
Java address this problem? Can you suggest another way to fix the problem? Explain your
answers.

Spring 2007

9 of 10

ECS 140A Programming Languages

Midterm

(c) (8 points) Show how to find the type of the function f given below using type inference:
fun f(x,y) = y(y(x))
Show the graph for the code and steps to derive fs type.

Spring 2007

10 of 10

Anda mungkin juga menyukai