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

1_Introduction to Compiler

Uploaded by

Anonymous Racoon
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)
16 views26 pages

1_Introduction to Compiler

Uploaded by

Anonymous Racoon
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

Introduction to

Compiler
Compiler
• Meaning of “Compile”?
• Collect / Gather.
• So compiler is something that collects or
gathers.

01/06/2025 2
Compiler
• In CS, a program that translates one language to
another.
• Better Definition

A compiler is a program that takes as its input a program written in its


source language and produces an equivalent program written in its target
language.

• Source Language - High-level language (C,C++)


• Target Language – Object code / Machine code.

01/06/2025 3
Compiler
• A compiler acts as a translator.
• Transforms human-oriented programming
languages into computer oriented machine
language.
• Benefit?
• Gives programmers more time to think on problems.
• Reduces error produced by the programmer.

01/06/2025 4
Why Study Compiler
• Understand how programming language works.
• Learn about complex software system.
• Create new programming language.
• Develop problem-solving skill.

01/06/2025 5
Language Processing System
• Compiler alone is not enough to translate a program.
• A language processing system is a set of programs that
are used to translate a program written in a high-level
language into a low-level language that can be executed
by a computer.

01/06/2025 6
Components of Language
Processing System
• Preprocessor
• Collects source codes
• Expands macros and directives
• Compiler
• Translates the source code into low level language program.
• Assembler
• Translates low level language code into machine code.

01/06/2025 7
Components of Language
Processing System
• Linker
• Combines the object files produced by the compiler and assembler into an
executable program.
• Loader
• Loads the executable program into the computer's memory and starting its
execution.

01/06/2025 8
Structure of Compiler
Character stream Semantic
Lexical Analyzer Syntax Analyzer
Analyzer

Symbol Table Intermediate Code


Generator

Machine
Machine Dependent
Code Generator Independent Code
Target machine Code Optimizer
code Optimizer

01/06/2025 9
Structure of Compiler
• Consider the statement

pos = i + r * 5

01/06/2025 10
Lexical Analysis
• Reads the stream of characters.
• Groups the character into meaningful sequence
(Lexeme).
• Produces output token of the form:

<token-name, attribute-value>

• Token-name: abstract symbol used in syntax analysis.


• Attribute-value: points to entry in symbol table.

01/06/2025 11
Lexical Analysis
pos = i + r * 5

• pos is a lexeme
• Mapped into <id, 1>
• id abstract symbol standing for “Identifier”.
• 1 points to symbol table entry for position.
• = is a lexeme
• Mapped into <=>
• No attribute – value needed. Hence omitted.

01/06/2025 12
Lexical Analysis
pos = i + r * 5

<id,1> <=> <id,2> <+> <id,3><*> <5>

01/06/2025 13
Syntax Analysis
• Uses first component of the tokens produced by the
lexical analyzer.
• Creates a tree-like intermediate representation.

01/06/2025 14
Semantic Analysis
• Uses syntax tree and symbol table.
• Checks for semantic consistency.
• Gathers type information.
• Performs type checking.
• Ensures each operator has matching operands.

01/06/2025 15
Semantic Analysis

01/06/2025 16
Intermediate Code Generation
• Not a mandatory step.
• An explicit low-level or machine-like intermediate code
is generated.
• Code should maintain two important properties
• Easy to produce
• Easy to translate
• We will study an intermediate form called “Three-
address Code”.

01/06/2025 17
Intermediate Code Generation
• Three-address code
• At most three operand per instruction.
• Each operand can act as register.
• At most one operator on the right.

01/06/2025 18
Intermediate Code Generation
pos = i + r * 5

t1 = inttofloat(5)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3

01/06/2025 19
Code Optimization
• Attempts to improve the intermediate code.
• To produce better target code.
• Better code means
• Faster
• Shorter
• Consume less power

01/06/2025 20
Code Optimization
• Optimizer can deduce that conversion of 5 to float can be done
once in compile time.
• inttofloat operation can be eliminated.
• Replace 5 with 5.0.
• t3 is used once to transmit its value to id1.

t2 = id3 * 5.0
id1 = id2 + t2

01/06/2025 21
Code Generation
• Takes an intermediate representation of source program.
• Maps it into target code.
• If target code is machine code register and memory location
might be used.

01/06/2025 22
Code Generation
t2 = id3 * 5.0
id1 = id2 + t2

LDF R2, id3


MULF R2, R2, #5.0
LDF R1, id2
ADDF R1, R1, R2
STF id1, R1
01/06/2025 23
Symbol Table
• A data structure containing a record for each variable name,
with fields for the attributes of the name.
• Allows compiler to
• Find record for each name quickly.
• Store or retrieve data quickly.

01/06/2025 24
Compiler Construction Tools
• Parser Generator.
• Produce syntax analyzer from a grammatical description.
• Scanner Generator
• Produce lexical analyzer from a regular expression description.
• Syntax Directed Translation Engine
• Produces collection of routine for walking a parse tree and
generating intermediate code.
• Code-generator Generator
• Produce code generator from a collection of rules for translating
each operation of the intermediate language into the machine
language for a target machine.

01/06/2025 25
Compiler Construction Tools
• Data-flow analysis engine
• Gathers information about how values are transmitted from
one part of a program to each other part.
• Key part of code optimization.
• Compiler-construction toolkits
• Provides an integrated set of routines for constructing various
phases of a compiler.

01/06/2025 26

You might also like