Docsity

22 Questions with Solutions - Programming Languages | CS 4610, Exams of Programming Languages

Toggle sidebar
bg1
CS415 Anderson – Spring 2005 Page 1 of 9
CS 415 Midterm Exam – Spring 2005 -
SOLUTION
Name _____________________________________________________
Email Address _________________ Student ID # ___________________
Pledge:
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!
Score
Fortran & Algol 60
Compilation
Names, Bindings, Scope
Functional Programming
Total /94
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download 22 Questions with Solutions - Programming Languages | CS 4610 and more Exams Programming Languages in PDF only on Docsity!

CS 415 Midterm Exam – Spring 2005 -

SOLUTION

Name _____________________________________________________

Email Address _________________ Student ID # ___________________

Pledge:

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!

Score

Fortran & Algol 60

Compilation

Names, Bindings, Scope

Functional Programming

Total /

Fortran and Algol 60 (28 points)

  1. [2 points] Early Fortran had recursion. TRUE or FALSE (circle one)
  2. [4 points] What effect did your answer to question #1 have on the implementation of Fortran?

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.

  1. [4 points] a) Describe the scoping rules in Fortran.

global scope and for variables local to a subroutine, local scope.

b) Fortran had: static scoping or dynamic scoping (circle one)

  1. [8 points] a) Describe how call by name worked.

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]

  1. [6 points] Describe what happens during the scanning phase of a compiler. (what is the input, what is the output, what happens inside).

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.

  1. [2 points] What is the principle of longest match? Give an example?

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.

  1. [6 points] Why are compilers often separated into a front end and a back end?

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.

  1. [2 points] A pushdown automata is to a _ context free grammar _ as a finite automata is to a regular expression.
  1. [4 points] Write a regular expression for an identifier in the cs415 language. Identifiers must start with cs415, followed by one or more upper or lowercase letters. The c and the s in cs415 can be either upper or lowercase. Examples of valid identifiers include: “CS415rocks” and “cS415isEARLY”.

[Cc][Ss]415[A-Za-z] +

  1. [4 points] Draw a finite automaton for the regular expression you gave in the previous question above. Be sure to properly denote the accepting states.
  1. [2 poinst] Define: referencing environment

The set of bindings in effect at a given point in a program.

  1. [2 points] Define: scope

The textual region of the program where a binding is active.

  1. [4 points] What is the static link? What is it used for?

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.

  1. [4 points] In some languages, the user manages the heap. Why is this potentially dangerous?

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)

  1. [4 points] a) What is a first class citizen in a programming language?

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.

  1. [4 points] What is programming in a “purely functional style”?

Programming without side effect, using only the composition of functions to accomplish things.

  1. [2 points] What is the result of the following in Scheme:

(map (lambda (x) (+ x 50)) ‘(1 2 3 4))

(51 52 53 54)

  1. [4 points] Assuming that the following definitions are executed in this order:

(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)