SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
TCS 502 Compiler Designp g
CS416 Compiler Design 1P K Singh
Course Information
Textbook:
Alfred V Aho Ravi Sethi and Jeffrey D UllmanAlfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman,
“Compilers: Principles, Techniques, and Tools”
Addison-Wesley, 1986.Addison Wesley, 1986.
• Course Web Page: http://pksmmmec.googlepages.com
CS416 Compiler Design 2P K Singh
Preliminaries Required
• Basic knowledge of programming languages.
• Basic knowledge of FSA and CFGBasic knowledge of FSA and CFG.
• Knowledge of a high programming language for the
programming assignments.
CS416 Compiler Design 3P K Singh
Course Outline
• Introduction to Compiling
• Lexical Analysisy
• Syntax Analysis
– Context Free GrammarsContext Free Grammars
– Top-Down Parsing, LL Parsing
– Bottom-Up Parsing, LR Parsingp g g
CS416 Compiler Design 4P K Singh
Course Outline
• Syntax-Directed Translation
– Attribute Definitions
– Evaluation of Attribute Definitions
• Semantic Analysis, Type Checking
• Run-Time Organization
• Intermediate Code GenerationIntermediate Code Generation
• Code Optimization
P K Singh CS416 Compiler Design 5
COMPILERS
• A compiler is a program takes a program written in a source
language and translates it into an equivalent program in a targetg g q p g g
language.
source program COMPILER target program
( ll i i ( ll h i l i
error messages
( Normally a program written in
a high-level programming language)
( Normally the equivalent program in
machine code – relocatable object file)
error messages
CS416 Compiler Design 6P K Singh
Major Parts of Compilers
• There are two major parts of a compiler: Analysis and
SynthesisSynthesis
• In analysis phase an intermediate representation is• In analysis phase, an intermediate representation is
created from the given source program.
– Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts ofy y y y p
this phase.
• In synthesis phase, the equivalent target program is
t d f thi i t di t t ticreated from this intermediate representation.
– Intermediate Code Generator, Code Generator, and Code Optimizer are
the parts of this phase.
CS416 Compiler Design 7P K Singh
Phases of A Compiler
Lexical
Analyzer
Semantic
Analyzer
Syntax
Analyzer
Intermediate
Code Generator
Code
Optimizer
Code
Generator
Target
Program
Source
Program
• Each phase transforms the source program from one representationp p g p
into another representation.
• They communicate with error handlers• They communicate with error handlers.
• They communicate with the symbol table.
CS416 Compiler Design 8P K Singh
Lexical Analyzer
• Lexical Analyzer reads the source program character by
character and returns the tokens of the source program.
• A token describes a pattern of characters having same meaning
in the source program. (such as identifiers, operators, keywords,
numbers, delimeters and so on), )
Ex: newval := oldval + 12 => tokens: newval identifier
:= assignment operator
oldval identifier
+ add operator
12 a number
• Puts information about identifiers into the symbol table.u s o a o abou de e s o e sy bo ab e
• Regular expressions are used to describe tokens (lexical
constructs).
A (Deterministic) Finite State Automaton can be used in the
CS416 Compiler Design 9
• A (Deterministic) Finite State Automaton can be used in the
implementation of a lexical analyzer.
P K Singh
Syntax Analyzer
• A Syntax Analyzer creates the syntactic structure (generally a
parse tree) of the given program.p ) g p g
• A syntax analyzer is also called as a parser.
• A parse tree describes a syntactic structure.
assgstmt
identifier := expression • In a parse tree, all terminals are at leaves.
newval expression + expression
id tifi b
p ,
• All inner nodes are non-terminals in
a context free grammar.
identifier number
oldval 12
CS416 Compiler Design 10P K Singh
Syntax Analyzer (CFG)
• The syntax of a language is specified by a context free
grammar (CFG).g ( )
• The rules in a CFG are mostly recursive.
• A syntax analyzer checks whether a given program satisfies the
rules implied by a CFG or not.
– If it satisfies, the syntax analyzer creates a parse tree for the given program.
• Ex: We use BNF (Backus Naur Form) to specify a CFG
assgstmt -> identifier := expression
expression -> identifier
expression -> number
expression -> expression + expression
CS416 Compiler Design 11P K Singh
Syntax Analyzer versus Lexical Analyzer
• Which constructs of a program should be recognized by the
lexical analyzer, and which ones by the syntax analyzer?y , y y y
– Both of them do similar things; But the lexical analyzer deals with simple non-
recursive constructs of the language.
– The syntax analyzer deals with recursive constructs of the language.
– The lexical analyzer simplifies the job of the syntax analyzer.
– The lexical analyzer recognizes the smallest meaningful units (tokens) in a source
program.
– The syntax analyzer works on the smallest meaningful units (tokens) in a source
program to recognize meaningful structures in our programming language.
CS416 Compiler Design 12P K Singh
Parsing Techniques
• Depending on how the parse tree is created, there are different
parsing techniques.
These parsing techniques are categorized into two groups:• These parsing techniques are categorized into two groups:
– Top-Down Parsing,
– Bottom-Up Parsingp g
• Top-Down Parsing:
– Construction of the parse tree starts at the root, and proceeds towards the leaves.
– Efficient top-down parsers can be easily constructed by hand.Efficient top down parsers can be easily constructed by hand.
– Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing).
• Bottom-Up Parsing:
– Construction of the parse tree starts at the leaves and proceeds towards the rootConstruction of the parse tree starts at the leaves, and proceeds towards the root.
– Normally efficient bottom-up parsers are created with the help of some software
tools.
– Bottom-up parsing is also known as shift-reduce parsing.
CS416 Compiler Design 13
– Operator-Precedence Parsing – simple, restrictive, easy to implement
– LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR
P K Singh
Semantic Analyzer
• A semantic analyzer checks the source program for semantic
errors and collects the type information for the code generation.yp g
• Type-checking is an important part of semantic analyzer.
• Normally semantic information cannot be represented by a
context-free language used in syntax analyzers.
• Context-free grammars used in the syntax analysis are integrated
with attributes (semantic rules)with attributes (semantic rules)
– the result is a syntax-directed translation,
– Attribute grammars
E• Ex:
newval := oldval + 12
Th t f th id tifi l t t h ith t f th i ( ld l 12)
CS416 Compiler Design 14
• The type of the identifier newval must match with type of the expression (oldval+12)
P K Singh
Intermediate Code Generation
• A compiler may produce an explicit intermediate codes
representing the source program.p g p g
• These intermediate codes are generally machine (architecture
independent). But the level of intermediate codes is close to the
le el of machine codeslevel of machine codes.
• Ex:
newval := oldval * fact + 1
id1 := id2 * id3 + 1
MULT id2,id3,temp1 Intermediates Codes (Quadraples)
ADD temp1,#1,temp2
MOV temp2,,id1
CS416 Compiler Design 15
p ,,
P K Singh
Code Optimizer (for Intermediate Code Generator)
• The code optimizer optimizes the code produced by the
intermediate code generator in the terms of time and space.g p
• Ex:
MULT id2,id3,temp1
ADD temp1,#1,id1ADD temp1,#1,id1
CS416 Compiler Design 16P K Singh
Code Generator
• Produces the target language in a specific architecture.
• The target program is normally is a relocatable object fileThe target program is normally is a relocatable object file
containing the machine codes.
• Ex:
( assume that we have an architecture with instructions whose at least one of its operands
isis
a machine register)
MOVE id2 R1MOVE id2,R1
MULT id3,R1
ADD #1,R1
MOVE R1 id1
CS416 Compiler Design 17
MOVE R1,id1
P K Singh

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Csci360 08-subprograms
Csci360 08-subprogramsCsci360 08-subprograms
Csci360 08-subprograms
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Unit 5 cspc
Unit 5 cspcUnit 5 cspc
Unit 5 cspc
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Lex
LexLex
Lex
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and Lexemes
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Subprogram
SubprogramSubprogram
Subprogram
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 

Destacado

Midlands Alternative Mining Indaba Declaration 2014
Midlands Alternative Mining Indaba Declaration  2014Midlands Alternative Mining Indaba Declaration  2014
Midlands Alternative Mining Indaba Declaration 2014ZELA_infor
 
Aarad Homer's Visual Resume
Aarad Homer's Visual ResumeAarad Homer's Visual Resume
Aarad Homer's Visual ResumeAarad Homer
 
Excel - формули и функции
Excel - формули и функцииExcel - формули и функции
Excel - формули и функцииvenkoandonov
 
Toni bou i mena
Toni bou i menaToni bou i mena
Toni bou i menabielgiro
 
SQL Server Worst Practices
SQL Server Worst PracticesSQL Server Worst Practices
SQL Server Worst PracticesGianluca Sartori
 
конференц сервис Happy spaces
конференц сервис Happy spacesконференц сервис Happy spaces
конференц сервис Happy spacesKaterina Dehtyaruk
 
Learn Database Design with MySQL - Chapter 4 - Data types
Learn Database Design with MySQL - Chapter 4 - Data typesLearn Database Design with MySQL - Chapter 4 - Data types
Learn Database Design with MySQL - Chapter 4 - Data typesEduonix Learning Solutions
 
Jonas Salk by Carlee
Jonas Salk by CarleeJonas Salk by Carlee
Jonas Salk by CarleeJolinspeeps
 
Midlands PAMI press statement-final
Midlands PAMI press statement-finalMidlands PAMI press statement-final
Midlands PAMI press statement-finalZELA_infor
 
How to have the best Christmas
How to have the best ChristmasHow to have the best Christmas
How to have the best ChristmasJen Vuhuong
 

Destacado (16)

internet securityand cyber law Unit2
internet securityand  cyber law Unit2internet securityand  cyber law Unit2
internet securityand cyber law Unit2
 
Anti stresssong
Anti stresssongAnti stresssong
Anti stresssong
 
Midlands Alternative Mining Indaba Declaration 2014
Midlands Alternative Mining Indaba Declaration  2014Midlands Alternative Mining Indaba Declaration  2014
Midlands Alternative Mining Indaba Declaration 2014
 
Aarad Homer's Visual Resume
Aarad Homer's Visual ResumeAarad Homer's Visual Resume
Aarad Homer's Visual Resume
 
นักพัฒ
นักพัฒนักพัฒ
นักพัฒ
 
Painted feathers
Painted feathersPainted feathers
Painted feathers
 
Excel - формули и функции
Excel - формули и функцииExcel - формули и функции
Excel - формули и функции
 
Toni bou i mena
Toni bou i menaToni bou i mena
Toni bou i mena
 
SQL Server Worst Practices
SQL Server Worst PracticesSQL Server Worst Practices
SQL Server Worst Practices
 
2014 the boulder history museum
2014 the boulder history museum2014 the boulder history museum
2014 the boulder history museum
 
конференц сервис Happy spaces
конференц сервис Happy spacesконференц сервис Happy spaces
конференц сервис Happy spaces
 
Learn Database Design with MySQL - Chapter 4 - Data types
Learn Database Design with MySQL - Chapter 4 - Data typesLearn Database Design with MySQL - Chapter 4 - Data types
Learn Database Design with MySQL - Chapter 4 - Data types
 
Expand Your Loft Space
Expand Your Loft Space Expand Your Loft Space
Expand Your Loft Space
 
Jonas Salk by Carlee
Jonas Salk by CarleeJonas Salk by Carlee
Jonas Salk by Carlee
 
Midlands PAMI press statement-final
Midlands PAMI press statement-finalMidlands PAMI press statement-final
Midlands PAMI press statement-final
 
How to have the best Christmas
How to have the best ChristmasHow to have the best Christmas
How to have the best Christmas
 

Similar a Introduction

Similar a Introduction (20)

Compiler1
Compiler1Compiler1
Compiler1
 
1 compiler outline
1 compiler outline1 compiler outline
1 compiler outline
 
1 cc
1 cc1 cc
1 cc
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.ppt
 
Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of Compiler
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
CD U1-5.pptx
CD U1-5.pptxCD U1-5.pptx
CD U1-5.pptx
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
 
Compiler design
Compiler designCompiler design
Compiler design
 
Cd econtent link1
Cd econtent link1Cd econtent link1
Cd econtent link1
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 

Más de Royalzig Luxury Furniture

A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...Royalzig Luxury Furniture
 
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production UnitIndia's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production UnitRoyalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chairHand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chairRoyalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table Royalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa setHand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa setRoyalzig Luxury Furniture
 
Royalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood BedRoyalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood BedRoyalzig Luxury Furniture
 

Más de Royalzig Luxury Furniture (20)

A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
 
French Style Slim Carving Bedroom Sets
French Style Slim Carving Bedroom SetsFrench Style Slim Carving Bedroom Sets
French Style Slim Carving Bedroom Sets
 
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production UnitIndia's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
 
Luxury furniture manufacturer in india
Luxury furniture manufacturer in indiaLuxury furniture manufacturer in india
Luxury furniture manufacturer in india
 
bone inlay hand carved luxury table
bone inlay hand carved luxury table bone inlay hand carved luxury table
bone inlay hand carved luxury table
 
Hand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chairHand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chair
 
beautiful hand carved dressing table
beautiful hand carved dressing table  beautiful hand carved dressing table
beautiful hand carved dressing table
 
Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table
 
Hand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa setHand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa set
 
Royalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood BedRoyalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood Bed
 
hand carved luxury wedding throne
hand carved luxury wedding thronehand carved luxury wedding throne
hand carved luxury wedding throne
 
Hand carved Luxury wood divan
Hand carved Luxury wood divanHand carved Luxury wood divan
Hand carved Luxury wood divan
 
Royalzig luxury furniture
Royalzig luxury furnitureRoyalzig luxury furniture
Royalzig luxury furniture
 
Royalzigppt 004
Royalzigppt 004Royalzigppt 004
Royalzigppt 004
 
Royalzig high end furniture
Royalzig high end furnitureRoyalzig high end furniture
Royalzig high end furniture
 
Royalzigppt 002
Royalzigppt 002Royalzigppt 002
Royalzigppt 002
 
Transcript
TranscriptTranscript
Transcript
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Run time
Run timeRun time
Run time
 

Último

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 

Último (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 

Introduction

  • 1. TCS 502 Compiler Designp g CS416 Compiler Design 1P K Singh
  • 2. Course Information Textbook: Alfred V Aho Ravi Sethi and Jeffrey D UllmanAlfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, “Compilers: Principles, Techniques, and Tools” Addison-Wesley, 1986.Addison Wesley, 1986. • Course Web Page: http://pksmmmec.googlepages.com CS416 Compiler Design 2P K Singh
  • 3. Preliminaries Required • Basic knowledge of programming languages. • Basic knowledge of FSA and CFGBasic knowledge of FSA and CFG. • Knowledge of a high programming language for the programming assignments. CS416 Compiler Design 3P K Singh
  • 4. Course Outline • Introduction to Compiling • Lexical Analysisy • Syntax Analysis – Context Free GrammarsContext Free Grammars – Top-Down Parsing, LL Parsing – Bottom-Up Parsing, LR Parsingp g g CS416 Compiler Design 4P K Singh
  • 5. Course Outline • Syntax-Directed Translation – Attribute Definitions – Evaluation of Attribute Definitions • Semantic Analysis, Type Checking • Run-Time Organization • Intermediate Code GenerationIntermediate Code Generation • Code Optimization P K Singh CS416 Compiler Design 5
  • 6. COMPILERS • A compiler is a program takes a program written in a source language and translates it into an equivalent program in a targetg g q p g g language. source program COMPILER target program ( ll i i ( ll h i l i error messages ( Normally a program written in a high-level programming language) ( Normally the equivalent program in machine code – relocatable object file) error messages CS416 Compiler Design 6P K Singh
  • 7. Major Parts of Compilers • There are two major parts of a compiler: Analysis and SynthesisSynthesis • In analysis phase an intermediate representation is• In analysis phase, an intermediate representation is created from the given source program. – Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts ofy y y y p this phase. • In synthesis phase, the equivalent target program is t d f thi i t di t t ticreated from this intermediate representation. – Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase. CS416 Compiler Design 7P K Singh
  • 8. Phases of A Compiler Lexical Analyzer Semantic Analyzer Syntax Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program Source Program • Each phase transforms the source program from one representationp p g p into another representation. • They communicate with error handlers• They communicate with error handlers. • They communicate with the symbol table. CS416 Compiler Design 8P K Singh
  • 9. Lexical Analyzer • Lexical Analyzer reads the source program character by character and returns the tokens of the source program. • A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, delimeters and so on), ) Ex: newval := oldval + 12 => tokens: newval identifier := assignment operator oldval identifier + add operator 12 a number • Puts information about identifiers into the symbol table.u s o a o abou de e s o e sy bo ab e • Regular expressions are used to describe tokens (lexical constructs). A (Deterministic) Finite State Automaton can be used in the CS416 Compiler Design 9 • A (Deterministic) Finite State Automaton can be used in the implementation of a lexical analyzer. P K Singh
  • 10. Syntax Analyzer • A Syntax Analyzer creates the syntactic structure (generally a parse tree) of the given program.p ) g p g • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. assgstmt identifier := expression • In a parse tree, all terminals are at leaves. newval expression + expression id tifi b p , • All inner nodes are non-terminals in a context free grammar. identifier number oldval 12 CS416 Compiler Design 10P K Singh
  • 11. Syntax Analyzer (CFG) • The syntax of a language is specified by a context free grammar (CFG).g ( ) • The rules in a CFG are mostly recursive. • A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. – If it satisfies, the syntax analyzer creates a parse tree for the given program. • Ex: We use BNF (Backus Naur Form) to specify a CFG assgstmt -> identifier := expression expression -> identifier expression -> number expression -> expression + expression CS416 Compiler Design 11P K Singh
  • 12. Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized by the lexical analyzer, and which ones by the syntax analyzer?y , y y y – Both of them do similar things; But the lexical analyzer deals with simple non- recursive constructs of the language. – The syntax analyzer deals with recursive constructs of the language. – The lexical analyzer simplifies the job of the syntax analyzer. – The lexical analyzer recognizes the smallest meaningful units (tokens) in a source program. – The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language. CS416 Compiler Design 12P K Singh
  • 13. Parsing Techniques • Depending on how the parse tree is created, there are different parsing techniques. These parsing techniques are categorized into two groups:• These parsing techniques are categorized into two groups: – Top-Down Parsing, – Bottom-Up Parsingp g • Top-Down Parsing: – Construction of the parse tree starts at the root, and proceeds towards the leaves. – Efficient top-down parsers can be easily constructed by hand.Efficient top down parsers can be easily constructed by hand. – Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing). • Bottom-Up Parsing: – Construction of the parse tree starts at the leaves and proceeds towards the rootConstruction of the parse tree starts at the leaves, and proceeds towards the root. – Normally efficient bottom-up parsers are created with the help of some software tools. – Bottom-up parsing is also known as shift-reduce parsing. CS416 Compiler Design 13 – Operator-Precedence Parsing – simple, restrictive, easy to implement – LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR P K Singh
  • 14. Semantic Analyzer • A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation.yp g • Type-checking is an important part of semantic analyzer. • Normally semantic information cannot be represented by a context-free language used in syntax analyzers. • Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules)with attributes (semantic rules) – the result is a syntax-directed translation, – Attribute grammars E• Ex: newval := oldval + 12 Th t f th id tifi l t t h ith t f th i ( ld l 12) CS416 Compiler Design 14 • The type of the identifier newval must match with type of the expression (oldval+12) P K Singh
  • 15. Intermediate Code Generation • A compiler may produce an explicit intermediate codes representing the source program.p g p g • These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the le el of machine codeslevel of machine codes. • Ex: newval := oldval * fact + 1 id1 := id2 * id3 + 1 MULT id2,id3,temp1 Intermediates Codes (Quadraples) ADD temp1,#1,temp2 MOV temp2,,id1 CS416 Compiler Design 15 p ,, P K Singh
  • 16. Code Optimizer (for Intermediate Code Generator) • The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space.g p • Ex: MULT id2,id3,temp1 ADD temp1,#1,id1ADD temp1,#1,id1 CS416 Compiler Design 16P K Singh
  • 17. Code Generator • Produces the target language in a specific architecture. • The target program is normally is a relocatable object fileThe target program is normally is a relocatable object file containing the machine codes. • Ex: ( assume that we have an architecture with instructions whose at least one of its operands isis a machine register) MOVE id2 R1MOVE id2,R1 MULT id3,R1 ADD #1,R1 MOVE R1 id1 CS416 Compiler Design 17 MOVE R1,id1 P K Singh