NOTE: The midterm exam is on Thursday, March 20.
The exam will cover the following material.
- From the textbook Programming Language Pragmatics.
Note: At the end of each section in the textbook, be sure to look at the "Check Your Understanding" questions.
- Chapter 1, Section 1.4 (pages 13-20) and Section 1.6 (pages 22-31),
- Chapter 2, Section 2.1 (pages 37-46), Sections 2.3 and 2.3.1 (pages 61-70),
- Chapter 3, Sections 3.1-3.3 (pages 103-134) and Sections 3.5-3.6 (pages 136-149).
- part of Section 6.6 from page 287 to page 291,
- Chapter 10, Section 10.1-10.3 (pages 523-539).
- From the online Scheme textbook The Scheme Programming Language
From the class notes, be sure to study:
- Problem 1:
- For each of the following errors, determine if the error is a syntactic error, a static semantic error, or a dynamic semantic error. Explain each of your answers.
(a) A function defined to have two formal parameters is called with only one actual parameter.
(b) An array named A is declared to have 100 entries, numbered from 0 to 99, and elsewhere in the code there is the expression A[100] .
(c) The identifier f is declared to be the name of a function. Elsewhere in the code there is the expression f+100 .
(d) The identifier g is declared to be the name of a function. Elsewhere in the code there is the expression g(0] .
- Problem 2:
- For each of the following expressions, rewrite the expression in both prefix and postfix notation, and draw an abstract syntax tree (i.e., an expression tree) for the expression.
(a) a * b + c
(b) a * (b + c)
(c) a * b + c * d
(d) a * (b + c) * d
- Problem 3:
- Describe in English the language defined by the following grammar. (Note: Do not use English to describe the grammar, describe the language defined by the grammar.)
<S> -> <A> <B> <C>
<A> -> 'a' <A> | 'a'
<B> -> 'b' <B> | 'b'
<C> -> 'c' <C> | 'c'
- Problem 4:
- Consider the following grammar.
<S> -> <A> 'a' <B> 'b'
<A> -> <A> 'b' | 'b'
<B> -> 'a' <B> | 'a'
Which of the following words are in the language generated by this grammar?
(a) baab
(b) bbbab
(c) bbaaaaa
(d) bbaab
- Problem 5:
- Consider the following grammar.
<S> -> 'a' <S> 'c' <B> | <A> | 'b'
<A> -> 'c' <A> | 'c'
<B> -> <A> | 'd'
Which of the following words are in the language generated by this grammar?
(a) abcd
(b) acccbd
(c) acccbcc
(d) acd
(e) accc
- Problem 6:
- For each of the following parts, define a grammar for the words, over the alphabet containing just 0 and 1, that are described.
(a) All words ending with a 1.
(b) All words of length exactly 8.
(c) All words that contain the string 0101.
(d) All words with odd length. Show how to parse the word 00100 using your grammar.
- Problem 7:
- The following grammar is motivated by declarations in C.
Declaration ::= Type Declarator ';'
Type ::= 'int' | 'char'
Declarator ::= '*' Declarator
| Declarator '[' Digit ']'
| Declarator '(' Type ')'
| '(' Declarator ')'
| Letter
Letter ::= 'a' | 'b' | ... | 'y' | 'z'
Digit ::= '1' | '2' | ... | '8' | '9'
(a) Give 5 examples of strings generated by this grammar.
(b) Show that this grammar is ambiguous.
- Problem 8:
- The following C function computes the number of decimal digits in a positive integer.
int numdigits(int x)
{ int t = x, n = 1;
while (t >= 10)
{ n++;
t = t/10;
}
return n;
}
(a) Implement this function in Scheme using a tail recursive function.
(b) Modify your function from part (a) so that it works with negative integers.
- Problem 9:
- In Scheme, implement
(invert lst) where lst is a list of 2-lists (lists of length 2) and the return value is a list with each 2-list reversed. So
(invert '((a 1) (b 2) (c 3) (d 4))) ==> ((1 a) (2 b) (3 c) (4 d))
- Problem 10:
- The following function implements a simple mathematical function that might come up in any algebra or calculus course. Write this mathematical function using standard mathematical notation. Explain in detail how this Scheme function implements the mathematical function.
(define f
(lambda (x y)
((if (> y 0) + -) x y)))
- Problem 11:
- Each of the following is a syntactic form. For each of these forms, explain how the evaluation of the form differs from the rule for evaluating a function application. In other words, give the evaluation rule for each of these forms.
(a) (if exp1 exp2 exp3)
(b) (and exp1 exp2)
(c) (define var exp)
- Problem 12:
- Is the following function tail recursive? Explain your answer. Also, explain what the function does.
(define f
(lambda (n)
(if (= n 0) 0 (+ 1 (f (- n 1))))))
- Problem 13:
- In each of the following expressions, draw an arrow from each variable reference to its associated declaration as a formal parameter.
(lambda (x)
(lambda (y)
((lambda (x)
(x y))
x)))
(lambda (z)
((lambda (a b c)
(a (lambda (a) (+ a c)) b))
(lambda (f x)
(f (z x)))))
- Problem 14:
- If two procedures
f and g are both procedures of a single argument such that the values produced by g are legal arguments to f , then the composition of f and g is defined to be the procedure that first applies g to its argument and then applies f to the result. Write a procedure called compose that takes any two one-argument procedures and returns the procedure that is their composition. For example,
((compose sqrt abs) -4) ==> 2
- Problem 15:
- Consider the following definitions.
(define make-scaled
(lambda (scale f)
(lambda (x)
(* scale (f x)))))
(define add-one
(lambda (x)
(+ 1 x)))
(define mystery
(make-scaled 3 add-one))
For each of the following questions, be sure to explain how you arrived at your answer.
(a) What is the value of (mystery 4) ?
(b) What is the value of
((make-scaled 2 (make-scaled 3 add-one)) 4)
- Problem 16:
- In the following C program there are two assignments to the entity
**x . For each of these assignments, draw a picture that would help someone visualize the state, at that point in the program, of all of the variables and their memory locations being used by this program. Also, what does this program print?
#include <stdio.h>
int main()
{ int **x;
int *y;
int z;
x = (int**) malloc(sizeof(int*));
y = (int*) malloc(sizeof(int));
z = 1;
*y = 2;
*x = y;
**x = z;
printf("%d\n", *y);
z = 3;
printf("%d\n", *y);
**x = 4;
printf("%d\n", z);
return 0;
}
- Problem 17:
- The following program prints two integer numbers; the first number typically is garbage (explain why) but the second number might be 2. Give a detailed explanation of why the second number could be 2. The second number could also be garbage (but that is much less likely). Give an explanation of why the second number could be garbage (Hint: The activation record need not be on a stack).
#include <stdio.h>
void p()
{ int y;
printf("%d\n", y);
y = 2;
}
int main()
{ p();
p();
return 0;
}
- Problem 18:
- The following function prints two integer numbers; the first number typically is garbage (explain why) but the second number might be 2 or it could also be garbage. Give a detailed explanation of why the second number could be 2. Give a detailed explanation of why the second number could be garbage. (Note: To get the two possibilities, you need to imagine that the language isn't C (why?).)
int main()
{
{ int x;
printf("%d\n", x);
x = 2;
}
{ int y;
printf("%d\n", y);
}
return 0;
}
- Problem 19:
- Consider the following outline of a Pascal program.
program main;
var x, y, z : integer;
procedure sub1;
var a, y, z : integer;
procedure sub2;
var a, b, z : integer;
begin {sub2}
{ some code }
end; {sub2}
begin {sub1}
{ some code }
end; {sub1}
procedure sub3;
var a, x, w : integer;
begin {sub3}
{ some code }
end; {sub3}
begin {main}
{ some code }
end. {main}
For each of the procedure bodies sub1 , sub2 , and sub3 , list all of the variables that are visible in the body and specify where each visible variable was declared. Assume static scoping.
- Problem 20:
- Consider the following outline of a program written in a block structured language that uses dynamic scoping.
program main;
var x, y, z;
procedure sub1;
var a, y, z;
begin
// some code
end;
procedure sub2;
var a, b, z;
begin
// some code
end;
procedure sub3;
var a, x, w;
begin
// some code
end;
begin // main
// some code
end.
(a) Suppose that main calls sub1 , sub1 calls sub2 , and sub2 calls sub3 . List all of the variables that are visible in the activation of sub3 and specify where each visible variable was declared.
(b) Suppose that main calls sub2 , sub2 calls sub3 , and sub3 calls sub1 . List all of the variables that are visible in the activation of sub1 and specify where each visible variable was declared.
(c) Suppose that main calls sub3 and sub3 calls sub1 . List all of the variables that are visible in the activation of sub1 and specify where each visible variable was declared.
- Problem 21:
- Suppose that we have functions nested in the following manner.
f1
/ | \
/ | \
f2 f3 f4
/ \ |
/ \ |
f5 f6 f7
| / | \
| / | \
f8 f9 f10 f11
|
|
f12
Sketch a picture of the linked list of activation records, including nesting links, when function f1 calls f2 which calls f5 and then function f5 calls f1 and passes to f1 a reference to f6 and then f1 calls f4 and passes to it its reference to f6, and then f4 calls f7 and passes to it the reference to f6, and then f7 calls f6.
|