Introduction to Compiler Construction
Compiler
Interpreter
Interpreter
Interpreter
Compilers and Interpreters
Compiler
Error messages
Source
Program
Target
Program
Input
Output
Compilers and Interpreters (cont’d)
Interpreter
Source
Program
Input
Output
Error messages
The Analysis-Synthesis Model of Compilation
This is also known as the front-end of the compiler. It reads the source program, divides it into core parts and then checks for lexical, grammar and syntax errors. The analysis phase generates an intermediate representation of the source program and symbol table, which should be fed to the Synthesis phase as input
Its also known as the back-end of the compiler.
It generates the target program with the help of intermediate source code representation and symbol table.
Other Tools that Use the Analysis-Synthesis Model
Preprocessors, Compilers, Assemblers and Linkers
An assembler translates assembly language programs into machine code. The output of an assembler is called an object file, which contains a combination of machine instructions as well as the data required to place these instructions in memory.
Preprocessors, Compilers, Assemblers and Linkers
A computer program that links and merges various object files together in order to make an executable file.
Compiler Design - Architecture of a Compiler
Phases of a Compiler
Traditional three pass compiler
Front end
Source
code
Machine
code
errors
IR
Back end
Middle
end
IR
Phases of a Compiler - Front end
Phases of a Compiler - Front end cont’d
The front end includes all analysis phases and the intermediate code generator.
Lexical Analysis
Lexical Analysis
Front end: Terminologies
Token and Lexeme
At lexical analysis phase,
Input: stream of characters
Output: Token
Token Template:
<token-name, attribute-value>
Hence,
<id, 1><=>< id, 2>< +><id, 3 >< * >< 5>
Token and Lexeme Cont’d
Syntax Analysis
Why Grammar ?
Syntax Analysis cont’d
Syntactic Analysis
Syntactic Analysis cont’d
Semantic Analysis
Semantic Analyzer
Semantic Analysis cont’d
Syntax tree is a compressed representation of the parse tree (a hierarchical structure that represents the derivation of the grammar to obtain input strings) in which the operators appear as interior nodes and the operands of the operator are the children of the node for that operator.
Semantic Analyzer
Semantic analysis is the third phase of compiler.
Semantic Analyzer
Front-end, Back-end division
Front end
Source
code
Machine
code
errors
IR
Back end
Front end
Scanner
Source
code
IR
errors
tokens
Parser
Front end
Scanner
Source
code
IR
errors
tokens
Parser
Front end
Scanner
Source
code
IR
errors
tokens
Parser
Phases of a Compiler cont’d
Middle End – The Optimizer
Middle end (optimizer)
Back end
Instruction
selection
IR
Machine code
errors
Register
Allocation
Phases of a Compiler
Phases of a Compiler
Phases of a Compiler - Instruction selection
Back end
Instruction
selection
IR
Machine code
errors
Register
Allocation
Intermediate Code Generation
After semantic analysis the compiler generates an intermediate code of the source code for the target machine.
Code Optimization
Code Generation
Symbol Table
Compiler-Construction Tools