Spos Unit 2 PPT 2022 Compressed

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 89

SYLLABUS :-

Introduction, Features of a Macro facility:


Macro instruction arguments, Conditional Macro
expansion,
Macro calls within Macros, Macro
Defining Macro, Design instructions, two
processor,
of Concept of single pass Macro
pass Macro
processor.

Introduction to Compilers: Phases of Compiler with


one example, Comparison of Compiler and
Interpreter.
CONTENTS :-
1. Introduction, Features of a Macro facility: Macro
instruction arguments,
2. Conditional Macro expansion,
3. Macro calls within Macros
4. Macro instructions, Defining Macro
5. Macro Definition (Macro Processor)
6. Compare Macro & Subroutines
7. Concept of single pass Macro processor
8. Introduction to Compilers: Phases of Compiler with
one example, Comparison of Compiler and
Interpreter
MACRO DEFINITION
Writing a macro is another way of ensuring modular programming

in assembly language.

•A macro is a sequence of instructions, assigned by a name and

could be used anywhere in the program.

•In NASM, macros are defined

with %macro and %endmacro directives.

•The macro begins with the %macro directive and ends with
the
MACRO DEFINITION
The Syntax for macro definition −
%macro macro_name number_of_params

<macro body>

%endmacro
FEATURES OF MACROPROCESSOR
1. Macro represents a group of commonly used statements in the source
programming language.

2. Macro Processor replaces each macro instruction with the corresponding


group of source language statements. This is known as the expansion of
macros.

3. Using Macro instructions programmer can leave the mechanical details to


be handled by the macro processor.

4. Macro Processor designs are not directly related to the computer


architecture on which it runs.

5. Macro Processor involves definition, invocation, and expansion.


MACRO DEFINITION
• .
COMPARE MACRO & SUBROUTINE
Sr
Macro Subroutine
.
No
Macro can be called only in the Subroutine can be called from other
1
program it is defined. programs also.

2 More space is required Less space is required

3 Execution speed is faster. Execution speed is slower

4 Macro Can not handle labels Subroutines can handle labels

5 Macro is executed by assembler Subroutine is executed by hardware

6 Code size increase Code size does not increase.

7 Simple to write and use Complex to write and understand


Macro name[parameter] Subroutine name(parameter)
8
Mend end
DEFINING MACRO
CALLING MACRO
MACRO EXPANSION
MACRO WITH KEYWORD
PARAMETER
MACRO WITH MIXED PARAMETER
NESTED MACRO CALL
NESTED MACRO DEFINITION
ADVANCED MACRO FACILITY
EXPANSION TIME VARIABLE
DESIGN OF MACROPROCESSOR
DATABASES USED IN PASS – I OF TWO
PASS MACRO PROCESSOR
DATABASES USED IN PASS – II OF TWO
PASS MACRO PROCESSOR
HANDLING OF NESTED MACRO CALL
HANDLING OF NESTED MACRO
DECLARATION
History of Compiler
• The “compiler” word was first used in the early 1950s
by Grace Murray Hopper
• The first compiler was build by John Backum and his
group between 1954 and 1957 at IBM
• COBOL was the first programming language which
was compiled on multiple platforms in 1960
• The study of the scanning and parsing issues were
pursued in the 1960s and 1970s to provide a complete
solution
Compiler
• Compiler is a translator which converts the
high level language into low level language.
• Benefits of writing a program in a high
level language :
• Increases productivity: It is very easy to write
a program in a high level language.
• Machine Independence: A program written in
a high level language is machine independent.
Features of Compiler
• Compilation speed.
• The correctness of machine code.
• The meaning of code should not change.
• Speed of machine code.
• Good error detection.
• Checking the code correctly according
to grammar.
Uses / Applications of Compiler
• Helps to make the code independent of
the platform.
• Makes the code free of syntax and
semantic errors.
• Generate executable files of code.
• Translates the code from one language
to another.
Steps in language processing system
COMPILE
RS
• “Compilation”
– Translation of a program written in a source
language into a semantically equivalent program
written in a target language
Input

Sourc Target
Compile
e Progra
r
Progra m
m
Error Outpu 3
message t
s
Phases of Compiler
ANALYSIS–
SYNTHESIS
MODE
L

5
8
3/17/2019 PROF. ANAND GHARU
Example : https://youtu.be/P1bQUyl__t0

PHASESOFCOMPILE
R

58

3/17/2019 PROF. ANAND GHARU


Example : https://youtu.be/P1bQUyl__t0
C
FRONTEND&
BACKENDOF COMPILER

59

3/17/2019 PROF. ANAND GHARU


Phases of Compiler
• Lexical Analysis :

1. It takes the high-level language source code as the input.

2. It scans the characters of source code from left to right.


Hence, the name scanner also.

3. It groups the characters into lexemes. Lexemes


are a group of characters which has some meaning.

4. Each lexeme corresponds to form a token.

5. It removes white spaces and comments.

6. I t c h ecks and removes


3 /1 7/ 20 19
th e l e x i c a l errors.
PR OF . AN A N D G H AR U
Phases of Compiler
• Syntax Analysis :

1. ‘Parser’ is the other name for the syntax analyzer.

2. The output of the lexical analyzer is its input.

3. It checks for syntax errors in the source code.

4. It does this by constructing a parse tree of all the tokens.

5. For the syntax to be correct, the parse tree


should be according to the rules of source code
grammar.

6. The grammar for such codes is context-free grammar.


3/17/2019 PROF. ANAND GHARU
Phases of Compiler
• Semantic Analysis :

1. It verifies the parse tree of the syntax analyzer.

2. It checks the validity of the code terms


in programming language. Like, of of
compatibility
types, declaration, and initialization of variables, data
etc.

3. It also produces a verified parse tree. Furthermore,


we also call this tree an annotated parse tree.

4. It also performs flow checking, type checking, etc.


3/17/2019 PROF. ANAND GHARU
Phases of Compiler
• Intermediate Code Generation :
1. It generates an intermediate code.

2. This code is neither in high-level language nor in machine


language. It is in an intermediate form.

3. It is converted to machine language but, the last two phases are


platform dependent.

4. The intermediate code is the same for all the compilers.


Further, we generate the machine code according to the
platform.

5. A7/2n019example of an intermPROeFd. AiaNAtNeDcGoHAdReU is three


3/1
Phases of Compiler
• Code Optimizer :
1. It optimizes the intermediate code.

2. Its function is to convert the code so that it executes


faster using fewer resources (CPU, memory).

3. It removes any useless lines of code and rearranges the code.

4. The meaning of the source code remains the same.

3/17/2019 PROF. ANAND GHARU


Phases of Compiler
• Code Generator :

1. Finally, it converts the optimized intermediate code


into the machine code.

2. This is the final stage of the compilation.

3. The machine code which is produced is relocatable.

3/17/2019 PROF. ANAND GHARU


Phases of Compiler
• Lexical Analysis :

Lexical analyzer phase is the first phase of compilation process. It takes source
code as input. It reads the source program one character at a time and converts it
into meaningful lexemes. Lexical analyzer represents these lexemes in the form
of tokens.

• 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.

3/17/2019 PROF. ANAND GHARU


Phases of Compiler
• 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.

• Intermediate Code Generation :

In the intermediate code generation, compiler generates the source code into the
intermediate code. Intermediate code is generated between the high-level
language and the machine language. The intermediate code should be generated
in such a way that you can easily translate it into the target machine code.
3/17/2019 PROF. ANAND GHARU
Phases of Compiler
• 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.
3/17/2019 PROF. ANAND GHARU
Example :

3/17/2019
Types of Compiler
1. Cross Compilers
They produce an executable machine code for a platform but, this platform
is not the one on which the compiler is running.

2. Bootstrap Compilers
The process of writing a compiler (or Assembler) in the target
programming language which has to be compiled is known as
"Bootstrapping“

3. Source to source/transcompiler
These compilers convert the source code of one programming language
to the source code of another programming language.
Types of Compiler
4. Incremental compiler :
Incremental Compiler is a compiler, which performs the recompilation
of only modified source rather than compiling the whole source program

Decompiler
Basically, it is not a compiler. It is just the reverse of the compiler.
It converts the machine code into high-level language.
ISSUES IN
COMPILATION
Hierarchy of operations need to be maintained to
determine correct order of expressionevaluation

Maintain data type integrity with automatic type


conversions

Handle user defined data types.

Develop appropriate storage mappings

7
3
3/17/2019 PROF. ANAND GHARU
ISSUES IN
COMPILATION
Resolve occurrence of each variable name in a program
i.e construct separate symbol tables for different
namespaces.

Handle different control structures.

Perform optimization

74

3/17/2019 PROF. ANAND GHARU


BLOCK SCHEMATIC
OF LEXICAL
ANALYZER

3/17/2019 PROF. ANAND GHARU


BLOCK SCHEMATIC
OF LEXICAL
ANALYZER
Lexical analysis is the process of converting a sequence
of characters from source program into a sequence of
tokens.
A program which performs lexical analysis is termed as
a lexical analyzer (lexer), tokenizer or scanner.

Lexical analysis consists of two stages of processing which


are as follows:
• Scanning
3/17/2019 PROF. ANAND GHARU
BLOCK SCHEMATIC
OF LEXICAL
ANALYZER
Roles of the Lexical analyzer
Lexical analyzer performs below given tasks:

1. Helps to identify token into the symbol table


2. Removes white spaces and comments from the source program
3. Correlates error messages with the source program
4. Helps you to expands the macros if it is found in the
source program
5. Read input characters from the source program.

3/17/2019 PROF. ANAND GHARU


BLOCK SCHEMATIC
OF LEXICAL
ANALYZER
Lexical Analyzer vs. Parser

Lexical Analyser Parser

Scan Input program Perform syntax analysis

Identify Tokens Create an abstract representation of


the code

Insert tokens into Symbol Table Update symbol table entries

It generates lexical errors It generates a parse tree of the


source code

3/17/2019 PROF. ANAND GHARU


BASIC TERMINOLOGIES
OF LEXICAL
ANALYSIS
 Major Terms for Lexical Analysis?
 TOKEN
 A classification for a common set of strings
 Examples Include <Identifier>, <number>, etc.
 PATTERN
 The rules which characterize the set of strings for
a token
 Recall File and OS Wildcards ([A-Z]*.*)
 LEXEME
 Actual sequence of characters that matches pattern
and is classified by a token
 Identifiers: x, count, name, etc…
4
0
3/17/2019 PROF. ANAND GHARU
BASIC TERMINOLOGIES
OF LEXICAL
ANALYSIS

4
0
3/17/2019 PROF. ANAND GHARU
INTERPRTER
S
• rpretation”
“Inte
– Performing the operations implied by
the source program

Source
Progra
m Interpreter Outpu
Input t

Error Messages

8
1
3/17/2019 PROF. ANAND GHARU
Position of a Parser in
the Compiler Model

Token,
tokenva
Source l Parser Intermediate
Lexical
Progra and rest representatio
Analyze
m Get of front- n
r
next end
token
Lexical Syntax error
error Semantic
error

Symbol Table
Position of a Parser in
the Compiler Model
The Role Of Parser
• A parser implements a C-F grammar
• The role of the parser is two fold:
1. To check syntax (= string recognizer)
– And to report syntax errors accurately
2. To invoke semantic actions
– For static semantics checking, e.g. type checking
of expressions, functions, etc.
– For syntax-directed translation of the source code to an
intermediate representation
The Role Of Parser
1.It verifies the structure generated by the tokens based
on the grammar.
2. It constructs the parse tree.
3. It reports the errors.
4. It performs error recovery.

Issues :
Parser cannot detect errors such as:
5. Variable re-declaration
6. Variable initialization before use
7. Data type mismatch for an operation.
The above issues are handled by Semantic Analysis
phase.
The Role Of Parser
Syntax error handling :
Programs can contain errors at many different levels. For
example :
1. Lexical, such as misspelling an identifier, keyword or
operator.
2.Syntactic, such as an arithmetic expression with
unbalanced parentheses.
3.Semantic, such as an operator applied to an
incompatible operand.
4. Logical, such as an infinitely recursive call.
The Role Of Parser
Functions of error handler :
1. It should report the presence of errors clearly and
accurately.
2.It should recover from each error quickly enough to be
able to detect subsequent errors.
3.It should not significantly slow down the processing of
correct programs.
TYPES OF
ERRORS
A parser should be able to detect and report any error in the
program. It is expected that when an error is encountered, the
parser should be able to handle it and carry on parsing the rest
of the input. Mostly it is expected from the parser to check
for errors but errors may be encountered at various stages of
the compilation process. A program may have the
following kinds of errors at various stages:

Lexical error : name of some identifier typed incorrectly


Syntactical error: missing semicolon or
unbalanced parenthesis
Semantical error : incompatible value assignment
Logical error: code not reachable, infinite loop 4
4
3/1C7/2o0m19 pile time PROF. ANAND GHARU
TYPES OF
ERRORS

4
4
3/17/2019 PROF. ANAND GHARU

You might also like