Chapter 1 - Language Implementation
Two Main Implementation Methods
- Programs in a source language are implemented using
- Compilation:
- Translate program from source language to target language
- Program in target language is then executed
- Compiler finished before program executed
- Diagram:
- source -> compiler -> target
- input -> target -> output
- Interpretation:
- Directly execute source language program
- Fetch source program, decode it, execute it
- Interpreter executes while program executing
- Diagram:
- source + input -> interpreter -> output
- Or both:
- Compile source program to intermediate language version
- Interpret intermediate language version
- Diagram:
- source -> compiler -> intermediate
- intermediate + input -> interpreter -> output
Examples
- Complilation:
-
gnatmake foo.adb
creates foo.exe
-
foo.exe
contains machine language and OS calls
-
foo.exe
is directly executed by machine and OS
- Interpretation:
- Prolog interpreter (ie xsb on rucs)
- Python interpreter (ie python on rucs)
- Actually, these use compilation and interpretation
- Pure interpretation is rare
- Compilation and Interpretation:
- Prolog/Python interpreter first converts to intermediate code then executes
-
javac Foo.java
creates foo.class
- java Foo interprets foo.class
Pros and Cons of Compilation and Interpretation
- Compilation: compiled code faster than interpreted
- Interpreters:
- Easier to write than compilers
- Allow greater flexibility (eg create and execute programs on the fly)
- Slower: Why? If a statement is in a loop (for example):
- Interpreter analyzes the statement each time through loop
- Compiler analyzes the statement once
- Combination: provides flexibility of interpretation with increased speed
- Combination: allows platform independence
- Intermediate code runs on any machine with an interpreter
- Combination: relatively easy to port to new machine
- Hand translate byte-code interpreter to existing language
- Use interpreter to interpret byte-code version of compiler
Physical and Virtual Computers
- Hardware provides physical computer
- Machine language instructions interpreted by hardware
- Stored programs provide virtual computers
- Figure 1.2 (with some changes)
- Levels - levels execute instructions of levels above them:
- Bare machine: executes machine code
- Macro Instruction interpreter: executes macro instructions
- Operating System: executes OS routine calls
- Application Level - accesses next 2 levels:
- accesses next 2 levels
- provides virtual computers
- Examples:
- Ada Compiler
- Ada application
- C Compiler
- C application
- Java Virtual Machine
- Java Compiler
- Java application
- .NET Common Language Runtime
- C# Compiler
- VB.NET Compiler
- C# application
- Prolog interpreter
- Python interpreter
- OS Shell interpreter
Compiler Phases
- A Compiler Operates in Phases:
- Lexical Analysis (aka Scanner)
- Syntactic Analysis (aka Parser)
- Intermediate Code Generation (and semantic analysis)
- Optimization (optional)
- Code Generation
- All phases involve the Symbol Table
Symbol Table
- Used to communicate information between phases
- Stores information about symbols (ie identifiers)
- Symbols include
- Variables
- Types
- Procedures and functions
- Packages/Classes
- For example, information for variables includes
- Early phases primarily store information
- Late phases primarily use information
What Each Phase Does
- The input - > of each phase is:
- Lexical analysis: seq of characters -> seq of tokens
- Syntactic analysis: seq of tokens -> parse tree
- Intermediate code generation: parse tree -> interm. code
- Code generation: interm. code -> machine code
- Example: x := 12 + y * 34;
Optimization
- Misnomer: optimize means find best
- Optimization improves code
- Examples:
- Strength reduction: eg replace x*4 with shift left 2 bits
- Loop unrolling
- Factor code out of a loop
- Inline procedures
Truth in Advertising Alert
-
Actually, the lexical analyzer produces a single tokens each time it
is called by the parser
- Parse trees are not always produced
- Output from code generator can be assembly, which goes into assembler
- Another phase is linking: joining object code with libraries
ITEC 420 Course Page,
Last modified on