Lec 1
Lec 1
المترجمات
COMPILERS
DESIGN
Batch 40
11-<#>
COURSE OBJECTIVES
General Objectives
The course is intended to teach the
students the basic techniques that underlie
the practice of Compiler Construction.
The course will introduce the theory and
tools that can be standardly employed in
order to translates high-level programming
language into an executable code.
21-<#>
COURSE OBJECTIVES…
CONT.
Specific Objectives:
Upon successful completion of the course
the students will be able to:
Better understanding of and appreciation for
programming languages.
Know how to use compiler construction tools,
such as generators of scanners and parsers.
Build a compiler for a (simplified)
programming language.
31-<#>
COURSE CONTENTS
Lecture 1 :
Introduction to Compilers
Token classes
Specification of Tokens
FormalLanguages
Scanner tool
41-<#>
COURSE
CONTENTS ...CONT.
Lectures 3,4,5 and 6: Syntax
Analysis
Introduction
Formal Languages
Derivations
Semantic checks
Attribute grammars
Symbol tables and type-checking
61-<#>
COURSE
CONTENTS...CONT.
Lecture 10: Intermediate Code
generator
Introduction
71-<#>
PRACTICAL
2. Assignments
81-<#>
ASSESSMENT
Attendance 5 - 10%
Practical 25 - 30%
91-<#>
BOOKS:
S. D. Bergmann, Compiler Design: Theory ,
tools. 2006
101-
<#>
INTRODUCTION
What is a Compiler?
Compiler is a program which translates a
program written in one language (the source
language) to an equivalent program in other
language (the target language).
Usually the source language is a high level
language like Java, C etc. whereas the target
language is machine code or "code" that a
computer's processor understands.
Source Target
Program
Compiler Program
Error messages 11
INTRODUCTION...CONT.
Some examples of compilers are:
A Java compiler
A C++ compiler
Input
141-
<#>
COMPILERS AND
INTERPRETERS
The compiler output and the interpreter output for the following C+
+ source code:
for (i=1; i<=4; i++) cout << i*3;
Compiler Interpreter
LOD R1,='4' 3 6 9 12
STO R1,Temp1
MOV i,='1'
L1:CMP i,Temp1
BH L2 {Branch if i>Temp1}
LOD R1,i
MUL R1,='3'
STO R1,Temp2
PUSH Temp2
CALL Write
ADD i,='1' {Add 1 to i}
B L1 151-
<#>
THE ADVANTAGES OF A HIGH-LEVEL
LANGUAGE OVER MACHINE OR ASSEMBLY
LANGUAGE
Machine language (and even assembly language) is
difficult to work with and difficult to maintain.
2. Efficiency:
How efficiency (fast) the compiler is generating
the code (compile time).
3. Debuggability/ Usability:
intt a = 5;
.
The compiler stages are operated in
sequence (the output of one stage is input
to other stage)
191-
<#>
String of characters
Parse tree
Semantic Analyzer
Annotated Parse tree
Intermediate-Code
Generator
Intermediate Back end
representation
Code Generator
Target-machine code
20
String of
characters
Lexical
Analyzer
o RE ( Regular expression )
o FLEX
-Intermediate
Code generator
Code
Generator
21
Lexical
Analyzer
Code
Generator
221-
<#>
Lexical
Analyzer
-Intermediate
Code generator
Code
Generator
231-
<#>
Lexical
Analyzer
-Intermediate
Code generator
Three
address
code
Code
Generator
24
Lexical
Analyzer
Syntax
Code Generator Analyzer
-Intermediate
Code generator
Three
address
code
Code
Generator
251-
<#>Machine
code
SUMMARY
The compiler serves as a translator from any
program in a given high-level language to an
equivalent program in a given machine
language.
Compiler front-end: lexical analysis, syntax
analysis, semantic analysis
Tasks: understanding the source code, making
sure the source code is written correctly
Compiler back-end: Intermediate code
generation and code generation
Tasks: translating the program to a
semantically the same program (in a different
language). 261-
<#>