Preliminaries
The Translation Process - Getting from Source code to Execution
Compilation
- Source is translated to machine code
- Machine code is then executed on machine
Interpretation
- Source code is fed to a software program (interpreter) and is directly
executed.
- The interpreter is a software simulator of a machine whose machine
language is the high-level language
The above are the two extremes with all sorts of variations in between
- Massage source removing unnecessary whitespace and comments and then interpret
- Compile the source to assembler
- Compile the source to some other high-level language
- Compile the source to some for of intermediate language
- Compile the source to some other interpreted machine language
- Hardware computer- actual computer
- Software simulator - virtual computer
Other Possible Phases of Translation
Preprocessing
- textual replacement of macros
- Can provide
- a manifest constant facility
- a file inclusion facility
- conditional compilation
- Often (usually?) separate from the language proper
Linking
- 'Editing' of separately compiled modules
- Provides
- Ability to create code modules
- Library facilities
Program Structure as Related to Translation and Execution
Lexical Structure of a Program
The rules regarding the words and symbols of a program (think of it like spelling). The
words and symbols are known as tokens.
- Identifiers: names of various entities (things) in the program
- variables
- function names
- keywords: identifiers reserved for use by the language:
if
,
while
, int
- Symbols: non-alphabetic sequences:
- Operators: symbols that accept one or operands and produce a result:
+
, &&
, --
- punctuation:
;
, .
, "
- literals:
- value that represents itself
- value that occurs in a program that cannot be changed
14
, "Hello"
, 9.08
, 'A'
Example of lexical compilation errors:
- "Hello unterminated literal
- 12GB invalid token
- @ invalid (unknown) token
Syntactic Structure of a Program
The rules regarding the 'sentences' and 'paragraphs' of the program (think of it like grammar).
- Construct: a grammatical phrase (think of sentences, paragraphs, clauses):
if
construct function
construct, expression
Example of syntactic compilation errors:
- if a == b) printf("Hi"); missing '(' -- syntax error
- else printf("Hi"); 'else' without 'if'
- a = c + d missing ';' -- syntax error
Semantics of a Program
The rules regarding valid 'meaning' a program (whether the program 'makes sense').
- Types must be correct
- Declaration appears before usage
Example of semantic compilation errors:
Logic of a Program
What's correct/incorrect is up to the programmer.
/* This program reads in a number and prints the number and its square */
main() {
int x;
scanf("%d", &n);
printf("The square of %d is %d\n"
Binding and Binding Times
We would like to be able to speak of the point at which a program element
is bound (i.e., associated with) to a particular characteristic or property.
Binding time is the point at which this association (or choice) is made.
Various Binding Times and Examples
- Execution time
- Variables to values
- Variables and data structures to particular storage locations
Furthermore, execution time may be subdivided:
- On entry to a function or block
- Arguments are bound to formal parameters
- Local variables are bound to their storage locations
- At arbitrary points of execution
- At any point of the execution, an assignment binds the
lhs variable to the rhs value
- Translation or compile time
- Variables to types
- Sizes of structures
- Some particular categories of variables to values or locations
Translating Java
- Java is compiled then interpreted
- Compilation phase
- Checks for various lexical/syntax/semantic errors
- Generates
Java byte code
- Interpreter
- Emulates the
Java Virtual Machine (JVM)
- Executes the byte code on the virtual machine