






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The cs415 midterm exam from spring 2002 at anderson university. The exam covers various topics related to compilers and interpreters, including fortran design, algol 60, compilation phases, advantages and disadvantages of interpretation and compilation, top-down leftmost derivation, static and dynamic scoping, binding, memory management, and first-class citizens in programming languages. Students are expected to answer questions related to these topics.
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!







This exam is closed note, closed book. Good Luck!
Fortran (10 points)
Common Blocks allowed subroutines to share access to the same block of memory. In this way values stored in that memory were shared between subroutines.
A drawback to the use of common blocks was that although each subroutine explicitly โimportedโ the common block, each subroutine was allowed to create its own names for that memory. This could cause aliases, as in the case below where the first location in /stuff/ is referred to as the int I in subroutine A, but as the real values(1) in subroutine B.
subroutine A(n) common /stuff/ i, values(100), j
subroutine B(n) common /stuff/ values(100), i, j
Another way of sharing variables is by passing parameters, as could be done using the parameter n and m above โ the same value could be passed to each subroutine.
Fortran was based on the assembly language of the IBM 704. As a result, many of the instructions in early Fortran were merely another way of saying a particular assembly instruction โ rather than a language design focused on appropriate constructs for expressing algorithms. One of the main constraints was that a compiler for the language had to produce code that would be competitive with hand-coded assembly programs. Other constraints include small memory sizes at the time, which lead to space-saving techniques that re-used memory such as common blocks and equivalences.
Success โ still used today, served as a proof of concept for other high level languages and compilers, with all its faults it was still an improvement over writing assembly code directly.
Algol 60 (10 points)
Backus-Naur Form was originally introduced in the Algol 60 Report. It is used to specify language syntax.
Advantages of Interpretation: more flexible, better error messages, good for rapid response (no compilation)
Advantages of Compilation: better performance
regular expressions! lex! scanner
context free grammar! yacc! parser
a = b * (c + a)
(For partial credit do any sort of derivation.) Write each step in the derivation on a separate line. Be careful not to skip any steps!
S! ID = E ID! a | b | c E! E + E | E * E | ( E) | ID
! a = E ! a = E * E ! a = ID * E ! a = b * E ! a = b * (E) ! a = b * (E + E) ! a = b * (ID + E) ! a = b * (c + E) ! a = b * (c + ID) ! a = b * (c + a)
Yes. An expression such as: a + b * c can generate two different parse trees:
/ \ /
a * + c / \ /
b c a b
An association between two things, such as a name and the thing (function, value) it names.
Early binding generally leads to greater efficiency (compilation approach)
Late binding general leads to greater flexibility (interpreter approach)
Object *ptr = new Object; // line 1 ptr = new Object; // line 2
Now the object created on line 1 is garbage or a memory leak. The object lives on, but we no longer have a binding to that object (assuming there are no other pointers to it).
parameters, local variables, return address, saved registers
Java, Scheme, Lisp, Python, Modula-3, ML
An alias is an extra name for something that already has a name in the current scope. โTwo names for the same object.โ
Example: Fortran common blocks and equivalences can create aliases. In C++, passing a reference to an object to a subroutine that already references that object directly. (see p. 126 in Scott)
Aliases make a program more confusing and can prevent inhibit optimizations.
A symbol table maps each identifier to the information that is known about that identifier, such as its type, internal structure, scope, memory address. It is used in semantic analysis and in the debugger. It can be used to tell if an identifier is declared before it is used, to check whether something is a subroutine or a variable, check that the correct type and number of parameters is being passed, etc.
Example: Index Symbol Type 1 Circle type 2 A 1 3 foo subroutine 4 B integer
Functional programming (30 points)
A first class citizen can be passed as a parameter, returned from a subroutine, or assigned into a variable. Integers are first class in most programming languages. Functions are first class in functional languages
Normal Order โ evaluate arguments only when needed (sometimes this means you never have to evaluate them)
Applicative Order โ evaluate arguments before passing them to function
Lazy evaluation is a way of implementing normal order where you memo-ize results โ keep them around if you compute them so you do not have to compute them over again.
a function that takes a function as a parameter or returns a function as a result.
(define a 6) (define b โ(3 14 27)) (define c (cons a (cdr b)))
What is the result of typing the following into the Scheme interpreter:
c => ??? ( 6 14 27 )
(car (cdr c)) => ??? 14
Example: (nth โ(4 5 6) 1) => 4
(define (nth alist n) (if (= 1 n) (car alist) (nth (cdr alist) (- n 1) ) ) )