0% found this document useful (0 votes)
25 views26 pages

Lec 1

Uploaded by

hishamalzain2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views26 pages

Lec 1

Uploaded by

hishamalzain2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

‫تصميم‬

‫المترجمات‬
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

 Lecture 2 : Lexical Analysis


 Introduction

 Token classes
 Specification of Tokens
 FormalLanguages
 Scanner tool

41-<#>
COURSE
CONTENTS ...CONT.
 Lectures 3,4,5 and 6: Syntax
Analysis
 Introduction

 Formal Languages
 Derivations

 Backus-Naur Form (BNF)


 Top-down parsing
 Bottom-up parsing 51-<#>
COURSE
CONTENTS...CONT.
 Lecture 7: Parsing Tool

 Lecture 8,9: Semantic Analysis


 Introduction

 Semantic checks
 Attribute grammars
 Symbol tables and type-checking

61-<#>
COURSE
CONTENTS...CONT.
 Lecture 10: Intermediate Code

generator
 Introduction

 Choosing an intermediate language


 Syntax-directed translation
 Generating code from expressions

71-<#>
PRACTICAL

1. Compiler generator tools

Flex and Bison

2. Assignments

81-<#>
ASSESSMENT

 Attendance 5 - 10%

 Midterm exam 20%

 Practical 25 - 30%

 Final exam 45%

91-<#>
BOOKS:
 S. D. Bergmann, Compiler Design: Theory ,

Tools , and Examples C/C++ Edition. 2010.

 A. V Aho, M. S. Lam, and J. D. Ullman,

Compiler principles, techniques, &

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

 It is important to put in mind that when


processing a statement such as x = x ∗ 9;
the compiler does not perform the
multiplication. The compiler generates, as
output, a sequence of instructions,
including a "multiply" instruction. 121-
<#>
INTRODUCTION...CONT.
 If a portion of the input to a C++ compiler looked like
this:
A=B+C∗D;
 The output corresponding to this input might look

something like this:

 LOD R1,C // Load the value of C into reg 1


 MUL R1,D // Multiply the value of D by reg 1
 STO R1,TEMP1 // Store the result in TEMP1
 LOD R1,B // Load the value of B into reg 1
 ADD R1,TEMP1 // Add value of Temp1 to reg1
 STO R1,TEMP2 // Store the result in TEMP2
 MOV A,TEMP2 // Move TEMP2 to A, the final result
131-
<#>
COMPILERS AND
INTERPRETERS
 “Compilation”
 Translation of a program written in a
source language into a semantically
equivalent program written in a target
language
 “Interpretation”
 Performing the operations implied by the
source program
Source
Program
Interpreter Output

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.

 With a high-level language you have a much greater


degree of machine independence and portability
from one kind of computer to another (as long as the
other machine has a compiler for that language).

 High-level languages may support data abstraction


(through data structures) and program abstraction
(procedures and functions).
161-
<#>
THE DISADVANTAGES OF HIGH-
LEVEL LANGUAGES
 The programmer doesn’t have complete
control of the machine’s resources
(registers, interrupts, I/O buffers).
 The compiler may generate inefficient
machine language programs.
 A Program written in High-level languages
runs slowly rather than program written in
machine language
171-
<#>
THINGS TO TAKE INTO ACCOUNT IN
COMPILER DESIGN
1. Correctness:
 Correct output on execution
 Report errors correctly if the programmer is not
following the language syntax.

2. Efficiency:
 How efficiency (fast) the compiler is generating
the code (compile time).

3. Debuggability/ Usability:
 intt a = 5;

Undefined variable intt; line number and file


181- name
<#>
THE STRUCTURE OF A
COMPILER
 A compiler is structured into various stages

.
 The compiler stages are operated in
sequence (the output of one stage is input
to other stage)

191-
<#>
String of characters

Scanner Lexical Analyzer


String of tokens
Parser Syntax Analyzer Front end

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

Lexical Analyzer (Scanner ) Tokens

 The scanner begins the analysis of the Syntax


source program by reading the input, Analyzer
character by character, and grouping
characters into individual words and
Semantic
symbols (Tokens) Analyzer

o RE ( Regular expression )
o FLEX
-Intermediate
Code generator

Code
Generator
21
Lexical
Analyzer

Syntax Analyzer (Parser ) Tokens


Syntax
Analyzer
 Follow the rules of grammar of given
Parse Tree
language to a (Syntax Tree)
Semantic
Analyzer

 CFG ( Context-Free Grammar ) and BNF


( Backus-Naur Form ) -Intermediate
 LL, LR Parsers Code generator
 BISON

Code
Generator
221-
<#>
Lexical
Analyzer

Semantic Analyzer Syntax


Analyzer
 Uses the syntax tree and the Parse Tree
information in the symbol table to Semantic
check the source program for Analyzer
semantic consistency with the
Annotated
language definition.
Parse tree

-Intermediate
Code generator

Code
Generator
231-
<#>
Lexical
Analyzer

Intermediate-code generator Syntax


Analyzer
 Is a machine independent code,
which is more close to machine Semantic
instructions (Three address code) Analyzer
Annotated
Parse tree

-Intermediate
Code generator

Three
address
code
Code
Generator
24
Lexical
Analyzer

Syntax
Code Generator Analyzer

 Takes as input an intermediate Semantic


representation of the source program Analyzer
and maps it into machine language.

-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-
<#>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy