1 / 26

CSI 3104 /Winter 2006 : Introduction to Formal Languages Chapter 12: Context-Free Grammars

CSI 3104 /Winter 2006 : Introduction to Formal Languages Chapter 12: Context-Free Grammars. Chapter 12: Context-Free Grammar I. Theory of Automata  II. Theory of Formal Languages III. Theory of Turing Machines …. Chapter 12: Context-Free Grammars. programming languages

teigra
Download Presentation

CSI 3104 /Winter 2006 : Introduction to Formal Languages Chapter 12: Context-Free Grammars

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. CSI 3104 /Winter 2006: Introduction to Formal Languages Chapter 12: Context-Free Grammars Chapter 12: Context-Free Grammar I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines … Zaguia/Stojmenovic

  2. Chapter 12: Context-Free Grammars • programming languages • compiling a program: an operation that generates an equivalent program in machine or assembler language. • 2 phases: • parsing  • translation to machine language Zaguia/Stojmenovic

  3. Chapter 12: Context-Free Grammars Example: AE (Arithmetic Expressions) • Rule 1: Any number is in AE • Rule 2: If x and y are in AE, then so are: (x) – (x) (x+y) (x–y) (x*y) A different way for defining the set AE is to use a set of substitutions rules similar to the grammatical rules: Zaguia/Stojmenovic

  4. Chapter 12: Context-Free Grammars Substitution rules that define the AE’s: S  AE AE  (AE + AE) AE  (AE–AE) AE  (AE*AE) AE  (AE) AE –(AE) AE  NUMBER NUMBERS?? NUMBER  FIRST-DIGIT FIRST-DIGIT  FIRST-DIGIT OTHER-DIGIT FIRST-DIGIT  1 2 3 4 5 6 7 8 9 OTHER-DIGIT  0 1 2 3 4 5 6 7 8 9 Zaguia/Stojmenovic

  5. Chapter 12: Context-Free Grammars S  AE  (AE*AE)  ((AE+AE)*AE)  ((AE+AE)*(AE+AE)) …  ((3+4)*(6+7)) AE: S  AE AE  (AE + AE) AE  (AE–AE) AE  (AE*AE) AE  (AE) AE –(AE) AE  NUMBER How to generate the number 1066? NUMBER  FIRST-DIGIT  FIRST-DIGIT OTHER-DIGIT  FIRST-DIGIT OTHER-DIGIT OTHER-DIGIT  FIRST-DIGIT OTHER-DIGIT OTHER-DIGIT OTHER-DIGIT  1 0 6 6 NUMBERS NUMBER  FIRST-DIGIT FIRST-DIGIT  FIRST-DIGIT OTHER-DIGIT FIRST-DIGIT  1 2 3 4 5 6 7 8 9 OTHER-DIGIT  0 1 2 3 4 5 6 7 8 9 Zaguia/Stojmenovic

  6. Chapter 12: Context-Free Grammars Definition: A context free grammar (CFG) is: • an alphabet S of letters, called terminals. • a set of symbols, called nonterminals or variables. One symbol S is called the start symbol. • a finite set of productions of the form: A  a where A is a nonterminal and a is a finite sequence (word) of nonterminals and terminals. Zaguia/Stojmenovic

  7. EA: S  AE AE  (AE + AE) AE  (AE–AE) AE  (AE*AE) AE  (AE) AE –(AE) AE  NUMBER Chapter 12: Context-Free Grammars • Examples: Terminals: (, ), +, -, *, numbers Nonterminals: S, AE NUMBERS NUMBER  FIRST-DIGIT FIRST-DIGIT  FIRST-DIGIT OTHER-DIGIT FIRST-DIGIT  1 2 3 4 5 6 7 8 9 OTHER-DIGIT  0 1 2 3 4 5 6 7 8 9 Terminals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Nonterminals: S, FIRST-DIGIT, OTHER-DIGIT Zaguia/Stojmenovic

  8. Chapter 12: Context-Free Grammars Definition: A sequence of applications of productions starting with the start symbol and ending in a sequence of terminals is called a derivation. Definition:The language generated by a CFG is the set of all sequences of terminals produced by derivations. We also say language defined by, language derived from, or language produced by the CFG. Definition: A language generated by a CFG is called a context-free language. Zaguia/Stojmenovic

  9. Chapter 12: Context-Free Grammars Examples: S  aS S Λ S  aS  aaS  aaaS  aaaaS  aaaaaS  aaaaaaS  aaaaaaΛ = aaaaaa Generated Language: {Λ, a, aa, aaa, …} = language(a*). Zaguia/Stojmenovic

  10. Chapter 12: Context-Free Grammars S  SS S  a S Λ S  SS  SSS  SaS  SaSS  ΛaSS  ΛaaS  ΛaaΛ=aa (An infinite number of derivations for the word aa.) Generated Language: {Λ, a, aa, aaa, …} = language(a*). Zaguia/Stojmenovic

  11. Chapter 12: Context-Free Grammars In general: variables: Upper case letters terminals: Lower case letters The empty word: is it a nonterminal? L... no a terminal? LaaL = aa not exactly, because it is erased. N L N can simply be deleted. Zaguia/Stojmenovic

  12. Chapter 12: Context-Free Grammars • S  aS S  bS S  a S  b S  aS  abS  abbS  abba • S  X S  Y X ΛY  aY Y bY Y  a Y  b • S  aS S  bS S  a S  b S Λ S  aS  abS  abbS  abbaS  abba • S  aS S  bS S Λ Zaguia/Stojmenovic

  13. Chapter 12: Context-Free Grammars S  XaaX X  aX X  bX X Λ S  XaaX  aXaaX  abXaaX  abXaabX  abaab How many derivations are possible for the word baabaab? S  XY X  aX X  bX X  a Y  Ya Y Yb Y  a S  XY X  aX | bX | a Y  Ya | Yb | a Abbreviation: Zaguia/Stojmenovic

  14. Chapter 12: Context-Free Grammars S  SS | ES | SE | Λ | DSD E  aa | bb D  ab | ba EVEN-EVEN=language([aa+bb+(ab + ba)(aa+bb)*(ab+ba)]*) S  aSb | Λ S aSb  aaSbb  aaaSbbb  aaaaSbbbb  aaaaaSbbbbb  aaaaabbbbb S  aSa | bSb | Λ ? Zaguia/Stojmenovic

  15. S S S S A A A A A A A A b A A A A b A A A A b A A A A b A b a A a b A b a A a a a Chapter 12: Context-Free Grammars S  AA A  AAA | bA | Ab | a Parse Trees for the word bbaaaab bbaaaab Zaguia/Stojmenovic

  16. Chapter 12: Context-Free Grammars Parse trees are also called syntax trees, generation trees, production trees, or derivation trees. Remark: In a parse tree every internal nodes is labelled with a variable (nonterminal) and every leaf is labelled with a terminal. Zaguia/Stojmenovic

  17. S S S S * S S + + 5 S S * S S 3 3 4 4 5 Chapter 12: Context-Free Grammars Example: S  (S+S) | (S*S) | NUMBER NUMBER  … S (S+S)  (S+(S*S))  …  (3+(4*5)) S (S*S)  ((S+S)*S)  …  ((3+4)*5) Zaguia/Stojmenovic

  18. S S S S 3 + S S + 20 3 + * 5 4 * S S 3 S 4 5 S S * S S + 5 S S S 5 * 7 5 * 3 4 + 4 3 Chapter 12: Context-Free Grammars    23    35 Zaguia/Stojmenovic

  19. S S S S + S + * S S * 3 + 3 * 4 5 3 5 4 5 4 Chapter 12: Context-Free Grammars Lukasiewicz (Prefix) Notation +3*45 Zaguia/Stojmenovic

  20. S S S * S S + 5 S S * * 3 4 + 5 + 5 4 3 4 3 Chapter 12: Context-Free Grammars *+345 Zaguia/Stojmenovic

  21. S * + 6 5 * + + 2 1 4 3 Chapter 12: Context-Free Grammars * + * + 1 2 + 3 4 5 6 + 1 2  * + * 3 + 3 4 5 6 + 3 4  * + * 3 7 5 6 * 3 7  * + 21 5 6 + 21 5  * 26 6 * 26 6  156 Zaguia/Stojmenovic

  22. S B A a b S a a S a a S b Chapter 12: Context-Free Grammars Example:S  AB A  a B  b S AB  aB  ab S AB  Ab  ab • A CFG is ambiguous if there is at least one word in the language that has at least two derivation trees. It is called unambiguous otherwise. • Example: S  aSa S  bSb S  a S  b S L S aSa  aaSaa  aabaa Zaguia/Stojmenovic

  23. S S S S S a a S S a a S S a S a a S S a a S S a S a a a a a Chapter 12: Context-Free Grammars Language(a+) Example:S  aS | Sa | a Example:S  aS | a Zaguia/Stojmenovic

  24. S aa bX aXX aabX bab bb abX aXab aXb aabab aabb abab abb aabab abab aabb abb Chapter 12: Context-Free Grammars Total Language Trees S  aa | bX | aXX X  ab | b Zaguia/Stojmenovic

  25. aSb bS a S aaSbb abSb aab baSb bbS ba  aaaSbbb aabSbb aaabb aaaSbbb aaaSbbb bba . . . . . . . . Chapter 12: Context-Free Grammars Total language tree is infinite since S can call itself recursively S  aSb | bS | a Zaguia/Stojmenovic

  26. S b X aX aaX aaaX . . Chapter 12: Context-Free Grammars S  X | b X  aX Total language tree is infinite but language is finite Zaguia/Stojmenovic

More Related