Unit - 1 Overview of The Compiler & It's Structure: Prof. Dixita B. Kagathara
Unit - 1 Overview of The Compiler & It's Structure: Prof. Dixita B. Kagathara
GTU # 3170701
Unit – 1
Overview of the Compiler
&
it’s Structure
Translator
Translator
 A translator is a program that takes one form of program as input and converts it into another
  form.
 Types of translators are:
    1. Compiler
    2. Interpreter
    3. Assembler
                                                           Error
                                                         Messages (If any)
                                          #3170701 (CD)  Unit 1– Overview of the Compiler & it’s
     Prof. Dixita B Kagathara                                                                            8
Compiler
 A compiler is a program that reads a program written in source language and translates it into
  an equivalent program in target language.
   1. Analysis Phase
   2. Synthesis Phase
Compiler
                              Lexical analysis
                                                            Intermediate                                   Code
                                                                code                                   optimization
                              Syntax analysis                generation
                                                                                                      Code generation
                              Semantic analysis
Compiler
                              Lexical analysis
                                                            Intermediate                                   Code
                                                                code                                   optimization
                              Syntax analysis                generation
                                                                                                      Code generation
                              Semantic analysis
id1 +
                                                                                                      id2         *
                                                                                                            id3       60
Compiler
                              Lexical analysis
                                                            Intermediate                                   Code
                                                                code                                   optimization
                              Syntax analysis                generation
                                                                                                      Code generation
                              Semantic analysis
                                                                                                                 id2             *
                                                                                                                         id3         inttoreal
Compiler
                              Lexical analysis
                                                            Intermediate                                   Code
                                                                code                                   optimization
                              Syntax analysis                generation
                                                                                                      Code generation
                              Semantic analysis
Compiler
                              Lexical analysis
                                                            Intermediate                                   Code
                                                                code                                   optimization
                              Syntax analysis                generation
                                                                                                      Code generation
                              Semantic analysis
Code optimization
Compiler
                              Lexical analysis
                                                            Intermediate                                   Code
                                                                code                                   optimization
                              Syntax analysis                generation
                                                                                                      Code generation
                              Semantic analysis
Code generation
                                                                                     MOV id3, R2
                                                                                     MUL #60.0, R2
                                                                                     MOV id2, R1
                                                                                     ADD R2,R1
                                                                                     MOV R1, id1
                                                                                                                Id3R2
                                                                                                                Id2R1
                                 #3170701 (CD)  Unit 1– Overview of the Compiler & it’s
     Prof. Dixita B Kagathara                                                                                     27
Phases of compiler
                                                          Source program
                                                                                           Analysis Phase
                                                          Lexical analysis
Syntax analysis
                                                        Semantic analysis
                         Symbol table                                                                  Error detection
                                                                                                        and recovery
                                                        Intermediate code
                               Compiler                                                     Interpreter
    Scans the entire program and translates it It translates program’s one statement at a
    as a whole into machine code.              time.
    It generates intermediate code.            It does not generate intermediate code.
    An error is displayed after entire program is An error is displayed for every instruction
    checked.                                      interpreted if any.
    Memory requirement is more.                   Memory requirement is less.
    Example: C compiler                                          Example: Basic, Python, Ruby 
                                                                                                 Compiler
    Preprocessor
    Assembler                                                                                          Target Assembly
                                                                                                            Program
    Linker
                                                                                                 Assembler
    Loader
                                                                                                        Relocatable Object
                                                                                                              Code
                                                                           Libraries &         Linker / Loader
                                                                           Object Files
                                                                                             Absolute Machine
                                                                                                   Code
                                 #3170701 (CD)  Unit 1– Overview of the Compiler & it’s
     Prof. Dixita B Kagathara                                                                                        34
Context of compiler (Cousins of compiler)
                                                                                                  Skeletal Source Program
 Preprocessor
 Some of the task performed by preprocessor:                                                         Preprocessor
1.   Macro processing: Allows user to define macros. Ex: #define                                               Source Program
     PI 3.14159265358979323846
                                                                                                        Compiler
2.   File inclusion: A preprocessor may include the header file
     into the program. Ex: #include<stdio.h>                                                                   Target Assembly
3.   Rational preprocessor: It provides built in macro for                                                         Program
     construct like while statement or if statement.                                                    Assembler
4.   Language extensions: Add capabilities to the language by                                                  Relocatable Object
     using built-in macros.                                                                                          Code
      Ex: the language equal is a database query language                        Libraries &         Linker / Loader
                                                                                  Object Files
       embedded in C. Statement beginning with ## are taken by
       preprocessor to be database access statement unrelated
       to C and translated into procedure call on routines that                                     Absolute Machine
                                                                                                          Code
       perform the database access.
                                        #3170701 (CD)  Unit 1– Overview of the Compiler & it’s
       Prof. Dixita B Kagathara                                                                                             35
Context of compiler (Cousins of compiler)
                                                                                            Skeletal Source Program
   Compiler
 A compiler is a program that reads a program                                                  Preprocessor
                                                                                                         Target Assembly
                                                                                                             Program
                                                                                                  Assembler
                                                                                                         Relocatable Object
                                                                                                               Code
                                                                            Libraries &         Linker / Loader
                                                                            Object Files
                                                                                              Absolute Machine
                                                                                                    Code
                                  #3170701 (CD)  Unit 1– Overview of the Compiler & it’s
     Prof. Dixita B Kagathara                                                                                         36
Context of compiler (Cousins of compiler)
                                                                                          Skeletal Source Program
  Assembler
 Assembler is a translator which takes the assembly                                          Preprocessor
                                                                                                       Target Assembly
                                                                                                           Program
                                                                                                Assembler
                                                                                                       Relocatable Object
                                                                                                             Code
                                                                          Libraries &         Linker / Loader
                                                                          Object Files
                                                                                            Absolute Machine
                                                                                                  Code
                                #3170701 (CD)  Unit 1– Overview of the Compiler & it’s
     Prof. Dixita B Kagathara                                                                                       37
Context of compiler (Cousins of compiler)
                                                                                            Skeletal Source Program
    Linker
 Linker makes a single program from a several files                                            Preprocessor
Pass I:
Pass II: