SlideShare a Scribd company logo
TCS 502 Compiler Designp g
CS416 Compiler Design 1P K Singh
Course Information
Textbook:
Alfred V Aho Ravi Sethi and Jeffrey D UllmanAlfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman,
“Compilers: Principles, Techniques, and Tools”
Addison-Wesley, 1986.Addison Wesley, 1986.
• Course Web Page: http://pksmmmec.googlepages.com
CS416 Compiler Design 2P K Singh
Preliminaries Required
• Basic knowledge of programming languages.
• Basic knowledge of FSA and CFGBasic knowledge of FSA and CFG.
• Knowledge of a high programming language for the
programming assignments.
CS416 Compiler Design 3P K Singh
Course Outline
• Introduction to Compiling
• Lexical Analysisy
• Syntax Analysis
– Context Free GrammarsContext Free Grammars
– Top-Down Parsing, LL Parsing
– Bottom-Up Parsing, LR Parsingp g g
CS416 Compiler Design 4P K Singh
Course Outline
• Syntax-Directed Translation
– Attribute Definitions
– Evaluation of Attribute Definitions
• Semantic Analysis, Type Checking
• Run-Time Organization
• Intermediate Code GenerationIntermediate Code Generation
• Code Optimization
P K Singh CS416 Compiler Design 5
COMPILERS
• A compiler is a program takes a program written in a source
language and translates it into an equivalent program in a targetg g q p g g
language.
source program COMPILER target program
( ll i i ( ll h i l i
error messages
( Normally a program written in
a high-level programming language)
( Normally the equivalent program in
machine code – relocatable object file)
error messages
CS416 Compiler Design 6P K Singh
Major Parts of Compilers
• There are two major parts of a compiler: Analysis and
SynthesisSynthesis
• In analysis phase an intermediate representation is• In analysis phase, an intermediate representation is
created from the given source program.
– Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts ofy y y y p
this phase.
• In synthesis phase, the equivalent target program is
t d f thi i t di t t ticreated from this intermediate representation.
– Intermediate Code Generator, Code Generator, and Code Optimizer are
the parts of this phase.
CS416 Compiler Design 7P K Singh
Phases of A Compiler
Lexical
Analyzer
Semantic
Analyzer
Syntax
Analyzer
Intermediate
Code Generator
Code
Optimizer
Code
Generator
Target
Program
Source
Program
• Each phase transforms the source program from one representationp p g p
into another representation.
• They communicate with error handlers• They communicate with error handlers.
• They communicate with the symbol table.
CS416 Compiler Design 8P K Singh
Lexical Analyzer
• Lexical Analyzer reads the source program character by
character and returns the tokens of the source program.
• A token describes a pattern of characters having same meaning
in the source program. (such as identifiers, operators, keywords,
numbers, delimeters and so on), )
Ex: newval := oldval + 12 => tokens: newval identifier
:= assignment operator
oldval identifier
+ add operator
12 a number
• Puts information about identifiers into the symbol table.u s o a o abou de e s o e sy bo ab e
• Regular expressions are used to describe tokens (lexical
constructs).
A (Deterministic) Finite State Automaton can be used in the
CS416 Compiler Design 9
• A (Deterministic) Finite State Automaton can be used in the
implementation of a lexical analyzer.
P K Singh
Syntax Analyzer
• A Syntax Analyzer creates the syntactic structure (generally a
parse tree) of the given program.p ) g p g
• A syntax analyzer is also called as a parser.
• A parse tree describes a syntactic structure.
assgstmt
identifier := expression • In a parse tree, all terminals are at leaves.
newval expression + expression
id tifi b
p ,
• All inner nodes are non-terminals in
a context free grammar.
identifier number
oldval 12
CS416 Compiler Design 10P K Singh
Syntax Analyzer (CFG)
• The syntax of a language is specified by a context free
grammar (CFG).g ( )
• The rules in a CFG are mostly recursive.
• A syntax analyzer checks whether a given program satisfies the
rules implied by a CFG or not.
– If it satisfies, the syntax analyzer creates a parse tree for the given program.
• Ex: We use BNF (Backus Naur Form) to specify a CFG
assgstmt -> identifier := expression
expression -> identifier
expression -> number
expression -> expression + expression
CS416 Compiler Design 11P K Singh
Syntax Analyzer versus Lexical Analyzer
• Which constructs of a program should be recognized by the
lexical analyzer, and which ones by the syntax analyzer?y , y y y
– Both of them do similar things; But the lexical analyzer deals with simple non-
recursive constructs of the language.
– The syntax analyzer deals with recursive constructs of the language.
– The lexical analyzer simplifies the job of the syntax analyzer.
– The lexical analyzer recognizes the smallest meaningful units (tokens) in a source
program.
– The syntax analyzer works on the smallest meaningful units (tokens) in a source
program to recognize meaningful structures in our programming language.
CS416 Compiler Design 12P K Singh
Parsing Techniques
• Depending on how the parse tree is created, there are different
parsing techniques.
These parsing techniques are categorized into two groups:• These parsing techniques are categorized into two groups:
– Top-Down Parsing,
– Bottom-Up Parsingp g
• Top-Down Parsing:
– Construction of the parse tree starts at the root, and proceeds towards the leaves.
– Efficient top-down parsers can be easily constructed by hand.Efficient top down parsers can be easily constructed by hand.
– Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing).
• Bottom-Up Parsing:
– Construction of the parse tree starts at the leaves and proceeds towards the rootConstruction of the parse tree starts at the leaves, and proceeds towards the root.
– Normally efficient bottom-up parsers are created with the help of some software
tools.
– Bottom-up parsing is also known as shift-reduce parsing.
CS416 Compiler Design 13
– Operator-Precedence Parsing – simple, restrictive, easy to implement
– LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR
P K Singh
Semantic Analyzer
• A semantic analyzer checks the source program for semantic
errors and collects the type information for the code generation.yp g
• Type-checking is an important part of semantic analyzer.
• Normally semantic information cannot be represented by a
context-free language used in syntax analyzers.
• Context-free grammars used in the syntax analysis are integrated
with attributes (semantic rules)with attributes (semantic rules)
– the result is a syntax-directed translation,
– Attribute grammars
E• Ex:
newval := oldval + 12
Th t f th id tifi l t t h ith t f th i ( ld l 12)
CS416 Compiler Design 14
• The type of the identifier newval must match with type of the expression (oldval+12)
P K Singh
Intermediate Code Generation
• A compiler may produce an explicit intermediate codes
representing the source program.p g p g
• These intermediate codes are generally machine (architecture
independent). But the level of intermediate codes is close to the
le el of machine codeslevel of machine codes.
• Ex:
newval := oldval * fact + 1
id1 := id2 * id3 + 1
MULT id2,id3,temp1 Intermediates Codes (Quadraples)
ADD temp1,#1,temp2
MOV temp2,,id1
CS416 Compiler Design 15
p ,,
P K Singh
Code Optimizer (for Intermediate Code Generator)
• The code optimizer optimizes the code produced by the
intermediate code generator in the terms of time and space.g p
• Ex:
MULT id2,id3,temp1
ADD temp1,#1,id1ADD temp1,#1,id1
CS416 Compiler Design 16P K Singh
Code Generator
• Produces the target language in a specific architecture.
• The target program is normally is a relocatable object fileThe target program is normally is a relocatable object file
containing the machine codes.
• Ex:
( assume that we have an architecture with instructions whose at least one of its operands
isis
a machine register)
MOVE id2 R1MOVE id2,R1
MULT id3,R1
ADD #1,R1
MOVE R1 id1
CS416 Compiler Design 17
MOVE R1,id1
P K Singh

More Related Content

PPTX
Yacc
BBDITM LUCKNOW
 
PPT
Compiler Design Tutorial
Sarit Chakraborty
 
PDF
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
PDF
Compiler unit 1
BBDITM LUCKNOW
 
PPT
Introduction to course
nikit meshram
 
PPT
Cpcs302 1
guest5de1a5
 
PPTX
Flex (fast lexical analyzer generator )
Sandip Basnet
 
PDF
Compiler Design Lecture Notes
FellowBuddy.com
 
Compiler Design Tutorial
Sarit Chakraborty
 
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
Compiler unit 1
BBDITM LUCKNOW
 
Introduction to course
nikit meshram
 
Cpcs302 1
guest5de1a5
 
Flex (fast lexical analyzer generator )
Sandip Basnet
 
Compiler Design Lecture Notes
FellowBuddy.com
 

What's hot (20)

PPT
Csci360 08-subprograms
Boniface Mwangi
 
PPT
Unit 5 cspc
BBDITM LUCKNOW
 
PPTX
Compiler Design
Dr. Jaydeep Patil
 
DOC
Lex tool manual
Sami Said
 
PDF
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
KEY
Unit 1 cd
codereplugd
 
PPTX
Lex & yacc
Taha Malampatti
 
PPT
Compiler Design
Mir Majid
 
PPTX
7 compiler lab
MashaelQ
 
PPT
Lex and Yacc ppt
pssraikar
 
PDF
08 subprograms
baran19901990
 
PPTX
Structure of the compiler
Sudhaa Ravi
 
ODP
About Tokens and Lexemes
Ben Scholzen
 
PDF
Compiler Design Introduction
Richa Sharma
 
PDF
Compilers Design
Akshaya Arunan
 
PDF
Subprogram
baran19901990
 
PDF
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
PDF
Cs6660 compiler design
hari2010
 
PPTX
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Csci360 08-subprograms
Boniface Mwangi
 
Unit 5 cspc
BBDITM LUCKNOW
 
Compiler Design
Dr. Jaydeep Patil
 
Lex tool manual
Sami Said
 
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
Unit 1 cd
codereplugd
 
Lex & yacc
Taha Malampatti
 
Compiler Design
Mir Majid
 
7 compiler lab
MashaelQ
 
Lex and Yacc ppt
pssraikar
 
08 subprograms
baran19901990
 
Structure of the compiler
Sudhaa Ravi
 
About Tokens and Lexemes
Ben Scholzen
 
Compiler Design Introduction
Richa Sharma
 
Compilers Design
Akshaya Arunan
 
Subprogram
baran19901990
 
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
Cs6660 compiler design
hari2010
 
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Ad

Viewers also liked (9)

PPTX
Complier designer
Jagjit Wilku
 
PPT
4 lexical and syntax
Munawar Ahmed
 
PDF
4 lexical and syntax analysis
jigeno
 
PDF
LR Parsing
Eelco Visser
 
PPTX
Lexing and parsing
Elizabeth Smith
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPT
Module 11
bittudavis
 
PPT
Lexical analyzer
Ashwini Sonawane
 
PPTX
Lexical Approach
Muhittin Yakut
 
Complier designer
Jagjit Wilku
 
4 lexical and syntax
Munawar Ahmed
 
4 lexical and syntax analysis
jigeno
 
LR Parsing
Eelco Visser
 
Lexing and parsing
Elizabeth Smith
 
Compiler Design Basics
Akhil Kaushik
 
Module 11
bittudavis
 
Lexical analyzer
Ashwini Sonawane
 
Lexical Approach
Muhittin Yakut
 
Ad

Similar to Introduction (20)

PPT
Compiler1
Natish Kumar
 
PPTX
1 compiler outline
ASHOK KUMAR REDDY
 
PPT
Compiler Design in Computer Applications
Mohit422982
 
PDF
lec00-Introduction.pdf
wigewej294
 
PPT
Compiler design computer science engineering.ppt
khandareshobhit17
 
PPTX
CD U1-5.pptx
Himajanaidu2
 
PDF
Chapter#01 cc
abdulbaki3
 
PPT
Compier Design_Unit I.ppt
sivaganesh293
 
PPT
Compier Design_Unit I.ppt
sivaganesh293
 
PPTX
Introduction to Compilers | Phases & Structure
International Institute of Information Technology (I²IT)
 
PDF
Assignment1
Sunita Milind Dol
 
DOCX
Techniques & applications of Compiler
Preethi AKNR
 
PPTX
Ss ui lecture 2
Avinash Kapse
 
PPTX
1 cc
Jay Soni
 
PPT
Unit1.ppt
BerlinShaheema2
 
PPT
Phases of compiler
PANKAJKUMAR2519
 
DOCX
Compiler Design Material
Dr. C.V. Suresh Babu
 
PPTX
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
PPT
Compier Design_Unit I_SRM.ppt
Apoorv Diwan
 
PPTX
1._Introduction_.pptx
Anbarasan Radhakrishnan R
 
Compiler1
Natish Kumar
 
1 compiler outline
ASHOK KUMAR REDDY
 
Compiler Design in Computer Applications
Mohit422982
 
lec00-Introduction.pdf
wigewej294
 
Compiler design computer science engineering.ppt
khandareshobhit17
 
CD U1-5.pptx
Himajanaidu2
 
Chapter#01 cc
abdulbaki3
 
Compier Design_Unit I.ppt
sivaganesh293
 
Compier Design_Unit I.ppt
sivaganesh293
 
Introduction to Compilers | Phases & Structure
International Institute of Information Technology (I²IT)
 
Assignment1
Sunita Milind Dol
 
Techniques & applications of Compiler
Preethi AKNR
 
Ss ui lecture 2
Avinash Kapse
 
1 cc
Jay Soni
 
Unit1.ppt
BerlinShaheema2
 
Phases of compiler
PANKAJKUMAR2519
 
Compiler Design Material
Dr. C.V. Suresh Babu
 
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
Compier Design_Unit I_SRM.ppt
Apoorv Diwan
 
1._Introduction_.pptx
Anbarasan Radhakrishnan R
 

More from Royalzig Luxury Furniture (20)

PDF
Royalzig Unveils India’s First World-Class Luxury Furniture Experience Center...
Royalzig Luxury Furniture
 
PPTX
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
Royalzig Luxury Furniture
 
PPTX
French Style Slim Carving Bedroom Sets
Royalzig Luxury Furniture
 
PDF
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
Royalzig Luxury Furniture
 
PPTX
Luxury furniture manufacturer in india
Royalzig Luxury Furniture
 
PPTX
bone inlay hand carved luxury table
Royalzig Luxury Furniture
 
PPTX
Hand Carved Luxury & Designer Wooden arm chair
Royalzig Luxury Furniture
 
PPTX
beautiful hand carved dressing table
Royalzig Luxury Furniture
 
PPTX
Hand Carved Luxury & Designer Wooden Dining Table
Royalzig Luxury Furniture
 
PPTX
Hand Carved Luxury & Designer Wooden sofa set
Royalzig Luxury Furniture
 
PPTX
Royalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Luxury Furniture
 
PPTX
hand carved luxury wedding throne
Royalzig Luxury Furniture
 
PPTX
Hand carved Luxury wood divan
Royalzig Luxury Furniture
 
PPTX
Royalzig luxury furniture
Royalzig Luxury Furniture
 
PPTX
Royalzigppt 004
Royalzig Luxury Furniture
 
PPTX
Royalzig high end furniture
Royalzig Luxury Furniture
 
PPTX
Royalzigppt 002
Royalzig Luxury Furniture
 
PDF
Topdown parsing
Royalzig Luxury Furniture
 
PDF
Syntaxdirected
Royalzig Luxury Furniture
 
Royalzig Unveils India’s First World-Class Luxury Furniture Experience Center...
Royalzig Luxury Furniture
 
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
Royalzig Luxury Furniture
 
French Style Slim Carving Bedroom Sets
Royalzig Luxury Furniture
 
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
Royalzig Luxury Furniture
 
Luxury furniture manufacturer in india
Royalzig Luxury Furniture
 
bone inlay hand carved luxury table
Royalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden arm chair
Royalzig Luxury Furniture
 
beautiful hand carved dressing table
Royalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden Dining Table
Royalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden sofa set
Royalzig Luxury Furniture
 
Royalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Luxury Furniture
 
hand carved luxury wedding throne
Royalzig Luxury Furniture
 
Hand carved Luxury wood divan
Royalzig Luxury Furniture
 
Royalzig luxury furniture
Royalzig Luxury Furniture
 
Royalzigppt 004
Royalzig Luxury Furniture
 
Royalzig high end furniture
Royalzig Luxury Furniture
 
Royalzigppt 002
Royalzig Luxury Furniture
 
Topdown parsing
Royalzig Luxury Furniture
 

Recently uploaded (20)

PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Basics and rules of probability with real-life uses
ravatkaran694
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 

Introduction

  • 1. TCS 502 Compiler Designp g CS416 Compiler Design 1P K Singh
  • 2. Course Information Textbook: Alfred V Aho Ravi Sethi and Jeffrey D UllmanAlfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, “Compilers: Principles, Techniques, and Tools” Addison-Wesley, 1986.Addison Wesley, 1986. • Course Web Page: http://pksmmmec.googlepages.com CS416 Compiler Design 2P K Singh
  • 3. Preliminaries Required • Basic knowledge of programming languages. • Basic knowledge of FSA and CFGBasic knowledge of FSA and CFG. • Knowledge of a high programming language for the programming assignments. CS416 Compiler Design 3P K Singh
  • 4. Course Outline • Introduction to Compiling • Lexical Analysisy • Syntax Analysis – Context Free GrammarsContext Free Grammars – Top-Down Parsing, LL Parsing – Bottom-Up Parsing, LR Parsingp g g CS416 Compiler Design 4P K Singh
  • 5. Course Outline • Syntax-Directed Translation – Attribute Definitions – Evaluation of Attribute Definitions • Semantic Analysis, Type Checking • Run-Time Organization • Intermediate Code GenerationIntermediate Code Generation • Code Optimization P K Singh CS416 Compiler Design 5
  • 6. COMPILERS • A compiler is a program takes a program written in a source language and translates it into an equivalent program in a targetg g q p g g language. source program COMPILER target program ( ll i i ( ll h i l i error messages ( Normally a program written in a high-level programming language) ( Normally the equivalent program in machine code – relocatable object file) error messages CS416 Compiler Design 6P K Singh
  • 7. Major Parts of Compilers • There are two major parts of a compiler: Analysis and SynthesisSynthesis • In analysis phase an intermediate representation is• In analysis phase, an intermediate representation is created from the given source program. – Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts ofy y y y p this phase. • In synthesis phase, the equivalent target program is t d f thi i t di t t ticreated from this intermediate representation. – Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase. CS416 Compiler Design 7P K Singh
  • 8. Phases of A Compiler Lexical Analyzer Semantic Analyzer Syntax Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program Source Program • Each phase transforms the source program from one representationp p g p into another representation. • They communicate with error handlers• They communicate with error handlers. • They communicate with the symbol table. CS416 Compiler Design 8P K Singh
  • 9. Lexical Analyzer • Lexical Analyzer reads the source program character by character and returns the tokens of the source program. • A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, delimeters and so on), ) Ex: newval := oldval + 12 => tokens: newval identifier := assignment operator oldval identifier + add operator 12 a number • Puts information about identifiers into the symbol table.u s o a o abou de e s o e sy bo ab e • Regular expressions are used to describe tokens (lexical constructs). A (Deterministic) Finite State Automaton can be used in the CS416 Compiler Design 9 • A (Deterministic) Finite State Automaton can be used in the implementation of a lexical analyzer. P K Singh
  • 10. Syntax Analyzer • A Syntax Analyzer creates the syntactic structure (generally a parse tree) of the given program.p ) g p g • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. assgstmt identifier := expression • In a parse tree, all terminals are at leaves. newval expression + expression id tifi b p , • All inner nodes are non-terminals in a context free grammar. identifier number oldval 12 CS416 Compiler Design 10P K Singh
  • 11. Syntax Analyzer (CFG) • The syntax of a language is specified by a context free grammar (CFG).g ( ) • The rules in a CFG are mostly recursive. • A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. – If it satisfies, the syntax analyzer creates a parse tree for the given program. • Ex: We use BNF (Backus Naur Form) to specify a CFG assgstmt -> identifier := expression expression -> identifier expression -> number expression -> expression + expression CS416 Compiler Design 11P K Singh
  • 12. Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized by the lexical analyzer, and which ones by the syntax analyzer?y , y y y – Both of them do similar things; But the lexical analyzer deals with simple non- recursive constructs of the language. – The syntax analyzer deals with recursive constructs of the language. – The lexical analyzer simplifies the job of the syntax analyzer. – The lexical analyzer recognizes the smallest meaningful units (tokens) in a source program. – The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language. CS416 Compiler Design 12P K Singh
  • 13. Parsing Techniques • Depending on how the parse tree is created, there are different parsing techniques. These parsing techniques are categorized into two groups:• These parsing techniques are categorized into two groups: – Top-Down Parsing, – Bottom-Up Parsingp g • Top-Down Parsing: – Construction of the parse tree starts at the root, and proceeds towards the leaves. – Efficient top-down parsers can be easily constructed by hand.Efficient top down parsers can be easily constructed by hand. – Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing). • Bottom-Up Parsing: – Construction of the parse tree starts at the leaves and proceeds towards the rootConstruction of the parse tree starts at the leaves, and proceeds towards the root. – Normally efficient bottom-up parsers are created with the help of some software tools. – Bottom-up parsing is also known as shift-reduce parsing. CS416 Compiler Design 13 – Operator-Precedence Parsing – simple, restrictive, easy to implement – LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR P K Singh
  • 14. Semantic Analyzer • A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation.yp g • Type-checking is an important part of semantic analyzer. • Normally semantic information cannot be represented by a context-free language used in syntax analyzers. • Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules)with attributes (semantic rules) – the result is a syntax-directed translation, – Attribute grammars E• Ex: newval := oldval + 12 Th t f th id tifi l t t h ith t f th i ( ld l 12) CS416 Compiler Design 14 • The type of the identifier newval must match with type of the expression (oldval+12) P K Singh
  • 15. Intermediate Code Generation • A compiler may produce an explicit intermediate codes representing the source program.p g p g • These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the le el of machine codeslevel of machine codes. • Ex: newval := oldval * fact + 1 id1 := id2 * id3 + 1 MULT id2,id3,temp1 Intermediates Codes (Quadraples) ADD temp1,#1,temp2 MOV temp2,,id1 CS416 Compiler Design 15 p ,, P K Singh
  • 16. Code Optimizer (for Intermediate Code Generator) • The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space.g p • Ex: MULT id2,id3,temp1 ADD temp1,#1,id1ADD temp1,#1,id1 CS416 Compiler Design 16P K Singh
  • 17. Code Generator • Produces the target language in a specific architecture. • The target program is normally is a relocatable object fileThe target program is normally is a relocatable object file containing the machine codes. • Ex: ( assume that we have an architecture with instructions whose at least one of its operands isis a machine register) MOVE id2 R1MOVE id2,R1 MULT id3,R1 ADD #1,R1 MOVE R1 id1 CS416 Compiler Design 17 MOVE R1,id1 P K Singh