0% found this document useful (0 votes)
5 views

CC_unit_1

The document outlines the phases of compiler construction, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It discusses the features of a good compiler, the importance of symbol table management, and error handling at various stages of compilation. Additionally, it introduces compiler construction tools such as parser and scanner generators, and explains the linking and loading process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CC_unit_1

The document outlines the phases of compiler construction, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It discusses the features of a good compiler, the importance of symbol table management, and error handling at various stages of compilation. Additionally, it introduces compiler construction tools such as parser and scanner generators, and explains the linking and loading process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

Modified CO Statements

CO Statement
Course No
CS601.1 Relate compiler phases and compiler construction tools.

CS601.2 Implement lexical analyser for a simple language.

CS601.3 Analyse and differentiate different parsing techniques.

Use syntax directed translations and syntax directed definitions to


CS601.4 generate intermidiate code.

Apply different code optimization techniques for various


CS601.5 statements.

CS601.6 Demonstrate techniques of code generation.


Unit 1
Introduction To Compiling
Compiler
It is a program that translate a high level

language program into a functionally equivalent


low level language program.

It is a software component.

It is classified as single pass , multi pass ,

debugging, optimizing etc.


Features of good compiler

A good compiler compiles the large amount of code in less

time.
It requires less amount of memory space to compile the source

language.
It compile only the modified code segment if frequent

modifications are required in the source program.


While handling the hardware interrupts the good compiler

interact closely with operating system.


Phases of compiler
 The structure of compiler consists of two parts:

 Analysis part

• Analysis part breaks the source program into constituent pieces and
imposes a grammatical structure on them which further uses this
structure to create an intermediate representation of the source program.

• It is also termed as front end of compiler.

• Information about the source program is collected and stored in a data


structure called symbol table.
Analysis part
S/W Tools Used For Analysis

Structure editor

Pretty printers

Static checkers

Interpreters

Cross compiler

bootstrapping
Synthesis part

• Synthesis part takes the intermediate


representation as input and transforms it to
the target program.

• It is also termed as back end of compiler.


Phases of compiler
1. Lexical analysis

2. Syntax analysis

3. Semantic analysis

4. Intermediate code generation

5. Code optimization

6. Code generation
Phases of compiler
Phases of compiler

All of the aforementioned phases involve the


following tasks:
• Symbol table management.
• Error handling.
Phases of compiler:-Lexical Analysis

• Lexical analysis is the first phase of compiler


which is also termed as scanning.

• Source program is scanned to read the stream of


characters and those characters are grouped to
form a sequence called lexemes which produces
token as output.
Phases of compiler:-Lexical Analysis
Token: Token is a sequence of characters that represent
lexical unit, which matches with the pattern, such as
keywords, operators, identifiers etc.

Lexeme: Lexeme is instance of a token i.e., group of


characters forming a token.

Pattern: Pattern describes the rule that the lexemes of a token


takes. It is the structure that must be matched by strings.
Phases of compiler:-Lexical Analysis

• Once a token is generated the corresponding entry is made


in the symbol table.
Input: stream of characters

Output: Token

Token Template: <token-name, attribute-value>


Phases of compiler:-Syntax Analysis

• Syntax analysis is the second phase of compiler


which is also called as parsing.

• Parser converts the tokens produced by lexical


analyzer into a tree like representation called
parse tree.

• A parse tree describes the syntactic structure of


the input.
Phases of compiler:-Syntax Analysis

• Syntax tree is a compressed representation


of the parse tree in which the operators
appear as interior nodes and the operands of
the operator are the children of the node for
that operator.
Input: Tokens

Output: Syntax tree


Phases of compiler:-Syntax Analysis
Phases of compiler:-Semantic Analysis

• Semantic analysis is the third phase of compiler.


 In this compiler tries to find the meaning of evaluated

statement.

• It checks for the semantic consistency.

• Performs type checking.

 In type checking compiler checks that each operator

has operands that are permitted by the source

language specification.
Phases of compiler:-Semantic Analysis
Phases of compiler:-Intermediate Code Generation

• Intermediate code generation produces intermediate representations


for the source program which are of the following forms:

o Postfix notation

o Three address code (max 3 addresses or operands existed)

o Syntax tree
 Most commonly used form is the three address code.

t1 = inttofloat (5)

t2 = id3* tl

t3 = id2 + t2

id1 = t3
Phases of compiler:-Intermediate Code Generation

Properties of intermediate code

• It should be easy to produce.

• It should be easy to translate into target


program.
Phases of compiler:-Code Optimization

• Code optimization phase gets the


intermediate code as input and produces
optimized intermediate code as output.

• It results in faster running machine code.


Phases of compiler:-Code Optimization

It can be done by reducing the number of

lines of code for a program.


• This phase reduces the redundant code and
attempts to improve the intermediate code so
that faster-running machine code will result.
During the code optimization, the result of

the program is not affected.


Phases of compiler:-Code Optimization

• To improve the code generation, the optimization involves

o Deduction and removal of dead code (unreachable code).

o Calculation of constants in expressions and terms.

o Collapsing of repeated expression into temporary string.

o Loop unrolling.

o Moving code outside the loop.

o Removal of unwanted temporary variables.

t1 = id3* 5.0

id1 = id2 + t1
Phases of compiler:-Code Generation

• Code generation is the final phase of a compiler.


• It gets input from code optimization phase and
produces the target code or object code as result.
• Intermediate instructions are translated into a
sequence of machine instructions that perform the
same task.
Phases of compiler:-Code Generation
•The code generation involves
o Allocation of register and memory.

o Generation of correct references.

o Generation of correct data types.

o Generation of missing code.

LDF R2, id3


MULF R2, # 5.0
LDF R1, id2
ADDF R1, R2
STF id1, R1
Symbol Table Management

• Symbol table is used to store all the information about


identifiers used in the program.

• It is a data structure containing a record for each


identifier, with fields for the attributes of the identifier.
It allows finding the record for each identifier quickly
and to store or retrieve data from that record.

• Whenever an identifier is detected in any of the phases,


it is stored in the symbol table
Error Handling

• Each phase can encounter errors. After


detecting an error, a phase must handle the
error so that compilation can proceed.
• In lexical analysis, errors occur in separation
of tokens.
• In syntax analysis, errors occur during
construction of syntax tree.
Error Handling

In semantic analysis, errors may occur at the


following cases:

(i) When the compiler detects constructs that have


right syntactic structure but no meaning

(ii) During type conversion.

• In code optimization, errors occur when the result


is affected by the optimization. In code generation,
it shows error when code is missing etc.
Error Handling

A program may have the following kinds of


errors at various stages:
Lexical Errors

It includes incorrect or misspelled name


of some identifier i.e., identifiers typed
incorrectly.
Error Handling

 Syntactical Errors

It includes missing semicolon or unbalanced parenthesis.


Syntactic errors are handled by syntax analyzer (parser).
When an error is detected, it must be handled by parser to
enable the parsing of the rest of the input. In general,
errors may be expected at various stages of compilation
but most of the errors are syntactic errors and hence the
parser should be able to detect and report those errors in
the program.
Goals Of Error Handler In Parser

• Report the presence of errors clearly and


accurately.

• Recover from each error quickly enough to


detect subsequent errors.

• Add minimal overhead to the processing of


correcting programs.
Error Handling

Semantical Errors

These errors are a result of incompatible value


assignment. The semantic errors that the semantic
analyzer is expected to recognize are:
• Type mismatch.
• Undeclared variable.
• Reserved identifier misuse.
• Multiple declaration of variable in a scope.
• Accessing an out of scope variable.
• Actual and formal parameter mismatch.
Error Handling

Logical errors
These errors occur due to not reachable
code-infinite loop.
Compiler construction tools
Compiler construction tools

The compiler writer can use some specialized

tools that help in implementing various


phases of a compiler. These tools assist in the
creation of an entire compiler or its parts.
Compiler construction tools

Some commonly used compiler construction tools


include:
 Parser Generator

 Scanner Generator

 Syntax directed translation engines

 Automatic code generators

 Data-flow analysis engines

 Compiler construction toolkits


Parser Generator

It produces syntax analyzers (parsers) from

the input that is based on a grammatical


description of programming language or on a
context-free grammar. It is useful as the
syntax analysis phase is highly complex and
consumes more manual and compilation time.
Example:PIC, EQM
Parser Generator
Scanner Generator

It generates lexical analyzers from the input

that consists of regular expression


description based on tokens of a language. It
generates a finite automation to recognize
the regular expression.
Example: Lex
Scanner Generator
Syntax directed translation engines

It generates intermediate code with three

address format from the input that consists of


a parse tree. These engines have routines to
traverse the parse tree and then produces the
intermediate code. In this, each node of the
parse tree is associated with one or more
translations.
Automatic code generators

It generates the machine language for a

target machine. Each operation of the


intermediate language is translated using a
collection of rules and then is taken as an
input by the code generator. Template
matching process is used. An intermediate
language statement is replaced by its
equivalent machine language statement using
Data-flow analysis engines

It is used in code optimization. Data flow

analysis is a key part of the code optimization


that gathers the information, that is the
values that flow from one part of a program
to another.
Compiler construction toolkits

It provides an integrated set of routines that

aids in building compiler components or in


the construction of various phases of
compiler.
Cousins Of Compilers
Linking and loading
It has four functions:
 Allocation:

It means get the memory portions from operating system and storing the object
data.
 Relocation:

It maps the relative address to the physical address and relocating the object code.
 Linker:

It combines all the executable object module to pre single executable file.
 Loader:

It loads the executable file into permanent storage.

You might also like