SlideShare a Scribd company logo
1 of 24
Compiler Construction: An
      Introduction




          Delivery By:
          Ms. Kavita Choudhary
          Assistant Professor (Sr.), CSE
Outline
•   Do we know……What is a compiler…?
•   Compilers… Why?
•   Is there any prerequisite?
•   Phases of Compiler
•   References
•   Queries???
Do we know……What is a compiler…?
• A compiler is a program takes a program written
  in a source language and translates it into an
  equivalent program in a target language.

  Source Program      COMPILER         Target
                                       Program

                      Error Messages
Compilers…. Why ??
• Isn’t it a very old discipline?
   – Yes, it is a well-established discipline: Algorithms,
     methods and techniques are researched and
     developed in early stages of computer science growth.
   – Now, there are many compilers available around and
     many tools to generate them automatically.

• So, why do we need to learn it?
   – But the techniques we learn is useful in many tasks
     like writing an interpreter for a scripting language,
     validation checking for forms and so on.
                                              contd…
– Techniques used in a lexical analyzer can be used
  in text editors, information retrieval system, and
  pattern recognition programs.
– Techniques used in a parser can be used in a query
  processing system such as SQL.
– Many software having a complex front-end may
  need techniques used in compiler design.
– Most of the techniques used in compiler design
  can be used in Natural Language Processing (NLP)
  systems.
Is there any prerequisite…???

• Basic knowledge of Programming languages.
• Basic knowledge of FSA and CFG.
• An open perspective to visualize the concepts.
Phases of Compilers
• Analysis phase:
  An intermediate representation is created from
  the given source program.
  – Lexical Analyzer, Syntax Analyzer and Semantic
    Analyzer are the parts of this phase.
• Synthesis phase:
  The equivalent target program is created from
  the intermediate representation.
  – Intermediate Code Generator, Code Generator, and
    Code Optimizer are the parts of this phase.
Lexical Analysis
• 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,
  delimiters and so on)

  Ex:   newval := oldval + 12   => tokens:   newval   identifier
                                             :=       assignment operator
                                             oldval   identifier
                                             +        add operator
                                             12       a number
                                                                        9
Syntax Analysis
• A Syntax Analyzer creates the syntactic structure (generally
  a parse tree) of the given program.
• A syntax analyzer is also called as a parser.
• A parse tree describes a syntactic structure.
                    assgstmt

       identifier     :=       expression

       newval              expression   +   expression

                           identifier       number

                           oldval             12


                                                             10
What is Syntax?
• The syntax of a language is specified by a context free
  grammar (CFG).
• 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: To specify a CFG
       assg_stmt    ->   identifier := expression
       expression   ->   identifier
       expression   ->   number
       expression   ->   expression + expression

                                                                11
Which constructs of a program should be recognized
by the lexical analyzer, and which ones by the syntax
analyzer?
  – 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.
                                                          12
Semantic Analysis
• A semantic analyzer checks the source program for semantic errors and
  collects the type information for the code generation.
• 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)
   – the result is a syntax-directed translation,
   – Attribute grammars
• Ex:
      newval := oldval + 12
        • The type of the identifier newval must match with type of the
          expression (oldval+12)

                                                                     13
Intermediate Code Generation
• A compiler may produce an explicit intermediate codes
  representing the source program.
• These intermediate codes are generally machine
  (architecture independent). But the level of intermediate
  codes is close to the level 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

                                                            14
Code Optimization
• The code optimizer optimizes the code
  produced by the intermediate code generator
  in the terms of time and space.

• Ex:
        MULT id2,id3,temp1
        ADD temp1,#1,id1


                                            15
Code Generation
• Produces the target language in a specific architecture.
• The 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 is a machine register)


     MOVE      id2,R1
     MULT      id3,R1
     ADD       #1,R1
     MOVE      R1,id1


                                                                        16
One/ Multi- Pass Compilers
• Passes are introduced due to hardware resource
  limitations.
• Compilers were split up into smaller programs
  which each made a pass over the source (or some
  representation of it) performing some of the
  required analysis and translations.
• One- pass Compilers are easy to write and even
  they are faster than Multi- pass compilers but
  fails in generating a high quality code.
• Multi- pass compilers are the requirements of a
  language & its features.
Types of Compilers
•   Native/ Hosted Compilers
•   Cross Compilers
•   Hardware Compatible Compilers
•   Online Compilers
•   Source- to- source Compiler
•   Stage Compiler
•   Just- in –time Compiler
Exercise 1
• Consider a statement:
            z := a + b * c – d / e
  Here, z, b, e are integers & a, c, d are float.

1. Lexical Analysis:
  Z, a, b, c, d, e  Identifiers
  :=, +, *, -, /  Operators
2. Syntax Analysis:
                :=

           z           -

                +           /

           a          * d       e

                b           c
3. Semantic Analysis:
  Type Conversion is required:
  3.1. int- to- float….???
  3.2. float – to- int….???

4. Intermediate Code Generation:
      id1 := id2 + id3 * id4 – id5 / id6
  Generate three address code from this..
5. Code Optimization

6. Code Generation
References
• TextBook:
  “Compilers: Principles, Techniques, and Tools”
  Alfred V. Aho, Ravi Sethi, and Jeffrey D.
  Ullman, Addison-Wesley, 1986.
• Reference Books:
  “Basics of Compiler Design” Torben Ægidius
  Mogensen.
1 cc

More Related Content

What's hot

What's hot (19)

Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Introduction to compilers
Introduction to compilersIntroduction to compilers
Introduction to compilers
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
Presentation1
Presentation1Presentation1
Presentation1
 
Chap 1-language processor
Chap 1-language processorChap 1-language processor
Chap 1-language processor
 
Chapter One
Chapter OneChapter One
Chapter One
 
Presentation compiler design
Presentation compiler designPresentation compiler design
Presentation compiler design
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1
 
Compiler design
Compiler designCompiler design
Compiler design
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - Introduction
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 

Viewers also liked

Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursorsinfo_zybotech
 
Shipping and freight
Shipping and freightShipping and freight
Shipping and freightCele Ko
 
Shipping and freight
Shipping and freightShipping and freight
Shipping and freightCele Ko
 
Service goes accessible_2013_sh
Service goes accessible_2013_shService goes accessible_2013_sh
Service goes accessible_2013_shTomppa Järvinen
 
Imc2+f2012 (1)
Imc2+f2012 (1)Imc2+f2012 (1)
Imc2+f2012 (1)Emma Daly
 
Climate Change
Climate ChangeClimate Change
Climate Changesneapa
 

Viewers also liked (11)

Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
Shipping and freight
Shipping and freightShipping and freight
Shipping and freight
 
Shipping and freight
Shipping and freightShipping and freight
Shipping and freight
 
Service goes accessible_2013_sh
Service goes accessible_2013_shService goes accessible_2013_sh
Service goes accessible_2013_sh
 
Busy codersguidetoandroid
Busy codersguidetoandroidBusy codersguidetoandroid
Busy codersguidetoandroid
 
Imc2+f2012 (1)
Imc2+f2012 (1)Imc2+f2012 (1)
Imc2+f2012 (1)
 
Evaluation+question+4
Evaluation+question+4Evaluation+question+4
Evaluation+question+4
 
Cv emprenedores
Cv emprenedoresCv emprenedores
Cv emprenedores
 
Guila real
 Guila real Guila real
Guila real
 
Opening Credit Timeline
Opening Credit TimelineOpening Credit Timeline
Opening Credit Timeline
 
Climate Change
Climate ChangeClimate Change
Climate Change
 

Similar to 1 cc

Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of CompilerPreethi AKNR
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdfwigewej294
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction Sarmad Ali
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compilerAbha Damani
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overviewamudha arul
 
introduction to computer vision and image processing
introduction to computer vision and image processingintroduction to computer vision and image processing
introduction to computer vision and image processingpakboy12
 

Similar to 1 cc (20)

1 compiler outline
1 compiler outline1 compiler outline
1 compiler outline
 
Compiler1
Compiler1Compiler1
Compiler1
 
Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of Compiler
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
Presentation1
Presentation1Presentation1
Presentation1
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
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
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Compiler design
Compiler designCompiler design
Compiler design
 
Cd econtent link1
Cd econtent link1Cd econtent link1
Cd econtent link1
 
Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
System software
System softwareSystem software
System software
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
introduction to computer vision and image processing
introduction to computer vision and image processingintroduction to computer vision and image processing
introduction to computer vision and image processing
 
Introduction
IntroductionIntroduction
Introduction
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

1 cc

  • 1. Compiler Construction: An Introduction Delivery By: Ms. Kavita Choudhary Assistant Professor (Sr.), CSE
  • 2. Outline • Do we know……What is a compiler…? • Compilers… Why? • Is there any prerequisite? • Phases of Compiler • References • Queries???
  • 3. Do we know……What is a compiler…? • A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. Source Program COMPILER Target Program Error Messages
  • 4. Compilers…. Why ?? • Isn’t it a very old discipline? – Yes, it is a well-established discipline: Algorithms, methods and techniques are researched and developed in early stages of computer science growth. – Now, there are many compilers available around and many tools to generate them automatically. • So, why do we need to learn it? – But the techniques we learn is useful in many tasks like writing an interpreter for a scripting language, validation checking for forms and so on. contd…
  • 5. – Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern recognition programs. – Techniques used in a parser can be used in a query processing system such as SQL. – Many software having a complex front-end may need techniques used in compiler design. – Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.
  • 6. Is there any prerequisite…??? • Basic knowledge of Programming languages. • Basic knowledge of FSA and CFG. • An open perspective to visualize the concepts.
  • 7. Phases of Compilers • Analysis phase: An intermediate representation is created from the given source program. – Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase. • Synthesis phase: The equivalent target program is created from the intermediate representation. – Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase.
  • 8.
  • 9. Lexical Analysis • 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, delimiters and so on) Ex: newval := oldval + 12 => tokens: newval identifier := assignment operator oldval identifier + add operator 12 a number 9
  • 10. Syntax Analysis • A Syntax Analyzer creates the syntactic structure (generally a parse tree) of the given program. • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. assgstmt identifier := expression newval expression + expression identifier number oldval 12 10
  • 11. What is Syntax? • The syntax of a language is specified by a context free grammar (CFG). • 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: To specify a CFG assg_stmt -> identifier := expression expression -> identifier expression -> number expression -> expression + expression 11
  • 12. Which constructs of a program should be recognized by the lexical analyzer, and which ones by the syntax analyzer? – 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. 12
  • 13. Semantic Analysis • A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. • 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) – the result is a syntax-directed translation, – Attribute grammars • Ex: newval := oldval + 12 • The type of the identifier newval must match with type of the expression (oldval+12) 13
  • 14. Intermediate Code Generation • A compiler may produce an explicit intermediate codes representing the source program. • These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the level 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 14
  • 15. Code Optimization • The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space. • Ex: MULT id2,id3,temp1 ADD temp1,#1,id1 15
  • 16. Code Generation • Produces the target language in a specific architecture. • The 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 is a machine register) MOVE id2,R1 MULT id3,R1 ADD #1,R1 MOVE R1,id1 16
  • 17. One/ Multi- Pass Compilers • Passes are introduced due to hardware resource limitations. • Compilers were split up into smaller programs which each made a pass over the source (or some representation of it) performing some of the required analysis and translations. • One- pass Compilers are easy to write and even they are faster than Multi- pass compilers but fails in generating a high quality code. • Multi- pass compilers are the requirements of a language & its features.
  • 18. Types of Compilers • Native/ Hosted Compilers • Cross Compilers • Hardware Compatible Compilers • Online Compilers • Source- to- source Compiler • Stage Compiler • Just- in –time Compiler
  • 19. Exercise 1 • Consider a statement: z := a + b * c – d / e Here, z, b, e are integers & a, c, d are float. 1. Lexical Analysis: Z, a, b, c, d, e  Identifiers :=, +, *, -, /  Operators
  • 20. 2. Syntax Analysis: := z - + / a * d e b c
  • 21. 3. Semantic Analysis: Type Conversion is required: 3.1. int- to- float….??? 3.2. float – to- int….??? 4. Intermediate Code Generation: id1 := id2 + id3 * id4 – id5 / id6 Generate three address code from this..
  • 22. 5. Code Optimization 6. Code Generation
  • 23. References • TextBook: “Compilers: Principles, Techniques, and Tools” Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, Addison-Wesley, 1986. • Reference Books: “Basics of Compiler Design” Torben Ægidius Mogensen.