Compiler HandMade Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

What is a translator?

A program written in high-level language is called as source code. To convert the source code into
machine code, translators are needed.
A translator takes a program written in source language as input and converts it into a program in
target language as output.
It also detects and reports the error during translation.
Roles of translator are:
• Translating the high-level language program input into an equivalent machine language
program.
• Providing diagnostic messages wherever the programmer violates specification of the high-level
language program.
Different type of translators

1. Compiler

Compiler is a translator which is used to convert programs in high-level language to low-level language.

It translates the entire program and also reports the errors in source program encountered during the

translation.

2. Interpreter

Interpreter is a translator which is used to convert programs in high-level language to low-


level language. Interpreter translates line by line and reports the error once it encountered
during the translation process.
It directly executes the operations specified in the source program when the input is given
by the user.
It gives better error diagnostics than a compiler.
3. Assembler

Assembler is a translator which is used to translate the assembly language code into machine

language code.

Structure of a Compiler
Phases of a compiler: A compiler operates in phases. A phase is a logically interrelated
operation that takes source program in one representation and produces output in another
representation. The phases of a compiler are shown in below There are two phases of
compilation.

a. Analysis (Machine Independent/Language Dependent)

b. Synthesis(Machine Dependent/Language independent)


Lexical Analysis:
Lexical analyzer phase is the first phase of compilation process. It takes source code as input. It reads
the source program one character at a time and converts it into meaningful lexemes. Lexical analyzer
represents these lexemes in the form of tokens.

Syntax Analysis
Syntax analysis is the second phase of compilation process. It takes tokens as input and generates a
parse tree as output. In syntax analysis phase, the parser checks that the expression made by the
tokens is syntactically correct or not.

Semantic Analysis
Semantic analysis is the third phase of compilation process. It checks whether the parse tree follows
the rules of language. Semantic analyzer keeps track of identifiers, their types and expressions. The
output of semantic analysis phase is the annotated tree syntax.

Intermediate Code Generation


In the intermediate code generation, compiler generates the source code into the intermediate code.
Intermediate code is generated between the high-level language and the machine language. The
intermediate code should be generated in such a way that you can easily translate it into the target
machine code.

Code Optimization
Code optimization is an optional phase. It is used to improve the intermediate code so that the
output of the program could run faster and take less space. It removes the unnecessary lines of the
code and arranges the sequence of statements in order to speed up the program execution.

Code Generation
Code generation is the final stage of the compilation process. It takes the optimized intermediate
code as input and maps it to the target machine language. Code generator translates the
intermediate code into the machine code of the specified computer.
Table Management OR Book-keeping :- A compiler needs to collect information about all the
data objects that appear in the source program. The information about data objects is
collected by the early phases of the compiler-lexical and syntactic analyzers. The data
structure used to record this information is called as Symbol Table.

Error Handing :- One of the most important functions of a compiler is the detection and
reporting of errors in the source program. The error message should allow the programmer to
determine exactly where the errors have occurred. Errors may occur in all or the phases of a
compiler. Whenever a phase of the compiler discovers an error, it must report the error to the
error handler, which issues an appropriate diagnostic msg. Both of the table-management and
error-Handling routines interact with all phases of the compiler.

Compiler construction tools-------------------------------------

Interpreter and the related issues--------------------------------------------

Cross compiler :
A Cross compiler is a compiler that generates executable code for a platform other than one
on which the compiler is running. For example a compiler that running on Linux/x86 box is
building a program which will run on a separate Arduino/ARM.

Native Compiler Cross Compiler

Translates program for same Translates program for different


hardware/platform/machine on it is hardware/platform/machine other than the
running. platform which it is running.

It is used to build programs for same It is used to build programs for other
system/machine & OS it is installed. system/machine like AVR/ARM.

It is dependent on System/machine and


OS It is independent of System/machine and OS

It can generate executable file like .exe It can generate raw code .hex

TurboC or GCC is native Compiler. Keil is a cross compiler.


Incremental compiler……………………
Bootstrapping ---

Bootstrapping is a process in which simple language is used to translate more complicated


program which in turn may handle for more complicated program. This complicated program
can further handle even more complicated program and so on.

Lexical Analysis

You might also like