SlideShare uma empresa Scribd logo
1 de 15
Lecture 6
Parsing trees and Ambiguity
Dr. Hussien Sharaf
Computer Science Department
dr.sharaf@from-masr.com
Parsing
CFG grammar is about categorizing
the statements of a language.
Parsing using CFG means
categorizing a certain statements
into categories defined in the CFG.
Parsing can be expressed using a
special type of graph called Trees
where no cycles exist.
A parse tree is the graph
representation of a derivation.
Dr. Hussien M. Sharaf 2
Parse tree
Dr. Hussien M. Sharaf 3
(1)A vertex with a label which is a
Non-terminal symbol is a parse tree.
(2) If A → y1 y2 … yn is a rule in R,
then the tree
A
y1 y2 yn. . .
is a parse tree.
Ambiguity
A grammar can generate the same
string in different ways.
Ambiguity occurs when a string has
two or more leftmost derivations for
the same CFG.
There are ways to eliminate
ambiguity such as using Chomsky
Normal Form (CNF) which does n’t
use Λ.
Λ cause ambiguity.
Dr. Hussien M. Sharaf 4
Ex 1
 i.e. {Λ, ab, abbaabba,… }
 S → aSa| bSb| Λ Derive abaaba
Dr. Hussien M. Sharaf 5
S
S
Λ
aa
S bb
S aa
Example2
Deduce CFG of a addition and parse
the following expression 2+3+5
1] S→S+S|N
2] N→1|2|3|4|5|6|7|8|9|0
Dr. Hussien M. Sharaf 6
S
S+N
S
+
N
5
S
+
3
N
2
N
Can u make
another parsing
tree ?
Example3
Deduce CFG of a
addition/multiplication and parse the
following expression 2+3*5
1] S→S+S|S*S|N
2] N→1|2|3|4|5|6|7|8|9|0| N
Dr. Hussien M. Sharaf 7
S
S*S
S
*
N
5
S
+
3
N
2
N
Can u make
another parsing
tree ?
Ex4 CFG without ambiguity
 Deduce CFG of a addition/multiplication
and parse the following expression 2*3+5
1] S→ Term|Term + S
2] Term → N|N * Term
3] N→1|2|3|4|5|6|7|8|9|0
Dr. Hussien M. Sharaf 8
S
S+N
S
+
N
5
S
*
3
N
2
N
Can u make
another parsing
tree ?
Example 5
Dr. Hussien M. Sharaf 9
S A | A B
A Λ| a | A b | A A
B b | b c | B c | b B
Sample derivations:
S AB AAB
aAB aaB aabB aabb
S AB AbB Abb AAbb
Aabb aabb
S
A B
AA Bb
a a b
S
BA
b
A
b
AA
a a
Example 6
Dr. Hussien M. Sharaf 10
S A | A B
A Λ | a | A b | A A
B b | b c | B c | b B
S
A B
AA b
a
a
bA
S
A
A A
AA bA
a
a
bA
S
A B
AA Bb
a a b
Dr. Hussien M. Sharaf 11
Eliminate ambiguity.
Eliminate “useless” variables.
Eliminate Λ-productions: A Λ.
Eliminate unit productions: A B.
Eliminate redundant productions.
Avoid left recursion by trading left-
and right-recursion.
Dr. Hussien M. Sharaf 12
PDA
 A Pushdown Automata (PDA ) is an NFA
equipped with a single stack.
Dr. Hussien M. Sharaf 13
1
4
3
2
PDA as a deterministic machine
Its inputs are
1. The current state (of its “NFA”),
2. The current input symbol (or
Λ), and
3. The current symbol on top of its
stack.
Dr. Hussien M. Sharaf 14
1
4
3
2
Its actions are:
1. Change state.
2. Change stack if needed.
Dr. Hussien M. Sharaf 15

Mais conteúdo relacionado

Mais procurados

Probability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and CombinationsProbability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and Combinations
Kate Nowak
 
Volume of Prisms
Volume of PrismsVolume of Prisms
Volume of Prisms
bujols
 
Permutation and Combination Maths
Permutation and Combination MathsPermutation and Combination Maths
Permutation and Combination Maths
Vardhan Jain
 

Mais procurados (20)

Probability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and CombinationsProbability Day 3 - Permutations and Combinations
Probability Day 3 - Permutations and Combinations
 
Permutation & Combination
Permutation & CombinationPermutation & Combination
Permutation & Combination
 
Volume of Prisms
Volume of PrismsVolume of Prisms
Volume of Prisms
 
4.1 Inverse Functions
4.1 Inverse Functions4.1 Inverse Functions
4.1 Inverse Functions
 
Discrete mathematics presentation
Discrete mathematics presentationDiscrete mathematics presentation
Discrete mathematics presentation
 
4.3 Logarithmic Functions
4.3 Logarithmic Functions4.3 Logarithmic Functions
4.3 Logarithmic Functions
 
Logarithm
 Logarithm Logarithm
Logarithm
 
Sets
SetsSets
Sets
 
2.8A Function Operations
2.8A Function Operations2.8A Function Operations
2.8A Function Operations
 
Factorising
FactorisingFactorising
Factorising
 
Permutation and Combination Maths
Permutation and Combination MathsPermutation and Combination Maths
Permutation and Combination Maths
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Combinations and permutations(1)
Combinations and permutations(1)Combinations and permutations(1)
Combinations and permutations(1)
 
Functions by mstfdemirdag
Functions by mstfdemirdagFunctions by mstfdemirdag
Functions by mstfdemirdag
 
Deriving the composition of functions
Deriving the composition of functionsDeriving the composition of functions
Deriving the composition of functions
 
Lecture2 B
Lecture2 BLecture2 B
Lecture2 B
 
Deriving the inverse of a function2 (composite functions)
Deriving the inverse of a function2 (composite functions)Deriving the inverse of a function2 (composite functions)
Deriving the inverse of a function2 (composite functions)
 
Grammar generated by language
Grammar generated by languageGrammar generated by language
Grammar generated by language
 
Grammar generated by language iv
Grammar generated by language ivGrammar generated by language iv
Grammar generated by language iv
 
Grammar generated by language ii
Grammar generated by  language iiGrammar generated by  language ii
Grammar generated by language ii
 

Destaque

Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computation
prasadmvreddy
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

Destaque (12)

Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computation
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Dfa vs nfa
Dfa vs nfaDfa vs nfa
Dfa vs nfa
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 

Mais de Arab Open University and Cairo University

Mais de 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 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 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
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 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
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
 
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
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 

Último

Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
ont65320
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
 
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
 
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service AvailableCall Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
 
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRLBhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
 
Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
 
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
 
Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...
Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...
Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...
 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
 
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
 
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
 
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
 
Hire 💕 8617697112 North Sikkim Call Girls Service Call Girls Agency
Hire 💕 8617697112 North Sikkim Call Girls Service Call Girls AgencyHire 💕 8617697112 North Sikkim Call Girls Service Call Girls Agency
Hire 💕 8617697112 North Sikkim Call Girls Service Call Girls Agency
 
📞 Contact Number 8617697112 VIP East Sikkim Call Girls
📞 Contact Number 8617697112 VIP East Sikkim Call Girls📞 Contact Number 8617697112 VIP East Sikkim Call Girls
📞 Contact Number 8617697112 VIP East Sikkim Call Girls
 
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
 

Theory of computation Lec6

  • 1. Lecture 6 Parsing trees and Ambiguity Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. Parsing CFG grammar is about categorizing the statements of a language. Parsing using CFG means categorizing a certain statements into categories defined in the CFG. Parsing can be expressed using a special type of graph called Trees where no cycles exist. A parse tree is the graph representation of a derivation. Dr. Hussien M. Sharaf 2
  • 3. Parse tree Dr. Hussien M. Sharaf 3 (1)A vertex with a label which is a Non-terminal symbol is a parse tree. (2) If A → y1 y2 … yn is a rule in R, then the tree A y1 y2 yn. . . is a parse tree.
  • 4. Ambiguity A grammar can generate the same string in different ways. Ambiguity occurs when a string has two or more leftmost derivations for the same CFG. There are ways to eliminate ambiguity such as using Chomsky Normal Form (CNF) which does n’t use Λ. Λ cause ambiguity. Dr. Hussien M. Sharaf 4
  • 5. Ex 1  i.e. {Λ, ab, abbaabba,… }  S → aSa| bSb| Λ Derive abaaba Dr. Hussien M. Sharaf 5 S S Λ aa S bb S aa
  • 6. Example2 Deduce CFG of a addition and parse the following expression 2+3+5 1] S→S+S|N 2] N→1|2|3|4|5|6|7|8|9|0 Dr. Hussien M. Sharaf 6 S S+N S + N 5 S + 3 N 2 N Can u make another parsing tree ?
  • 7. Example3 Deduce CFG of a addition/multiplication and parse the following expression 2+3*5 1] S→S+S|S*S|N 2] N→1|2|3|4|5|6|7|8|9|0| N Dr. Hussien M. Sharaf 7 S S*S S * N 5 S + 3 N 2 N Can u make another parsing tree ?
  • 8. Ex4 CFG without ambiguity  Deduce CFG of a addition/multiplication and parse the following expression 2*3+5 1] S→ Term|Term + S 2] Term → N|N * Term 3] N→1|2|3|4|5|6|7|8|9|0 Dr. Hussien M. Sharaf 8 S S+N S + N 5 S * 3 N 2 N Can u make another parsing tree ?
  • 9. Example 5 Dr. Hussien M. Sharaf 9 S A | A B A Λ| a | A b | A A B b | b c | B c | b B Sample derivations: S AB AAB aAB aaB aabB aabb S AB AbB Abb AAbb Aabb aabb S A B AA Bb a a b S BA b A b AA a a
  • 10. Example 6 Dr. Hussien M. Sharaf 10 S A | A B A Λ | a | A b | A A B b | b c | B c | b B S A B AA b a a bA S A A A AA bA a a bA S A B AA Bb a a b
  • 11. Dr. Hussien M. Sharaf 11 Eliminate ambiguity. Eliminate “useless” variables. Eliminate Λ-productions: A Λ. Eliminate unit productions: A B. Eliminate redundant productions. Avoid left recursion by trading left- and right-recursion.
  • 12. Dr. Hussien M. Sharaf 12
  • 13. PDA  A Pushdown Automata (PDA ) is an NFA equipped with a single stack. Dr. Hussien M. Sharaf 13 1 4 3 2
  • 14. PDA as a deterministic machine Its inputs are 1. The current state (of its “NFA”), 2. The current input symbol (or Λ), and 3. The current symbol on top of its stack. Dr. Hussien M. Sharaf 14 1 4 3 2 Its actions are: 1. Change state. 2. Change stack if needed.
  • 15. Dr. Hussien M. Sharaf 15