Docsity

Lecture 1 for Compiler Construction, Study notes of Compiler Construction

Partial preview of the text

Download Lecture 1 for Compiler Construction and more Study notes Compiler Construction in PDF only on Docsity!

Compiler Construction

Compiler Construction

Objectives of This Course

 Be familiar with the fundamental concepts in

compiler construction

 Be familiar with formalisms like Finite

Automata and context free grammars

Be familiar with various phases in compiler

construction

 Build a compiler for a subset of a high level

programming language, e.g. C/C++

Readings/References

 Text Book:

 Alfred V. Aho, Monica S. Lam , Ravi Sethi and

Jeffrey D. Ullman, "Compilers: Principles,

Techniques, and Tools"

Additional Readings:

 Internet

Pre-Requisites

 Theory of Automata

In this lecture we will cover the following

  • (^) First Compilers
  • (^) Cross Compiler and its uses
  • (^) Compiler
  • (^) Classification of Compilers
  • (^) Analysis-Synthesis Model of Compilation
  • (^) Lexemes and Tokens
  • (^) Editors and their types

Today’s Topics

  • (^) In computing, a compiler is a computer program that transforms source code written in a programming language or computer language (the source language ), into another computer language (the target language , often having a binary form known as object code ). The most common reason for wanting to transform source code is to create an executable program. Compiler Construction

First Compilers

  • (^) Software for early computers was primarily written in assembly language. It is usually more productive for a programmer to use a high level language, and programs written in a high level language are able to be reused on different kinds of computers. Even so, it took a while for compilers to become established, because they generated code that did not perform as well as very limited memory capacity of early computers created many technical problems for practical compiler implementations.
  • (^) The first compiler was written by Grace Hopper, in 1952, for the A-0 System language. The term compiler was coined by Hopper. The FORTRAN team led by John W. Backus at IBM is generally credited as having introduced the first complete compiler, in
    1. The first FORTRAN compiler took 18 person-years to create.

First Compilers (Contd...)

  • (^) By 1960, an extended FORTRAN compiler, ALTAC, was also available on the Philco 2000 (Computer), so a FORTRAN program was compiled for both computer architectures in mid-
  • (^) The first known demonstrated cross-platform high level language was COBOL. In a demonstration in December 1960, a COBOL program was compiled and executed on both the UNIVAC II and the RCA 501.
  • (^) The COBOL compiler for the UNIVAC II was probably the first to be written in a high level language, namely FLOW-MATIC, by a team led by Grace Hopper.

First Compilers (Contd...)

Uses of Cross Compiler

  • (^) The fundamental use of a cross compiler is to separate the build environment from target environment. This is useful in a number of situations. For example:
  • (^) Embedded computers where a device has extremely limited resources. For example, a microwave oven will have an extremely small computer to read its touchpad and door sensor, provide output to a digital display and speaker, and to control the machinery for cooking food. This computer will not be powerful enough to run a compiler, a file system, or a development environment. Since debugging and testing may also require more resources than are available on an embedded system, cross-compilation can be less involved and less prone to errors than native compilation.
  • (^) Compiling for multiple machines. For example, a company may wish to support several different versions of same operating system or to support several different operating systems. By using a cross compiler, a single build environment can be set up to compile for each of these targets.

Compiler

  • (^) A compiler can be defined as a program written in any
language, which reads a program written in one language
(called source program) and translates it into an equivalent
program in another language (called target program).
  • (^) In fact, compiler is a super program because its data are other
large program. During the process of compilation / translation,
the compiler identifies the errors (if any) in the source program.
  • (^) Generally, compiler is considered as a program that converts a user written program into machine readable form. But compiler can do a lot more than this. Actually a compiler can be generally defined as a program that converts a program from one form to another. That is why it falls in the category of “translation” software.

Classification of Compilers (Contd...)

  • (^) Load and go Such compilers are meant to assign actual memory to the target code so that it may execute.
  • (^) Debuggers Their sole purpose is to detect errors in the program as well as to see the values of variables at some specific step of the program.
  • (^) Optimizers Such compilers are designed to reduce (optimize) the code of source program so that faster running target code may result.

The Analysis-Synthesis model of compilation

Compilation process can be divided into two parts: I. Analysis II. Synthesis Analysis: The analysis part breaks up a program into its constituent pieces called tokens and creates an intermediate representation of source program. Synthesis: The synthesis part constructs the desired target program from the intermediate representation. Of the two parts, synthesis part requires more specialized techniques

Editors

  • (^) Editors are programs which are use to create and modify the text.
  • (^) Some editors are written to reduce the task of a compiler by performing some sort of compilation during the writing of a code.
  • (^) The types of editors are as follows: I. Structure Editors
  • (^) The structure editors not only perform the text creation and modification functions of an ordinary text editor but it also analyzes the program text putting an appropriate hierarchical structure of the program.
  • (^) Thus structure editors perform additional tasks that are useful in preparation of programs. For example it can check that input is in correct format. Similarly it can also supply keywords automatically.
  • (^) The structure editor is a graphical editor that you can use to model organizational structure and relationship.

Editors (Contd…)

II. Pretty Printers

  • (^) A pretty printer analyzes a program and prints it in such a way that the structure of the program becomes clearly visible e.g. comments may appear in a special font and statements may appear with an amount of indentation. III. Static Checkers
  • (^) A static checker reads a program analyzes it and attempts to discover potential bugs in it without running the program e.g. a static checker may detect those parts of the source program which can never be executed or a certain variable which might be used before being defined.