+(reset)-

Compiler

In this part you will learn what the compiler and the interpreter are.

Compilation

A compiler is a computer program (or set of programs) that transforms source code written in a programming language into another computer language.

A compiler requires:

  1. determining the correctness of the syntax of programs,
  2. generating correct and efficient object code,
  3. run-time organization,
  4. formatting output according to assembler and/or linker conventions.

CompilerSchematic of the compiler


A Translator is a computer program that translates one programming language instruction(s) into another programming language instruction(s) without the loss of original meaning. Or,the translator will translate X language and produce X’ language. In some advanced translator will even change the logic (not meaning) or will simplify the logic without losing the essence.

Compilation is the translation of source code into object code by a compiler.

Each compilation consists of two parts:

  • analysis
  • synthesis

Analysis and synthesis

An analysis divides program into components and creates the intermediate representation. This part consists of three phases:

  1. Lexical analysis (lexing,scanning) breaks the source code text into small pieces called tokens. Each token is a single atomic unit of the language,for instance a keyword,identifier or symbol name. The token syntax is typically a regular language,so a finite state automaton constructed from a regular expression can be used to recognize it. This phase is also called lexing or scanning,and the software doing lexical analysis is called a lexical analyzer or scanner.
  2. Syntactic analysis (parsing) is the process of analyzing a text,made of a sequence of tokens,to determine its grammatical structure with respect to a given formal grammar.
  3. Semantic analysis is the phase in which the compiler adds semantic information to the parse tree and builds the symbol table. This phase performs semantic checks such as type checking (checking for type errors),or object binding (associating variable and function references with their definitions),or definite assignment (requiring all local variables to be initialized before use),rejecting incorrect programs or issuing warnings.

A synthesis requiresmore specialized methods. It involves converting intermediate representation in the executing program.

The structure of a compiler

A compiler consists of three main parts:the frontend,the middle-end,and the backend.

The front end checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported,if any,in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation of the source code for processing by the middle-end.

The middle end is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code,discovery and propagation of constant values,relocation of computation to a less frequently executed place,or specialization of computation based on the context. The middle-end generates another intermediate representation for the following backend. Most optimization efforts are focused on this part.

The back end is responsible for translating the intermediate representation from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Variables are also selected for the registers. Backend utilizes the hardware by figuring out how to keep parallel FUs busy,filling delay slots,and so on. Although most algorithms for optimization are in NP,heuristic techniques are well-developed.

A compiler and an interpreter

An interpreter is a computer program that executes,i.e. performs ,instructions written in a programming language. An interpreter may be a program that either

  1. executes the source code directly
  2. translates source code into some efficient intermediate representation and immediately executes this
  3. explicitly executes stored precompiled code made by a compiler which is part of the interpreter system
CompilerInterpreter
  • creates  an executable program
  • does not create  an executable program
  • does not execute any instructions of the program source
  • executes the program instructions for making a translation of the instructions
  • each instruction is translated only once
  • each instruction can be translated many times
  • programs after the compilation are executed  faster than through the interpretation
  • takes less storage space in memory,because there are only stored instructions currently being executed
  • compiled program can be optimized
  • interpreters construction is usually simpler,because they usually work for the first errorin the source code

Questions

  • What is the compiler?
  • Why does the compiler must report error messages?
  • What is the interpreter?

If you do not know the answer to these questions,read again the above content.

The structure of a compiler