Compiler Design Tutorial: What is, Types, Tools, Example
What is a Compiler?
A compiler is a computer program which helps you transform source code written in a high-level language into low-level machine language. It translates the code written in one programming language to some other language without changing the meaning of the code. The compiler also makes the end code efficient which is optimized for execution time and memory space.
The compiling process includes basic translation mechanisms and error detection. Compiler process goes through lexical, syntax, and semantic analysis at the front end, and code generation and optimization at a back-end.
In single pass Compiler source code directly transforms into machine code. For example, Pascal language.
Two Pass Compiler
Two pass Compiler is divided into two sections, viz.
Front end: It maps legal code into Intermediate Representation (IR).
Back end: It maps IR onto the target machine
The Two pass compiler method also simplifies the retargeting process. It also allows multiple front ends.
The multipass compiler processes the source code or syntax tree of a program several times. It divided a large program into multiple small programs and process them. It develops multiple intermediate codes. All of these multipass take the output of the previous phase as an input. So it requires less memory. It is also known as 'Wide Compiler'.
Tasks of Compiler
Main tasks performed by the Compiler are:
Breaks up the up the source program into pieces and impose grammatical structure on them
Allows you to construct the desired target program from the intermediate representation and also create the symbol table
Compiles source code and detects errors in it
Manage storage of all variables and codes.
Support for separate compilation
Read, analyze the entire program, and translate to semantically equivalent
Translating the source code into object code depending upon the type of machine
History of Compiler
Important Landmark of Compiler's history are as follows:
The "compiler" word was first used in the early 1950s by Grace Murray Hopper
The first compiler was build by John Backum and his group between 1954 and 1957 at IBM
COBOL was the first programming language which was compiled on multiple platforms in 1960
The study of the scanning and parsing issues were pursued in the 1960s and 1970s to provide a complete solution
Steps for Language processing systems
Before knowing about the concept of compilers, you first need to understand a few other tools which work with compilers.
Preprocessor: The preprocessor is considered as a part of the Compiler. It is a tool which produces input for Compiler. It deals with macro processing, augmentation, language extension, etc.
Interpreter: An interpreter is like Compiler which translates high-level language into low-level machine language. The main difference between both is that interpreter reads and transforms code line by line. Compiler reads the entire code at once and creates the machine code.
Assembler: It translates assembly language code into machine understandable language. The output result of assembler is known as an object file which is a combination of machine instruction as well as the data required to store these instructions in memory.
Linker: The linker helps you to link and merge various object files to create an executable file. All these files might have been compiled with separate assemblers. The main task of a linker is to search for called modules in a program and to find out the memory location where all modules are stored.
Loader: The loader is a part of the OS, which performs the tasks of loading executable files into memory and run them. It also calculates the size of a program which creates additional memory space.
Cross-compiler: A cross compiler is a platform which helps you to generate executable code.
Source-to-source Compiler: Source to source compiler is a term used when the source code of one programming language is translated into the source of another language.
Compiler Construction Tools
Compiler construction tools were introduced as computer-related technologies spread all over the world. They are also known as a compiler- compilers, compiler- generators or translator.
These tools use specific language or algorithm for specifying and implementing the component of the compiler.
Scanner generators: This tool takes regular expressions as input. For example LEX for Unix Operating System.
Syntax-directed translation engines: These software tools offer an intermediate code by using the parse tree. It has a goal of associating one or more translations with each node of the parse tree.
Parser generators: A parser generator takes a grammar as input and automatically generates source code which can parse streams of characters with the help of a grammar.
Automatic code generators: Takes intermediate code and converts them into Machine Language
Data-flow engines: This tool is helpful for code optimization. Here, information is supplied by user and intermediate code is compared to analyze any relation. It is also known as data-flow analysis. It helps you to find out how values are transmitted from one part of the program to another part.
Why use a Compiler?
Compiler verifies entire program, so there are no syntax or semantic errors
The executable file is optimized by the compiler, so it is executes faster
Allows you to create internal structure in memory
There is no need to execute the program on the same machine it was built
Translate entire program in other language
Generate files on disk
Link the files into an executable format
Check for syntax errors and data types
Helps you to enhance your understanding of language semantics
Helps to handle language performance issues
Opportunity for a non-trivial programming project
The techniques used for constructing a compiler can be useful for other purposes as well
Application of Compilers
Compiler design helps full implementation Of High-Level Programming Languages
Support optimization for Computer Architecture Parallelism
Design of New Memory Hierarchies of Machines
Widely used for Translating Programs
Used with other Software Productivity Tools
A compiler is a computer program which helps you transform source code written in a high-level language into low-level machine language
Correctness, speed of compilation, preserve the correct the meaning of the code are some important features of compiler design
Compilers are divided into three parts 1) Single Pass Compilers 2)Two Pass Compilers, and 3) Multipass Compilers
The "compiler" was word first used in the early 1950s by Grace Murray Hopper
Steps for Language processing system are: Preprocessor, Interpreter, Assembler, Linker/Loader
Important compiler construction tools are 1) Scanner generators, 2)Syntax-3) directed translation engines, 4) Parser generators, 5) Automatic code generators
The main task of the compiler is to verify the entire program, so there are no syntax or semantic errors