SlideShare a Scribd company logo
2
Most read
9
Most read
10
Most read
COMPILER DESIGN
Dr R Jegadeesan Prof-CSE
Jyothishmathi Institute of Technology and Science, Karimnagar
SYLLABUS
Introduction: Language Processors, the structure of a compiler, the science of building a compiler, programming
language basics.
Lexical Analysis: The Role of the Lexical Analyzer, Input Buffering, Recognition of Tokens, The Lexical-Analyzer
Generator Lex, Finite Automata, From Regular Expressions to Automata, Design of a Lexical-Analyzer
Generator, Optimization of DFA-Based Pattern Matchers
UNIT-I : INTRODUCTION
Topic Name : Language processors
Aim & Objective : convert language into target code.
Principle & Operation/ Detailed Explanation :
A translator is a programming language processor that takes a program
written in source code and converts it into machine code. It discovers
and identifies the error during translation.
There are 3 different types of translators as follows:
Compiler
A compiler is a translator used to convert high-level
programming language to low-level programming language. It
converts the whole program in one session and reports errors
detected after the conversion.
Interpreter
Just like a compiler, is a translator used to convert high-level programming
language to low-level programming language. It converts the program one at a
time and reports errors detected at once, while doing the conversion. With
this, it is easier to detect errors than in a compiler. An interpreter is faster than
a compiler as it immediately executes the code upon reading the code.
Assembler
An assembler is is a translator used to translate assembly language to machine
language. It is like a compiler for the assembly language but interactive like
an interpreter. Assembly language is difficult to understand as it is a low-level
programming language. An assembler translates a low-level language, an
assembly language to an even lower-level language, which is the machine
code. The machine code can be directly understood by the CPU.
Universities & Important Questions:
1. What are the differences between Compiler and Interpreter?
The Structure of the Compiler
Topic Name : Structure of the compiler
Aim & Objective : show different of forms of code before machine code..
Principle & Operation/ Detailed Explanation :
Lexical Analyzer ( scanner) –
It takes the output of preprocessor as the input which is in pure high level language. It
reads the characters from source program and groups them into lexemes (sequence of
characters that “go together”). Each lexeme corresponds to a token. Tokens are defined
by regular expressions which are understood by the lexical analyzer. It also removes
lexical errors (for e.g. erroneous characters), comments and white space.
Syntax Analyzer – It is sometimes called as parser. It constructs the parse tree. It takes all
the tokens one by one and uses Context Free Grammar to construct the parse tree.
Semantic Analyzer – It verifies the parse tree, whether it’s meaningful or not. It
furthermore produces a verified parse tree.It also does type checking, Label checking and
Flow control checking.
Intermediate Code Generator – It generates intermediate code, that is a form which
can be readily executed by machine . Example – Three address code etc. Intermediate
code is converted to machine language using the last two phases which are platform
dependent.
Code Optimizer – It transforms the code so that it consumes fewer resources and produces more speed.
The meaning of the code being transformed is not altered. Optimisation can be categorized into two
types: machine dependent and machine independent.
Target Code Generator – The main purpose of Target Code generator is to write a code that the
machine can understand and also register allocation, instruction selection etc. The output is
dependent on the type of assembler. This is the final stage of compilation.
Universities & Important Questions:
1. Explain phases of compiler with neat diagram ?
INPUT BUFFERING
Topic Name : Input Buffering.
Aim & Objective : Reduce time to read to input string.
Principle & Operation/ Detailed Explanation :
The lexical analyzer scans the input from left to right one character at a time. It uses two pointers begin
ptr(bp) and forward to keep track of the pointer of the input scanned.
The forward ptr moves ahead to search for end of lexeme. As soon as the blank
space is encountered, it indicates end of lexeme. In above example as soon as ptr
(fp) encounters a blank space the lexeme “int” is identified.
Lexical Analyzer Generator-Lex
An input file, which we call l e x . l , is written in the Lex language and
describes the lexical analyzer to be generated. The Lex compiler transforms l e x
. 1 to a C program, in a file that is always named l e x . y y . c. The latter file is
compiled by the C compiler into a file called a . o u t , as always. The C-compiler
output is a working lexical analyzer that can take a stream of input characters
and produce a stream of tokens.
Structure of Lex Programs
A Lex program has the following form:
declarations
°/.0/.
translation rules
°/.0/.
auxiliary functions
The declarations section includes declarations of variables, manifest constants (identifiers
declared to stand for a constant, e.g., the name of a token), and regular definitions.
The translation rules each have the form
Pattern { Action }
The third section holds whatever additional functions are used in the actions. Alternatively,
these functions can be compiled separately and loaded with the lexical analyzer.
Finite Automata
Finite Automata(FA) is the simplest machine to recognize patterns.
A Finite Automata consists of the following :
Q : Finite set of states.
∑ : set of Input Symbols.
q : Initial state.
F : set of Final States.
δ : Transition Function.
Formal specification of machine is
{ Q, ∑, q, F, δ }.
 FA is characterized into two types:
1) Deterministic Finite Automata (DFA)
2) Nondeterministic Finite Automata(NFA)
Deterministic Finite Automata
In a DFA, for a particular input character, the machine goes to one state only. A transition function is
defined on every state for every input symbol. Also in DFA null (or ε) move is not allowed, i.e., DFA
cannot change state without any input character.
For example, below DFA with ∑ = {0, 1} accepts all strings ending with 0.
Non-Deterministic Finite Automata
 NFA is similar to DFA except following additional features:
1. Null (or ε) move is allowed i.e., it can move forward without reading symbols.
2. Ability to transmit to any number of states for a particular input.
However, these above features don’t add any power to NFA. If we compare both in terms
of power, both are equivalent.
Due to above additional features, NFA has a different transition function, rest is same as
DFA.
δ: Transition Function
δ: Q X (∑ U ϵ ) --> 2 ^ Q.
As you can see in transition function is for any input including null (or ε), NFA can go to any
state number of states.
For example, below is a NFA for above problem
Thank you

More Related Content

PPTX
Phases of Compiler
PPTX
Lexical analysis - Compiler Design
PPT
basics of compiler design
PPTX
Ide description
PPTX
Text Editor for System Software
PDF
loaders and linkers
PPTX
Editor structure
PPTX
Compiler design syntax analysis
Phases of Compiler
Lexical analysis - Compiler Design
basics of compiler design
Ide description
Text Editor for System Software
loaders and linkers
Editor structure
Compiler design syntax analysis

What's hot (20)

PDF
P code
PPT
PPTX
COMPILER DESIGN OPTIONS
PPTX
Bootstrapping in Compiler
PPTX
Finite automata-for-lexical-analysis
PPTX
System Programming- Unit I
PDF
Lecture 01 introduction to compiler
PDF
Language processors
PPTX
Need for Software Engineering
PPTX
Performance analysis(Time & Space Complexity)
PPTX
Phases of compiler
PPT
1.Role lexical Analyzer
PPTX
Ch 3 Assembler in System programming
PPTX
Introduction to system programming
PPTX
Lexical Analysis - Compiler Design
PPTX
System programming
PPTX
Compiler Chapter 1
PPTX
The analysis synthesis model of compilation
PPTX
The role of the parser and Error recovery strategies ppt in compiler design
PPTX
Part I:Introduction to assembly language
P code
COMPILER DESIGN OPTIONS
Bootstrapping in Compiler
Finite automata-for-lexical-analysis
System Programming- Unit I
Lecture 01 introduction to compiler
Language processors
Need for Software Engineering
Performance analysis(Time & Space Complexity)
Phases of compiler
1.Role lexical Analyzer
Ch 3 Assembler in System programming
Introduction to system programming
Lexical Analysis - Compiler Design
System programming
Compiler Chapter 1
The analysis synthesis model of compilation
The role of the parser and Error recovery strategies ppt in compiler design
Part I:Introduction to assembly language
Ad

Similar to COMPILER DESIGN- Introduction & Lexical Analysis: (20)

PPTX
CD U1-5.pptx
PPTX
Compiler Design Introduction With Design
PPTX
Introduction to Compilers
PPT
Introduction to compiler design and phases of compiler
PPTX
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
PPTX
CD UNIT-1.3 LEX PPT.pptx
PPT
Unit1.ppt
PPTX
Presentation compiler design
DOCX
Compiler Design
PPT
Compier Design_Unit I.ppt
PPT
Compier Design_Unit I.ppt
PPT
Compiler Design in Computer Applications
PPTX
Structure of the compiler
PPTX
Phases of Compiler.pptx
PDF
Chapter#01 cc
PDF
compiler.pdfljdvgepitju4io3elkhldhyreyio4uw
PDF
Chapter1pdf__2021_11_23_10_53_20.pdf
PPTX
1 compiler outline
PDF
COMPILER DESIGN Engineering learinin.pdf
PDF
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
CD U1-5.pptx
Compiler Design Introduction With Design
Introduction to Compilers
Introduction to compiler design and phases of compiler
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
CD UNIT-1.3 LEX PPT.pptx
Unit1.ppt
Presentation compiler design
Compiler Design
Compier Design_Unit I.ppt
Compier Design_Unit I.ppt
Compiler Design in Computer Applications
Structure of the compiler
Phases of Compiler.pptx
Chapter#01 cc
compiler.pdfljdvgepitju4io3elkhldhyreyio4uw
Chapter1pdf__2021_11_23_10_53_20.pdf
1 compiler outline
COMPILER DESIGN Engineering learinin.pdf
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
Ad

More from Jyothishmathi Institute of Technology and Science Karimnagar (20)

PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
PDF
JAVA PROGRAMMING - The Collections Framework
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
PDF
Java programming -Object-Oriented Thinking- Inheritance
PDF
Compiler Design- Machine Independent Optimizations
PDF
PDF
COMPILER DESIGN- Syntax Directed Translation
PPTX
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
PDF
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
PDF
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
PDF
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
PDF
Computer Forensics Working with Windows and DOS Systems
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING – Packages - Stream based I/O
Java programming -Object-Oriented Thinking- Inheritance
Compiler Design- Machine Independent Optimizations
COMPILER DESIGN- Syntax Directed Translation
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
Computer Forensics Working with Windows and DOS Systems

Recently uploaded (20)

PDF
Omni-Path Integration Expertise Offered by Nor-Tech
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Advanced IT Governance
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PPTX
Cloud computing and distributed systems.
PDF
Sensors and Actuators in IoT Systems using pdf
Omni-Path Integration Expertise Offered by Nor-Tech
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Chapter 3 Spatial Domain Image Processing.pdf
Smarter Business Operations Powered by IoT Remote Monitoring
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
Advanced Soft Computing BINUS July 2025.pdf
Event Presentation Google Cloud Next Extended 2025
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Advanced IT Governance
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 2 Digital Image Fundamentals.pdf
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
CroxyProxy Instagram Access id login.pptx
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Cloud computing and distributed systems.
Sensors and Actuators in IoT Systems using pdf

COMPILER DESIGN- Introduction & Lexical Analysis:

  • 1. COMPILER DESIGN Dr R Jegadeesan Prof-CSE Jyothishmathi Institute of Technology and Science, Karimnagar
  • 2. SYLLABUS Introduction: Language Processors, the structure of a compiler, the science of building a compiler, programming language basics. Lexical Analysis: The Role of the Lexical Analyzer, Input Buffering, Recognition of Tokens, The Lexical-Analyzer Generator Lex, Finite Automata, From Regular Expressions to Automata, Design of a Lexical-Analyzer Generator, Optimization of DFA-Based Pattern Matchers
  • 3. UNIT-I : INTRODUCTION Topic Name : Language processors Aim & Objective : convert language into target code. Principle & Operation/ Detailed Explanation : A translator is a programming language processor that takes a program written in source code and converts it into machine code. It discovers and identifies the error during translation. There are 3 different types of translators as follows: Compiler A compiler is a translator used to convert high-level programming language to low-level programming language. It converts the whole program in one session and reports errors detected after the conversion.
  • 4. Interpreter Just like a compiler, is a translator used to convert high-level programming language to low-level programming language. It converts the program one at a time and reports errors detected at once, while doing the conversion. With this, it is easier to detect errors than in a compiler. An interpreter is faster than a compiler as it immediately executes the code upon reading the code. Assembler An assembler is is a translator used to translate assembly language to machine language. It is like a compiler for the assembly language but interactive like an interpreter. Assembly language is difficult to understand as it is a low-level programming language. An assembler translates a low-level language, an assembly language to an even lower-level language, which is the machine code. The machine code can be directly understood by the CPU. Universities & Important Questions: 1. What are the differences between Compiler and Interpreter?
  • 5. The Structure of the Compiler Topic Name : Structure of the compiler Aim & Objective : show different of forms of code before machine code.. Principle & Operation/ Detailed Explanation :
  • 6. Lexical Analyzer ( scanner) – It takes the output of preprocessor as the input which is in pure high level language. It reads the characters from source program and groups them into lexemes (sequence of characters that “go together”). Each lexeme corresponds to a token. Tokens are defined by regular expressions which are understood by the lexical analyzer. It also removes lexical errors (for e.g. erroneous characters), comments and white space. Syntax Analyzer – It is sometimes called as parser. It constructs the parse tree. It takes all the tokens one by one and uses Context Free Grammar to construct the parse tree. Semantic Analyzer – It verifies the parse tree, whether it’s meaningful or not. It furthermore produces a verified parse tree.It also does type checking, Label checking and Flow control checking. Intermediate Code Generator – It generates intermediate code, that is a form which can be readily executed by machine . Example – Three address code etc. Intermediate code is converted to machine language using the last two phases which are platform dependent.
  • 7. Code Optimizer – It transforms the code so that it consumes fewer resources and produces more speed. The meaning of the code being transformed is not altered. Optimisation can be categorized into two types: machine dependent and machine independent. Target Code Generator – The main purpose of Target Code generator is to write a code that the machine can understand and also register allocation, instruction selection etc. The output is dependent on the type of assembler. This is the final stage of compilation. Universities & Important Questions: 1. Explain phases of compiler with neat diagram ?
  • 8. INPUT BUFFERING Topic Name : Input Buffering. Aim & Objective : Reduce time to read to input string. Principle & Operation/ Detailed Explanation : The lexical analyzer scans the input from left to right one character at a time. It uses two pointers begin ptr(bp) and forward to keep track of the pointer of the input scanned.
  • 9. The forward ptr moves ahead to search for end of lexeme. As soon as the blank space is encountered, it indicates end of lexeme. In above example as soon as ptr (fp) encounters a blank space the lexeme “int” is identified.
  • 10. Lexical Analyzer Generator-Lex An input file, which we call l e x . l , is written in the Lex language and describes the lexical analyzer to be generated. The Lex compiler transforms l e x . 1 to a C program, in a file that is always named l e x . y y . c. The latter file is compiled by the C compiler into a file called a . o u t , as always. The C-compiler output is a working lexical analyzer that can take a stream of input characters and produce a stream of tokens.
  • 11. Structure of Lex Programs A Lex program has the following form: declarations °/.0/. translation rules °/.0/. auxiliary functions The declarations section includes declarations of variables, manifest constants (identifiers declared to stand for a constant, e.g., the name of a token), and regular definitions. The translation rules each have the form Pattern { Action } The third section holds whatever additional functions are used in the actions. Alternatively, these functions can be compiled separately and loaded with the lexical analyzer.
  • 12. Finite Automata Finite Automata(FA) is the simplest machine to recognize patterns. A Finite Automata consists of the following : Q : Finite set of states. ∑ : set of Input Symbols. q : Initial state. F : set of Final States. δ : Transition Function. Formal specification of machine is { Q, ∑, q, F, δ }.  FA is characterized into two types: 1) Deterministic Finite Automata (DFA) 2) Nondeterministic Finite Automata(NFA)
  • 13. Deterministic Finite Automata In a DFA, for a particular input character, the machine goes to one state only. A transition function is defined on every state for every input symbol. Also in DFA null (or ε) move is not allowed, i.e., DFA cannot change state without any input character. For example, below DFA with ∑ = {0, 1} accepts all strings ending with 0.
  • 14. Non-Deterministic Finite Automata  NFA is similar to DFA except following additional features: 1. Null (or ε) move is allowed i.e., it can move forward without reading symbols. 2. Ability to transmit to any number of states for a particular input. However, these above features don’t add any power to NFA. If we compare both in terms of power, both are equivalent. Due to above additional features, NFA has a different transition function, rest is same as DFA. δ: Transition Function δ: Q X (∑ U ϵ ) --> 2 ^ Q. As you can see in transition function is for any input including null (or ε), NFA can go to any state number of states. For example, below is a NFA for above problem