Theory of computation Lec6

584 views

Published on

Published in: Entertainment & Humor
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
584
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Theory of computation Lec6

  1. 1. Lecture 6 Parsing trees and Ambiguity Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 12. Dr. Hussien M. Sharaf 12
  13. 13. PDA  A Pushdown Automata (PDA ) is an NFA equipped with a single stack. Dr. Hussien M. Sharaf 13 1 4 3 2
  14. 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. 15. Dr. Hussien M. Sharaf 15

×