0% found this document useful (0 votes)
32 views31 pages

CS-352 - Spring 2024 - Lec1

Uploaded by

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

CS-352 - Spring 2024 - Lec1

Uploaded by

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

Faculty of Information Technology

Spring 2024

Compiler Design
CS-352
Lecture (1)

1
Textbook

2
3
Outline

• Compilers
• Compilers VS. Interpreters
• Compiler Architecture
• Compiler Phases
• Translation of an Assignment Statement

4
Compilers

5
Compilers (Cont.)
• A program that reads a source program and
translates it into a semantically equivalent target
program.

Source Program COMPILER Target Program


(Normally a program written in a (Normally the equivalent
high-level programming language) program in machine)

• Traditionally, compilers go from high-level


languages to low-level languages.

6
Compilers VS. Interpreters

7
Compilers VS. Interpreters

• “Compilation”
– Translation of a program written in a source
language into a semantically equivalent
program written in a target language.
– Oversimplified view:-
Input

Source Target
Compiler
Program Program

Error messages Output


8
Compilers VS. Interpreters (Cont.)

• “Interpretation”
– Performing the operations implied by the
source program.
– Oversimplified view:-

Source
Program
Interpreter Output
Input

Error messages
9
Compilers VS. Interpreters (Cont.)

10
Compiler Architecture

11
Compiler Architecture (Cont.)

Analysis Phase Synthesis Phase


An intermediate representation is The equivalent target program is
created from the given source created from that intermediate
program. representation.
Lexical Analyzer, Intermediate Code Generator,
Syntax Analyzer and Code Generator, and
Semantic Analyzer are the parts of Code Optimizer are the parts of
this phase. this phase.

12
Compiler Phases

13
Lexical Analysis (Scanner)

Scanner tokens Parser Semantic Code


Source (lexical (syntax Generator
Analysis
language analysis) analysis) (IC generator)

Code
Optimizer

• Tokens described formally


• Breaks input into tokens
Symbol
• Remove white space
Table

14
Lexical Analysis (Scanner)

• The lexical analyzer (also called the scanner) groups


the characters making up the source program into
meaningful sequences called lexemes.

• The lexemes are mapped into tokens of the form


<token_name, attribute_value>

• The tokens in the above form are used later by the


syntax analyzer.
• The token_name is an abstract symbol that tells the kind of
the lexeme, e.g. ID, +, *, if, 60, etc.
• The attribute_value points to an entry in the symbol table for
this token.

15
Lexical Analysis (Scanner)

Input: result = a + b * c / d

• Tokens:
‘result’, ‘=‘, ‘a’, ‘+’, ‘b’, ‘*’, ‘c’, ‘/’, ‘d’

Identifiers
Operators

16
Syntax Analysis (Parser)

tokens Syntactic
Scanner Parser structure Semantic Code
Source Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)

Code
Optimizer

• Syntax described formally


• Tokens organized into syntax tree
that describes structure Symbol
• Error checking (syntax) Table

17
Syntax Analysis (Parser)

Input: result = a + b * c / d

Assign ::= ID ‘=‘ Exp Assign

Exp ::= Exp ‘+’ Exp ID ‘=‘ Exp


| Exp ‘-’ Exp
| Exp ‘*’ Exp Exp ‘+’ Exp
| Exp ‘/’ Exp
ID Exp ‘*’ Exp
| ID
ID Exp ‘/’ Exp

ID ID
18
Semantic Analysis

Syntactic/semantic
Syntactic structure
Scanner Parser structure Semantic Code
Source Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)

Syntactic/semantic
structure
Code
Optimizer

• “Meaning”
• Type/Error Checking
• Intermediate Code Generation –
abstract machine Symbol
Table

19
Semantic Analysis (Cont.)

• The semantic analyzer uses the syntax tree and the


information in the symbol table to check the source program
for semantic consistency with the language definition.

• It gathers type information and saves it in either the syntax


tree or the symbol table, for the code generation.

• Type checking is an important role of the semantic analyzer.


It checks that each operator has operands of the right type.

• The semantic analyzer may add to the syntax tree extra


nodes if type conversion of some operands is needed.

20
Code Optimization

Scanner Parser Semantic Code


Source Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)
Syntactic/semantic
structure
Syntactic/semantic
Code structure
Optimizer

• Improving efficiency (machine


independent)
• Finding optimal code is NP Symbol
Table

21
Code Generation

Syntactic/semantic
structure
Scanner Parser Semantic Code
Source Target
language
(lexical (syntax Analysis Generator language
analysis) analysis) (IC generator)

Syntactic/semantic
Code structure
Optimizer

• IC to real machine code


• Memory management, register
allocation, instruction selection, Symbol
instruction scheduling, … Table

22
Translation of an Assignment
Statement

24
Translation of an Assignment
Statement (Cont.)

Symbol table
1 position … The symbol table stores information
about each identifier such as: its
2 initial … name and type.
3 rate …
… … …
25
Translation of an Assignment
Statement (Cont.)

26
Translation of an Assignment
Statement (Cont.)

27
Translation of an Assignment
Statement (Cont.)

28
Translation of an Assignment
Statement (Cont.)

29
Translation of an Assignment
Statement (Cont.)

30

You might also like