Errata for Modern Compiler Design

Find an error?  Send it to galles@usfca.edu

Page
Error
30
The lexical analyzer should recognize the following keywords and symbols, in addition to integer literals and identifiers:
class do else false for if new return true while + - * / [ ] { } ( ) . , ; == != < > <= >= = && || !
29
Comment delminters should be // and not \\.  

\\ Single line comment!!
identifier

should be

// Single line comment
identifier

and

\\ Comment 1
\\ Comment 2
identifier

Should be

// Comment1
// Comment2
identifier
33
The while loop in Figure 2.21 is missing a statement.  Correction is below in red:

while (t.kind != sjavaConstants.EOF) {
   System.out.println("Token : " + t + " : ");
   System.out.println(sjavaConstants.tokenImage[t.kind]);
   t = tm.getNetToken();
}


54
Bottom of the page, replace
(2) S → C.  Add {} to First(S)
with
(2) S → Cf.  Add {} to First(S)

And, replace

(2) S → C. Add {f} to First(S)
with
(2) S → Cf.  Add {f} to First(S)
55
Top of the page, replace
(2)  S → C.  Add {f,g} to First(S) (no change)
with
(2) S → Cf.  Add {f,g} to First(S) (no change)
55
The two First set tables at the top of the page should have 'g' added to the First set of S', as follows:

Non-Terminal
First
S'
{e,f,g,h}
S
{e,f,g,h}
A
{e, ε}
B
{h}
C
{f, g}
D
{g}
56
In the algorithm to calculate Follow Sets of All Non-terminals in a Context-free Grammar G:
Step 3, substep ii should be (changes in red)

ii.  If the rule is of the form A → α A1 Β, where α and Β are (possibly empty) strongs of terminals and non-terminals, and First(Β) does contain ε, add all elements of First(Β) except ε to Follow(A1), and add all elements of Follow(A) to Follow(A1)
59
Figure 4.7.  Add S' to the set of Non-Terminals, Start symbol should be S'
In the Parse table, Row C, column ) should be C -> epsilon
86
Figure 5.2.  Replace num with id in the header of the second column
104
Figure 5.15.  Row 14, column = should be blank, instead of r(3)
125
Figure 6.12: the line:
class ASTIntegerLitera extends ASTExpression {
should be
class ASTIntegerLiteral extends ASTExpression {
229
At the bottom of the page, the Actual Assembly for MEM[FP-12] should be (changes in red):

sw   $FP,  0($ESP)     % Store frame pointer on the top of the expressin stack
addi $ESP, $ESP,  -4   % Update the expression stack pointer
addi $t1,  $zero, 12   % Load the constant value 12 into the register $t1
sw   $t1,  0($ESP)     % Store $t1 on the top of the expression stack
addi $ESP, $ESP,  -4   % update the expression stack pointer
lw   $t1,  8($ESP)     % load the first operand into temporary $t1
lw   $t2,  4($ESP)     % load the second operand into temparary $t2
sub  $t1,  $t1,    $t2 % do the subtraction, storing result in $t1
sw   $t1,  8($ESP)     % store the result on the expression stack
add  $ESP, $ESP,   4   % update the expression stack pointer
lw   $t1,  4($ESP)     % Pop the address to dereference off the top of
                       % the expression stack
lw   $t1, 0($t1)       % Dereference the pointer
sw   $t1, 4($ESP)      % Push the result back on the expression stack

230
The code on page 230 is correct, but the comments are misleading.  The corrected version follows (changes in red)
lw   $t1   4($ESP)    % Pop the second argument off the expression stack
sw   $t1   0($SP)     % Store second argument on the call stack
lw   $t1   8($ESP)    % Pop first argument off the expression stack
sw   $t1   -4($SP)    % Store first argument on the call stack
addi $SP,  $SP, -8    % Update call stack pointer
addi $ESP, $ESP, 8    % Update expression stack pointer
jal  foo              % Make the function call
addi $SP,  $SP,  8    % Update call stack pointer
sw   $result,0($ESP)  % Store result of function on expression stack
addi $ESP, $ESP, -4   % Update expression stack pointer



235
At the bottom of the page, the code for a Move register tile, replace

addi $rt, $ACC, 0

with

addi $r1, $ACC, 0
236
Arguements to functions and procedures should be pushed onto the call stack in reverse order, so that the first arguement is at location (FP+WORDSIZE) when the function is called.   Also, the SP should be modified before arguments are calcuated, so that function calls like foo(bar(x),bar(y)) don't cause problems.   Thus, the code for tiling a function/procedure call using an accumulator on page 236 should be (changes in red):

addi $SP,  $SP,  -<4n>        %  Update call stack pointer
<Code for first argument>
sw   $ACC, 4($SP)             %  Store first argument on the call stack
<Code for second argument>  
sw   $ACC, 8($SP)             %  Store second argument on the call stack
...
<Code for nth argument>
sw   $ACC, 4n($SP)            %  Store nth argument on the call stack

jal  foo                      %  Make the function call
addi $SP,  $SP,   <4n>        %  Update call stack pointer
addi $ACC, $result,0          %  Store result of function in accumulator





287
Figure 11.5: The return type of the constructor should be the ClassType for Integer, not void.  The corrected diagram can be found here
319
The list of reserved words for simpleJava should be
class do else false if for new return true while

(The reseverd word new was left out in the original text)