1 / 39

Chomsky & Greibach Normal Forms

Chomsky & Greibach Normal Forms. Presentation Outline. Introduction Chomsky normal form Greibach Normal Form Algorithm ( with Example) Summary. Introduction. Grammar: G = (V, T, P, S). T = { a, b }. Terminals. V = A, B, C. Variables. S. Start Symbol. P = S → A. Production.

hovan
Download Presentation

Chomsky & Greibach Normal Forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chomsky & Greibach Normal Forms

  2. Presentation Outline • Introduction • Chomsky normal form • Greibach Normal Form • Algorithm ( with Example) • Summary

  3. Introduction • Grammar: G = (V, T, P, S) T = { a, b } • Terminals V = A, B, C • Variables S • Start Symbol P = S → A • Production

  4. Presentation Outline • Introduction • Chomsky normal form • Greibach Normal Form • Algorithm ( with Example) • Summary

  5. Chomsky Normal Form A context free grammar is said to be in Chomsky Normal Form if all productions are in the following form: A → BC A → α • A, B and C are non terminal symbols • α is a terminal symbol

  6. Presentation Outline • Introduction • Chomsky normal form • Greibach Normal Form • Algorithm ( with Example) • Summary

  7. Greibach Normal Form A context free grammar is said to be in Greibach Normal Form if all productions are in the following form: A → αX • A is a non terminal symbols • α is a terminal symbol • X is a sequence of non terminal symbols. • It may be empty.

  8. Presentation Outline • Introduction • Chomsky normal form • Greibach Normal Form • Algorithm ( with Example) • Summary

  9. Conversion • Convert from Chomsky to Greibach in two steps: • From Chomsky to intermediate grammar • Eliminate direct left recursion • Use A  uBv rules transformations to improve references (explained later) • From intermediate grammar into Greibach

  10. Eliminate direct left recursion • Before A  Aa| b • After A  bZ | b Z  aZ | a • Remove the rule with direct left recursion, and create a new one with recursion on the right

  11. Eliminate direct left recursion • Before A  Aa| Ab| b| c • After A  bZ| cZ| b| c Z  aZ | bZ | a| b • Remove the rules with direct left recursion, and create new ones with recursion on the right

  12. Eliminate direct left recursion • Before A  AB| BA| a B  b | c • After A  BAZ| aZ| BA| a Z  BZ | B B  b | c

  13. Transform A  uBv rules • Before A  uBb B  w1 | w1 |…| wn • After Add A  uw1b | uw1b |…| uwnb DeleteA  uBb

  14. Conversion: Step 1 • Goal: construct intermediate grammar in this format • A  aw • A  Bw • S   where w  V* and B comes after A

  15. Conversion: Step 1 • Assign a number to all variables starting with S, which gets 1 • Transform each rule following the order according to given number from lowest to highest • Eliminate direct left recursion • If RHS of rule starts with variable with lower order, apply A  uBb transformation to fix it

  16. Conversion: Step 2 • Goal: construct Greibach grammar out of intermediate grammar from step 1 • Fix A  Bw rules into A  aw format • After step 1, last original variable should have all its rules starting with a terminal • Working from bottom to top, fix all original variables using A  uBb transformation technique, so all rules become A  aw • Fix introduced recursive rules same way

  17. Conversion Example • Convert the following grammar from Chomsky normal form, into Greibach normal form • S AB |  • A  AB | CB | a • B  AB | b • C  AC | c

  18. Conversion Strategy • Goal: transform all rules which RHS does not start with a terminal • Apply two steps conversion • Work rules in sequence, eliminating direct left recursion, and enforcing variable reference to higher given number • Fix all original rules, then new ones

  19. Step 1: S rules • Starting with S since it has a value to of 1 • S AB |  • S rules comply with two required conditions • There is no direct left recursion • Referenced rules A and B have a given number higher than 1. A corresponds to 2 and B to 3.

  20. Step 1: A rules • A  AB | CB | a • Direct left recursive rule A  AB needs to be fixed. Other A rules are fine • Apply direct left recursion transformation A  CBR1 | aR1 | CB | a R1  BR1 | B

  21. Step 1: B rules • B  AB | b • B  AB rule needs to be fixed since B corresponds to 3 and A to 2. B rules can only have on their RHS variables with number equal or higher. Use A  uBb transformation technique • B  CBR1B | aR1B | CBB | aB| b

  22. Step 1: C rules • C  AC | c • C  AC rule needs to be fixed since C corresponds to 4 and A to 2. Use same A  uBb transformation technique • C  CBR1C | aR1C | CBC | aC| c • Now variable references are fine according to given number, but we introduced direct left recursion in two rules…

  23. Step 1: C rules • C  CBR1C | aR1C |CBC | aC| c • Eliminate direct left recursion C  aR1CR2 | aCR2| cR2 | aR1C | aC| c R2  BR1CR2 | BCR2 | BR1C | BC

  24. Step 1: Intermediate grammar • S AB |  • A  CBR1 | aR1 | CB | a • B  CBR1B | aR1B | CBB | aB | b • C  aR1CR2 | aCR2| cR2 | aR1C | aC | c • R1  BR1 | B • R2  BR1CR2 | BCR2 | BR1C | BC

  25. Step 2: Fix starting symbol • Rules S, A, B and C don’t have direct left recursion, and RHS variables are of higher number • All C rules start with terminal symbol • Proceed to fix rules B, A and S in bottom-up order, so they start with terminal symbol. • Use A  uBb transformation technique

  26. Step 2: Fixing B rules • Before B  CBR1B | aR1B | CBB | aB | b • After B  aR1B | aB | b B  aR1CR2BR1B | aCR2BR1B| cR2BR1B | aR1CBR1B | aCBR1B| cBR1B B  aR1CR2BB | aCR2BB| cR2BB | aR1CBB | aCBB| cBB

  27. Step 2: Fixing A rules • Before A  CBR1 | aR1 | CB | a • After A  aR1| a A  aR1CR2BR1 | aCR2BR1| cR2BR1 | aR1CBR1 | aCBR1| cBR1 A  aR1CR2B | aCR2B| cR2B | aR1CB | aCB| cB

  28. Step 2: Fixing S rules • Before SAB |  • After S  S  aR1B| aB S  aR1CR2BR1B | aCR2BR1B| cR2BR1B | aR1CBR1B | aCBR1B| cBR1B S  aR1CR2BB | aCR2BB| cR2BB | aR1CBB | aCBB| cBB

  29. Step 2: Complete conversion • All original rules S, A, B and C are fully converted now • New recursive rules need to be converted next R1  BR1 | B R2  BR1CR2 | BCR2 | BR1C | BC • Use same A  uBb transformation technique replacing starting variable B

  30. Conclusions • After conversion, since B has 15 rules, and R1 references B twice, R1 ends with 30 rules • Similar for R2which referencesB four times. Therefore, R2 ends with 60 rules • All rules start with a terminal symbol (with the exception of S  ) • Parsing algorithms top-down or bottom-up would complete on a grammar converted to Greibach normal form

  31. Greibach Normal Form Example: S → XA | BB B → b | SB X → b A → a S = A1 X = A2 A = A3 B = A4 A1 → A2A3 | A4A4 A4 → b | A1A4 A2 → b A3 → a CNF New Labels Updated CNF

  32. Greibach Normal Form Example: Ai → AjXkj >= i A1 → A2A3 | A4A4 A4 → b | A1A4 A2 → b A3 → a First Step Xk is a string of zero or more variables A4 → A1A4

  33. Greibach Normal Form Example: Ai → AjXk j > i First Step | A4A4A4 | b A4 → bA3A4 A4 → A1A4 A1 → A2A3 | A4A4 A4 → b | A1A4 A2 → b A3 → a | A4A4A4 | b A4 → A2A3A4

  34. Greibach Normal Form Example: A1 → A2A3 | A4A4 A4 → bA3A4 | A4A4A4 | b A2 → b A3 → a Second Step Eliminate Left Recursions A4 → A4A4A4

  35. Greibach Normal Form Example: Second Step Eliminate Left Recursions A4 → bA3A4 | b | bA3A4Z| bZ A1 → A2A3 | A4A4 A4 → bA3A4 | A4A4A4 | b A2 → b A3 → a Z → A4A4 | A4A4Z

  36. Greibach Normal Form Example: A1 → A2A3 | A4A4 A4 → bA3A4 | b | bA3A4Z | bZ Z → A4A4 | A4A4 Z A2 → b A3 → a A → αX GNF

  37. Greibach Normal Form Example: A1 → A2A3 | A4A4 A4 → bA3A4 | b | bA3A4Z | bZ Z → A4A4 | A4A4 Z A2 → b A3 → a A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4Z| bA4Z| bA3A4ZA4 Z| bZA4 Z

  38. Greibach Normal Form Example: A1 → bA3 |bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 A4 → bA3A4 | b | bA3A4Z | bZ Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4Z| bA4Z| bA3A4ZA4 Z| bZA4 Z A2 → b A3 → a Grammar in Greibach Normal Form

  39. Presentation Outline Thank You!

More Related