Java Reference
In-Depth Information
and then executed (Figure 1.2). Examples of programming languages whose programs may
be interpreted directly are the UNIX shell languages, such as bash and csh, Forth, and many
versions of LISP.
FIGURE 1.2 Interpretation
One might ask, \Why not interpret all programs directly?" There are two answers.
First is performance. Native machine code programs run faster than interpreted high-
level language programs. To see why this is so, consider what an interpreter must do with
each statement it executes: it must parse and analyze the statement to decode its meaning
every time it executes that statement; a limited form of compilation is taking place for every
execution of every statement. It is much better to translate all statements in a program to
native code just once, and execute that 4 .
Second is secrecy. Companies often want to protect their investment in the programs
that they have paid programmers to write. It is more dicult (albeit not impossible) to
discern the meaning of machine code programs than of high-level language programs.
But, compilation is not always suitable. The overhead of interpretation does not always
justify writing (or, buying) a compiler. An example is the Unix Shell (or Windows shell)
programming language. Programs written in shell script have a simple syntax and so are
easy to interpret; moreover, they are not executed often enough to warrant compilation.
And, as we have stated, compilation maps names to addresses; some dynamic programming
languages (LISP is a classic example, but there are a myriad of newer dynamic languages)
depend on keeping names around at run-time.
1.2 Why Should We Study Compilers?
So why study compilers? Haven't all the compilers been written? There are several reasons
for studying compilers.
1. Compilers are larger programs than the ones you have written in your programming
courses. It is good to work with a program that is like the size of the programs you
will be working on when you graduate.
2. Compilers make use of all those things you have learned about earlier: arrays, lists,
queues, stacks, trees, graphs, maps, regular expressions and finite state automata,
4 Not necessarily always; studies have shown that just-in-time compilation, where a method is translated
the first time it is invoked, and then cached, or hotspot compilation, where only code that is interpreted
several times is compiled and cached, can provide better performance. Even so, in both of these techniques,
programs are partially compiled to some intermediate form such as Oracle's Java Virtual Machine (JVM),
or Microsoft's Common Language Runtime (CLR). The intermediate forms are smaller, and space can play
a role in run-time performance. We discuss just-in-time compilation and hotspot compilation in Chapter 8.
 
Search WWH ::




Custom Search