Compiler HandMade Notes
Compiler HandMade Notes
Compiler HandMade Notes
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
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.
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.
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.
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.
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 can generate executable file like .exe It can generate raw code .hex
Lexical Analysis