pcc 1
pcc 1
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 inlining.
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
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 or transcompiler or transpiler is a compiler that
translates source code written in one programming language into the source code
of another programming language.
Language Processing Systems
We know a computer is a logical assembly of Software and Hardware. The hardware
knows a language, that is hard for us to grasp, consequently, we tend to write
programs in a high-level language, that is much less complicated for us to
Compiler design
comprehend and maintain in our thoughts. Now, these programs go through a series
of transformations so that they can readily be used by machines. This is where
language procedure systems come in handy.
Difference between High Level and Low level languages
Both High level language and low level language are the programming languages’s
types. The main difference between high level language and low level language is that,
Programmers can easily understand or interpret or compile the high level language in
comparison of machine. On the other hand, Machine can easily understand the low level
language in comparison of human beings. Examples of high level languages are C, C++,
Java, Python, etc. Let’s see the difference between high level and low level languages:
2. High level language is less memory Low level language is high memory
efficient. efficient.
6. It is portable. It is non-portable.
They are not universal since for each platform we have one. The output of the
assembler is called an object file. Its translates assembly language to machine
code.
Interpreter: An interpreter converts high-level language into low-level machine
language, just like a compiler. But they are different in the way they read the input.
The Compiler in one go reads the inputs, does the processing, and executes the
source code whereas the interpreter does the same line by line. A compiler scans
the entire program and translates it as a whole into machine code whereas an
interpreter translates the program one statement at a time. Interpreted programs
are usually slower concerning compiled ones. For example: Let in the source
program, it is written #include “Stdio. h”. Pre-Processor replaces this file with its
contents in the produced output. The basic work of a linker is to merge object
codes (that have not even been connected), produced by the compiler, assembler,
standard library function, and operating system resources. The codes generated by
the compiler, assembler, and linker are generally re-located by their nature, which
means to say, the starting location of these codes is not determined, which means
they can be anywhere in the computer memory, Thus the basic task of loaders to
find/calculate the exact address of these memory locations.
Relocatable Machine Code: It can be loaded at any point and can be run. The
address within the program will be in such a way that it will cooperate with the
program movement.
Loader/Linker: Loader/Linker converts the relocatable code into absolute code
and tries to run the program resulting in a running program or an error message (or
sometimes both can happen). Linker loads a variety of object files into a single file
to make it executable. Then loader loads it in memory and executes it.
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.
Compiler design
Phases of Compiler
Analysis Phase
Synthesis Phase
Types of Compiler
There are mainly three types of compilers.
Single Pass Compilers
Two Pass Compilers
Multipass Compilers
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 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.
Do note : usually whenever loader linker are called, (in frontend , like c++
coding)linker is used/called first then loader.