SlideShare a Scribd company logo
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

8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
venkatapranaykumarGa
 
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptxCOMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
Ranjeet Reddy
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
Akshaya Arunan
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
 
Parsing
ParsingParsing
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
arpitaeron555
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
Archana 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 Key
appasami
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Anujashejwal
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysisjigeno
 
Syntax analysis
Syntax analysisSyntax 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...
Bhavin Darji
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
Taha Shakeel
 
Syntax
SyntaxSyntax
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
Sadia Akter
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
akila 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.pdf
TANZINTANZINA
 

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

8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
 
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptxCOMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
 
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
 

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.pptx
gadisaAdamu
 
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
gadisaAdamu
 
Chapter 4 Software Project Planning.pptx
Chapter 4 Software Project Planning.pptxChapter 4 Software Project Planning.pptx
Chapter 4 Software Project Planning.pptx
gadisaAdamu
 
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
gadisaAdamu
 
Chapter 7 - Resource Monitoring & Management.ppt
Chapter 7 - Resource Monitoring & Management.pptChapter 7 - Resource Monitoring & Management.ppt
Chapter 7 - Resource Monitoring & Management.ppt
gadisaAdamu
 
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
gadisaAdamu
 
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
gadisaAdamu
 
Derartu Habtamu research Presentation.pptx
Derartu Habtamu  research Presentation.pptxDerartu Habtamu  research Presentation.pptx
Derartu Habtamu research Presentation.pptx
gadisaAdamu
 
Chapter five software Software Design.pptx
Chapter five software  Software Design.pptxChapter five software  Software Design.pptx
Chapter five software Software Design.pptx
gadisaAdamu
 
OSI open system interconnection LAYERS.pdf
OSI open system interconnection LAYERS.pdfOSI open system interconnection LAYERS.pdf
OSI open system interconnection LAYERS.pdf
gadisaAdamu
 
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
gadisaAdamu
 
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
gadisaAdamu
 
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
gadisaAdamu
 
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. pptx
gadisaAdamu
 
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
gadisaAdamu
 
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
gadisaAdamu
 
Chapter one-Introduction to Computer.pptx
Chapter one-Introduction to Computer.pptxChapter one-Introduction to Computer.pptx
Chapter one-Introduction to Computer.pptx
gadisaAdamu
 
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
gadisaAdamu
 
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
gadisaAdamu
 

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

20 slides of research movie and artists .pdf
20 slides of research movie and artists .pdf20 slides of research movie and artists .pdf
20 slides of research movie and artists .pdf
ameli25062005
 
Design Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinkingDesign Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinking
cy0krjxt
 
White wonder, Work developed by Eva Tschopp
White wonder, Work developed by Eva TschoppWhite wonder, Work developed by Eva Tschopp
White wonder, Work developed by Eva Tschopp
Mansi Shah
 
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
jyz59f4j
 
PORTFOLIO FABIANA VILLANI ARCHITECTURE.pdf
PORTFOLIO FABIANA VILLANI ARCHITECTURE.pdfPORTFOLIO FABIANA VILLANI ARCHITECTURE.pdf
PORTFOLIO FABIANA VILLANI ARCHITECTURE.pdf
fabianavillanib
 
Book Formatting: Quality Control Checks for Designers
Book Formatting: Quality Control Checks for DesignersBook Formatting: Quality Control Checks for Designers
Book Formatting: Quality Control Checks for Designers
Confidence Ago
 
Borys Sutkowski portfolio interior design
Borys Sutkowski portfolio interior designBorys Sutkowski portfolio interior design
Borys Sutkowski portfolio interior design
boryssutkowski
 
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
taqyed
 
一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理
一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理
一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理
9a93xvy
 
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
708pb191
 
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...
Mansi Shah
 
Design Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinkingDesign Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinking
cy0krjxt
 
Can AI do good? at 'offtheCanvas' India HCI prelude
Can AI do good? at 'offtheCanvas' India HCI preludeCan AI do good? at 'offtheCanvas' India HCI prelude
Can AI do good? at 'offtheCanvas' India HCI prelude
Alan Dix
 
Exploring the Future of Smart Garages.pdf
Exploring the Future of Smart Garages.pdfExploring the Future of Smart Garages.pdf
Exploring the Future of Smart Garages.pdf
fastfixgaragedoor
 
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
h7j5io0
 
一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理
一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理
一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理
9a93xvy
 
Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...
Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...
Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...
ameli25062005
 
一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理
一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理
一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理
h7j5io0
 
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
7sd8fier
 
Common Designing Mistakes and How to avoid them
Common Designing Mistakes and How to avoid themCommon Designing Mistakes and How to avoid them
Common Designing Mistakes and How to avoid them
madhavlakhanpal29
 

Recently uploaded (20)

20 slides of research movie and artists .pdf
20 slides of research movie and artists .pdf20 slides of research movie and artists .pdf
20 slides of research movie and artists .pdf
 
Design Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinkingDesign Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinking
 
White wonder, Work developed by Eva Tschopp
White wonder, Work developed by Eva TschoppWhite wonder, Work developed by Eva Tschopp
White wonder, Work developed by Eva Tschopp
 
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
 
PORTFOLIO FABIANA VILLANI ARCHITECTURE.pdf
PORTFOLIO FABIANA VILLANI ARCHITECTURE.pdfPORTFOLIO FABIANA VILLANI ARCHITECTURE.pdf
PORTFOLIO FABIANA VILLANI ARCHITECTURE.pdf
 
Book Formatting: Quality Control Checks for Designers
Book Formatting: Quality Control Checks for DesignersBook Formatting: Quality Control Checks for Designers
Book Formatting: Quality Control Checks for Designers
 
Borys Sutkowski portfolio interior design
Borys Sutkowski portfolio interior designBorys Sutkowski portfolio interior design
Borys Sutkowski portfolio interior design
 
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
 
一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理
一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理
一比一原版(RHUL毕业证书)伦敦大学皇家霍洛威学院毕业证如何办理
 
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
 
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...
 
Design Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinkingDesign Thinking Design thinking Design thinking
Design Thinking Design thinking Design thinking
 
Can AI do good? at 'offtheCanvas' India HCI prelude
Can AI do good? at 'offtheCanvas' India HCI preludeCan AI do good? at 'offtheCanvas' India HCI prelude
Can AI do good? at 'offtheCanvas' India HCI prelude
 
Exploring the Future of Smart Garages.pdf
Exploring the Future of Smart Garages.pdfExploring the Future of Smart Garages.pdf
Exploring the Future of Smart Garages.pdf
 
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
 
一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理
一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理
一比一原版(CITY毕业证书)谢菲尔德哈勒姆大学毕业证如何办理
 
Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...
Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...
Коричневый и Кремовый Деликатный Органический Копирайтер Фрилансер Марке...
 
一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理
一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理
一比一原版(BU毕业证书)伯恩茅斯大学毕业证成绩单如何办理
 
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
 
Common Designing Mistakes and How to avoid them
Common Designing Mistakes and How to avoid themCommon Designing Mistakes and How to avoid them
Common Designing Mistakes and How to avoid them
 

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