SlideShare a Scribd company logo
1 of 21
Chapter – three
Syntax analysis
Top Down Parsing
And
Bottom Up Parsing
1
Outline
 Introduction
 Context free grammar (CFG)
 Derivation
 Parse tree
 Ambiguity
 Top-down parsing
 Bottom up parsing
2
Introduction
 Syntax: the way in which tokens are put together to
form expressions, statements, or blocks of statements.
 The rules governing the formation of statements in a
programming language.
 Syntax analysis: the task concerned with fitting a
sequence of tokens into a specified syntax.
 Parsing: To break a sentence down into its component
parts with an explanation of the form, function, and
syntactical relationship of each part.
 The syntax of a programming language is usually given
by the grammar rules of a context free grammar (CFG).
3
4
Parser
lexical
analyzer
Syntax
analyzer
symbol
table
get next
token
Source
Program
get next
char
next char next token
(Contains a record
for each identifier)
Lexical
Error
Syntax
Error
Parse tree
Introduction…
 The syntax analyzer (parser) checks whether a given
source program satisfies the rules implied by a CFG
or not.
 If it satisfies, the parser creates the parse tree of that
program.
 Otherwise, the parser gives the error messages.
 A CFG:
 gives a precise syntactic specification of a
programming language.
 A grammar can be directly converted in to a parser by
some tools (yacc).
5
Introduction…
 The parser can be categorized into two groups:
 Top-down parser
 The parse tree is created top to bottom, starting from
the root to leaves.
 Bottom-up parser
 The parse tree is created bottom to top, starting from
the leaves to root.
 Both top-down and bottom-up parser scan the input
from left to right (one symbol at a time).
 Efficient top-down and bottom-up parsers can be
implemented by making use of context-free-
grammar.
 LL for top-down parsing
 LR for bottom-up parsing
6
Context free grammar (CFG)
 A context-free grammar is a specification for the
syntactic structure of a programming language.
 Context-free grammar has 4-tuples:
G = (T, N, P, S) where
 T is a finite set of terminals (a set of tokens)
 N is a finite set of non-terminals (syntactic variables)
 P is a finite set of productions of the form
A → α where A is non-terminal and
α is a strings of terminals and non-terminals (including the
empty string)
 S ∈ N is a designated start symbol (one of the non-
terminal symbols)
7
Example: grammar for simple arithmetic
expressions
expression  expression + term
expression  expression - term
expression  term
term  term * factor
term  term / factor
term  factor
factor  (expression )
factor  id
8
Terminal symbols
id + - * / ( )
Non-terminals
expression
term
Factor
Start symbol
expression
Derivation
 A derivation is a sequence of replacements of structure names
by choices on the right hand sides of grammar rules.
 Example: E → E + E | E – E | E * E | E / E | -E
E → ( E )
E → id
E => E + E means that E + E is derived from E
- we can replace E by E + E
- we have to have a production rule E → E+E in our grammar.
E=>E+E =>id+E=>id+id means that a sequence of replacements of
non-terminal symbols is called a derivation of id+id from E.
9
Derivation…
 If we always choose the left-most non-terminal in each
derivation step, this derivation is called left-most derivation.
Example: E=>-E=>-(E)=>-(E+E)=>-(id+E)=>-(id+id)
 If we always choose the right-most non-terminal in each
derivation step, this derivation is called right-most
derivation.
Example: E=>-E=>-(E)=>-(E+E)=>-(E+id)=>-(id+id)
 We will see that the top-down parser try to find the left-most
derivation of the given source program.
 We will see that the bottom-up parser try to find right-most
derivation of the given source program in the reverse order.
10
Parse tree
 A parse tree is a graphical representation of a
derivation.
 It filters out the order in which productions are applied
to replace non-terminals.
 A parse tree corresponding to a derivation is a labeled
tree in which:
• the interior nodes are labeled by non-terminals,
• the leaf nodes are labeled by terminals, and
• the children of each internal node represent the
replacement of the associated non-terminal in one
step of the derivation.
11
12
Parse tree and Derivation
E  E + E | E  E | ( E ) | - E | id
Lets examine this derivation:
E  -E  -(E)  -(E + E)  -(id + id)
E E
E
-
E
E
-
E
( )
E
E
-
E
( )
+
E E
E
E
-
E
( )
+
E E
id id
This is a top-down derivation
because we start building the
parse tree at the top parse tree
Grammar
13
Which parse tree is correct?
Ambiguity: example
Construct parse tree for the expression: id + id  id
E E
+
E E
E
+
E E

E E
E
+
E E

E E
id id
id
E E

E E
E

E E
+
E E
E

E E
+
E E
id id
id
E  E + E | E  E | ( E ) | - E | id
14
According to the grammar, both are correct.
Ambiguity: example…
Find a derivation for the expression: id + id  id
E
+
E E

E E
id id
id
E
*
E E
+
E E
id id
id
A grammar that produces more than one
parse tree for any input sentence is said
to be an ambiguous grammar.
E  E + E | E  E | ( E ) | - E | id
Syntax analysis
 Every language has rules that prescribe the syntactic
structure of well formed programs.
 The syntax can be described using Context Free
Grammars (CFG) notation.
 The use of CFGs has several advantages:
 helps in identifying ambiguities
 it is possible to have a tool which produces automatically
a parser using the grammar
 a properly designed grammar helps in modifying the
parser easily when the language changes
15
Top-down parsing
Recursive Descent Parsing (RDP)
 This method of top-down parsing can be considered as
an attempt to find the left most derivation for an input
string. It may involve backtracking.
 To construct the parse tree using RDP:
 we create one node tree consisting of S.
 two pointers, one for the tree and one for the input, will
be used to indicate where the parsing process is.
 initially, they will be on S and the first input symbol,
respectively.
 then we use the first S-production to expand the tree.
The tree pointer will be positioned on the left most
symbol of the newly created sub-tree.
16
Bottom up Parsing
 Top-down parsers:
• Starts constructing the parse tree at the
top (root) of the tree and move down
towards the leaves.
• Easy to implement by hand, but work
with restricted grammars.
 example: predictive parsers
18
Bottom-Up and Top-Down
Parsers
Top-down parsers:
• Starts constructing the parse tree at the top (root) of the
tree and move down towards the leaves.
• Easy to implement by hand, but work with restricted
grammars.
example: predictive parsers
Bottom-up parsers:
• build the nodes on the bottom of the parse tree first.
• Suitable for automatic parser generation, handle a larger
class of grammars.
examples: shift-reduce parser (or LR(k) parsers)
19
Bottom-Up Parser
A bottom-up parser, or a shift-reduce parser, begins
at the leaves and works up to the top of the tree.
The reduction steps trace a rightmost derivation
on reverse.
S  aABe
A  Abc | b
B  d
Consider the Grammar:
We want to parse the input string abbcde.
This parser is known as an LR Parser because
it scans the input from Left to right, and it constructs
a Rightmost derivation in reverse order.
Bottom-up parser (LR parsing)
S  aABe
A  Abc | b
B  d
abbcde  aAbcde  aAde  aABe  S
 At each step, we have to find α such that α is a
substring of the sentence and replace α by A, where
A  α
20
21
12/24/2023
.

More Related Content

Similar to Chapter-3 compiler.pptx course materials

Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfarpitaeron555
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysisjigeno
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnfTaha Shakeel
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designSadia Akter
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questionsakila viji
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)bolovv
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxssuser039bf6
 

Similar to Chapter-3 compiler.pptx course materials (20)

Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 
Parsing
ParsingParsing
Parsing
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
Syntax
SyntaxSyntax
Syntax
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptx
 

More from gadisaAdamu

Chapter 1. Introduction to Software Engineering.pptx
Chapter 1. Introduction to Software Engineering.pptxChapter 1. Introduction to Software Engineering.pptx
Chapter 1. Introduction to Software Engineering.pptxgadisaAdamu
 
Chapter_2_Software_Development_Life_Cycle_and_Process_Models.pptx
Chapter_2_Software_Development_Life_Cycle_and_Process_Models.pptxChapter_2_Software_Development_Life_Cycle_and_Process_Models.pptx
Chapter_2_Software_Development_Life_Cycle_and_Process_Models.pptxgadisaAdamu
 
Chapter 4 Software Project Planning.pptx
Chapter 4 Software Project Planning.pptxChapter 4 Software Project Planning.pptx
Chapter 4 Software Project Planning.pptxgadisaAdamu
 
Chapter 5 Software Design of software engineering.pptx
Chapter 5 Software Design of software engineering.pptxChapter 5 Software Design of software engineering.pptx
Chapter 5 Software Design of software engineering.pptxgadisaAdamu
 
Chapter 7 - Resource Monitoring & Management.ppt
Chapter 7 - Resource Monitoring & Management.pptChapter 7 - Resource Monitoring & Management.ppt
Chapter 7 - Resource Monitoring & Management.pptgadisaAdamu
 
Chapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptx
Chapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptxChapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptx
Chapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptxgadisaAdamu
 
Chapter 8 - nsa Introduction to Linux.ppt
Chapter 8 -  nsa Introduction to Linux.pptChapter 8 -  nsa Introduction to Linux.ppt
Chapter 8 - nsa Introduction to Linux.pptgadisaAdamu
 
Derartu Habtamu research Presentation.pptx
Derartu Habtamu  research Presentation.pptxDerartu Habtamu  research Presentation.pptx
Derartu Habtamu research Presentation.pptxgadisaAdamu
 
Chapter five software Software Design.pptx
Chapter five software  Software Design.pptxChapter five software  Software Design.pptx
Chapter five software Software Design.pptxgadisaAdamu
 
OSI open system interconnection LAYERS.pdf
OSI open system interconnection LAYERS.pdfOSI open system interconnection LAYERS.pdf
OSI open system interconnection LAYERS.pdfgadisaAdamu
 
computer network and chapter 7 OSI layers.pptx
computer network and chapter 7 OSI layers.pptxcomputer network and chapter 7 OSI layers.pptx
computer network and chapter 7 OSI layers.pptxgadisaAdamu
 
IP Address in data communication and computer notework.ppt
IP Address in data communication and computer notework.pptIP Address in data communication and computer notework.ppt
IP Address in data communication and computer notework.pptgadisaAdamu
 
ERROR DETECTION data communication and computer network.pptx
ERROR DETECTION data communication and computer network.pptxERROR DETECTION data communication and computer network.pptx
ERROR DETECTION data communication and computer network.pptxgadisaAdamu
 
Ch1Intro.pdf Computer organization and org.
Ch1Intro.pdf Computer organization and org.Ch1Intro.pdf Computer organization and org.
Ch1Intro.pdf Computer organization and org.gadisaAdamu
 
Computer application-chapter four lecture note. pptx
Computer application-chapter four lecture note. pptxComputer application-chapter four lecture note. pptx
Computer application-chapter four lecture note. pptxgadisaAdamu
 
Computer application lecture note-Chapter-5.pptx
Computer application lecture note-Chapter-5.pptxComputer application lecture note-Chapter-5.pptx
Computer application lecture note-Chapter-5.pptxgadisaAdamu
 
Chapter 03-Number System-Computer Application.pptx
Chapter 03-Number System-Computer Application.pptxChapter 03-Number System-Computer Application.pptx
Chapter 03-Number System-Computer Application.pptxgadisaAdamu
 
Chapter one-Introduction to Computer.pptx
Chapter one-Introduction to Computer.pptxChapter one-Introduction to Computer.pptx
Chapter one-Introduction to Computer.pptxgadisaAdamu
 
Chapter Three, four, five and six.ppt ITEtx
Chapter Three, four, five and six.ppt ITEtxChapter Three, four, five and six.ppt ITEtx
Chapter Three, four, five and six.ppt ITEtxgadisaAdamu
 
Chapter One & Two.pptx introduction to emerging Technology
Chapter One & Two.pptx introduction to emerging TechnologyChapter One & Two.pptx introduction to emerging Technology
Chapter One & Two.pptx introduction to emerging TechnologygadisaAdamu
 

More from gadisaAdamu (20)

Chapter 1. Introduction to Software Engineering.pptx
Chapter 1. Introduction to Software Engineering.pptxChapter 1. Introduction to Software Engineering.pptx
Chapter 1. Introduction to Software Engineering.pptx
 
Chapter_2_Software_Development_Life_Cycle_and_Process_Models.pptx
Chapter_2_Software_Development_Life_Cycle_and_Process_Models.pptxChapter_2_Software_Development_Life_Cycle_and_Process_Models.pptx
Chapter_2_Software_Development_Life_Cycle_and_Process_Models.pptx
 
Chapter 4 Software Project Planning.pptx
Chapter 4 Software Project Planning.pptxChapter 4 Software Project Planning.pptx
Chapter 4 Software Project Planning.pptx
 
Chapter 5 Software Design of software engineering.pptx
Chapter 5 Software Design of software engineering.pptxChapter 5 Software Design of software engineering.pptx
Chapter 5 Software Design of software engineering.pptx
 
Chapter 7 - Resource Monitoring & Management.ppt
Chapter 7 - Resource Monitoring & Management.pptChapter 7 - Resource Monitoring & Management.ppt
Chapter 7 - Resource Monitoring & Management.ppt
 
Chapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptx
Chapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptxChapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptx
Chapter_2_Network_Operating_System_NOS_and_Windows_Network_Concepts.pptx
 
Chapter 8 - nsa Introduction to Linux.ppt
Chapter 8 -  nsa Introduction to Linux.pptChapter 8 -  nsa Introduction to Linux.ppt
Chapter 8 - nsa Introduction to Linux.ppt
 
Derartu Habtamu research Presentation.pptx
Derartu Habtamu  research Presentation.pptxDerartu Habtamu  research Presentation.pptx
Derartu Habtamu research Presentation.pptx
 
Chapter five software Software Design.pptx
Chapter five software  Software Design.pptxChapter five software  Software Design.pptx
Chapter five software Software Design.pptx
 
OSI open system interconnection LAYERS.pdf
OSI open system interconnection LAYERS.pdfOSI open system interconnection LAYERS.pdf
OSI open system interconnection LAYERS.pdf
 
computer network and chapter 7 OSI layers.pptx
computer network and chapter 7 OSI layers.pptxcomputer network and chapter 7 OSI layers.pptx
computer network and chapter 7 OSI layers.pptx
 
IP Address in data communication and computer notework.ppt
IP Address in data communication and computer notework.pptIP Address in data communication and computer notework.ppt
IP Address in data communication and computer notework.ppt
 
ERROR DETECTION data communication and computer network.pptx
ERROR DETECTION data communication and computer network.pptxERROR DETECTION data communication and computer network.pptx
ERROR DETECTION data communication and computer network.pptx
 
Ch1Intro.pdf Computer organization and org.
Ch1Intro.pdf Computer organization and org.Ch1Intro.pdf Computer organization and org.
Ch1Intro.pdf Computer organization and org.
 
Computer application-chapter four lecture note. pptx
Computer application-chapter four lecture note. pptxComputer application-chapter four lecture note. pptx
Computer application-chapter four lecture note. pptx
 
Computer application lecture note-Chapter-5.pptx
Computer application lecture note-Chapter-5.pptxComputer application lecture note-Chapter-5.pptx
Computer application lecture note-Chapter-5.pptx
 
Chapter 03-Number System-Computer Application.pptx
Chapter 03-Number System-Computer Application.pptxChapter 03-Number System-Computer Application.pptx
Chapter 03-Number System-Computer Application.pptx
 
Chapter one-Introduction to Computer.pptx
Chapter one-Introduction to Computer.pptxChapter one-Introduction to Computer.pptx
Chapter one-Introduction to Computer.pptx
 
Chapter Three, four, five and six.ppt ITEtx
Chapter Three, four, five and six.ppt ITEtxChapter Three, four, five and six.ppt ITEtx
Chapter Three, four, five and six.ppt ITEtx
 
Chapter One & Two.pptx introduction to emerging Technology
Chapter One & Two.pptx introduction to emerging TechnologyChapter One & Two.pptx introduction to emerging Technology
Chapter One & Two.pptx introduction to emerging Technology
 

Recently uploaded

Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`dajasot375
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)jennyeacort
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Yantram Animation Studio Corporation
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...babafaisel
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130Suhani Kapoor
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...Amil baba
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CANestorGamez6
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentationamedia6
 
Kieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF PortfolioKieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF Portfolioktksalaria
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Narsimha murthy
 
ARt app | UX Case Study
ARt app | UX Case StudyARt app | UX Case Study
ARt app | UX Case StudySophia Viganò
 
shot list for my tv series two steps back
shot list for my tv series two steps backshot list for my tv series two steps back
shot list for my tv series two steps back17lcow074
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Roomdivyansh0kumar0
 

Recently uploaded (20)

Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentation
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
Kieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF PortfolioKieran Salaria Graphic Design PDF Portfolio
Kieran Salaria Graphic Design PDF Portfolio
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
 
ARt app | UX Case Study
ARt app | UX Case StudyARt app | UX Case Study
ARt app | UX Case Study
 
shot list for my tv series two steps back
shot list for my tv series two steps backshot list for my tv series two steps back
shot list for my tv series two steps back
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
 

Chapter-3 compiler.pptx course materials

  • 1. Chapter – three Syntax analysis Top Down Parsing And Bottom Up Parsing 1
  • 2. Outline  Introduction  Context free grammar (CFG)  Derivation  Parse tree  Ambiguity  Top-down parsing  Bottom up parsing 2
  • 3. Introduction  Syntax: the way in which tokens are put together to form expressions, statements, or blocks of statements.  The rules governing the formation of statements in a programming language.  Syntax analysis: the task concerned with fitting a sequence of tokens into a specified syntax.  Parsing: To break a sentence down into its component parts with an explanation of the form, function, and syntactical relationship of each part.  The syntax of a programming language is usually given by the grammar rules of a context free grammar (CFG). 3
  • 4. 4 Parser lexical analyzer Syntax analyzer symbol table get next token Source Program get next char next char next token (Contains a record for each identifier) Lexical Error Syntax Error Parse tree
  • 5. Introduction…  The syntax analyzer (parser) checks whether a given source program satisfies the rules implied by a CFG or not.  If it satisfies, the parser creates the parse tree of that program.  Otherwise, the parser gives the error messages.  A CFG:  gives a precise syntactic specification of a programming language.  A grammar can be directly converted in to a parser by some tools (yacc). 5
  • 6. Introduction…  The parser can be categorized into two groups:  Top-down parser  The parse tree is created top to bottom, starting from the root to leaves.  Bottom-up parser  The parse tree is created bottom to top, starting from the leaves to root.  Both top-down and bottom-up parser scan the input from left to right (one symbol at a time).  Efficient top-down and bottom-up parsers can be implemented by making use of context-free- grammar.  LL for top-down parsing  LR for bottom-up parsing 6
  • 7. Context free grammar (CFG)  A context-free grammar is a specification for the syntactic structure of a programming language.  Context-free grammar has 4-tuples: G = (T, N, P, S) where  T is a finite set of terminals (a set of tokens)  N is a finite set of non-terminals (syntactic variables)  P is a finite set of productions of the form A → α where A is non-terminal and α is a strings of terminals and non-terminals (including the empty string)  S ∈ N is a designated start symbol (one of the non- terminal symbols) 7
  • 8. Example: grammar for simple arithmetic expressions expression  expression + term expression  expression - term expression  term term  term * factor term  term / factor term  factor factor  (expression ) factor  id 8 Terminal symbols id + - * / ( ) Non-terminals expression term Factor Start symbol expression
  • 9. Derivation  A derivation is a sequence of replacements of structure names by choices on the right hand sides of grammar rules.  Example: E → E + E | E – E | E * E | E / E | -E E → ( E ) E → id E => E + E means that E + E is derived from E - we can replace E by E + E - we have to have a production rule E → E+E in our grammar. E=>E+E =>id+E=>id+id means that a sequence of replacements of non-terminal symbols is called a derivation of id+id from E. 9
  • 10. Derivation…  If we always choose the left-most non-terminal in each derivation step, this derivation is called left-most derivation. Example: E=>-E=>-(E)=>-(E+E)=>-(id+E)=>-(id+id)  If we always choose the right-most non-terminal in each derivation step, this derivation is called right-most derivation. Example: E=>-E=>-(E)=>-(E+E)=>-(E+id)=>-(id+id)  We will see that the top-down parser try to find the left-most derivation of the given source program.  We will see that the bottom-up parser try to find right-most derivation of the given source program in the reverse order. 10
  • 11. Parse tree  A parse tree is a graphical representation of a derivation.  It filters out the order in which productions are applied to replace non-terminals.  A parse tree corresponding to a derivation is a labeled tree in which: • the interior nodes are labeled by non-terminals, • the leaf nodes are labeled by terminals, and • the children of each internal node represent the replacement of the associated non-terminal in one step of the derivation. 11
  • 12. 12 Parse tree and Derivation E  E + E | E  E | ( E ) | - E | id Lets examine this derivation: E  -E  -(E)  -(E + E)  -(id + id) E E E - E E - E ( ) E E - E ( ) + E E E E - E ( ) + E E id id This is a top-down derivation because we start building the parse tree at the top parse tree Grammar
  • 13. 13 Which parse tree is correct? Ambiguity: example Construct parse tree for the expression: id + id  id E E + E E E + E E  E E E + E E  E E id id id E E  E E E  E E + E E E  E E + E E id id id E  E + E | E  E | ( E ) | - E | id
  • 14. 14 According to the grammar, both are correct. Ambiguity: example… Find a derivation for the expression: id + id  id E + E E  E E id id id E * E E + E E id id id A grammar that produces more than one parse tree for any input sentence is said to be an ambiguous grammar. E  E + E | E  E | ( E ) | - E | id
  • 15. Syntax analysis  Every language has rules that prescribe the syntactic structure of well formed programs.  The syntax can be described using Context Free Grammars (CFG) notation.  The use of CFGs has several advantages:  helps in identifying ambiguities  it is possible to have a tool which produces automatically a parser using the grammar  a properly designed grammar helps in modifying the parser easily when the language changes 15
  • 16. Top-down parsing Recursive Descent Parsing (RDP)  This method of top-down parsing can be considered as an attempt to find the left most derivation for an input string. It may involve backtracking.  To construct the parse tree using RDP:  we create one node tree consisting of S.  two pointers, one for the tree and one for the input, will be used to indicate where the parsing process is.  initially, they will be on S and the first input symbol, respectively.  then we use the first S-production to expand the tree. The tree pointer will be positioned on the left most symbol of the newly created sub-tree. 16
  • 17. Bottom up Parsing  Top-down parsers: • Starts constructing the parse tree at the top (root) of the tree and move down towards the leaves. • Easy to implement by hand, but work with restricted grammars.  example: predictive parsers
  • 18. 18 Bottom-Up and Top-Down Parsers Top-down parsers: • Starts constructing the parse tree at the top (root) of the tree and move down towards the leaves. • Easy to implement by hand, but work with restricted grammars. example: predictive parsers Bottom-up parsers: • build the nodes on the bottom of the parse tree first. • Suitable for automatic parser generation, handle a larger class of grammars. examples: shift-reduce parser (or LR(k) parsers)
  • 19. 19 Bottom-Up Parser A bottom-up parser, or a shift-reduce parser, begins at the leaves and works up to the top of the tree. The reduction steps trace a rightmost derivation on reverse. S  aABe A  Abc | b B  d Consider the Grammar: We want to parse the input string abbcde. This parser is known as an LR Parser because it scans the input from Left to right, and it constructs a Rightmost derivation in reverse order.
  • 20. Bottom-up parser (LR parsing) S  aABe A  Abc | b B  d abbcde  aAbcde  aAde  aABe  S  At each step, we have to find α such that α is a substring of the sentence and replace α by A, where A  α 20