Compiler Design: Introduction to Language Processing Systems

Telechargé par abbesintisar
1
Chapter 1- INTRODUCTION
INTRODUCTION OF LANGUAGE PROCESSING SYSTEM
A compiler translates the code written in one language to some other language without changing the
meaning of the program. It is also expected that a compiler should make the target code efficient and
optimized in terms of time and space. Compiler is a software which converts a program written in high
level language (Source Language) to low level language (Object/Target/Machine Language).
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 source code of another programming language.
Compiler design principles provide an in-depth view of translation and optimization process.
It includes lexical, syntax, and semantic analysis as front end, and code generation and optimization
as back-end.
Language Processing System
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 high-level language, that is much less
complicated for us to comprehend and maintain in thoughts. Now these programs go through a series of
transformation so that they can readily be used machines. This is where language procedure systems
come handy.
1. High Level Language If a program contains #define or #include directives such as #include or #define it is
called HLL. They are closer to humans but far from machines. These (#) tags are called pre-processor
directives. They direct the pre-processor about what to do.
2. Preprocessor: A preprocessor, generally considered as a part of compiler, is a tool that produces input for
compilers. It deals with macro-processing, augmentation, file inclusion, language extension, etc.
2
Preprocessor
A preprocessor produce input to compilers. They may perform the following functions.
i) Macro processing: A preprocessor may allow a user to define macros that are short hands for longer constructs.
ii) File inclusion: A preprocessor may include header files into the program text.
iii) Rational preprocessor: these preprocessors augment older languages with more modern flow-of-control
and data structuring facilities.
iv) Language Extensions: These preprocessor attempts to add capabilities to the language by certain amounts to
build-in macro.
3. Assembly Language Its neither in binary form nor high level. It is an intermediate state that is a
combination of machine instructions and some other useful data needed for execution.
4. ASSEMBLER
Programmers found it difficult to write or read programs in machine language. They begin to use a mnemonic
(symbols) for each machine instruction, which they would subsequently translate into machine language. Such a
mnemonic machine language is now called an assembly language. An assembler translates assembly language
programs into machine code. The output of an assembler is called an object file, which contains a combination of
machine instructions as well as the data required to place these instructions in memory.
5. Interpreter
An interpreter, like a compiler, translates high-level language into low-level machine language. The difference lies in
the way they read the source code or input. A compiler reads the whole source code at once, creates tokens, checks
semantics, generates intermediate code, executes the whole program and may involve many passes. In contrast, an
interpreter reads a statement from the input, converts it to an intermediate code, executes it, then takes the next
3
statement in sequence. If an error occurs, an interpreter stops execution and reports it. whereas a compiler reads
the whole program even if it encounters several errors.
6. 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 for the program movement.
7. Linker: Linker is a computer program that links and merges various object files together in order to make an
executable file. All these files might have been compiled by separate assemblers.
8. Loader : Loader is a part of operating system and is responsible for loading executable files into memory and
executes them. It calculates the size of a program (instructions and data) and creates memory space for it. It
initializes various registers to initiate execution.
Loader It 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.
9. Cross-compiler
A compiler that runs on platform (A) and is capable of generating executable code for platform (B) is called a cross-
compiler.
COMAPARISION OF COMPILER AND INTERPRETER PROCESSS
BASIS FOR
COMPARISON
COMPILER (C, C++)
INTERPRETER (JAVA)
Input
It takes an entire program at a time.
It takes a single line of code or instruction
at a time.
Output
It generates intermediate object code.
It does not produce any intermediate object
code.
Working mechanism
The compilation is done before
execution.
Compilation and execution take place
simultaneously.
Speed
Comparatively faster
Slower
Memory
Memory requirement is more due to
the creation of object code.
It requires less memory as it does not
create intermediate object code.
Errors
Display all errors after compilation, all
at the same time.
Displays error of each line one by one.
Error detection
Difficult
Easier comparatively
Pertaining Programming
languages
C, C++, C#, Scala, typescript uses
compiler.
PHP, Perl, Python, Ruby uses an
interpreter.
4
COMPILER DESIGN ISSUES
The compilation process is a sequence of various phases. Each phase takes input from its previous stage, has its
own representation of source program, and feeds its output to the next phase of the compiler. Let us understand the
phases of a compiler.
5
Lexical Analysis
The first phase of scanner works as a text scanner. This phase scans the source code as a stream of characters and
converts it into meaningful lexemes. Lexical analyzer represents these lexemes in the form of tokens as:
<token-name, attribute-value>
Lexemes are said to be a sequence of characters (alphanumeric) in a token
For example, in C language, the variable declaration line
int value = 100;
contains the tokens:
int (keyword), value (identifier), = (operator), 100 (constant) and ; (symbol).
Alphabets
Any finite set of symbols {0,1} is a set of binary alphabets, {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} is a set of Hexadecimal
alphabets, {a-z, A-Z} is a set of English language alphabets.
Strings
Any finite sequence of alphabets is called a string. Length of the string is the total number of occurrence of alphabets,
e.g., the length of the string tutorials point is 14 and is denoted by |tutorials point| = 14. A string having no alphabets,
i.e. a string of zero length is known as an empty string and is denoted by ε (epsilon).
Arithmetic Symbols
Addition(+), Subtraction(-), Modulo(%), Multiplication(*), Division(/)
Punctuation
Comma(,), Semicolon(;), Dot(.), Arrow(->)
Assignment
=
Special Assignment
+=, /=, *=, -=
Comparison
==, !=, <, <=, >, >=
Preprocessor
#
Location Specifier
&
Logical
&, &&, |, ||, !
Shift Operator
>>, >>>, <<, <<<
1 / 13 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!