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

PPT
Introduction to Compiler Construction
PDF
loaders and linkers
PDF
Lecture1 introduction compilers
PPTX
Compiler Design
PPTX
Syntax Analysis in Compiler Design
PPTX
Compiler design
PPTX
Introduction to system programming
PPTX
Syntax analyzer
Introduction to Compiler Construction
loaders and linkers
Lecture1 introduction compilers
Compiler Design
Syntax Analysis in Compiler Design
Compiler design
Introduction to system programming
Syntax analyzer

What's hot (20)

PPT
High level languages representation
PPTX
Basic of compiler
PPTX
Interrupts
PPTX
High Level Language (HLL)
PPTX
System programming
PPTX
Intermediate code- generation
PPTX
Assemblers
PPTX
Macro Processor
PDF
Instruction code
PPT
basics of compiler design
PPT
Compiler1
PPTX
Loaders ( system programming )
PDF
Unit 3
PPTX
Compiler vs interpreter
PPTX
Structure of the compiler
PPTX
Exception handling c++
PPTX
DeadLock in Operating-Systems
PPT
Computer Organization and Assembly Language
PPTX
MACRO PROCESSOR
PPTX
Compiler vs interpreter
High level languages representation
Basic of compiler
Interrupts
High Level Language (HLL)
System programming
Intermediate code- generation
Assemblers
Macro Processor
Instruction code
basics of compiler design
Compiler1
Loaders ( system programming )
Unit 3
Compiler vs interpreter
Structure of the compiler
Exception handling c++
DeadLock in Operating-Systems
Computer Organization and Assembly Language
MACRO PROCESSOR
Compiler vs interpreter
Ad

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

PPTX
Unit2_CD.pptx more about compilation of the day
PDF
Compiler design lecture 1 introduction computer science
PDF
COMPILER DESIGN Engineering learinin.pdf
PDF
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
PDF
design intoduction of_COMPILER_DESIGN.pdf
PDF
compiler.pdfljdvgepitju4io3elkhldhyreyio4uw
PDF
Chapter1pdf__2021_11_23_10_53_20.pdf
DOCX
Compiler Design Material
PPTX
A Lecture of Compiler Design Subject.pptx
PDF
Chapter#01 cc
DOCX
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
PPTX
Phases of Compiler.pptx
PPT
Compiler Construction introduction
PDF
unit1pdf__2021_12_14_12_37_34.pdf
PPTX
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
PPT
A basic introduction to compiler design.ppt
PPT
A basic introduction to compiler design.ppt
PDF
Compiler_Lecture1.pdf
PDF
Language translators
DOC
Compiler Design(Nanthu)
Unit2_CD.pptx more about compilation of the day
Compiler design lecture 1 introduction computer science
COMPILER DESIGN Engineering learinin.pdf
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
design intoduction of_COMPILER_DESIGN.pdf
compiler.pdfljdvgepitju4io3elkhldhyreyio4uw
Chapter1pdf__2021_11_23_10_53_20.pdf
Compiler Design Material
A Lecture of Compiler Design Subject.pptx
Chapter#01 cc
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
Phases of Compiler.pptx
Compiler Construction introduction
unit1pdf__2021_12_14_12_37_34.pdf
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
A basic introduction to compiler design.ppt
A basic introduction to compiler design.ppt
Compiler_Lecture1.pdf
Language translators
Compiler Design(Nanthu)
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)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Event Presentation Google Cloud Next Extended 2025
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
DevOps & Developer Experience Summer BBQ
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PDF
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Event Presentation Google Cloud Next Extended 2025
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Understanding_Digital_Forensics_Presentation.pptx
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
CroxyProxy Instagram Access id login.pptx
DevOps & Developer Experience Summer BBQ
Reimagining Insurance: Connected Data for Confident Decisions.pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Dell Pro 14 Plus: Be better prepared for what’s coming
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
A Day in the Life of Location Data - Turning Where into How.pdf
NewMind AI Weekly Chronicles - August'25 Week I
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
madgavkar20181017ppt McKinsey Presentation.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