Introduction to compiler structure
The compiler is software that converts a program written in a high-level
language (Source Language) to a low-level language
(Object/Target/Machine Language/0, 1’s).
A translator or language processor is a program that translates an input
program written in a programming language into an equivalent program
in another language. The compiler is a type of translator, which takes a
program written in a high-level programming language as input and
translates it into an equivalent program in low-level languages such as
machine language or assembly language.
The program written in a high-level language is known as a source
program, and the program converted into a low-level language is known
as an object (or target) program. Without compilation, no program
written in a high-level language can be executed. For every programming
language, we have a different compiler; however, the basic tasks
performed by every compiler are the same. The process of translating the
source code into machine code involves several stages, including lexical
analysis, syntax analysis, semantic analysis, code generation, and
optimization.
Compiler is an intelligent program as compare to an assembler. Compiler
verifies all types of limits, ranges, errors, etc. Compiler program takes
more time to run and it occupies huge amount of memory space. The
speed of compiler is slower than other system software. It takes time
because it enters through the program and then does translation of the
full program. When compiler runs on same machine and produces
machine code for the same machine on which it is running. Then it is
called as self-compiler or resident compiler. Compiler may run on one
machine and produces the machine codes for other computer then in that
case it is called as cross compiler.
High-Level Programming Language
A high-level programming language is a language that has an abstraction
of attributes of the computer. High-level programming is more
convenient to the user in writing a program.
Low-Level Programming Language
A low-Level Programming language is a language that doesn’t require
programming ideas and concepts.
Stages of Compiler Design
1. Lexical Analysis: The first stage of compiler design is lexical
analysis, also known as scanning. In this stage, the compiler reads the
source code character by character and breaks it down into a series of
tokens, such as keywords, identifiers, and operators. These tokens are
then passed on to the next stage of the compilation process.
2. Syntax Analysis: The second stage of compiler design is syntax
analysis, also known as parsing. In this stage, the compiler checks the
syntax of the source code to ensure that it conforms to the rules of the
programming language. The compiler builds a parse tree, which is a
hierarchical representation of the program’s structure, and uses it to
check for syntax errors.
3. Semantic Analysis: The third stage of compiler design is semantic
analysis. In this stage, the compiler checks the meaning of the source
code to ensure that it makes sense. The compiler performs type
checking, which ensures that variables are used correctly and that
operations are performed on compatible data types. The compiler also
checks for other semantic errors, such as undeclared variables and
incorrect function calls.
4. Code Generation: The fourth stage of compiler design is code
generation. In this stage, the compiler translates the parse tree into
machine code that can be executed by the computer. The code
generated by the compiler must be efficient and optimized for the
target platform.
5. Optimization: The final stage of compiler design is optimization. In
this stage, the compiler analyzes the generated code and makes
optimizations to improve its performance. The compiler may perform
optimizations such as constant folding, loop unrolling, and function
in lining.
Overall, compiler design is a complex process that involves multiple
stages and requires a deep understanding of both the programming
language and the target platform. A well-designed compiler can greatly
improve the efficiency and performance of software programs, making
them more useful and valuable for users.
Compiler
.
Types of Compiler
There are mainly three types of compilers.
Single Pass Compilers
Two Pass Compilers
Multipass Compilers
Single Pass Compiler
When all the phases of the compiler are present inside a single module,
it is simply called a single-pass compiler. It performs the work of
converting source code to machine code.
Two Pass Compiler
Two-pass compiler is a compiler in which the program is translated
twice, once from the front end and the back from the back end known as
Two Pass Compiler.
Multipass Compiler
When several intermediate codes are created in a program and a syntax
tree is processed many times, it is called Multi pass Compiler. It breaks
codes into smaller programs. A program’s source code or syntax tree is
processed many times by the multipass compiler. It breaks down a huge
programme into numerous smaller programs and runs them all at the same
time. It creates a number of intermediate codes. All of these multipasses
use the previous phase’s output as an input. As a result, it necessitates less
memory. ‘Wide Compiler’ is another name for it.
Cross Compiler that runs on a machine ‘A’ and produces a code
for another machine ‘B’. It is capable of creating code for a platform
other than the one on which the compiler is running.
Source-to-source Compiler is a compiler that translates source
code written in one programming language into the source code of
another programming language
Operations of Compiler
The important tasks executed by the compiler are:
Breaks down the source programme into chunks and applies
grammatical structure to each one.
Enables you to create the symbol table and the target programme
you’re after from the intermediate representation.
Helps in compiling source code and detecting the errors.
Organize and save all codes and variables.
Separate compilation is supported.
Read the full programme, analyze it, and translate it to a
semantically similar language.
Depending on the type of machine, converting source code to
object code.
Phases of a Compiler
There are two major phases of compilation, which in turn have many
parts. Each of them takes input from the output of the previous level and
works in a coordinated way.
Phases of Compiler
Analysis Phase
An intermediate representation is created from the given source code :
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate Code Generator
The lexical analyzer divides the program into “tokens”, the Syntax
analyzer recognizes “sentences” in the program using the syntax of the
language and the Semantic analyzer checks the static semantics of each
construct. Intermediate Code Generator generates “abstract” code.
Synthesis Phase
An equivalent target program is created from the intermediate
representation. It has two parts:
Code Optimizer
Code Generator
Code Optimizer optimizes the abstract code, and the final Code
Generator translates abstract intermediate code into specific machine
instructions.
Operations of Compiler
These are some operations that are done by the compiler.
It breaks source programs into smaller parts.
It enables the creation of symbol tables and intermediate
representations.
It helps in code compilation and error detection.
it saves all codes and variables.
It analyses the full program and translates it.
Convert source code to machine code.
Advantages of Compiler Design
Efficiency: Compiled programs are generally more efficient than
interpreted programs because the machine code produced by the compiler
is optimized for the specific hardware platform on which it will run.
Portability: Once a program is compiled, the resulting machine code can
be run on any computer or device that has the appropriate hardware and
operating system, making it highly portable.
Error Checking: Compilers perform comprehensive error checking
during the compilation process, which can help catch syntax, semantic,
and logical errors in the code before it is run.
Optimizations: Compilers can make various optimizations to the
generated machine code, such as eliminating redundant instructions or
rearranging code for better performance.
Disadvantages of Compiler Design
1. Longer Development Time: Developing a compiler is a complex
and time-consuming process that requires a deep understanding of
both the programming language and the target hardware platform.
2. Debugging Difficulties: Debugging compiled code can be more
difficult than debugging interpreted code because the generated
machine code may not be easy to read or understand.
3. Lack of Interactivity: Compiled programs are typically less
interactive than interpreted programs because they must be compiled
before they can be run, which can slow down the development and
testing process.
4. Platform-Specific Code: If the compiler is designed to generate
machine code for a specific hardware platform, the resulting code
may not be portable to other platforms.