Code Translation Flashcards
What is code translation?
In simple terms, it is the process of translating source code to object code.
Special Programs for Translation
Four examples of programs used for translation include:
- Compiler
- Interpreter
- Assembler
- Linker
Platform Independent
This means source code is portable, and writes the code as one simple text file. Then uses a compiler that targets a specific platform (source code and byte code).
Platform Dependent
Compiler outputs assembly code or object code that will only run on the platform for which it was compiled (compile a game in c++, and run it on PC, Xbox etc). Examples include assembly code and machine code, or object code.
Compiler v Interpreter
Compiler translates the whole code (returns a file of errors) and adds it to an executable, whilst interpreter runs line-by-line.
With compilers, translations and executions phases are separate, while for interpreters, they are interlaced.
Compiler Advantages
Compilation process is faster than interpreter.
Compiled code runs much faster.
Checks for syntax AND semantic errors, but interpreters can only check for syntax errors.
Interpreter Advantages
Syntax errors are easily identifiable.
Useful for rapid prototyping.
Debugging tools are easier to use.
Java’s Exception with Compiler and Interpreter
Java uses best of both worlds, compiling the java with the java development kit (javac in terminal), and then running it with the interpreter, the java virtual machine (java in terminal).
Interpreter Creation
At the origin of every interpreter there will be a compiler. Essentially, a compiled language will be needed to write the code for the interpreter.
Single Pass Compiler
Moves through source code line-by-line and generates object code along the way.
Multi-pass Compiler
Reads the entire source file into an internal data structure, and analyses and augments the data structure in multiple stages.
Linking
There can be many source files, which compiled turns into object files, so it is the linker’s job to put all these together into one final executable program image.
Static Linking
Static linking copies all the libraries used in our code into the final executable file.
Dynamic Linking
Dynamic linking is the process of loading the external shared libraries into the program and then binds those shared libraries dynamically to the program.
Lexical Component
List of all keywords allowed in language (import, public, else, while, for).
Syntactical Component
Defines form and structure of legal expressions in the language (instead of saying private x double in Java, we would say private double x).
Semantic Component
Adds meaning to an expression made from keywords (actions from an if-else statement etc).
Grammar
Set of formal syntax rules.
Tokens
Grouped characters from the translator.
Expressions
One or more tokens.
Non-terminal Expressions
Defined by other rules.
Terminal Expressions
Defined in their final form with simple characters.
Extended Backus-Naur Form
Rules that are defined as productions of non-terminal and terminal expressions.
EBNF Matching
Allow the compiler to match the source code against the EBNF grammar of the language.
EBNF Rules
-> defines a rule
“|” OR
<name> Non-terminal Symbol
symbol Terminal Symbol
[tokens] Optional Tokens
{tokens} Tokens repeated 0 or more times
* Tokens repeated zero or more times
\+ Tokens repeated one or more times
</name>