SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
Operator-
Precedence
Parser
 Operator grammar
 small, but an important class of grammars
 we may have an efficient operator precedence
parser (a shift-reduce parser) for an operator
grammar.
 In an operator grammar, no production rule can have:
  at the right side
 two adjacent non-terminals at the right side.
 Ex:
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
Precedence Relations
 In operator-precedence parsing, we define
three disjoint precedence relations between
certain pairs of terminals.
a <. b b has higher precedence than a
a =· b b has same precedence as a
a .> b b has lower precedence than a
 The determination of correct precedence
relations between terminals are based on the
traditional notions of associativity and
precedence of operators. (Unary minus causes a
problem).
Using Operator-Precedence
Relations
 The intention of the precedence relations is
to find the handle of a right-sentential form,
<. with marking the left end,
=· appearing in the interior of the handle,
and
.> marking the right hand.
 In our input string $a1a2...an$, we insert the
precedence relation between the pairs of
terminals
Using Operator -
Precedence Relations
E  E+E | E-E | E*E | E/E | E^E | (E) | -E | id
The partial operator-precedence
table for this grammar
 Then the input string (id + id*id) with the
precedence relations inserted will be:
$ <. id .> + <. id .> * <. id .> $
id + * $
id .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
To Find The Handles
1. Scan the string from left end until the first .> is
encountered.
2. Then scan backwards (to the left) over any =· until a <. is
encountered.
3. The handle contains everything to left of the first .> and to
the right of the <. is encountered.
$ <. id .> + <. id .> * <. id .> $ E  id $ id + id * id $
$ <. + <. id .> * <. id .> $ E  id $ E + id * id $
$ <. + <. * <. id .> $ E  id $ E + E * id $
$ <. + <. * .> $ E  E*E $ E + E * .E $
$ <. + .> $ E  E+E $ E + E $
$ $ $ E $
Operator-Precedence Parsing
Algorithm
The input string is w$, the initial stack is $ and a table holds precedence
relations between certain terminals
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;
if ( a <. b or a =· b ) then { /* SHIFT */
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
Algorithm -- Example
stack input action
$ id+id*id$ $ <. id shift
$id +id*id$ id .> + reduceE  id
$ +id*id$ shift
$+ id*id$ shift
$+id *id$ id .> * reduceE  id
$+ *id$ shift
$+* id$ shift
$+*id $id .> $ reduceE  id
$+* $* .> $ reduceE  E*E
$+ $+ .> $ reduceE  E+E
$ $ accept
id + * $
id .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <.
How to Create Operator-
Precedence Relations
 We use associativity and precedence relations among operators.
1. If operator 1 has higher precedence than operator  2,
  1
.>  2 and  2 <.  1
2. If operator  1 and operator  2 have equal precedence,
they are left-associative   1
.>  2 and  2
.>  1
they are right-associative   1 <.  2 and  2 <.  1
3. For all operators ,  <. id, id .> ,  <. (, (<. ,  .> ), ) .> ,  .> $, and
$ <. 
4. Also, let
(=·) $ <. ( id .> ) ) .> $
( <. ( $ <. id id .> $ ) .> )
Operator-Precedence
Relations
+ - * / ^ id ( ) $
+ .> .> <. <. <. <. <. .> .>
- .> .> <. <. <. <. <. .> .>
* .> .> .> .> <. <. <. .> .>
/ .> .> .> .> <. <. <. .> .>
^ .> .> .> .> <. <. <. .> .>
id .> .> .> .> .> .> .>
( <. <. <. <. <. <. <. =·
) .> .> .> .> .> .> .>
$ <. <. <. <. <. <. <.
Handling Unary Minus
 Operator-Precedence parsing cannot handle the unary minus
when we also have the binary minus in our grammar.
 The best approach to solve this problem, let the lexical analyzer
handle this problem.
 The lexical analyzer will return two different tokens for the unary minus
and the binary minus.
 The lexical analyzer will need a lookhead to distinguish the binary
minus from the unary minus.
 Then, we make
 <. unary-minus for any operator
unary-minus .>  if unary-minus has higher precedence than 
unary-minus <.  if unary-minus has lower (or equal)
precedence than 
Precedence Functions
 Compilers using operator precedence parsers do not
need to store the table of precedence relations.
 The table can be encoded by two precedence
functions f and g that map terminal symbols to integers.
 For symbols a and b.
f(a) < g(b) whenever a <. b
f(a) = g(b) whenever a =· b
f(a) > g(b) whenever a .> b
Constructing precedence functions
Method:
1. Create symbols fa and gb for each a that is a terminal or $.
2. Partition the created symbols into as many groups as possible,
in such a way that if a =. b, then fa and gb are in the same group.
3. Create a directed graph whose nodes are the groups found in (2).
For any a and b, if a <.b , place an edge from the group of gb to
the group of fa. Of a .> b, place an edge from the group of fa to
that of gb.
4. If the graph constructed has a cycle, then no precedence
functions exist. If there are no cycle, let f(a) be the length of the
longest path beginning at the group of fa; let g(a) be the length
of the longest path beginning at the group of ga.
Example
f*
f$
fid
f+
gid
g+
g*
g$
+ * Id $
f 2 4 4 0
g 1 3 5 0
Disadvantages of Operator
Precedence Parsing
 Disadvantages:
 It cannot handle the unary minus (the lexical
analyzer should handle the unary minus).
 Small class of grammars.
 Difficult to decide which language is
recognized by the grammar.
 Advantages:
 simple
 powerful enough for expressions in
programming languages
Error Recovery in Operator-
Precedence Parsing
Error Cases:
1. No relation holds between the terminal on
the top of stack and the next input symbol.
2. A handle is found (reduction step), but
there is no production with this handle as a
right side
Error Recovery:
1. Each empty entry is filled with a pointer to
an error routine.
2. Decides the popped handle “looks like”
which right hand side. And tries to recover
from that situation.
Handling Shift/Reduce
Errors
When consulting the precedence matrix to decide whether to
shift or reduce, we may find that no relation holds between the
top stack and the first input symbol.
To recover, we must modify (insert/change)
1. Stack or
2. Input or
3. Both.
We must be careful that we don’t get into an infinite loop.
Example
id ( ) $
id e3 e3 .> .>
( <. <. =. e4
) e3 e3 .> .>
$ <. <. e2 e1
e1: Called when : whole expression is missing
insert id onto the input
issue diagnostic: ‘missing operand’
e2: Called when : expression begins with a right parenthesis
delete ) from the input
issue diagnostic: ‘unbalanced right parenthesis’
Example
id ( ) $
id e3 e3 .> .>
( <. <. =. e4
) e3 e3 .> .>
$ <. <. e2 e1
e3: Called when : id or ) is followed by id or (
insert + onto the input
issue diagnostic: ‘missing operator’
e4: Called when : expression ends with a left parenthesis
pop ( from the stack
issue diagnostic: ‘missing right parenthesis’

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 

Ähnlich wie Operator precedence

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
 
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
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 

Ähnlich wie Operator precedence (20)

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
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Assignment7
Assignment7Assignment7
Assignment7
 
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
 
Ch06
Ch06Ch06
Ch06
 
Introduction to Python Language and Data Types
Introduction to Python Language and Data TypesIntroduction to Python Language and Data Types
Introduction to Python Language and Data Types
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
 
Curvefitting
CurvefittingCurvefitting
Curvefitting
 
ComplexMastersExample
ComplexMastersExampleComplexMastersExample
ComplexMastersExample
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 

Mehr von Akshaya Arunan

OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security Using
Akshaya Arunan
 

Mehr von Akshaya Arunan (9)

Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDN
 
Akshayappt
AkshayapptAkshayappt
Akshayappt
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security Using
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 

Kürzlich hochgeladen

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Kürzlich hochgeladen (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 

Operator precedence

  • 2.  Operator grammar  small, but an important class of grammars  we may have an efficient operator precedence parser (a shift-reduce parser) for an operator grammar.  In an operator grammar, no production rule can have:   at the right side  two adjacent non-terminals at the right side.  Ex: 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
  • 3. Precedence Relations  In operator-precedence parsing, we define three disjoint precedence relations between certain pairs of terminals. a <. b b has higher precedence than a a =· b b has same precedence as a a .> b b has lower precedence than a  The determination of correct precedence relations between terminals are based on the traditional notions of associativity and precedence of operators. (Unary minus causes a problem).
  • 4. Using Operator-Precedence Relations  The intention of the precedence relations is to find the handle of a right-sentential form, <. with marking the left end, =· appearing in the interior of the handle, and .> marking the right hand.  In our input string $a1a2...an$, we insert the precedence relation between the pairs of terminals
  • 5. Using Operator - Precedence Relations E  E+E | E-E | E*E | E/E | E^E | (E) | -E | id The partial operator-precedence table for this grammar  Then the input string (id + id*id) with the precedence relations inserted will be: $ <. id .> + <. id .> * <. id .> $ id + * $ id .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <.
  • 6. To Find The Handles 1. Scan the string from left end until the first .> is encountered. 2. Then scan backwards (to the left) over any =· until a <. is encountered. 3. The handle contains everything to left of the first .> and to the right of the <. is encountered. $ <. id .> + <. id .> * <. id .> $ E  id $ id + id * id $ $ <. + <. id .> * <. id .> $ E  id $ E + id * id $ $ <. + <. * <. id .> $ E  id $ E + E * id $ $ <. + <. * .> $ E  E*E $ E + E * .E $ $ <. + .> $ E  E+E $ E + E $ $ $ $ E $
  • 7. Operator-Precedence Parsing Algorithm The input string is w$, the initial stack is $ and a table holds precedence relations between certain terminals 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; if ( a <. b or a =· b ) then { /* SHIFT */ 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(); }
  • 8. Operator-Precedence Parsing Algorithm -- Example stack input action $ id+id*id$ $ <. id shift $id +id*id$ id .> + reduceE  id $ +id*id$ shift $+ id*id$ shift $+id *id$ id .> * reduceE  id $+ *id$ shift $+* id$ shift $+*id $id .> $ reduceE  id $+* $* .> $ reduceE  E*E $+ $+ .> $ reduceE  E+E $ $ accept id + * $ id .> .> .> + <. .> <. .> * <. .> .> .> $ <. <. <.
  • 9. How to Create Operator- Precedence Relations  We use associativity and precedence relations among operators. 1. If operator 1 has higher precedence than operator  2,   1 .>  2 and  2 <.  1 2. If operator  1 and operator  2 have equal precedence, they are left-associative   1 .>  2 and  2 .>  1 they are right-associative   1 <.  2 and  2 <.  1 3. For all operators ,  <. id, id .> ,  <. (, (<. ,  .> ), ) .> ,  .> $, and $ <.  4. Also, let (=·) $ <. ( id .> ) ) .> $ ( <. ( $ <. id id .> $ ) .> )
  • 10. Operator-Precedence Relations + - * / ^ id ( ) $ + .> .> <. <. <. <. <. .> .> - .> .> <. <. <. <. <. .> .> * .> .> .> .> <. <. <. .> .> / .> .> .> .> <. <. <. .> .> ^ .> .> .> .> <. <. <. .> .> id .> .> .> .> .> .> .> ( <. <. <. <. <. <. <. =· ) .> .> .> .> .> .> .> $ <. <. <. <. <. <. <.
  • 11. Handling Unary Minus  Operator-Precedence parsing cannot handle the unary minus when we also have the binary minus in our grammar.  The best approach to solve this problem, let the lexical analyzer handle this problem.  The lexical analyzer will return two different tokens for the unary minus and the binary minus.  The lexical analyzer will need a lookhead to distinguish the binary minus from the unary minus.  Then, we make  <. unary-minus for any operator unary-minus .>  if unary-minus has higher precedence than  unary-minus <.  if unary-minus has lower (or equal) precedence than 
  • 12. Precedence Functions  Compilers using operator precedence parsers do not need to store the table of precedence relations.  The table can be encoded by two precedence functions f and g that map terminal symbols to integers.  For symbols a and b. f(a) < g(b) whenever a <. b f(a) = g(b) whenever a =· b f(a) > g(b) whenever a .> b
  • 13. Constructing precedence functions Method: 1. Create symbols fa and gb for each a that is a terminal or $. 2. Partition the created symbols into as many groups as possible, in such a way that if a =. b, then fa and gb are in the same group. 3. Create a directed graph whose nodes are the groups found in (2). For any a and b, if a <.b , place an edge from the group of gb to the group of fa. Of a .> b, place an edge from the group of fa to that of gb. 4. If the graph constructed has a cycle, then no precedence functions exist. If there are no cycle, let f(a) be the length of the longest path beginning at the group of fa; let g(a) be the length of the longest path beginning at the group of ga.
  • 15. Disadvantages of Operator Precedence Parsing  Disadvantages:  It cannot handle the unary minus (the lexical analyzer should handle the unary minus).  Small class of grammars.  Difficult to decide which language is recognized by the grammar.  Advantages:  simple  powerful enough for expressions in programming languages
  • 16. Error Recovery in Operator- Precedence Parsing Error Cases: 1. No relation holds between the terminal on the top of stack and the next input symbol. 2. A handle is found (reduction step), but there is no production with this handle as a right side Error Recovery: 1. Each empty entry is filled with a pointer to an error routine. 2. Decides the popped handle “looks like” which right hand side. And tries to recover from that situation.
  • 17. Handling Shift/Reduce Errors When consulting the precedence matrix to decide whether to shift or reduce, we may find that no relation holds between the top stack and the first input symbol. To recover, we must modify (insert/change) 1. Stack or 2. Input or 3. Both. We must be careful that we don’t get into an infinite loop.
  • 18. Example id ( ) $ id e3 e3 .> .> ( <. <. =. e4 ) e3 e3 .> .> $ <. <. e2 e1 e1: Called when : whole expression is missing insert id onto the input issue diagnostic: ‘missing operand’ e2: Called when : expression begins with a right parenthesis delete ) from the input issue diagnostic: ‘unbalanced right parenthesis’
  • 19. Example id ( ) $ id e3 e3 .> .> ( <. <. =. e4 ) e3 e3 .> .> $ <. <. e2 e1 e3: Called when : id or ) is followed by id or ( insert + onto the input issue diagnostic: ‘missing operator’ e4: Called when : expression ends with a left parenthesis pop ( from the stack issue diagnostic: ‘missing right parenthesis’