Upload
phamcong
View
215
Download
1
Embed Size (px)
Citation preview
Compiler DesignLecture 1
Sunday, October 3, 2010
Problem in Real Life
Sunday, October 3, 2010
Problem in Real Life
Egyptian
Arabic
Sunday, October 3, 2010
Problem in Real Life
Egyptian British
Arabic English
Sunday, October 3, 2010
?
Problem in Real Life
Egyptian British
Arabic English
Sunday, October 3, 2010
Solution
Egyptian British ?
Arabic English
Sunday, October 3, 2010
Solution
Egyptian British ?
Arabic English
Sol(1):No Translator (En)advantages:
•Faster interaction.disadvantages:
•Difficult (He must learn english).
Sunday, October 3, 2010
Solution
Egyptian British ?
Arabic English Sol(2):Translator (Ar)advantages:
•He can use his native language, easier.disadvantages:
•Slower (additional person).
Sol(1):No Translator (En)advantages:
•Faster interaction.disadvantages:
•Difficult (He must learn english).
Sunday, October 3, 2010
Problem in Computer World
You Computer
High level language Machine language
Sunday, October 3, 2010
SolutionYou Computer
High level language
Machine language Sol(2):Translator (HL)
advantages:•You can use high level language.
✓Easier to debug.✓Readable.
disadvantages:•Need additional phase (translation).
Sol(1):No Translator (ML)advantages:
•Faster (no translation is required).disadvantages:
•Difficult,you must use machine language.
Sunday, October 3, 2010
Translator
Translates a program into a form in which it can be executed by a computer.
The most common kinds:
๏Compiler.
๏ Interpreter.
Sunday, October 3, 2010
Compiler
Compiler
Source Program
Target Program
Errors
Fig1. Translation
Sunday, October 3, 2010
Compiler
Compiler
Source Program
Target Program
Errors Target Program
Inputs
Outputs
Fig1. Translation Fig2. Execution
Sunday, October 3, 2010
Compiler
Compiler
Source Program
Target Program
Errors Target Program
Inputs
Outputs
Fig1. Translation Fig2. Execution
•It does translation only (Produces target program).•Target program maps inputs to outputs without having to compile the source program again.
Sunday, October 3, 2010
Interpreter
Interpreter
Source Program
Outputs
Errors Inputs
Fig1. Translation+Execution
Sunday, October 3, 2010
Interpreter
Interpreter
Source Program
Outputs
Errors Inputs
Fig1. Translation+Execution
•Translation + Execution (statement by statement)•Every time the source program is run, it will do translation.•Do translation whether or not there are changes in the source program.
Sunday, October 3, 2010
Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?
Sunday, October 3, 2010
Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?
True
Sunday, October 3, 2010
Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?
Target program produce by compiler:Processing time=Execution time only-->small
processing time-->faster
True
Sunday, October 3, 2010
Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?
Target program produce by compiler:Processing time=Execution time only-->small
processing time-->fasterInterpreted source program:
Processing time=Translation time+Execution time--> large processing time-->slower
True
Sunday, October 3, 2010
Language Processing SystemPrograms needed to create an executable generated code.
Sunday, October 3, 2010
Language Processing SystemPrograms needed to create an executable generated code.
Compiler
Source Program
Assembler
Linker
Assembly language program
Machine language module(s)
Loader
Library routines(ML)
Machine language program(exe)
Memory
Sunday, October 3, 2010
Assembler
Translate target program (assembly program) into machine language program.
Sunday, October 3, 2010
Linker
3
Object file format
• Object file header describes the size and position of the other pieces of the file
• Text segment contains the machine instructions• Data segment contains binary representation of data in
assembly file• Relocation info identifies instructions and data that depend
on absolute addresses• Symbol table associates addresses with external labels and
lists unresolved references• Debugging info
Object fileheader
Textsegment
Datasegment
Relocationinformation
Symboltable
Debugginginformation
Process for producing an executable file
Objectfile
Sourcefile Assembler
LinkerAssembler
Assembler Programlibrary
Objectfile
Objectfile
Sourcefile
Sourcefile
Executablefile
Sunday, October 3, 2010
Loader
Part of the OS that brings an executable file residing on disk into memory and starts its running.
Sunday, October 3, 2010
Compiler Structure
There are 2 parts(phases):
✓Analysis(Front-end):➡ Determines the operations implied by the source program which are
recorded in a tree structure.
➡ Collects information about the source program and stores it in a data structure called symbol table.
➡ Machine independent.
✓Synthesis(Back-end):➡ Constructs the target program from the intermediate representation and
information in symbol table.
➡ Machine dependent.
Sunday, October 3, 2010
Compiler StructureLexical Analyzer/Scanner
Syntax Analyzer/Parser
Semantic Analyzer
Intermediate Code Generator
Code Optimizer
Code Generator
Symbol Table
Sunday, October 3, 2010
Lexical Analyzer/ScannerPerforms lexical analysis (scanning).Steps:✓ Reads source program character by character from left to right.✓ Groups characters into lexeme.✓ For each lexeme, it produces a token.✓ Stores information about the token in the symbol table.
Sunday, October 3, 2010
Lexical Analyzer/ScannerPerforms lexical analysis (scanning).Steps:✓ Reads source program character by character from left to right.✓ Groups characters into lexeme.✓ For each lexeme, it produces a token.✓ Stores information about the token in the symbol table.
Source Programposition = initial + rate * 60
ScannerLexeme Tokenposition <id, 1>
= <=>initial <id ,2>
+ <+>rate <id, 3>
* <*>60 <60>
<id,1> <=> <id,2> <+> <id,3> <*> <60>
1 position ...
2 initial ...3 rate ...
Symbol Table
Sunday, October 3, 2010
Syntax Analyzer/Parser
Performs syntax analysis (parsing).
Checks if the token sequence is correct with respect to language specification.
If it is correct, it will build syntax tree using tokens as nodesIf it is not correct --> syntax error.
Example
Sunday, October 3, 2010
Semantic Analyzer
Uses syntax tree and the information in the symbol table.
Checks for semantic errors (type checking).
Possibly performs coercions.
Sunday, October 3, 2010
Intermediate Code GeneratorGenerates an intermediate code (machine independent).This code should be:✓easy to produce.✓easy to translate into the target language.
Example: three-address code✓assembly-like instructions✓at most three operands per instruction.✓at most one operator on RHS.✓temporary names hold the computed values.
Sunday, October 3, 2010
Code Optimizer
Improves the intermediate code so that faster and shorter target code that consumes less power can be obtained.
Reduce redundancy.
Example.
Sunday, October 3, 2010
Code Generator
Maps (optimized) intermediate code into the target code.
Registers and memory locations are selected for each of the variables.
Example.
Sunday, October 3, 2010