• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 4 out of 266 pages
Exam (elaborations)

Programming Language Pragmatics (5th Ed.) — Solutions Manual (PDF) by Scott & Aldrich

Document preview thumbnail
Preview 4 out of 266 pages

Get the complete Solutions Manual for Programming Language Pragmatics, 5th Edition by Michael L. Scott and Jonathan Aldrich. Includes fully worked answers for all 16 chapters covering lexical/syntax analysis and parsing (LL/LR), semantics, names/scopes/binding, type systems and type checking, data/control abstractions, functional and object-oriented paradigms, logic and concurrent programming, memory management and garbage collection, runtime systems/virtual machines, intermediate representations, code generation, exceptions, modules, metaprogramming, and language design trade-offs—perfect for coursework, exam prep, and instructor reference. programming language pragmatics solutions, Scott PLP 5th edition, language semantics, parsing LL LR, compiler design, interpreters, type systems, static typing, dynamic typing, scoping and binding, data abstractions, control flow, functional programming, object-oriented programming, concurrency threads, synchronization locks, memory management, garbage collection, runtime systems, intermediate representation, code generation, lambda calculus

Content preview

ALL 16 CHAPTERS COVERED




SOLUTIONS MANUAL

, 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 de-
tected 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


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 program-
mers 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

Document information

Uploaded on
October 19, 2025
Number of pages
266
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$18.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
TestBanksStuvia
3.9
(331)
Sold
3252
Followers
1210
Items
2232
Last sold
4 hours ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions