<var>::= A | B | C
<factor>::=<var> | ( <expr> )
<term>::=<factor> | <term> * <factor><expr>::=<term> | <expr> + <term>
Exercise 3
The else part can be associated with both of two if statements, resulting the following two interpretation:
Exercise 4
In order to prove a certain grammar is ambiguous, we need a sentence which can be parsed in more than one way. In this case, AAA is such a sentence.
Exercise 5
without declarations
with declarations
code length
short
long
misspelling
difficult to find
easy to find
maintainability
easy to write, hard to read
hard to write, easy to read
In many languages, variable declarations are accompanied by information such as types (e.g. int, double), scopes (e.g. global, local), and access modifiers (e.g. public, private):
Compilers can take advantage of such information (e.g. type checking, memory allocation)
Code is less flexible because more features are fixed at compile time.
Exercise 6
Static scoping: 5
Dynamic scoping: 10
Exercise 7
Suppose data A and B refer to each other. When all references
from outside are lost, A and B are unreachable, but their
reference counts are 1.
Exercise 8
Suppose there are two variables A and B, and B refers to a
heap area C.
Data that can be followed from A are marked by GC.
Some part of the program is executed where the values of A
and B are changed. Now A refers to C, and B refers to
somewhere else.
Data that can be followed from B are marked by GC.
Though A refers to C, C is discarded since it is not marked.
Exercise 9
The library function strtok breaks a string into a series of tokens
using some delimeters. On its first call we should provide a string
for the first parameter, but we should give NULL when we retrieve the
succeeding tokens. This is because strtok has a static local
variable to store the string.
It should not use global variables because it is a library function.
It would requre memory allocation of an array of strings if
you want to return all tokens at once.
The language specification can be kept small. In other
words, it is easier to implement a compiler.
It is possible to write concise code if 0 has a special
meaning (e.g. end of a character string, absence of data,
etc.).
while(*q++=*p++);/* copy a string from *p to *q */
Merits of Java
The code may be more human-readable.
Some kind of bugs can be detected at the compile time.
For example, it is a common mistake in C language to write
if(x=1) when we want to compare x and 1.
In Java, such code makes a type mismatch error.
It may be possible to optimize code using type information
(e.g. a boolean value consumes 1byte in the memory while
an integer needs 4bytes).
Exercise 11
(a->b)->[a]->[b]
Exercise 13
(a#b)$c
a!(b?c)
(a#b)!(c$d)
(a#b)!(c?d)
((a#b)$c)!(d?e)
Exercise 14
If the second argument has a side-effect, we may want to use
the non-short-circuiting operators so that the side-effect will
occur regardless of the value of the first argument.
Otherwise short-circuiting operators are more efficient.
Exercise 15
Error corresponds to a severe incident, so it is difficult to
recover by the user program.
RuntimeException includes:
exceptions caused by mistakes of programmers
(e.g. ArrayStoreException)
exceptions that have chances to occur at almost everywhere
in the program, so it is too cumbersome to write handlers
(e.g. ArithmeticException)
Exercise 16
x: 2, list: {1, 3, 5, 7, 9}
x: 2, list: {3, 1, 5, 7, 9}
x: 2, list: {3, 2, 1, 7, 9}
Exercise 17
Variable i is declared in the surrounding environment of
function(){alert(i);};, being shared by all generated
closures. The value of i is 11 after execution of the
loop, hence every button shows number 11.
Note that if we used for(leti=1;i<=10;i++), it
would work fine because the variable name i is bound to a
newly created variable in each iteration.
Exercise 18
Subclass may have new methods in addition to methods
inherited from its superclass.
Static type checking should guarantee that no type-related
error will happen at run time.
The reason why Ax=newB() is allowed
‘x has type A’ means we may call methods defined in
class A for an object stored in x. If the object is
actually an instance of class B, no error will happen at
run time because the object inherits all methods of A.
The reason why By=newA() is not allowed
‘y has type B’ means we may call methods defined in
class B for an object stored in y. If the object is
actually an instance of class A, an error will happen when we
call a method defined in B but not A.