Exercises 2

Compilers - Exercises II (1 - 4), Jan 29, 2014
When necessary, use available online web sources for answering the following questions.
1. (a) What is meant by metaprogramming? What kind of language feature is reflection? Is it
related to metaprogramming?
(b) What is a just-in-time (JIT) compiler? What are the potential advantages or disadvantages
of JIT compilation? How does JIT relate to dynamic compilation?
(c) Explore in detail the distinctions between interpretation and compilation? What are the
comparative advantages and disadvantages of the two approaches? What implementation
strategy was used by the original implementation of the Pascal programming language? Why?
Is Java (currently) "compiled" or "interpreted" (or what)? How about C#?
2. Answer the following questions concerning the architecture of language-processing systems.
a. List the principal phases of a compilation, and describe the work performed by each.
What distinguishes the front end of a compiler from the back end?
b. What is the difference between a phase and a pass of compilation? What is meant by a
"broad" compiler vs. a "narrow" compiler? Under what circumstances is it possible and
beneficial to use one single pass in a compiler? Under what circumstances does it make
sense for a compiler to have multiple passes? Consider the analysis phases of a compiler.
Should we implement lexical analysis (scanning) as a separate pass? Why/why not?
Should we implement syntactical analysis (parsing) as a separate pass? Why/why not?
3. Errors in a computer program can be classified according to when they are detected and, if
they are detected at compile time, what part of the compiler detects them. Using your favorite
programming language, give an example of the following errors. You may want to test your
suggestions for different kinds of errors with the compiler. Find out how it actually identifies
or perceives erroneous input and what kind of messages it produces.
(a) A lexical error, detected by the scanner recognizing the lexical tokens of the language.
(b) A syntax error, detected by the parser recognizing the parse tree for a program.
(c) A static semantic error (i.e., a contextual constraint) detected during the semantic analysis
of a program.
(d) A dynamic (semantic) error, detected at run time by code generated by the compiler.
(e) An error in the correctness of a program that the compiler can neither catch nor easily
generate code to catch. Depending on the programming language, the error may be or it
may not be a violation of the language definition.
4. (a) Compilers have been often written in the language they implement. Identify advantages and
disadvantages of this technique. Explain what is bootstrapping. Why is it a useful technique
for compiler writing?
(b) Assume you have the following Pascal language kit:
- a Pascal compiler, written in Pascal, that would generate output in P-code (a simple stackbased virtual machine language),
- the same Pascal compiler that generates P-code, already itself translated into a P-code
program (this implementation uses only half of the available P-code instructions), and
- a P-code interpreter, written in Pascal.
Assume that you don't yet have Pascal in a locally available machine, say IA32. Explain how
you would use these language processors to generate and install the language processor tools
listed below as (1) - (3). Note that all the required language processors may not be available,
and so you may have to work on new implementations or modifications (of existing programs).
Also remember that an interpreter alone can’t translate anything – it can only execute
programs. And pay close attention to what you already have and what you don’t have..
(1) a P-code interpreter that runs in the local machine,
(2) a Pascal compiler, in P-code, that generates local machine language, and
(3) a Pascal compiler, in machine language, that generates local machine language.
(c) Now assume that you already have a working Pascal compiler on another available
machine, producing machine code for its alpha processor. Show how you can use the Pascal
language kit and the already running Pascal-to-alpha compiler to port Pascal to a new
machine that uses, say, the SPARC V9 assembly code. Hint: consider applying so-called cross
compilation (look up the technique in available sources).