Top Banner
MODULE CODE:055CS603 MODULE NAME: PRINCIPLES OF COMPILER DESIGN PRESENTED BY: A.ALMA CANDIDA , B.Sc .,MCA., June 7, 2022 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
25

Principles of compiler design

Dec 14, 2014

Download

Technology

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Principles of compiler design

MODULE CODE:055CS603

MODULE NAME: PRINCIPLES OF COMPILER DESIGN

PRESENTED BY:A.ALMA CANDIDA , B.Sc .,MCA.,

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 2: Principles of compiler design

TOPICS TO BE COVERED

• DEFINITION OF COMPLIER• LANGUAGE PROCESSING SYSTEMS• PHASES OF COMPILER

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 3: Principles of compiler design

COMPILER

• Translates one language to another language.

• Role - report any errors in the source program.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

CompilerSourceProgram

TargetProgram

Page 4: Principles of compiler design

Interpreter

• Which convert from one language to another.• Convert line by line.• Error will be displayed at the time of

conversion.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 5: Principles of compiler design

Language Processing Systems

Preprocessor CompilerAssemblerLinkerLoader

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 6: Principles of compiler design

Steps for Language Processing System

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 7: Principles of compiler design

Pre Processor

• modules are stored in separate files.• source program is collected by a separate

program.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 8: Principles of compiler design

Compiler

• Source program is fed to a compiler. • Output- assembly-language program .• Assembly language is easier to produce as

output.• easier to debug.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 9: Principles of compiler design

Assembler

• Assembly language into relocatable machine code.

• Large programs are compiled into small modules.

• The relocatable machine code have to be linked together with other relocatable object files and library files .

• This code actually runs on the machine.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 10: Principles of compiler design

Linker

• Resolves external memory addresses,• The code in one file may refer to a location

in another file.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 11: Principles of compiler design

Loader

• Puts together all of the executable object files into memory for execution.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 12: Principles of compiler design

Phases Of Compiler

• There are six different phases available in the compiler

Lexical Analysis Syntax AnalysisSemantic AnalysisIntermediate Code GenerationCode OptimizationCode Generation

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 13: Principles of compiler design

Symbol-Table Management

• Data structure containing a record for each variable name, with fields for the attributes of the name.

• Designed to allow the compiler• Find the record for each name quickly and to

store or retrieve data from that record quickly.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 14: Principles of compiler design

Lexical Analysis

• This is also called as Scanning.• It collects sequences of characters into

meaningful units called tokens.• The text is read and divided into tokens.Lexemes• Reads the stream of characters • Groups the characters into meaningful

sequences called lexemes. • The output will be in the form of tokens.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 15: Principles of compiler design

Lexical Analysis (cont..)

• General Form: (token-name, attribute-value)token-name -> an abstract symbol attribute-value-> entry in the symbol table

for this token.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 16: Principles of compiler design

Example of Lexical Analysisposition = initial + rate * 60

<id,1><=> <id,2><+><id,3><*><60>• Position - mapped into a token (id, 1)–id ->identifier and 1 points to the symbol- table• Assignment symbol = mapped into the token (=). • Initial - mapped into the token (id, 2), • + - mapped into the token (+). • Rate mapped into the token (id, 3)• * mapped into the token (*) . • 60 mapped into the token (60)

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 17: Principles of compiler design

Syntax Analysis

• This is also called as Parsing.• Arrange tokens form of Tree called Syntax

Tree.• Interior node ->operation • Child node ->Arguments of the operations.• Output -> Tree.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 18: Principles of compiler design

Syntax Analysis(cont..)• Consider the example

• position = initial + rate * 60 • The Syntax tree is in the form of

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 19: Principles of compiler design

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 20: Principles of compiler design

Semantic Analysis

• Convert from one data type to another.• Example:

• position=initial + rate*60– position, initial, and rate have been declared to be

floating-point numbers, and that the lexeme 60 by itself forms an integer.

– the integer may be converted into a floating-point number.

– the output -> has an extra node for the operator inttofloat, which explicitly converts its integer argument into a floating-point number.

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 21: Principles of compiler design

Intermediate Code Generation

• translated to a simple machine-independent intermediate language.

• Convert into low level language• The output -> three-address code sequence.• Example:• tl = inttofloat (60)• t2 = id3 * tl • t3 = id2 + t2 • id1 = t3

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 22: Principles of compiler design

Code Optimization

• Attempts to improve the intermediate code so that better target code will result.

• Its is in the form of shorter sequence.

•t1=id3*60.0•id1=id2+t1

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 23: Principles of compiler design

CODE GENERATION• Input an intermediate representation of the source

program and maps it into the target language. • Target language is machine code, registers or memory

locations are selected for each of the variables used by the program.

• Registers R1 and R2, the intermediate translated into the machine code

LDF R2, id3 MULF R2, R2, #60.0 LDF Rl, id2 ADDF Rl, Rl, R2 STF idl, Rl

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 24: Principles of compiler design

PHASES OF THE COMPILER

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University

Page 25: Principles of compiler design

April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University