1 / 59

Context-Free Languages

Context-Free Languages. Context-Free Languages. Regular Languages. Context-Free Languages. Context-Free Grammars. Pushdown Automata. stack. automaton. Context-Free Grammars. Grammars. Grammars express languages Example: the English language grammar.

yaakov
Download Presentation

Context-Free Languages

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. Context-Free Languages Costas Busch - RPI

  2. Context-Free Languages Regular Languages Costas Busch - RPI

  3. Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton Costas Busch - RPI

  4. Context-Free Grammars Costas Busch - RPI

  5. Grammars • Grammars express languages • Example: the English language grammar Costas Busch - RPI

  6. Costas Busch - RPI

  7. Derivation of string “the dog walks”: Costas Busch - RPI

  8. Derivation of string “a cat runs”: Costas Busch - RPI

  9. Language of the grammar: L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” } Costas Busch - RPI

  10. Productions Sequence of Terminals (symbols) Variables Sequence of Variables Costas Busch - RPI

  11. Another Example Sequence of terminals and variables Grammar: Variable The right side may be Costas Busch - RPI

  12. Grammar: • Derivation of string : Costas Busch - RPI

  13. Grammar: • Derivation of string : Costas Busch - RPI

  14. Grammar: Other derivations: Costas Busch - RPI

  15. Grammar: Language of the grammar: Costas Busch - RPI

  16. A Convenient Notation • We write: • Instead of: for zero or more derivation steps Costas Busch - RPI

  17. In general we write: If: in zero or more derivation steps Trivially: Costas Busch - RPI

  18. Example Grammar Possible Derivations Costas Busch - RPI

  19. Another convenient notation: Costas Busch - RPI

  20. Formal Definitions Grammar: Set of variables Set of terminal symbols Start variable Set of productions Costas Busch - RPI

  21. Context-Free Grammar: All productions in are of the form Variable String of variables and terminals Costas Busch - RPI

  22. Example of Context-Free Grammar productions start variable variables terminals Costas Busch - RPI

  23. Language of a Grammar: • For a grammar with start variable String of terminals or Costas Busch - RPI

  24. Example: context-free grammar : Since, there is derivation for any Costas Busch - RPI

  25. A language is context-free • if there is a context-free grammar • with Context-Free Language: Costas Busch - RPI

  26. Example: is a context-free language since context-free grammar : generates Costas Busch - RPI

  27. Another Example Context-free grammar : Example derivations: Palindromes of even length Costas Busch - RPI

  28. Another Example Context-free grammar : Example derivations: Describes matched parentheses: () ((( ))) (( )) Costas Busch - RPI

  29. Derivation OrderandDerivation Trees Costas Busch - RPI

  30. Derivation Order Consider the following example grammar with 5 productions: Costas Busch - RPI

  31. Leftmost derivation order of string : At each step, we substitute the leftmost variable Costas Busch - RPI

  32. Rightmost derivation order of string : At each step, we substitute the rightmost variable Costas Busch - RPI

  33. Leftmost derivation of : Rightmost derivation of : Costas Busch - RPI

  34. Derivation Trees Consider the same example grammar: And a derivation of : Costas Busch - RPI

  35. yield Costas Busch - RPI

  36. yield Costas Busch - RPI

  37. yield Costas Busch - RPI

  38. yield Costas Busch - RPI

  39. Derivation Tree (parse tree) yield Costas Busch - RPI

  40. Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation: Give same derivation tree Costas Busch - RPI

  41. Ambiguity Costas Busch - RPI

  42. Grammar for mathematical expressions Example strings: Denotes any number Costas Busch - RPI

  43. A leftmost derivation for Costas Busch - RPI

  44. Another leftmost derivation for Costas Busch - RPI

  45. Two derivation trees for Costas Busch - RPI

  46. take Costas Busch - RPI

  47. Good Tree Bad Tree Compute expression result using the tree Costas Busch - RPI

  48. Two different derivation trees may cause problems in applications which use the derivation trees: • Evaluating expressions • In general, in compilers • for programming languages Costas Busch - RPI

  49. Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa) Costas Busch - RPI

  50. Example: this grammar is ambiguous since string has two derivation trees Costas Busch - RPI

More Related