COMPILER CONSTRUCTION
Instructor:
       Mr. Sheraz Babar
                          1
Lecture 02
             2
Language Processing System
Language Processing System
 Preprocessor
  A preprocessor produce input to compilers. They may
  perform the following functions.
       1. Macro processing: A preprocessor may allow a user
  to define macros that are short hands for longer
  constructs.
       2. File inclusion: A preprocessor may include header
  files into the program text.
       3. Rational preprocessor: these preprocessors
  augment older languages with more modern flow-ofcontrol
  and data structuring facilities.
       4. Language Extensions: These preprocessor attempts
  to add capabilities to the language by certain amounts to
  build-in macro                                       4
Language Processing System
 COMPILER
  Compiler is a translator program that translates a program
  written in (HLL) the source program and translate it into an
  equivalent program in (MLL) the target program. As an
  important part of a compiler is error showing to the
  programmer.
                                                          5
Language Processing System
 INTERPRETER
  An interpreter is a program that appears to
  execute a source program as if it were machine
  language.
                                               6
Language Processing System
 ASSEMBLER
  Programmers found it difficult to write or read programs in
  machine language. They begin to use a mnemonic
  (symbols) for each machine instruction, which they would
  subsequently translate into machine language. Such a
  mnemonic machine language is now called an assembly
  language. Programs known as assembler were written to
  automate the translation of assembly language in to
  machine language. The input to an assembler program is
  called source program, the output is a machine language
   translation (object program).
                                                          7
Language Processing System
 Linker
  Linker is a computer program that links and merges various
  object files together in order to make an executable file. All
  these files might have been compiled by separate assemblers.
  The major task of a linker is to search and locate referenced
  module/routines in a program and to determine the memory
  location where these codes will be loaded, making the program
  instruction to have absolute references.
 Loader
  Loader is a part of operating system and is responsible for
  loading executable files into memory and execute them. It
  calculates the size of a program instructionsanddata and
  creates memory space for it. It initializes various registers to
  initiate execution.                                              8
LIST OF COMPILERS
 1. Ada compilers
 2 .ALGOL compilers
 3 .BASIC compilers
 4 .C# compilers
 5 .C compilers
 6 .C++ compilers
 7 .COBOL compilers
 8 .Common Lisp compilers
 9. ECMAScript interpreters
 10. Fortran compilers
 11 .Java compilers
 12. Pascal compilers
 13. PL/I compilers
 14. Python compilers
 15. Smalltalk compilers
                              9
Compiler Architecture
A compiler can broadly be divided into two phases based
on the way they compile.
 Analysis Phase
  Known as the front-end of the compiler, the analysis
  phase of the compiler reads the source
  program, divides it into core parts and then checks for
  lexical, grammar and syntax errors.The
  analysis phase generates an intermediate representation
  of the source program and symbol table,
  which should be fed to the Synthesis phase as input.
                                                      10
Compiler Architecture
   Synthesis Phase
    Known as the back-end of the compiler, the synthesis phase
    generates the target program with
    the help of intermediate source code representation and symbol table.
    A compiler can have many phases and passes.
        Pass : A pass refers to the traversal of a compiler through
        the entire program.
        Phase : A phase of a compiler is a distinguishable stage, which
        takes input from the previous stage, processes and yields output
        that can be used as input for the next stage. A pass can have
        more than one phase.
                                                                      11
Phases of a compiler
                       12
Phases of a compiler
 Lexical Analysis:-
  LA or Scanners reads the source program one
  character at a time, carving the source program into a
  sequence of automic units called tokens.
 Syntax Analysis:-
  The second stage of translation is called Syntax
  analysis or parsing. In this phase expressions,
  statements, declarations etc… are identified by using
  the results of lexical analysis. Syntax analysis is
  aided by using techniques based on formal grammar
  of the programming language.
                                                    13
Phases of a compiler
 Semantic Analysis
      Semantic analysis checks whether the parse
  tree constructed follows the rules of language. For
  example, assignment of values is between
  compatible data types, and adding string to an
  integer.
      Also, the semantic analyzer keeps track of
  identifiers, their types and expressions; whether
  identifiers are declared before use or not etc. The
  semantic analyzer produces an annotated
  syntax tree as an output.
                                                 14
Phases of a compiler
 Intermediate Code Generations:-
  An intermediate representation of the final
  machine language code is produced. This phase
  bridges the analysis and synthesis phases of
  translation.
 Code Optimization :-
  This is optional phase described to improve the
  intermediate code so that the output runs faster
  and takes less space.
                                                 15
Phases of a compiler
 Code Generation:-
  The last phase of translation is code generation. A
  number of optimizations to reduce the length of
  machine language program are carried out during
  this phase. The output of the code generator is
  the machine language program of the specified
  computer.
                                                   16
                    The Phases of a Compiler
                   Phase                                         Output                                  Sample
Programmer (source code producer)               Source string                             A=B+C;
Scanner (performs lexical analysis)             Token string                              ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
                                                                                          And symbol table with names
Parser (performs syntax analysis based on the   Parse tree or abstract syntax tree          ;
                                                                                            |
grammar of the programming language)                                                        =
                                                                                           / \
                                                                                          A    +
                                                                                              / \
                                                                                            B    C
Semantic analyzer (type checking, etc)          Annotated parse tree or abstract syntax
                                                tree
Intermediate code generator                     Three-address code, quads, or             int2fp B                 t1
                                                RTL(Register Transfer Language)           +      t1         C      t2
                                                                                          :=     t2                A
Optimizer                                       Three-address code, quads, or RTL         int2fp B                 t1
                                                                                          +      t1       #2.3     A
Code generator                                  Assembly code                             MOVF #2.3,r1
                                                                                          ADDF2 r1,r2
                                                                                          MOVF r2,A
                                                                                                                         17
The Grouping of Phases
   Compiler front and back ends:
     • Front end: analysis (machine independent)
     • Back end: synthesis (machine dependent)
   Compiler passes:
   Pass is the reading of a source file once.
     • A collection of phases is done only once (single pass) or multiple times
       (multi pass)
        • Single pass: usually requires everything to be defined before being used in source
          program
        • Multi pass: compiler may have to keep entire program representation in memory
                                                                                         18
Compiler-Construction Tools
 Software development tools are available to
  implement one or more compiler phases
   •   Scanner generators
   •   Parser generators
   •   Syntax-directed translation engines
   •   Automatic code generators
   •   Data-flow engines
                                                19
The End
          20