Skip to main content

Solution Manual for Programming Language Pragmatics 4th Edition by Michael L. Scott

Page 1

Solution Manual for Programming Language Pragmatics 4th Edition by Michael L. Scott Chapter 1-16

1

Chapter 1

Introduction

1.9 Solutions Manual This manual contains suggested solutions to many of the PLP exercises. It is provided only to instructors who have adopted the text in their course.1

1.1

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 imperative language, give an example of each of the following.

(a) A lexical error, detected by the scanner (b) A syntax error, detected by the parser (c) A static semantic error, detected by semantic analysis (d) A dynamic semantic error, detected by code generated by the compiler (e) An error that the compiler can neither catch nor easily generate code to catch (this should be a violation of the language definition, not just a program bug) Answer:

There are many possible answers to this question. Here are possibilities in C:

Lexical error: ‘@’ sign outside of a string or comment Syntax error: mismatched parentheses in an arithmetic expression Static semantic error: use of an identifier that was never declared Dynamic semantic error: divide by zero Error that can’t reasonably be caught: failure to reclaim dynamically allocated objects that are no longer needed (“memory leak”)

1 Questions © 2015, Morgan Kaufmann Publishers, Inc.; solutions © 2015, Michael L. Scott. This material may not be copied or distributed without written permission of the publisher and author. S 1.1


S 1.2

Solutions Manual

1.2

Consider again the Pascal tool set distributed by Niklaus Wirth (Example 1.15). After successfully building a machine language version of the Pascal compiler, one could in principle discard the P-code interpreter and the P-code version of the compiler. Why might one choose not to do so? Answer: One obvious answer is that if the machine language version of the compiler were lost, or if one upgraded to a new machine architecture, one might need the tools to rebuild the compiler. Even if the compiler remains perfectly usable, however, it may be worthwhile to keep the tools around. The P-code version of a program tends to be significantly smaller than its machine language counterpart. On a circa 1970 machine, the savings in memory and disk requirements could really be important. Moreover, as noted in Section 1.4, an interpreter will often provide better run-time diagnostics than will the output of a compiler; these can be particularly valuable during program development. Finally, an interpreter allows a program to be rerun immediately after modification, without waiting for recompilation and linking—a feature that can also be handy for development. Some of the best programming environments for imperative languages (including most implementations of Java) include both a compiler and an interpreter.

1.3

Imperative languages like Fortran and C are typically compiled, while scripting languages, in which many issues cannot be settled until run time, are typically interpreted. Is interpretation simply what one “has to do” when compilation is infeasible, or are there actually some advantages to interpreting a language, even when a compiler is available? Answer: Compiled code usually runs significantly faster than interpreted code, so programmers interested in performance tend to demand compilers. Compilation also catches some errors earlier, when they are easier to fix, rather than waiting to detect them until the program actually runs. Interpretation definitely has its advantages, however; it is not just a last resort. It is very handy during development because it allows a newly modified program to be executed without waiting for potentially time-consuming compilation and linking steps. It may be desirable on small machines because it takes less space, both in memory when running and on disk (because there are no executables). Interpretation facilitates higher quality error messages, because the interpreter has access to the full program source. It is much easier to bootstrap or port an interpreter than a compiler, so experimental language implementations are very often based on interpreters.

1.4

The gcd program of Example 1.20 might also be written int main() { int i = getint(), j = getint(); while (i != j) { if (i > j) i = i % j; else j = j % i; } putint(i); }

Does this program compute the same result? If not, can you fix it? Under what circumstances would you expect one or the other to be faster?


1.9 Solutions Manual

S 1.3

Answer:

The difference between the two programs is in the two assignment statements: i := i - j and j := j - i, versus i := i % j and j := j % i. Suppose i > j. Then i % j == i - (j * (i / j)), where the slash (/) indicates integer division. The computation i % j

therefore comes close to accomplishing in one iteration of the loop what would happen over the course of (i / j) iterations of the original loop. The exception arises in the case where i is a multiple of j. In this case modular division produces a zero, after which the program aborts with a divide-by-zero error. One possible fix capitalizes on the observation that (for positive numbers) i % j is always smaller than i: int main() { int large = getint(), small = getint(); if (large < small) { int temp = small; small = large; large = temp; } while (small != 0) { int temp = small; small = large % small; large = temp; } putint(i); }

If we observe that when i < j, i % j = i, then we can also employ the following simpler program, at the expense of one useless extra division when i is initially smaller than j: int main () { int i, j, t; i = getint(); j = getint(); while (i != 0) { t = i; i = j % i; j = t; } putint(j); }

If i and j are about the same magnitude, the original (subtraction-based) program may be faster, because subtraction is faster than division on many machines. If i and j are of different magnitude, the %-based version is likely to be faster.

1.5

Expanding on Example 1.25, trace an interpretation of the gcd program on the inputs 12 and 8. Which syntax tree nodes are visited, in which order? Answer: program := call (3)

call getint


S 1.4

Solutions Manual

(return) (return) (5)

(return)

assign 12 to i

:= call (3)

call getint

(return) (return) (6)

(return)

assign 8 to j

while

/= (5)

(return) (6)

(return) (return)

true

if > (5)

(return) (6)

(return) (return) :=

true “then” branch

− (5)

(return) (6)

(return) (return) (return) (return) / =

assign 4 to i back to while node

(5)

(return) (6)

(return) (return)

true

if > (5)

(return) (6)

(return) (return) :=

−

false “else” branch


1.9 Solutions Manual

S 1.5

(6)

(return) (5)

(return) (return) (return) (return)

assign 4 to j back to while node

/= (5)

(return) (6)

(return) (return)

false

call (5)

(return)

1.6

(4)

call putint(4)

(return) (return) (return) (return) (return)

back to program node

Both interpretation and code generation can be performed by traversal of a syntax tree. Compare these two kinds of traversals. In what ways are they similar/different? Answer: Both kinds of traversal are most easily written as a set of mutually recursive subroutines, with (to first approximation) one routine for each kind of syntax tree node. The code generation traversal will tend to visit each node once, to generate corresponding target code. The interpretation traversal may never visit certain nodes (if their part of the program isn’t needed in this particular execution); other nodes it may visit many times (e.g., if they are part of a loop body or a subroutine that is called repeatedly).

1.7

In your local implementation of C, what is the limit on the size of integers? What happens in the event of arithmetic overflow? What are the implications of size limits on the portability of programs from one machine/compiler to another? How do the answers to these questions differ for Java? For Ada? For Pascal? For Scheme? (You may need to find a manual.) Answer: The answer here is of course implementation-dependent. Using gcc on a 3231 31 bit x86/Linux system, an int is 32 bits long, and has values from −2 . . . 2 − 1. Arithmetic overflow happens silently—numbers wrap around. Size limits significantly impact the portability of C programs. There are systems in which an int is 16 bits, or 36, or 64. The language definition requires at least 16 bits. Java is uniform across platforms: an int is always 32 bits, signed two’s complement. Ada allows considerable variation across platforms, but imposes a variety of constraints on 15 range and behavior. Among other things, the built-in Integer type must span at least −2 + 15 1 . . . 2 − 1.


Turn static files into dynamic content formats.

Create a flipbook