SlideShare a Scribd company logo
Operator Precedence Parsing
Bottom-up Parsing
In general the bottom-up parsing is also
known as shift-reduce parsing.
Types of bottom-up parsing:
1. Operator-Precedence parsing
2. LR parsing
Operator Precedence Parser
An operator precedence parser is a bottom-up
parser that interprets an operator grammar. This
parser is only used operator grammars. Ambiguous
grammars are not allowed in any parser except
operator precedence parser.
Operator precedence grammar is kinds of shift
reduce parsing method. It is applied to a small class
of operator grammars.
Operator Precedence Grammar
A grammar is said to be operator precedence
grammar if it has two properties:
No R.H.S. of any production has a ∈.
No two non-terminals are adjacent.
Operator precedence can only established between
the terminals of the grammar. It ignores the non-
terminal.
Operator Precedence Grammar
Example-1
E→AB E→EOE E→E+E |
A→a E→id E*E |
B→b O→+|*|/ E/E | id
not operator
grammar
not operator
grammar
operator
grammar
Operator Precedence Grammar
Example-2
S→SAS/a S→SbSbS/a/SbS
A→bSb/b
Replace non terminal A and
convert it into operator
grammar
not operator
grammar operator grammar
Operator Precedence Relations:
There are the three operator precedence
relations:-
a ⋗ b means that terminal "a" has the higher
precedence than terminal "b".
a ⋖ b means that terminal "a" has the lower
precedence than terminal "b".
a ≐ b means that the terminal "a" and "b" both
have same precedence.
Operator Precedence parsing Rule:
How to assign these relation ???
Rule-01:
If precedence of a is higher than precedence of b, a>b
If precedence of b is same as precedence of a, a = b
If precedence of a is lower than precedence of b, a <b
Rule-02:
An identifier(id) is always given the higher precedence than
any other symbol.
$ symbol is always given the lowest precedence.
Rule-03:
If two operators have the same precedence, then we consider
the rule of associativity.
Building the operator precedence parsing
table:
Example:-consider the following grammar
E-> E+E | E*E | id
For this grammar the precedence table looks
something like this:
The table size is n*n where n is the number of
terminal symbols in the grammar
c
id + * $
id - .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
acc
Operator-Precedence Parsing Algorithm
The input string is w$, the initial stack is $ and a table holds precedence
relations between certain terminals
Algorithm:
set p to point to the first symbol of w$ ;
repeat forever
if ( $ is on top of the stack and p points to $ ) then
return
else {
let a be the topmost terminal symbol on the stack and let b be the symbol pointed to
by p;
/* SHIFT */if ( a <. b or a =· b ) then {
push b onto the stack;
advance p to the next input symbol;
}
else if ( a .> b ) then /* REDUCE */
repeat pop stack
until ( the top of stack terminal is related by <. to the terminal most recently
popped );
else error();
}
Operator-Precedence Parsing –Example
Problem-01: Consider the following grammar-
E → EAE | id
A → + | x
Construct the operator precedence parser and parse the string id + id
x id.
Solution-
 We convert the given grammar into operator precedence grammar.
The equivalent operator precedence grammar is-
E → E + E | E x E | id
The terminal symbols in the grammar are
{ id, + , x , $ }
We construct the operator precedence
table as-
c
id + * $
id - .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
acc
Operator-Precedence Parsing Algorithm --
Example
Given Grammar
E → E + E | E x E | id
c
id + * $
id - .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
acc
stack
$
input
id+id*id$
action
$ <. id shift
$id +id*id$ id .> + reduce E  id
$ +id*id$ shift
$+ id*id$ shift
$+id *id$ id .> * reduce E  id
$+ *id$ shift
$+* id$ shift
$+*id
$+*
$
$
id .> $ reduce E  id
* .> $ reduce E  E*E
$+ $ + .> $ reduce E  E+E
$ $ accept
parse the string id + id x id$
Advantages and Disadvantages
Advantages:
– It is simple to implement operator precedence parsing.
– powerful enough for expressions in programming
languages
Disadvantages:
– It cannot handle the unary minus .
– It is applicable only to a small class of grammars.
Precedence Functions
The storage space however can be efficiently managed if
we can build an efficient data structure to represent
adequately the precedence relationships between the
terminals.
For this we can construct two functions f and g where the
following hold:
1. If a. >b then f(a)>g(b)
2. If a< . b then f(a)<g(b)
3. If a. =b then f(a)=g(b)
Constructing Precedence Functions
To construct these functions we create a directed graph
with nodes fi and gi where i is the ith terminal, obeying the
following rules:
 If a. =b then fa and fb are merged, and ga and gb are
merged
 If a. >b then a directed edge is introduced from fa to gb
 If a< . b then a directed edge is introduced to fa from gb
Following these rules, the precedence graph for the above
mentioned precedence table looks something like this:
Example: consider the following table c
id + * $
id - .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
acc
Constructing Precedence Functions
Using the algorithm leads to the following graph:
c
id + * $
id - .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
acc
f*
f$
fid
f+
gid
g+
g*
g$
Constructing Precedence Functions
From which we extract the following precedence functions:

More Related Content

What's hot

Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 
b+ tree
b+ treeb+ tree
b+ tree
bitistu
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithm
AYESHA JAVED
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
Dr Sandeep Kumar Poonia
 
Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AI
Amey Kerkar
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
I. Alpha-Beta Pruning in ai
I. Alpha-Beta Pruning in aiI. Alpha-Beta Pruning in ai
I. Alpha-Beta Pruning in ai
vikas dhakane
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
Iffat Anjum
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
shah zeb
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
HarisonFekadu
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
 
Hill climbing
Hill climbingHill climbing
Hill climbing
Mohammad Faizan
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
FamiDan
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithm
Hema Kashyap
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
Narayana Galla
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
shashidharPapishetty
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
Rajkumar R
 

What's hot (20)

Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
b+ tree
b+ treeb+ tree
b+ tree
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithm
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AI
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
I. Alpha-Beta Pruning in ai
I. Alpha-Beta Pruning in aiI. Alpha-Beta Pruning in ai
I. Alpha-Beta Pruning in ai
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Hill climbing
Hill climbingHill climbing
Hill climbing
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithm
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 

Similar to Operator precedance parsing

9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
venkatapranaykumarGa
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
BBDITM LUCKNOW
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica
"FENG "GEORGE"" YU
 
Ch06
Ch06Ch06
Ch06
Hankyo
 
Classification of Compiler
Classification of CompilerClassification of Compiler
Classification of Compiler
maharajdey
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx
venkatapranaykumarGa
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
Assignment9
Assignment9Assignment9
Assignment9
Sunita Milind Dol
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
Royalzig Luxury Furniture
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
Emmanuel college
 
Syntax Analysis.pptx
Syntax Analysis.pptxSyntax Analysis.pptx
Syntax Analysis.pptx
AshaS74
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...
ganeshjaggineni1927
 
2. operator
2. operator2. operator
2. operator
Shankar Gangaju
 

Similar to Operator precedance parsing (20)

9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica
 
Ch06
Ch06Ch06
Ch06
 
Classification of Compiler
Classification of CompilerClassification of Compiler
Classification of Compiler
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
Assignment9
Assignment9Assignment9
Assignment9
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Syntax Analysis.pptx
Syntax Analysis.pptxSyntax Analysis.pptx
Syntax Analysis.pptx
 
Java 8
Java 8Java 8
Java 8
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...
 
2. operator
2. operator2. operator
2. operator
 

Recently uploaded

IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 

Recently uploaded (20)

IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 

Operator precedance parsing

  • 2. Bottom-up Parsing In general the bottom-up parsing is also known as shift-reduce parsing. Types of bottom-up parsing: 1. Operator-Precedence parsing 2. LR parsing
  • 3. Operator Precedence Parser An operator precedence parser is a bottom-up parser that interprets an operator grammar. This parser is only used operator grammars. Ambiguous grammars are not allowed in any parser except operator precedence parser. Operator precedence grammar is kinds of shift reduce parsing method. It is applied to a small class of operator grammars.
  • 4. Operator Precedence Grammar A grammar is said to be operator precedence grammar if it has two properties: No R.H.S. of any production has a ∈. No two non-terminals are adjacent. Operator precedence can only established between the terminals of the grammar. It ignores the non- terminal.
  • 5. Operator Precedence Grammar Example-1 E→AB E→EOE E→E+E | A→a E→id E*E | B→b O→+|*|/ E/E | id not operator grammar not operator grammar operator grammar
  • 6. Operator Precedence Grammar Example-2 S→SAS/a S→SbSbS/a/SbS A→bSb/b Replace non terminal A and convert it into operator grammar not operator grammar operator grammar
  • 7. Operator Precedence Relations: There are the three operator precedence relations:- a ⋗ b means that terminal "a" has the higher precedence than terminal "b". a ⋖ b means that terminal "a" has the lower precedence than terminal "b". a ≐ b means that the terminal "a" and "b" both have same precedence.
  • 8. Operator Precedence parsing Rule: How to assign these relation ??? Rule-01: If precedence of a is higher than precedence of b, a>b If precedence of b is same as precedence of a, a = b If precedence of a is lower than precedence of b, a <b Rule-02: An identifier(id) is always given the higher precedence than any other symbol. $ symbol is always given the lowest precedence. Rule-03: If two operators have the same precedence, then we consider the rule of associativity.
  • 9. Building the operator precedence parsing table: Example:-consider the following grammar E-> E+E | E*E | id For this grammar the precedence table looks something like this: The table size is n*n where n is the number of terminal symbols in the grammar c id + * $ id - .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <. acc
  • 10. Operator-Precedence Parsing Algorithm The input string is w$, the initial stack is $ and a table holds precedence relations between certain terminals Algorithm: set p to point to the first symbol of w$ ; repeat forever if ( $ is on top of the stack and p points to $ ) then return else { let a be the topmost terminal symbol on the stack and let b be the symbol pointed to by p; /* SHIFT */if ( a <. b or a =· b ) then { push b onto the stack; advance p to the next input symbol; } else if ( a .> b ) then /* REDUCE */ repeat pop stack until ( the top of stack terminal is related by <. to the terminal most recently popped ); else error(); }
  • 11. Operator-Precedence Parsing –Example Problem-01: Consider the following grammar- E → EAE | id A → + | x Construct the operator precedence parser and parse the string id + id x id. Solution-  We convert the given grammar into operator precedence grammar. The equivalent operator precedence grammar is- E → E + E | E x E | id The terminal symbols in the grammar are { id, + , x , $ } We construct the operator precedence table as- c id + * $ id - .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <. acc
  • 12. Operator-Precedence Parsing Algorithm -- Example Given Grammar E → E + E | E x E | id c id + * $ id - .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <. acc stack $ input id+id*id$ action $ <. id shift $id +id*id$ id .> + reduce E  id $ +id*id$ shift $+ id*id$ shift $+id *id$ id .> * reduce E  id $+ *id$ shift $+* id$ shift $+*id $+* $ $ id .> $ reduce E  id * .> $ reduce E  E*E $+ $ + .> $ reduce E  E+E $ $ accept parse the string id + id x id$
  • 13. Advantages and Disadvantages Advantages: – It is simple to implement operator precedence parsing. – powerful enough for expressions in programming languages Disadvantages: – It cannot handle the unary minus . – It is applicable only to a small class of grammars.
  • 14. Precedence Functions The storage space however can be efficiently managed if we can build an efficient data structure to represent adequately the precedence relationships between the terminals. For this we can construct two functions f and g where the following hold: 1. If a. >b then f(a)>g(b) 2. If a< . b then f(a)<g(b) 3. If a. =b then f(a)=g(b)
  • 15. Constructing Precedence Functions To construct these functions we create a directed graph with nodes fi and gi where i is the ith terminal, obeying the following rules:  If a. =b then fa and fb are merged, and ga and gb are merged  If a. >b then a directed edge is introduced from fa to gb  If a< . b then a directed edge is introduced to fa from gb Following these rules, the precedence graph for the above mentioned precedence table looks something like this: Example: consider the following table c id + * $ id - .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <. acc
  • 16. Constructing Precedence Functions Using the algorithm leads to the following graph: c id + * $ id - .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <. acc f* f$ fid f+ gid g+ g* g$
  • 17. Constructing Precedence Functions From which we extract the following precedence functions: