forked from fool2fish/dragon-book-exercise-answers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fool2fish#31 from wjk20120522/master
Add partial answers of ch01
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Exercises for Section 1.1 | ||
|
||
### 1.1.1 | ||
|
||
What is the difference between a compiler and an interpreter? | ||
|
||
#### Answer | ||
|
||
A compiler is a program that can read a program in one language - the source language - and translate it into an equivalent program in another language – the target language and report any errors in the source program that it detects during the translation process. | ||
|
||
Interpreter directly executes the operations specified in the source program on inputs supplied by the user. | ||
|
||
### 1.1.2 | ||
|
||
What are the advantages of: | ||
(a) a compiler over an interpreter | ||
(b) an interpreter over a compiler? | ||
|
||
#### Answer | ||
|
||
a. The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs. | ||
|
||
b. An interpreter can usually give better error diagnostics than a compiler, because it executes the source program statement by statement. | ||
|
||
### 1.1.3 | ||
|
||
What advantages are there to a language-processing system in which the compiler | ||
produces assembly language rather than machine language? | ||
|
||
#### Answer | ||
|
||
The compiler may produce an assembly-language program as its output, because | ||
assembly language is easier to produce as output and is easier to debug. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Exercises for Section 1.6 | ||
|
||
### 1.6.1 | ||
|
||
For the block-structured C code of below , indicate the values assigned to w, x, y, and z . | ||
``` | ||
int w, x, y, z; | ||
int i = 4; int j = 5; | ||
{ | ||
int j = 7; | ||
i = 6; | ||
w = i + j; | ||
} | ||
x = i + j; | ||
{ | ||
int i = 8; | ||
y = i + j; | ||
} | ||
z = i + j; | ||
``` | ||
|
||
#### Answer | ||
|
||
w = 13, x = 11, y = 13, z = 11. | ||
|
||
|
||
### 1.6.2 | ||
|
||
Repeat Exercise 1 .6.1 for the code of below . | ||
``` | ||
int w, x, y, z; | ||
int i = 3; int j = 4; | ||
{ | ||
int i = 5; | ||
w = i + j; | ||
} | ||
x = i + j; | ||
{ | ||
int j = 6; | ||
i = 7; | ||
y = i + j; | ||
} | ||
z = i + j; | ||
``` | ||
|
||
#### Answer | ||
|
||
w = 9, x = 7, y = 13, z = 11. | ||
|
||
|
||
### 1.6.3 | ||
|
||
For the block-structured code of Fig. 1 . 14, assuming the usual static scoping of declarations, give the scope for each of the twelve declarations. | ||
|
||
#### Answer | ||
``` | ||
Block B1: | ||
declarations: -> scope | ||
w B1-B3-B4 | ||
x B1-B2-B4 | ||
y B1-B5 | ||
z B1-B2-B5 | ||
Block B2: | ||
declarations: -> scope | ||
x B2-B3 | ||
z B2 | ||
Block B3: | ||
declarations: -> scope | ||
w B3 | ||
x B3 | ||
Block B4: | ||
declarations: -> scope | ||
w B4 | ||
x B4 | ||
Block B5: | ||
declarations: -> scope | ||
y B5 | ||
z B5 | ||
``` | ||
|
||
### 1.6.4 | ||
|
||
What is printed by the fo llowing C code? | ||
``` | ||
#define a (x + 1) | ||
int x = 2; | ||
void b() { x = a; printf("%d\n", x); } | ||
void c() { int x = 1; printf("%d\n", a); } | ||
void main () { b(); c(); } | ||
``` | ||
|
||
#### Answer | ||
3 | ||
|
||
2 |