SlideShare ist ein Scribd-Unternehmen logo
1 von 13
WELCOME TO A
JOURNEY TO
CS419

Dr. Hussien Sharaf
Computer Science Department

dr.sharaf@from-masr.com
Dr. Hussien M. Sharaf

LR PARSING


LR(1) parsing is a technique of bottom-up parsing.







L: scan input string from left to right.
R: uses right most derivation.
1: uses one token ahead to decide which rule to use for
reduction.

Consists of:




Parser stack: that holds grammar symbols: non-terminals and
tokens.
Parsing table: that specifies the parser actions (Shift,
Reduce, Accept, Error).
Driver function: that interacts with parser stack, parsing table, and
scanner.
Output
Scanner

Next
token

Parser
driver

Parsing
table

Parsing stack
Dr. Hussien M. Sharaf

BOTTOM-UP PARSER ACTIONS







Shift: parser shifts the next token on the
parser stack.
Reduce: parser reduces the RHS of a
production to its LHS.
Accept: parsed successfully.
Error: failure.
Dr. Hussien M. Sharaf

BOTTOM-UP PARSING
A parser is a bottom-up if it traverse a parse
tree bottom up.
 A right most derivation is applied to each
step.
 Bottom-up parsers are preferred in practice.
 They do not need left factoring.
 It reduces the input string to the starting
symbol by using the reverse of production
rules.

Dr. Hussien M. Sharaf

BOTTOM-UP (CONT.)
Consider the following grammar:
E→T+E|T
T → id * T | id | (E)
; Input string: id + id * id


Input string
id * id + id
id * T + id
T + id
T+T
T+E
E

Rules
T → id
T → id * T
T → id
E→T
E→T+E

Is there another parsing for the same input string
using bottom up algorithm?
Dr. Hussien M. Sharaf

BOTTOM-UP (CONT.)
Input string
id * id + id
id * id + T
id * T + T
T+T
T+E
E




Rules
T → id
T → id
T → id * T
E→T
E→T+E

Is there another parsing for the same input
string using bottom up algorithm?
What happens if “T + T” was reduced to “E +
T”? Would the parsing still continue?
Dr. Hussien M. Sharaf

PARSING TREE
E
Input string
id * id + id
id * id + T
id * T + T
T+T
T+E
E

E

T
T

T

i
d

*

i
d

+

i
d
Dr. Hussien M. Sharaf

PARSING TREE
Consider the following grammar:
E→T+E|T
T → id * T | id | (E)
; Input string: id + id * id


But what if we start with reducing the first id to T as
follows:
Input string
id * id + id
T * id + id


Rules

T → id

We would be stuck with “T * id” or “T * T” or “T * E”
Dr. Hussien M. Sharaf

CONFUSING POINTS


How does the parser choose whether to shift
or reduce?
 General

strategy is to reduce whenever it is
possible otherwise shift.



How does the parser handles the situations
when there is more than one choice for
reducing?
 Even

non-ambigious grammars can cause this
situation. The parser uses back-tracking to
decide which choice is correct.
Dr. Hussien M. Sharaf

Example 1
Consider the following grammar….
A(A) | a

Stack

Input

Action

$

((a))$

Shift

$(

(a))$

Shift

$((

a))$

Shift

$((a

))$

Reduce A a

$((A

))$

Shift

$((A)

)$

Reduce A (A)

$(A

)$

Shift

$(A)

$

Reduce A (A)

$A

$

Accept
Dr. Hussien M. Sharaf

LR Parsing Example 2
Stack

Input

Action

$
$ id
$T
$E
$E+
$E+(
$ E + ( id
$E+(T
$E+(E
$E+(E+
$ E + ( E + id
$E+(E+T
$E+(E
$E+(E)
$E+T
$E

id + ( id + id ) $
+ ( id + id ) $
+ ( id + id ) $
+ ( id + id ) $
( id + id ) $
id + id ) $
+ id ) $
+ id ) $
+ id ) $
id ) $
)$
)$
)$
$
$
$

S
R, G3
R, G2
S
S
S
R, G3
R, G2
S
S
R, G3
R, G1
S
R, G4
R, G1
A

G1:
G2:
G3:
G4:

E
E
T
T






E+T
T
id
(E)
Dr. Hussien M. Sharaf

Example 3
Dr. Hussien M. Sharaf

THANK YOU

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Left factor put
Left factor putLeft factor put
Left factor put
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Module 11
Module 11Module 11
Module 11
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Parsing
ParsingParsing
Parsing
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 

Andere mochten auch

Andere mochten auch (17)

Parsing
ParsingParsing
Parsing
 
Parsing example
Parsing exampleParsing example
Parsing example
 
Ch5a
Ch5aCh5a
Ch5a
 
1. pendahuluan kompilasi
1. pendahuluan kompilasi1. pendahuluan kompilasi
1. pendahuluan kompilasi
 
Pigeonhole
PigeonholePigeonhole
Pigeonhole
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Pigeonhole Principle
Pigeonhole PrinciplePigeonhole Principle
Pigeonhole Principle
 
Introduction to Parse
Introduction to ParseIntroduction to Parse
Introduction to Parse
 
Pigeonhole Principle,Cardinality,Countability
Pigeonhole Principle,Cardinality,CountabilityPigeonhole Principle,Cardinality,Countability
Pigeonhole Principle,Cardinality,Countability
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR Parsing
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 

Ähnlich wie Cs419 lec11 bottom-up parsing

Top down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptxTop down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptxLaibaFaisal3
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
Similarity Search in High Dimensions via Hashing
Similarity Search in High Dimensions via HashingSimilarity Search in High Dimensions via Hashing
Similarity Search in High Dimensions via HashingMaruf Aytekin
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Quick and radix sort
Quick and radix sortQuick and radix sort
Quick and radix sortAaron Joaquin
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Bioinformatica 10-11-2011-t5-database searching
Bioinformatica 10-11-2011-t5-database searchingBioinformatica 10-11-2011-t5-database searching
Bioinformatica 10-11-2011-t5-database searchingProf. Wim Van Criekinge
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingzukun
 
Memorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsMemorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsPrincessNorberte
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptxskilljiolms
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingHaripritha
 

Ähnlich wie Cs419 lec11 bottom-up parsing (20)

Top down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptxTop down parsering and bottom up parsering.pptx
Top down parsering and bottom up parsering.pptx
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Similarity Search in High Dimensions via Hashing
Similarity Search in High Dimensions via HashingSimilarity Search in High Dimensions via Hashing
Similarity Search in High Dimensions via Hashing
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Quick and radix sort
Quick and radix sortQuick and radix sort
Quick and radix sort
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Bioinformatica 10-11-2011-t5-database searching
Bioinformatica 10-11-2011-t5-database searchingBioinformatica 10-11-2011-t5-database searching
Bioinformatica 10-11-2011-t5-database searching
 
TamingStatistics
TamingStatisticsTamingStatistics
TamingStatistics
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
 
Memorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsMemorization of Various Calculator shortcuts
Memorization of Various Calculator shortcuts
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
Data Structure
Data StructureData Structure
Data Structure
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
 
Es272 ch2
Es272 ch2Es272 ch2
Es272 ch2
 

Mehr von Arab Open University and Cairo University

Mehr von Arab Open University and Cairo University (20)

Infos2014
Infos2014Infos2014
Infos2014
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Lec4
Lec4Lec4
Lec4
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Cs419 lec5 lexical analysis using dfa
Cs419 lec5   lexical analysis using dfaCs419 lec5   lexical analysis using dfa
Cs419 lec5 lexical analysis using dfa
 
Cs419 lec4 lexical analysis using re
Cs419 lec4   lexical analysis using reCs419 lec4   lexical analysis using re
Cs419 lec4 lexical analysis using re
 
Cs419 lec3 lexical analysis using re
Cs419 lec3   lexical analysis using reCs419 lec3   lexical analysis using re
Cs419 lec3 lexical analysis using re
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 

Kürzlich hochgeladen

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
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 . pdfQucHHunhnh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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...christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Kürzlich hochgeladen (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Cs419 lec11 bottom-up parsing

  • 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. Dr. Hussien M. Sharaf LR PARSING  LR(1) parsing is a technique of bottom-up parsing.     L: scan input string from left to right. R: uses right most derivation. 1: uses one token ahead to decide which rule to use for reduction. Consists of:    Parser stack: that holds grammar symbols: non-terminals and tokens. Parsing table: that specifies the parser actions (Shift, Reduce, Accept, Error). Driver function: that interacts with parser stack, parsing table, and scanner. Output Scanner Next token Parser driver Parsing table Parsing stack
  • 3. Dr. Hussien M. Sharaf BOTTOM-UP PARSER ACTIONS     Shift: parser shifts the next token on the parser stack. Reduce: parser reduces the RHS of a production to its LHS. Accept: parsed successfully. Error: failure.
  • 4. Dr. Hussien M. Sharaf BOTTOM-UP PARSING A parser is a bottom-up if it traverse a parse tree bottom up.  A right most derivation is applied to each step.  Bottom-up parsers are preferred in practice.  They do not need left factoring.  It reduces the input string to the starting symbol by using the reverse of production rules. 
  • 5. Dr. Hussien M. Sharaf BOTTOM-UP (CONT.) Consider the following grammar: E→T+E|T T → id * T | id | (E) ; Input string: id + id * id  Input string id * id + id id * T + id T + id T+T T+E E Rules T → id T → id * T T → id E→T E→T+E Is there another parsing for the same input string using bottom up algorithm?
  • 6. Dr. Hussien M. Sharaf BOTTOM-UP (CONT.) Input string id * id + id id * id + T id * T + T T+T T+E E   Rules T → id T → id T → id * T E→T E→T+E Is there another parsing for the same input string using bottom up algorithm? What happens if “T + T” was reduced to “E + T”? Would the parsing still continue?
  • 7. Dr. Hussien M. Sharaf PARSING TREE E Input string id * id + id id * id + T id * T + T T+T T+E E E T T T i d * i d + i d
  • 8. Dr. Hussien M. Sharaf PARSING TREE Consider the following grammar: E→T+E|T T → id * T | id | (E) ; Input string: id + id * id  But what if we start with reducing the first id to T as follows: Input string id * id + id T * id + id  Rules T → id We would be stuck with “T * id” or “T * T” or “T * E”
  • 9. Dr. Hussien M. Sharaf CONFUSING POINTS  How does the parser choose whether to shift or reduce?  General strategy is to reduce whenever it is possible otherwise shift.  How does the parser handles the situations when there is more than one choice for reducing?  Even non-ambigious grammars can cause this situation. The parser uses back-tracking to decide which choice is correct.
  • 10. Dr. Hussien M. Sharaf Example 1 Consider the following grammar…. A(A) | a Stack Input Action $ ((a))$ Shift $( (a))$ Shift $(( a))$ Shift $((a ))$ Reduce A a $((A ))$ Shift $((A) )$ Reduce A (A) $(A )$ Shift $(A) $ Reduce A (A) $A $ Accept
  • 11. Dr. Hussien M. Sharaf LR Parsing Example 2 Stack Input Action $ $ id $T $E $E+ $E+( $ E + ( id $E+(T $E+(E $E+(E+ $ E + ( E + id $E+(E+T $E+(E $E+(E) $E+T $E id + ( id + id ) $ + ( id + id ) $ + ( id + id ) $ + ( id + id ) $ ( id + id ) $ id + id ) $ + id ) $ + id ) $ + id ) $ id ) $ )$ )$ )$ $ $ $ S R, G3 R, G2 S S S R, G3 R, G2 S S R, G3 R, G1 S R, G4 R, G1 A G1: G2: G3: G4: E E T T     E+T T id (E)
  • 12. Dr. Hussien M. Sharaf Example 3
  • 13. Dr. Hussien M. Sharaf THANK YOU