Java Reference
In-Depth Information
public static int subr(int b) {
if (b != 0)
return b+100;
}
If b is equal to zero, subr fails to return a value. Now consider the following:
public static int subr(int b) {
if (b != 0)
return b+100;
else if (10*b == 0)
return 1;
}
In this case, a proper return is always executed, since the else part is reached
only if b equals zero; this implies that 10*b is also equal to zero. Is the
compiler expected to duplicate this rather involved chain of reasoning? Java
compilers typically assume that a predicate could evaluate to true or false ,
even if a detailed program analysis refutes that assumption. Thus a compiler
may reject subr as semantically illegal and in so doing trade simplicity for
accuracy in its analysis. Indeed, the general problem of deciding whether
a particular statement in a program is reachable is undecidable ,provedby
reduction from the famous halting problem [HU79]. We certainly cannot ask
our Java compiler literally to do the impossible!
In practice, a trusted reference compiler can serve as a de facto language
definition. That is, a programming language is, in e
ect, defined by what a
compiler chooses to accept and how it chooses to translate language constructs.
In fact, the operational and natural semantic approaches introduced previously
take this view. A standard interpreter is defined for a language, and the
meaning of a program is precisely whatever the interpreter says. An early
(and very elegant) example of an operational definition is the seminal Lisp
interpreter [McC60]. There, all of Lisp was defined in terms of the actions of
a Lisp interpreter, assuming only seven primitive functions and the notions of
argument binding and function call.
Of course, a reference compiler or interpreter is no substitute for a clear and
precise semantic definition. Nonetheless, it is very useful to have a reference
against which to test a compiler that is under development.
ff
1.5 Organization of a Compiler
Compilers generally perform the following tasks:
 
 
Search WWH ::




Custom Search