1 - Introduction To Compilers
1 - Introduction To Compilers
Introduction to Compilers
Compilation
Translation of a program written in a source language into a semantically equivalent program written in a target language Input Oversimplified view:
Source Program
Compiler
Target Program
Output
Error messages
Interpretation
Performing the operations implied by the source program Oversimplified view:
Interpreter
Output
Error messages
Compiler: a program that translates an executable program in one language into an executable program in another language Interpreter: a program that reads an executable program and produces the results of running that program
A language-processing system
Skeletal Source Program Preprocessor Source Program Compiler Target Assembly Program Assembler Relocatable Object Code Linker Absolute Machine Code
Analysis
Lexical analysis
10
11
12
Semantic analysis
Checks source program for semantic errors Gathers type information for subsequent code generation (type checking) Identifies operator and operands of expressions and statements
13
Phases of a compiler
14
Symbol-Table Management
Symbol table data structure with a record for each identifier and its attributes Attributes include storage allocation, type, scope, etc All the compiler phases insert and modify the symbol table
15
Code Optimization
Improve intermediate code by producing code that runs faster
Code Generation
Generate target code, which is machine code or assembly code
17
Sample
Optimizer
Code generator
int2fp B + t1
#2.3
t1 A
18
Back end:
Code optimization and generation Independent of source language Machine dependent
19
Compiler passes:
A collection of phases is done only once (single pass) or multiple times (multi pass)
Single pass: reading input, processing, and producing output by one large compiler program; usually runs faster Multi pass: compiler split into smaller programs, each making a pass over the source; performs better code optimization
20
Compiler-Construction Tools
Software development tools are available to implement one or more compiler phases
Scanner generators Parser generators Syntax-directed translation engines Automatic code generators Data-flow engines
21