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.

asta
Télécharger la présentation

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 Prof. Busch - LSU

  2. Context-Free Languages Regular Languages Prof. Busch - LSU

  3. Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton Prof. Busch - LSU

  4. Context-Free Grammars Prof. Busch - LSU

  5. Grammars Grammars express languages Example: the English language grammar Prof. Busch - LSU

  6. Prof. Busch - LSU

  7. Derivation of string “the dog walks”: Prof. Busch - LSU

  8. Derivation of string “a cat runs”: Prof. Busch - LSU

  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” } Prof. Busch - LSU

  10. Productions Sequence of Terminals (symbols) Variables Sequence of Variables Prof. Busch - LSU

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

  12. Grammar: Derivation of string : Prof. Busch - LSU

  13. Grammar: Derivation of string : Prof. Busch - LSU

  14. Grammar: Other derivations: Prof. Busch - LSU

  15. Grammar: Language of the grammar: Prof. Busch - LSU

  16. A Convenient Notation We write: Instead of: for zero or more derivation steps Prof. Busch - LSU

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

  18. Example Grammar Possible Derivations Prof. Busch - LSU

  19. Another convenient notation: Prof. Busch - LSU

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

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

  22. Example of Context-Free Grammar productions start variable variables terminals Prof. Busch - LSU

  23. Language of a Grammar: For a grammar with start variable String of terminals or Prof. Busch - LSU

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

  25. A language is context-free if there is a context-free grammar with Context-Free Language: Prof. Busch - LSU

  26. Example: is a context-free language since context-free grammar : generates Prof. Busch - LSU

  27. Another Example Context-free grammar : Example derivations: Palindromes of even length Prof. Busch - LSU

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

  29. Derivation OrderandDerivation Trees Prof. Busch - LSU

  30. Derivation Order Consider the following example grammar with 5 productions: Prof. Busch - LSU

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

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

  33. Leftmost derivation of : Rightmost derivation of : Prof. Busch - LSU

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

  35. yield Prof. Busch - LSU

  36. yield Prof. Busch - LSU

  37. yield Prof. Busch - LSU

  38. yield Prof. Busch - LSU

  39. Derivation Tree (parse tree) yield Prof. Busch - LSU

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

  41. Ambiguity Prof. Busch - LSU

  42. Grammar for mathematical expressions Example strings: Denotes any number Prof. Busch - LSU

  43. A leftmost derivation for Prof. Busch - LSU

  44. Another leftmost derivation for Prof. Busch - LSU

  45. Two derivation trees for Prof. Busch - LSU

  46. take Prof. Busch - LSU

  47. Good Tree Bad Tree Compute expression result using the tree Prof. Busch - LSU

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

  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) Prof. Busch - LSU

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

More Related