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

Introduction To Compiler

Uploaded by

bhawana.tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Introduction To Compiler

Uploaded by

bhawana.tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to

Compiler
Presented By:
Dr. Bhawana Tyagi
Compiler
Steps for Execution
LLVM (Low Level Virtual
Machine)
• It is an open source compiler infrastructure that provides a set of tools and
libraries for building compilers, debuggers, and other software development
tools.
• It was originally developed at the University of Illinois at Urbana-Champaign
and is now maintained by the LLVM Foundation.
• It consists of a set of modular and reusable compiler and tool chain
technologies that can be used to build a wide range of compilers and other
tools.
• It includes a compiler front end, which translates high-level programming
languages into intermediate representations (IRs); a compiler back end,
which translates IRs into machine code that can be executed on a specific
target architecture
• It can be used to build compilers for a wide range of programming languages,
including C, C++, and Fortran, as well as other languages such as Swift and
Rust.
LLVM Compilation
Infrastructure
LLVM Compilation
Infrastructure
Lexical Analysis
• 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.
• 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.
Once a token is generated the corresponding entry is made in the
symbol table.
Lexical Analysis
Lexeme Token
Example: Input String a Identifier
= Assignment Operator
a=b+c*b+c*b+c
b Identifier
+ Operator
c Identifier
* Operator
b Identifier
+ Operator
c Identifier
* Operator
b Identifier
+ Operator
c Identifier
Syntax Analysis
• It converts the tokens produced by lexical analyzer into a tree
like representation called parse tree.
• Obtain tokens from the lexical analyzer.
• Checks if the expression is syntactically correct or not.
• Report all syntax errors.
• Construct a hierarchical structure which is known as a parse
tree.
Syntax Analysis
• a=b+c*b+c*b+c
Semantic Analysis
• Semantic analysis checks the semantic consistency of the
code.
• It uses the syntax tree of the previous phase along with the
symbol table to verify that the given source code is
semantically consistent.
• It also checks whether the code is conveying an appropriate
meaning.
• Semantic Analyzer will check for Type mismatches,
incompatible operands, a function called with improper
arguments etc.
Intermediate Code
Generation
• The compiler produces intermediate code for the target
machine following the completion of the semantic
analysis step.
• It is necessary to create this intermediate code in a way
that makes it simple to convert it into the target machine
code.
Intermediate Code
Generation
a=b+c*b+c*b+c
-------------------------------------
T1=c*b
T2=c*b
T3=b+T1
T4=T3+T2
T5=T4+c
a= T5
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.
• 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.
Code Optimization

a=b+c*b+c*b+c
-----------------------------------
T1=c*b
T2=b+T1
T3=T2+T1
T4=T3+c
a= T3
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.
Code Generation
Example: a = b + 60.0
----------------------------------------------
MOV b, R1
MULF #60.0, R2
ADD R2, R1
Store R1,a
Error Handling
In the compiler design process error may occur in all the below-
given phases:
• Lexical analyzer: Wrongly spelled tokens
• Syntax analyzer: Missing parenthesis
• Intermediate code generator: Mismatched operands for an
operator
• Code Optimizer: When the statement is not reachable
• Code Generator: When the memory is full or proper registers
are not allocated
• Symbol tables: Error of multiple declared identifiers.
Symbol Table
• A symbol table contains a record for each identifier with fields
for the attributes of the identifier.
• This component makes it easier for the compiler to search the
identifier record and retrieve it quickly.
• The symbol table also helps you for the scope management.
• The symbol table and error handler interact with all the phases
and symbol table update correspondingly.
Thank You

You might also like