Docsity

Compiler Design: Understanding the Process of Translating Source Code into Target Code, Study notes of Object Oriented Programming

Partial preview of the text

Download Compiler Design: Understanding the Process of Translating Source Code into Target Code and more Study notes Object Oriented Programming in PDF only on Docsity!

Compiler Design

Compilers

  • (^) A compiler is a program that reads a program in

one language, the source language and

translates into an equivalent program in another

language, the target language.

  • The translation process should also report the

presence of errors in the source program.

Source Program Compiler Target Program

Error Messages

Target program can be another programming language or machine language of any computer between microprocessor and supercomputer

Compiler design

  • (^) There are two parts to compilation:
    • (^) Analysis determines the operations

implied by the source program

which are recorded in a tree

structure

  • (^) Synthesis takes the tree structure

and translates the operations

therein into the target program

Some S/w Tools

  • Structure Editors
    • Takes sequence of commands as i/p to the source pgm; build appropriate hierarchical structure;(e.g) correct keywords, jump from begin to end left paranthesis to right
  • Pretty Printers
    • Print the pgm, so that structure of the pgm is clearly visible; (e.g) comments in special font, indentation
  • Static Checker
  • Discover bugs without running the pgm; (e.g) parts of pgm that neven been executed, catch logical errors
  • Interpreters
  • Performs operation implied by the source pgm; for assignment statement, built a tree and carryout the operation at the nodes as it walks the tree

Cousins of the Compiler

Preprocessor

Compiler

Assembler

Linker

Skeletal Source Program

Source Program

Target Assembly Program

Relocatable Object Code

Libraries and Relocatable Object Files

Absolute Machine Code

Analysis of the Source

Program

  • Linear analysis
    • It is a process of reading the characters from left to right and grouped into tokens
  • Hierarchical analysis
  • It does the grouping of tokens into grammatical

phases that are used by the compiler to synthesize output

  • Semantic analysis
  • It checks the source pgm for semantic errors and

gathers type information for the subsequent code generation phase

Cousins of the compiler

  • (^) Preprocessor.
    • Macro processing
    • File inclusion
    • Rational preprocessors
    • Language Extensions
  • Assembler.
    • One pass assembler
    • Two pass assembler
  • Loader and Link-editor.
    • Absolute loader
    • Re-locatable loader
    • Linkage editor
    • Dynamic linker

Grouping of phases

• Front end and back ends

• Passes

• Reducing the number of passes

Compiler construction tools

  • (^) Parser Generators:
    • The specification of input based on context-free grammar. They use powerful parsing algorithms that are too complex to be carried out by hand..
  • Scanner Generator:
    • The specification of input based on regular expression. The organization is based on finite automation.
  • Syntax-Directed Translation:
    • It walks the parse tee and as a result generate intermediate code.
  • Automatic Code Generators:
    • It takes collection of rules that defines translation for each operation of the intermediate code into machine language. It uses “template matching” technique.
  • Data-Flow Engines:
    • It does code optimization using data-flow analysis-how values are transformed from one part of a program to other parts.

The Grouping of Phases

  • (^) Compiler front and back ends :
    • (^) Front end: analysis ( machine independent )
    • (^) Back end: synthesis ( machine dependent )
  • (^) Compiler passes:
    • (^) 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