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
Dec 14, 2014
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
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
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
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
Language Processing Systems
Preprocessor CompilerAssemblerLinkerLoader
April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
Steps for Language Processing System
April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
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
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
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
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
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
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
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
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
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
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
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
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
April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
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
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
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
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
PHASES OF THE COMPILER
April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University
April 10, 2023 A.Alma Candida B.Sc.,MCA., DMI St.Eugene University