SlideShare a Scribd company logo
Harcourt butler technical University Kanpur
 A compiler is a program that reads a program
written in one language and translates into
equivalent target language
Source- - Target
|
|
|
Error Message
COMPILER
Compiler design and lexical analyser
 The Front end checks whether the
program is correctly written in terms of the
programming language syntax and
semantics
 The back end is responsible for translating
the source into assembly code.
Front End :
Lexical Analysis
Preprocessing
Syntax Analysis
Semantic Analysis
Back End
 Analysis
 Optimization
 Code generation
 Lexical Analyzer
 Syntax Analyzer
 Semantic Analyzer
 Intermediate code generator
 Code optimizer
 Code generator
Lexical
Syntax
Semantic
Code Generator
Code Optimizer
Intermediate Code Generator
Symbol
Table
manager
Error
Handler
 Also called Linear Analysis
 Characters read from left to right and
grouped into tokens that are a sequence of
characters with a collective meaning
Scans Input
Removes White spaces and comments
Manufacture Tokens
Generate Error if Any
◦ Example
◦ A=B+C
◦ Variable tokens - A ,B, C
◦ Symbolic token -- = +
11
Lex(Flex in recent implementation)
12
 The main job of a lexical analyzer (scanner) is
to break up an input stream into
tokens(tokenize input streams).
 Ex :-
a = b + c * d;
ID ASSIGN ID PLUS ID MULT ID SEMI
 Lex is an utility to help you rapidly generate
your scanners
PLLab, NTHU,Cs2403 Programming
Languages 13
(optional)
(required)
 Lex source is separated into three sections
by %% delimiters
 The general format of Lex source is
The absolute minimum Lex program is thus
{definitions}
%%
{transition rules}
%%
{user Code}
%%
 Declarations of ordinary C variables
,constants and Libraries.
%{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
%}
 flex definitions :- name definition
Digit [0-9] (Regular Definition)
14
15
Lex compiler
C compiler
a.out
Lex source
program
lex.yy.c
input
lex.yy.c
a.out
tokens
16
 yytext -- a string containing the lexeme
 yyleng -- the length of the lexeme
 yyin -- the input stream pointer
◦ the default input of default main() is stdin
 yyout -- the output stream pointer
◦ the default output of default main() is stdout.
17
 yylex()
◦ The default main() contains a call of yylex()
 yymore()
◦ return the next token
 yyless(n)
◦ retain the first n characters in yytext
 yywarp()
◦ is called whenever Lex reaches an end-of-file
◦ The default yywarp() always returns 1
PLLab, NTHU,Cs2403 Programming
Languages 18
Name Function
char *yytext pointer to matched string
int yyleng length of matched string
FILE *yyin input stream pointer
FILE *yyout output stream pointer
int yylex(void) call to invoke lexer, returns token
char* yymore(void) return the next token
int yyless(int n) retain the first n characters in yytext
int yywrap(void) wrapup, return 1 if done, 0 if not done
ECHO write matched string
REJECT go to the next alternative rule
INITAL initial start condition
BEGIN condition switch start condition
 Also called as Hierarchial Analysis
 A syntax tree[also called as parse tree] is generated
where
◦ Operators  Interior nodes
◦ Operands  Children of node for operators.
=
A + Interior
B C Children
 Characters grouped as tokens in Lexical Analysis are
recorded as Tables. Checks for semantic errors
 Collect TYPE information for the subsequent code
generation phase
 Sophisticated compilers typically perform multiple
passes over various intermediate forms.
 Many algorithms for code optimization are easier to
apply one at a time
 The input to one optimization relies on the
processing performed by another optimization
Concrete Parse tree
Abstract syntax tree
Converted into a linear sequence of instructions
Results in 3AC [ 3 Address Code]
 This phase attempts to improve the intermediate
code inorder to increase the running time
 Reduce the complexity of the code generated
 Leading to a faster execution of the program
 Increased Performance
 Platform Dependant/ Platform Independent
 Optimization can be automated by compilers or
performed by programmers
 Usually, the most powerful optimization is to find
a superior algorithm.
 Include activities like
◦ Optimization of LOOPS
◦ Optimization of Bottlenecks
 Succeeding step of Intermediate code
optimizer
 Consists of re-locatable machine
code/assembly code
 Intermediate instructions are converted
into a a sequence of machine instructions
THANK
YOU

More Related Content

PPT
Lexical Analyzers and Parsers
PPTX
Spell checker using Natural language processing
PPTX
Role-of-lexical-analysis
PPTX
White Box Testing
PPT
Lexical Analysis
PDF
Black Box Testing
PPTX
Language processing activity
PDF
Syntax analysis
Lexical Analyzers and Parsers
Spell checker using Natural language processing
Role-of-lexical-analysis
White Box Testing
Lexical Analysis
Black Box Testing
Language processing activity
Syntax analysis

What's hot (20)

PPTX
Structure of the compiler
PPTX
formal verification
PPTX
Lexical analyzer generator lex
PPTX
Compiler design syntax analysis
PDF
LR Parsing
PDF
Target language in compiler design
PPTX
PPTX
Phased life cycle model
PPTX
Kerberos
PPTX
Shift reduce parser
PDF
Lecture11 syntax analysis_7
PPTX
Lexical Analysis - Compiler Design
PPTX
Netcat - A Swiss Army Tool
PPT
Lexical analyzer
PPTX
Error Detection & Recovery
PDF
Lecture: Automata
PPTX
Code Optimization
PPTX
Single pass assembler
PPTX
Peephole optimization techniques in compiler design
PPTX
Integrated Development Environments (IDE)
Structure of the compiler
formal verification
Lexical analyzer generator lex
Compiler design syntax analysis
LR Parsing
Target language in compiler design
Phased life cycle model
Kerberos
Shift reduce parser
Lecture11 syntax analysis_7
Lexical Analysis - Compiler Design
Netcat - A Swiss Army Tool
Lexical analyzer
Error Detection & Recovery
Lecture: Automata
Code Optimization
Single pass assembler
Peephole optimization techniques in compiler design
Integrated Development Environments (IDE)
Ad

Similar to Compiler design and lexical analyser (20)

DOCX
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
PPTX
role of lexical anaysis
DOCX
Compiler Design
PPT
Unit1.ppt
PPTX
Plc part 2
PDF
Lecture 01 introduction to compiler
PPTX
The Phases of a Compiler
PDF
compiler.pdfljdvgepitju4io3elkhldhyreyio4uw
PDF
lec00-Introduction.pdf
PPTX
Presentation1
PPTX
Presentation1
PPTX
Compilers
PPTX
Introduction to Compilers
PPTX
Compiler Design Introduction With Design
PDF
Compilers Design
PPTX
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
PPTX
Chapter 1.pptx
PPT
Compiler Design in Computer Applications
PPTX
1 cc
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
role of lexical anaysis
Compiler Design
Unit1.ppt
Plc part 2
Lecture 01 introduction to compiler
The Phases of a Compiler
compiler.pdfljdvgepitju4io3elkhldhyreyio4uw
lec00-Introduction.pdf
Presentation1
Presentation1
Compilers
Introduction to Compilers
Compiler Design Introduction With Design
Compilers Design
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
Chapter 1.pptx
Compiler Design in Computer Applications
1 cc
Ad

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Geodesy 1.pptx...............................................
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Mechanical Engineering MATERIALS Selection
PDF
composite construction of structures.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
additive manufacturing of ss316l using mig welding
PDF
Well-logging-methods_new................
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
DOCX
573137875-Attendance-Management-System-original
PPTX
Sustainable Sites - Green Building Construction
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
Geodesy 1.pptx...............................................
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Lecture Notes Electrical Wiring System Components
Mechanical Engineering MATERIALS Selection
composite construction of structures.pdf
Foundation to blockchain - A guide to Blockchain Tech
additive manufacturing of ss316l using mig welding
Well-logging-methods_new................
CH1 Production IntroductoryConcepts.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
573137875-Attendance-Management-System-original
Sustainable Sites - Green Building Construction
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT

Compiler design and lexical analyser

  • 1. Harcourt butler technical University Kanpur
  • 2.  A compiler is a program that reads a program written in one language and translates into equivalent target language Source- - Target | | | Error Message COMPILER
  • 4.  The Front end checks whether the program is correctly written in terms of the programming language syntax and semantics  The back end is responsible for translating the source into assembly code.
  • 5. Front End : Lexical Analysis Preprocessing Syntax Analysis Semantic Analysis
  • 6. Back End  Analysis  Optimization  Code generation
  • 7.  Lexical Analyzer  Syntax Analyzer  Semantic Analyzer  Intermediate code generator  Code optimizer  Code generator
  • 8. Lexical Syntax Semantic Code Generator Code Optimizer Intermediate Code Generator Symbol Table manager Error Handler
  • 9.  Also called Linear Analysis  Characters read from left to right and grouped into tokens that are a sequence of characters with a collective meaning Scans Input Removes White spaces and comments Manufacture Tokens Generate Error if Any
  • 10. ◦ Example ◦ A=B+C ◦ Variable tokens - A ,B, C ◦ Symbolic token -- = +
  • 11. 11 Lex(Flex in recent implementation)
  • 12. 12  The main job of a lexical analyzer (scanner) is to break up an input stream into tokens(tokenize input streams).  Ex :- a = b + c * d; ID ASSIGN ID PLUS ID MULT ID SEMI  Lex is an utility to help you rapidly generate your scanners
  • 13. PLLab, NTHU,Cs2403 Programming Languages 13 (optional) (required)  Lex source is separated into three sections by %% delimiters  The general format of Lex source is The absolute minimum Lex program is thus {definitions} %% {transition rules} %% {user Code} %%
  • 14.  Declarations of ordinary C variables ,constants and Libraries. %{ #include <math.h> #include <stdio.h> #include <stdlib.h> %}  flex definitions :- name definition Digit [0-9] (Regular Definition) 14
  • 15. 15 Lex compiler C compiler a.out Lex source program lex.yy.c input lex.yy.c a.out tokens
  • 16. 16  yytext -- a string containing the lexeme  yyleng -- the length of the lexeme  yyin -- the input stream pointer ◦ the default input of default main() is stdin  yyout -- the output stream pointer ◦ the default output of default main() is stdout.
  • 17. 17  yylex() ◦ The default main() contains a call of yylex()  yymore() ◦ return the next token  yyless(n) ◦ retain the first n characters in yytext  yywarp() ◦ is called whenever Lex reaches an end-of-file ◦ The default yywarp() always returns 1
  • 18. PLLab, NTHU,Cs2403 Programming Languages 18 Name Function char *yytext pointer to matched string int yyleng length of matched string FILE *yyin input stream pointer FILE *yyout output stream pointer int yylex(void) call to invoke lexer, returns token char* yymore(void) return the next token int yyless(int n) retain the first n characters in yytext int yywrap(void) wrapup, return 1 if done, 0 if not done ECHO write matched string REJECT go to the next alternative rule INITAL initial start condition BEGIN condition switch start condition
  • 19.  Also called as Hierarchial Analysis  A syntax tree[also called as parse tree] is generated where ◦ Operators  Interior nodes ◦ Operands  Children of node for operators. = A + Interior B C Children
  • 20.  Characters grouped as tokens in Lexical Analysis are recorded as Tables. Checks for semantic errors  Collect TYPE information for the subsequent code generation phase
  • 21.  Sophisticated compilers typically perform multiple passes over various intermediate forms.  Many algorithms for code optimization are easier to apply one at a time  The input to one optimization relies on the processing performed by another optimization
  • 22. Concrete Parse tree Abstract syntax tree Converted into a linear sequence of instructions Results in 3AC [ 3 Address Code]
  • 23.  This phase attempts to improve the intermediate code inorder to increase the running time  Reduce the complexity of the code generated  Leading to a faster execution of the program  Increased Performance
  • 24.  Platform Dependant/ Platform Independent  Optimization can be automated by compilers or performed by programmers  Usually, the most powerful optimization is to find a superior algorithm.  Include activities like ◦ Optimization of LOOPS ◦ Optimization of Bottlenecks
  • 25.  Succeeding step of Intermediate code optimizer  Consists of re-locatable machine code/assembly code  Intermediate instructions are converted into a a sequence of machine instructions