SlideShare a Scribd company logo
1 of 14
Download to read offline
16HB04071 HARISON FEKADU WARI
DEC 22, 2020
Operator Precedence Grammar
Steps 3 and 4
Operator
Precedence
Grammar
HARISON FEKADU
Brief
In this presentation I will discuss the third and
fourth steps of Operator Precedence Parsing
which are:
• Parsing the input string based on the
relation table constructed in the previous
step.
• Constructing a parse tree from the parse
table.
Fig. 1 Operator
Precedence Parsing
Steps
Operator
Precedence
Grammar
HARISON FEKADU
Parsing the Input String
In this step we’re going to make use of a buffer
to hold the input string, a stack to store tokens
from the input string as we parse them and a
handle as a temporary storage.
The parsing process comprises of the
following steps:
1. Inclose the input string with $.
2. Shift the first item in the buffer to the stack.
3. Compare and identify the OP relation of the
current token in the buffer with the top-
most terminal in the stack. If the relation
doesn’t exist in the precedence table, report
error and end.
4. Perform an action based on the OP relation.
5. Finish if the action performed is accept, else
go to step 3.
Operator
Precedence
Grammar
HARISON FEKADU
Operator Precendence Relations
There are three operator precedence relations.
• a<b: a has a lower priority than b.
• a>b: a has a higher priority than b.
• a=b: a and b have the same precedence.
It is important to note that we don’t compare
non-terminals. If a non-terminal exists then we
compare with the closest terminal if it exists.
id + * $
id > > >
+ < > < >
* < > > >
$ < < < Accept
Table 1 A Simple Precedence Table
Operator
Precedence
Grammar
HARISON FEKADU
Possible Actions
Assuming the current token in the buffer is a
and the top-most terminal in the stack is b we
can perform the following four actions:
• Shift: If a>b or a=b we push a into the
stack.
• Reduce: If a<b we pop the stack and pre-
pend the value to the handle until a>b. Then
we replace the handle with the appropriate
non-terminal and push this non-terminal to
the stack.
• Accpet: If the value of both a and b is $ we
announce the successful completion of
parsing.
• Error: If there is no relation between a and
b in the precedence table or the parser can’t
reach accept.
Operator
Precedence
Grammar
HARISON FEKADU
Example 1
Given the following grammar determine
whether the input string id*id-id is valid or
not using operator precedence parsing:
S → A-A
A → A*A | id
id - * $
id > > >
- < > < >
* < > > >
$ < < < Accept
Table 2 Precedence table for the input string
Operator
Precedence
Grammar
HARISON FEKADU
Example 1 - Solution
Stack Buffer Relation Action
$ id*id-id$ $<id Shift: id
$id *id-id$ id>* Reduce: A→id
$A *id-id$ $<* Shift: *
$A* id-id$ *<id Shift: id
$A*id -id$ id>- Reduce: A→id
$A*A -id$ *>- Reduce: A→A*A
$A -id$ $<- Shift: -
$A- id$ -<id Shift: id
$A-id $ id>$ Reduce: A→id
$A-A $ ->$ Reduce: S→A-A
$S $ Accept
Table 3 Parse table
Grammar
S → A-A
A → A*A | id
Operator
Precedence
Grammar
HARISON FEKADU
Constructing a Parse Tree
• We will construct the Parse Tree from the
bottom up, meaning that we will start from
the input string and try to arrive at the start
symbol.
• We will do this by focusing on the reduce
actions as shown in the table on the left,
which is a simplified version of the parse
table from Example 1.
Input Action
id*id-id Reduce: A→id
A*id-id Reduce: A→id
A*A-id Reduce: A→A*A
A-id Reduce: A→id
A-A Reduce: S→A-A
S Accept
Table 4 Isolated reduce actions from Table 3
Operator
Precedence
Grammar
HARISON FEKADU
Parse Tree for Example 1
id * id - id
A A
A A
S
Fig. 2 Parse tree for Example 1
Input Action
id*id-id Reduce: A→id
A*id-id Reduce: A→id
A*A-id Reduce: A→A*A
A-id Reduce: A→id
A-A Reduce: S→A-A
S Accept
Table 4 Isolated reduce actions from Table 3
Operator
Precedence
Grammar
HARISON FEKADU
Example 2
Given the following grammar determine
whether the input string (id+id)/id is valid
or not using operator precedence parsing:
S → SBA|(A)
A → (A+A)|id
B → /
id + / ( ) $
id x > > x > >
+ < > < < > >
/ < > > < > >
( < < < < = x
) x > > x > >
$ < < < < x Accept
Table 5 Precedence table for the input string
Operator Precedence Grammar
S → S/A|(A)
A → A+A|id
Operator
Precedence
Grammar
HARISON FEKADU
Example 2 - Solution
Stack Buffer Relation Action
$ (id+id)/id$ $<( Shift: (
$( id+id)/id$ (<id Shift: id
$(id +id)/id$ id>+ Reduce: A→id
$(A +id)/id$ (<+ Shift: +
$(A+ id)/id$ +<id Shift: id
$(A+id )/id$ id>) Reduce: A→id
$(A+A )/id$ +>) Reduce: A→A+A
$(A )/id$ (=) Shift: )
$(A) /id$ )>/ Reduce: S→(A)
$S /id$ $</ Shift: /
$S/ id$ /<id Shift: id
$S/id $ id>$ Reduce: A→id
$S/A $ />$ Reduce: S→S/A
$S $ Accept
Table 6 Parse table
Grammar
S → S/A|(A)
A → A+A|id
Operator
Precedence
Grammar
HARISON FEKADU
Parse Tree for Example 2
id + id ) /
A A
A
A
S
(
Fig. 3 Parse tree for Example 2
Input Action
(id+id)/id Reduce: A→id
(A+id)/id Reduce: A→id
(A+A)/id Reduce: A→A+A
(A)/id Reduce: S→(A)
S/id Reduce: A→id
S/A Reduce: S→S/A
S Accept
Table 7 Isolated reduce actions from Table 6
id
S
Operator
Precedence
Grammar
HARISON FEKADU
Online Operator Precedence Parser
Demo
16HB04071 HARISON FEKADU WARI
DEC 22, 2020
Thank You!

More Related Content

What's hot

Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IVManoj Patil
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
 
Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 

What's hot (20)

Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Back patching
Back patchingBack patching
Back patching
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Code generation
Code generationCode generation
Code generation
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
Parsing
ParsingParsing
Parsing
 

Similar to Operator Precedence Grammar

Similar to Operator Precedence Grammar (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
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Ch06
Ch06Ch06
Ch06
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Team 4
Team 4Team 4
Team 4
 
C Operators
C OperatorsC Operators
C Operators
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
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
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
stack
stackstack
stack
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Stack unit-ii
Stack unit-iiStack unit-ii
Stack unit-ii
 

Recently uploaded

Cytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxCytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxVarshiniMK
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.PraveenaKalaiselvan1
 
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptArshadWarsi13
 
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |aasikanpl
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
insect anatomy and insect body wall and their physiology
insect anatomy and insect body wall and their  physiologyinsect anatomy and insect body wall and their  physiology
insect anatomy and insect body wall and their physiologyDrAnita Sharma
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Masticationvidulajaib
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫qfactory1
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsssuserddc89b
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 

Recently uploaded (20)

Cytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxCytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptx
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
 
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort ServiceHot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
 
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Aiims Metro Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.ppt
 
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
insect anatomy and insect body wall and their physiology
insect anatomy and insect body wall and their  physiologyinsect anatomy and insect body wall and their  physiology
insect anatomy and insect body wall and their physiology
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Mastication
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physics
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 

Operator Precedence Grammar

  • 1. 16HB04071 HARISON FEKADU WARI DEC 22, 2020 Operator Precedence Grammar Steps 3 and 4
  • 2. Operator Precedence Grammar HARISON FEKADU Brief In this presentation I will discuss the third and fourth steps of Operator Precedence Parsing which are: • Parsing the input string based on the relation table constructed in the previous step. • Constructing a parse tree from the parse table. Fig. 1 Operator Precedence Parsing Steps
  • 3. Operator Precedence Grammar HARISON FEKADU Parsing the Input String In this step we’re going to make use of a buffer to hold the input string, a stack to store tokens from the input string as we parse them and a handle as a temporary storage. The parsing process comprises of the following steps: 1. Inclose the input string with $. 2. Shift the first item in the buffer to the stack. 3. Compare and identify the OP relation of the current token in the buffer with the top- most terminal in the stack. If the relation doesn’t exist in the precedence table, report error and end. 4. Perform an action based on the OP relation. 5. Finish if the action performed is accept, else go to step 3.
  • 4. Operator Precedence Grammar HARISON FEKADU Operator Precendence Relations There are three operator precedence relations. • a<b: a has a lower priority than b. • a>b: a has a higher priority than b. • a=b: a and b have the same precedence. It is important to note that we don’t compare non-terminals. If a non-terminal exists then we compare with the closest terminal if it exists. id + * $ id > > > + < > < > * < > > > $ < < < Accept Table 1 A Simple Precedence Table
  • 5. Operator Precedence Grammar HARISON FEKADU Possible Actions Assuming the current token in the buffer is a and the top-most terminal in the stack is b we can perform the following four actions: • Shift: If a>b or a=b we push a into the stack. • Reduce: If a<b we pop the stack and pre- pend the value to the handle until a>b. Then we replace the handle with the appropriate non-terminal and push this non-terminal to the stack. • Accpet: If the value of both a and b is $ we announce the successful completion of parsing. • Error: If there is no relation between a and b in the precedence table or the parser can’t reach accept.
  • 6. Operator Precedence Grammar HARISON FEKADU Example 1 Given the following grammar determine whether the input string id*id-id is valid or not using operator precedence parsing: S → A-A A → A*A | id id - * $ id > > > - < > < > * < > > > $ < < < Accept Table 2 Precedence table for the input string
  • 7. Operator Precedence Grammar HARISON FEKADU Example 1 - Solution Stack Buffer Relation Action $ id*id-id$ $<id Shift: id $id *id-id$ id>* Reduce: A→id $A *id-id$ $<* Shift: * $A* id-id$ *<id Shift: id $A*id -id$ id>- Reduce: A→id $A*A -id$ *>- Reduce: A→A*A $A -id$ $<- Shift: - $A- id$ -<id Shift: id $A-id $ id>$ Reduce: A→id $A-A $ ->$ Reduce: S→A-A $S $ Accept Table 3 Parse table Grammar S → A-A A → A*A | id
  • 8. Operator Precedence Grammar HARISON FEKADU Constructing a Parse Tree • We will construct the Parse Tree from the bottom up, meaning that we will start from the input string and try to arrive at the start symbol. • We will do this by focusing on the reduce actions as shown in the table on the left, which is a simplified version of the parse table from Example 1. Input Action id*id-id Reduce: A→id A*id-id Reduce: A→id A*A-id Reduce: A→A*A A-id Reduce: A→id A-A Reduce: S→A-A S Accept Table 4 Isolated reduce actions from Table 3
  • 9. Operator Precedence Grammar HARISON FEKADU Parse Tree for Example 1 id * id - id A A A A S Fig. 2 Parse tree for Example 1 Input Action id*id-id Reduce: A→id A*id-id Reduce: A→id A*A-id Reduce: A→A*A A-id Reduce: A→id A-A Reduce: S→A-A S Accept Table 4 Isolated reduce actions from Table 3
  • 10. Operator Precedence Grammar HARISON FEKADU Example 2 Given the following grammar determine whether the input string (id+id)/id is valid or not using operator precedence parsing: S → SBA|(A) A → (A+A)|id B → / id + / ( ) $ id x > > x > > + < > < < > > / < > > < > > ( < < < < = x ) x > > x > > $ < < < < x Accept Table 5 Precedence table for the input string Operator Precedence Grammar S → S/A|(A) A → A+A|id
  • 11. Operator Precedence Grammar HARISON FEKADU Example 2 - Solution Stack Buffer Relation Action $ (id+id)/id$ $<( Shift: ( $( id+id)/id$ (<id Shift: id $(id +id)/id$ id>+ Reduce: A→id $(A +id)/id$ (<+ Shift: + $(A+ id)/id$ +<id Shift: id $(A+id )/id$ id>) Reduce: A→id $(A+A )/id$ +>) Reduce: A→A+A $(A )/id$ (=) Shift: ) $(A) /id$ )>/ Reduce: S→(A) $S /id$ $</ Shift: / $S/ id$ /<id Shift: id $S/id $ id>$ Reduce: A→id $S/A $ />$ Reduce: S→S/A $S $ Accept Table 6 Parse table Grammar S → S/A|(A) A → A+A|id
  • 12. Operator Precedence Grammar HARISON FEKADU Parse Tree for Example 2 id + id ) / A A A A S ( Fig. 3 Parse tree for Example 2 Input Action (id+id)/id Reduce: A→id (A+id)/id Reduce: A→id (A+A)/id Reduce: A→A+A (A)/id Reduce: S→(A) S/id Reduce: A→id S/A Reduce: S→S/A S Accept Table 7 Isolated reduce actions from Table 6 id S
  • 14. 16HB04071 HARISON FEKADU WARI DEC 22, 2020 Thank You!