





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to each question. Good Luck!
Fortran and Algol 60 (28 points)
No recursion meant that there would only be one instance of a subroutine/function active at a time. So local variables could be stored in a fixed location – did not need to use the stack to allocate local variables.
global scope and for variables local to a subroutine, local scope.
b) Fortran had: static scoping or dynamic scoping (circle one)
foo(int x) { print x i = i + print x }
int i = 0; call foo (A[i])
Every time the formal parameter x is accessed inside of foo, the actual parameter, in this case A[i], is re-evaluated. In this example first A[0] will be printed, then A[1]. One way of thinking of this is as if the body of the subroutine foo was pasted into the call site with all the occurrences of x replaced by A[i].
b) Give the name of AND describe the technique used to implement call by name.
Call by name was not implemented with the cut and paste method described above, instead a thunk – an anonymous procedure, was used. When the parameter was needed inside of a procedure, the thunk was called to get the correct address.
Compilation [24 points]
The scanning phase (lexical analysis) is the first phase of compilation. A stream of characters is provided as input, and a stream of tokens is produced as output, often a token at a time as the parser requests a token. The scanner attempts to recognize the characters passed in according to the regular expressions used to specify the tokens in the language. Other tasks performed during scanning include the removal of whitespace or comments and the recognition of keywords.
When scanning, recognize the longest sequence of characters that can be recognized as a token. For example, when seeing != in the code, the scanner should recognize this as a NOTEQUAL token as opposed to a NOT token followed by an EQUAL token.
The front end is generally thought of as machine-independent. The back end is thought of as containing features that apply to a particular hardware platform. Thus by separating a compiler into a front end and a back end, you can retarget your compiler to a new platform by merely re-writing the back end. Similarly you could write a front end for a new language and have it use an already existing back end for a particular platform.
In general what is the purpose of the front end?
Take a stream of tokens and convert it to an intermediate format (IR). The front end does analysis, attempting to understand the structure and meaning of the program. Generally scanning, parsing, and semantic analysis/IR generation/machine independent optimization are considered part of the front end.
In general what is the purpose of the back end?
Take a program in intermediate format and convert it into the target language (often this is the assembly language of a particular machine). Machine specific optimization and code generation are considered part of the back end.
[Cc][Ss]415[A-Za-z] +
The set of bindings in effect at a given point in a program.
The textual region of the program where a binding is active.
The static link is a pointer to the stack frame of the lexically enclosing scope. It is used for static scoping to allow the compiler to generate code that can find a variable declared in a lexically enclosing scope.
For example in C++ the user manages the heap by explicitly requesting memory from the heap via new and releasing it by delete. Two common problems that occur are:
dangling pointers: the user releases memory back to the heap “too early” or rather while there are still references to that memory in their program that may be accessed in the future.
memory leaks: when the user neglects to return memory to the heap when it is no longer needed, may cause you to eventually run out of memory on the heap.
Functional programming (24 points)
Something that can be passed to or returned from a function and also have a value assigned into it.
b) Give an example of a first class citizen in scheme.
int, and real are first class citizens in Scheme and many other programming languages. Functions are also first class citizens in Scheme.
Programming without side effect, using only the composition of functions to accomplish things.
(map (lambda (x) (+ x 50)) ‘(1 2 3 4))
(51 52 53 54)
(define x ‘(3 28 400)) (define y (cons (cdr x) ‘(6 15 77)))
What is the result of typing the following into the Scheme interpreter:
y => ??? ((28 400) 6 15 77)
(cons ‘x (cdr (cdr x))) => ??? (x 400)