Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

# Saving this for later?

### Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Standard text messaging rates apply

# Theory of computation Lec6

203
views

Published on

Published in: Entertainment & Humor

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
203
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
20
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 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