0% found this document useful (0 votes)
5 views13 pages

Module 2

The document outlines the components of a Language Processing System, detailing the roles of the preprocessor, compiler, assembler, linker, and loader in converting high-level source code into executable machine code. It describes the phases of compilation, including analysis and synthesis parts, and explains the functions of lexical, syntax, and semantic analyzers, as well as intermediate code generation and code optimization. Additionally, it discusses the importance of the symbol table, error detection, and the features of programming languages that influence compilation and execution processes.

Uploaded by

Aswin UK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Module 2

The document outlines the components of a Language Processing System, detailing the roles of the preprocessor, compiler, assembler, linker, and loader in converting high-level source code into executable machine code. It describes the phases of compilation, including analysis and synthesis parts, and explains the functions of lexical, syntax, and semantic analyzers, as well as intermediate code generation and code optimization. Additionally, it discusses the importance of the symbol table, error detection, and the features of programming languages that influence compilation and execution processes.

Uploaded by

Aswin UK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MODULE- 2

Language Processing System


 The high-level programs are then fed into a series of tools and OS
components to get the desired code that can be used by the machine. This
is known as Language Processing System.

Preprocessor:

 It is a program that reads the source code and prepares it for the translator
Roles of Preprocessor:

 Copies all library functions of a file into a source code

 Expand macros into a source code


Example: #include<stdio.h> here # is a processor directive. It copies all library
functions of stdio.h (standard input output) file into a program.

Compiler:
 Compiler is a program that reads a program in high level language known

as source code as its input and converts it into an equivalent program in


low level language as its output

 After compilation - high level language converted into assembly


language.
Assembler:

 The assembly language is then processed by a program called assembler


that produce relocatable machine code as its output.
Linker:

 Programs are divided into modules known as functions in C

 Some of the functions are written by user and others are stored in a file
(header file)

 A linker is an important utility program that takes the object files,


produced by the assembler and compiler, and other code to join them into
a single executable file.

 Function- The process of linking user functions and system functions to a


final executable code.
Loader:

 Once the program is linked, it is ready for execution

 The process of loading the program into primary memory for execution is
known as loader
Executable code: It is low-level and machine-specific code that the machine
can easily understand. Once the job of the linker and loader is done the object
code (relocatable machine code) is finally converted it into executable code.

Phases of Compiler (15-mark essay question)


 Above section we discussed about how a source program is converted
into an executable code, Now will see how a compiler will convert a
modified source program into an assembly language. This process is
called compilation
 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.
 A compiler is basically divided into two parts. They are
Analysis part
Synthesis part
 Analysis part
 Analysis part breaks up the source program into constituent pieces
and create an intermediate representation of the source code known
as intermediate code
 Analysis part is also collecting information about the source
program and stores it in a data structure called a symbol table
 Analysis part includes 3 phases of a compiler, that is lexical
analyzer, syntax analyzer and semantic analyzer.
 Synthesis part
 Synthesis part constructs desired target code from an intermediate
code and the information from the symbol table
 Synthesis part includes 3 phases of a compiler, that is intermediate
code generation, code optimization and code generation
Lexical Analyzer

 It is a first phase of a compiler

 Lexical Analyzer is also known as Scanner

 Lexical Analyzer reads the stream of characters from left to right and
groups the characters into meaningful sequences called Lexeme

 Roles of a Lexical Analyzer


 Produce the tokens
 Ignore all white spaces in a source code
 Ignore comments in a source code, if any
The general form of a token is <token-name, attribute-value>
where, token-name is an abstract symbol that is used during next phase
(syntax analyzer) of a compiler and attribute-value points to an entry in the
symbol table.
Example:1
result = number1 + number2 * 50
After lexical analyzer, the above statement would be
<id,1> <=> <id,2> <+> <id,3> <*> <number>
In this representation the token names=,+,* are abstract symbols for the
assignment, addition, multiplication respectively

Syntax Analyzer
 It is a second phase of compiler

 It is also known as parser or hierarchical analysis

 Parser collects the sufficient tokens which are generated by lexical


analyzer as its input and generates parse tree as its output

 Parser checks the source code i.e., whether it is syntactically correct or


not.

 The role of a syntax analyzer is to check the syntax errors in a source


program
Example:
Consider parse tree for the following example

(a+b) *c
Semantic Analyzer
 Semantic Analyzer takes parse tree as its input and construct meaningful

parse tree as its output

 The semantic analyser uses the parse tree and the information in the
symbol table to check the source code for meaning
Roles of a Semantic Analyzer

 Checks the meaning of a source program

 Construct syntax tree

 Saves the information in the symbol table

 Type Checking

 Type Conversion

 Evaluation of an expression

 Check variable is declared or not

Example:
o float x = 20.2;
o float y = x*30;
o In the above code, the semantic analyzer will typecast the integer
30 to float 30.0 before multiplication

Intermediate Code Generation

 Intermediate is the interface between analysis phase and synthesis phase


of a compiler

 Intermediate code generation takes a parse tree as its input and construct
one or more intermediate representations, which can have a variety of
forms
Roles of Intermediate Code Generation

 Construct intermediate representation of the source program called


intermediate code
 It is in between the high-level language and the machine language. This
intermediate code should be generated in such a way that it makes it
easier to be translated into the target machine code.
Example:

o Consider,
total = count + rate * 5
Intermediate code with the help of address code method is:

t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3

Code Optimization
 It is optional phase of a compiler

 The process of reducing the number of instructions without changing the


meaning of source program is known as code optimization

 Code Optimization takes intermediate code as its input and generates


optimized intermediate codes as its output if possible

 The role of a code optimization is to improve the intermediate code so


that the running time of the target program can be significantly improved
Example:
a = int to float (10)
b=c*a
d=e+b
f=d

Can become
b =c * 10.0
f = e+b

Code Generation:
 It is a final phase of a compiler
 The code generator takes intermediate or optimized intermediate code
and required information in symbol table as its input and maps it into the
target code

 If the output of a code generator is machine code, registers or memory


location are selected for each of the variables used by the program

 Then, the intermediate instructions are translated into sequences of


machine instructions that perform the same task
Symbol Table:
 The main function of the Compiler is to store the variable names used in
the source program and collect information about various attributes of
each name.
 These attributes provide various information about the variable used in
the program. In order to store the information about variables, the
compiler needs a data structure, which is why it needs a symbol table.
Error Detection and Handling
 In compilation, each phase detects errors, these errors must be reported to

error handler whose task is to handle the errors so that the compilation
can proceed

 The errors are reported in the form of a message, when the input
characters from the input do not form the token, the lexical analyzer
detects it as error

 Syntax analysis phase large number of errors can be detected. Such errors
are popularly called syntax errors

 Semantic analysis phase, type mismatch kind of error is usually detected.


Aspects of Compilation
 Compiler bridges gap between a PL Domain and Execution domain.

• Two aspects: – Generate code.


– Provide Diagnostics.
To implement these aspects properly, it depends on the 4 features of
programming language.
PL (Programming Language) Features:
1. Data Types
2. Data Structure
3. Scope Rules
4. Control Structures PL Domain Execution domain.

I. Data Types
 Data type refers to the type of value a variable has.
 A data type can be specified based on two rules
a) Legal values for variables of the type-
Example – int x- for a variable ‘x’, legal value that can assign is all
integers
b) Legal operation on legal value of the type- Example – We are not
supposed to add an integer and and a float.
 Having checked the legality of each operation and
determined the need for type conversion, the compiler must
generate type specific code to implement an operation.
Check the legality
Type conversion
Generate type specific code to implement an operation.
II. Data Structure
 In programming language, we are a supposed to declare and use
many data structures like array, stack, queue etc
 To compile an element in a data structure- Compiler must develop
a memory mapping -ie compiler will map to the memory word
where element is placed.
III.Scope Rules
 It determines the accessibility of variables declared in a program.
 Scope of a variable is the part where the entity is accessible.
 Normally scope of a variable is restricted to the block of program
in which it is declared
 Here compiler performs two operations- Scope Analysis and
Name resolutions to determine variable designated by the use of a
name in the source program.
IV. Control Structures
 It is a feature for altering the flow of control during the execution
of a program
 This includes – Conditional control transfer
-Iteration control
- function calls
 Here compiler must ensure that a source program does not violate
the semantics of control structure.
Linkers and Loaders
 When it comes to the execution of any programs, linker and loader
play a very important role.

 They are the utility programs that support a program while


executing.

 The purpose of a linker is to produce executable files whereas the


major aim of a loader is to load executable files to the memory.

S.No. Linker Loader

1 A linker is an important utility A loader is a vital component of an


program that takes the object operating system that is accountable for
files, produced by the loading programs and libraries.
assembler and compiler, and
other code to join them into a
single executable file.

2 It uses an input of object code It uses an input of executable files


produced by the assembler and produced by the linker.
compiler.

3 The foremost purpose of a The foremost purpose of a loader is to


linker is to produce executable load executable files to memory.
files.

4 Linker is used to join all the Loader is used to allocate the address to
modules. executable files.

5 It is accountable for managing It is accountable for setting up


objects in the program’s space. references that are utilized in the
program.
Steps for Execution:
Execution of a program written in language L involves the following steps:
1. Translation of program – This part will do by any language translators like
compiler, interpreter, assembler
2. Linking of program with other programs needed for its execution. -by linker
3. Relocation of the program to execute from the specific memory area
allocated to it. -by linker
4. Loading of the program in the memory for the purpose of execution. -by
loader

Terminologies used for Program entity at different times

1. Translation Time Address/Translated Address


 These are the address assigned by the Translator, whenever the
translator reading the program.
2. Linked Address
 These are the address assigned by the Linker to object code.
 Linker will link all the modules to our source program ,so address
may change
3. Load Time Address/Load Address
 Address assigned by loader
4. Translated Origin:
 Address of the origin assumed by the translator. This is the
address specified by the programmer in an ORIGIN statement.
5. Linked Origin:
 Address of the origin assigned by the linker while producing a
binary program.
6. Load Origin:
 Address of the origin assigned by the loader while loading the
program for execution.

You might also like