inf2080
play

INF2080 Context-Sensitive Langugaes Daniel Lupp Universitetet i - PowerPoint PPT Presentation

INF2080 Context-Sensitive Langugaes Daniel Lupp Universitetet i Oslo 15th February 2017 Department of University of Informatics Oslo INF2080 Lecture :: 15th February 1 / 20 Context-Free Grammar Definition (Context-Free Grammar) A


  1. INF2080 Context-Sensitive Langugaes Daniel Lupp Universitetet i Oslo 15th February 2017 Department of University of Informatics Oslo INF2080 Lecture :: 15th February 1 / 20

  2. Context-Free Grammar Definition (Context-Free Grammar) A context-free grammar is a 4-tuple ( V , Σ , R , S ) where 1 V is a finite set of variables 2 Σ is a finite set disjoint from V of terminals 3 R is a finite set of rules , each consisting of a variable and of a string of variables and terminals 4 and S is the start variable Rules are of the form A → B 1 B 2 B 3 . . . B m , where A ∈ V and each B i ∈ V ∪ Σ . INF2080 Lecture :: 15th February 2 / 20

  3. Why Context-Sensitive? Many building blocks of programming languages are context-free, but not all! INF2080 Lecture :: 15th February 3 / 20

  4. Why Context-Sensitive? Many building blocks of programming languages are context-free, but not all! consider the following toy programming language, where you can “declare” and “assign” a variable a value. S → declare v ; S | assign v : x ; S INF2080 Lecture :: 15th February 3 / 20

  5. Why Context-Sensitive? Many building blocks of programming languages are context-free, but not all! consider the following toy programming language, where you can “declare” and “assign” a variable a value. S → declare v ; S | assign v : x ; S this is context-free... INF2080 Lecture :: 15th February 3 / 20

  6. Why Context-Sensitive? Many building blocks of programming languages are context-free, but not all! consider the following toy programming language, where you can “declare” and “assign” a variable a value. S → declare v ; S | assign v : x ; S this is context-free... but what if we only want to allow assignment after declaration and an infinite amount of variable names? → context-sensitive! INF2080 Lecture :: 15th February 3 / 20

  7. Context-Sensitive Languages Some believe natural languages reside in the class of context-sensitive languages, though this is a controversial topic amongst linguists. INF2080 Lecture :: 15th February 4 / 20

  8. Context-Sensitive Languages Some believe natural languages reside in the class of context-sensitive languages, though this is a controversial topic amongst linguists. But many characteristics of natural languages (e.g., verb-noun agreement) are context-sensitive! INF2080 Lecture :: 15th February 4 / 20

  9. Context-Sensitive Grammars So, instead of allowing for a single variable on the left-hand side of a rule, we allow for a context : α B γ → αβγ (1) with α, β, γ ∈ ( V ∪ Σ) ∗ , but β � = ε . INF2080 Lecture :: 15th February 5 / 20

  10. Context-Sensitive Grammars So, instead of allowing for a single variable on the left-hand side of a rule, we allow for a context : α B γ → αβγ (1) with α, β, γ ∈ ( V ∪ Σ) ∗ , but β � = ε . Definition (Context-sensitive grammar) A context-sensitive grammar is a 4-tuple ( V , Σ , R , S ) consisting of a finite set V of variables ; a finite set Σ of terminals , disjoint from V ; a set R of rules of the form (1); a start variable S ∈ V . If S does not occur on any righthand side of a rule in S , we also allow for the rule S → ε in R . INF2080 Lecture :: 15th February 5 / 20

  11. Example Recall that the language { a n b n c n | n ≥ 1 } was not context-free. (pumping lemma for CFLs) INF2080 Lecture :: 15th February 6 / 20

  12. Example Recall that the language { a n b n c n | n ≥ 1 } was not context-free. (pumping lemma for CFLs) It is, however, context-sensitive! INF2080 Lecture :: 15th February 6 / 20

  13. Example Recall that the language { a n b n c n | n ≥ 1 } was not context-free. (pumping lemma for CFLs) It is, however, context-sensitive! A context-sensitive grammar that produces this language: S → ABC B ′ Z 2 → B ′ C BB ′ → BB S → ASB ′ C CB ′ → Z 1 B ′ A → a Z 1 B ′ → Z 1 Z 2 B → b Z 1 Z 2 → B ′ Z 2 C → c INF2080 Lecture :: 15th February 6 / 20

  14. Noncontracting Grammars So CSGs can be quite cumbersome... INF2080 Lecture :: 15th February 7 / 20

  15. Noncontracting Grammars So CSGs can be quite cumbersome...many rules needed to encode, e.g., the swapping rule cB → Bc . INF2080 Lecture :: 15th February 7 / 20

  16. Noncontracting Grammars So CSGs can be quite cumbersome...many rules needed to encode, e.g., the swapping rule cB → Bc . Definition (Noncontracting Grammars) A noncontracting grammar is a set of rules α → β , where α, β ∈ ( V ∪ Σ) ∗ and | α | ≤ | β | . In addition, it may contain S → ε if S does not occur on any righthand side of a rule. INF2080 Lecture :: 15th February 7 / 20

  17. Noncontracting Grammars So CSGs can be quite cumbersome...many rules needed to encode, e.g., the swapping rule cB → Bc . Definition (Noncontracting Grammars) A noncontracting grammar is a set of rules α → β , where α, β ∈ ( V ∪ Σ) ∗ and | α | ≤ | β | . In addition, it may contain S → ε if S does not occur on any righthand side of a rule. Examples: cC → abABc ab → de cB → Bc INF2080 Lecture :: 15th February 7 / 20

  18. Noncontracting Grammars So CSGs can be quite cumbersome...many rules needed to encode, e.g., the swapping rule cB → Bc . Definition (Noncontracting Grammars) A noncontracting grammar is a set of rules α → β , where α, β ∈ ( V ∪ Σ) ∗ and | α | ≤ | β | . In addition, it may contain S → ε if S does not occur on any righthand side of a rule. Examples: cC → abABc ab → de cB → Bc Note: none of these rules are context-sensitive! INF2080 Lecture :: 15th February 7 / 20

  19. Noncontracting vs. Context-sensitive Grammars First note: context-sensitive rules α B γ → αβγ are noncontracting, since we required β � = ε . INF2080 Lecture :: 15th February 8 / 20

  20. Noncontracting vs. Context-sensitive Grammars First note: context-sensitive rules α B γ → αβγ are noncontracting, since we required β � = ε . So it would seem that noncontracting grammars are quite a bit more expressive than context-sensitive grammars. After all, α → β is far more general... INF2080 Lecture :: 15th February 8 / 20

  21. Noncontracting vs. Context-sensitive Grammars First note: context-sensitive rules α B γ → αβγ are noncontracting, since we required β � = ε . So it would seem that noncontracting grammars are quite a bit more expressive than context-sensitive grammars. After all, α → β is far more general... Theorem Every noncontracting grammar can be transformed into a context-sensitive grammar that produces the same language. INF2080 Lecture :: 15th February 8 / 20

  22. Noncontracting vs. Context-sensitive Grammars First note: context-sensitive rules α B γ → αβγ are noncontracting, since we required β � = ε . So it would seem that noncontracting grammars are quite a bit more expressive than context-sensitive grammars. After all, α → β is far more general... Theorem Every noncontracting grammar can be transformed into a context-sensitive grammar that produces the same language. So, in the spirit of INF2080’s love of abbreviations: NCG = CSG! INF2080 Lecture :: 15th February 8 / 20

  23. Example The language { a n b n c n | n ≥ 1 } described by CSG: B ′ Z 2 → B ′ C S → ABC BB ′ → BB S → ASB ′ C CB ′ → Z 1 B ′ A → a Z 1 B ′ → Z 1 Z 2 B → b Z 1 Z 2 → B ′ Z 2 C → c INF2080 Lecture :: 15th February 9 / 20

  24. Example The language { a n b n c n | n ≥ 1 } described by NCG: S → abc S → aSBc cB → Bc bB → bb INF2080 Lecture :: 15th February 10 / 20

  25. Example The language { a n b n c n | n ≥ 1 } described by NCG: S → abc S → aSBc cB → Bc bB → bb Due to the equivalence, some people define context-senstive languages using noncontracting grammars. INF2080 Lecture :: 15th February 10 / 20

  26. Kuroda Normal Form Similar to CFG’s Chomsky Normal Form, CSG’s have a normal form of their own: Definition (Kuroda Normal Form) A noncontracting grammar is in Kuroda normal form if all rules are of the form AB → CD A → BC A → B A → a for variables A , B , C , D and terminals a . INF2080 Lecture :: 15th February 11 / 20

  27. Kuroda Normal Form Similar to CFG’s Chomsky Normal Form, CSG’s have a normal form of their own: Definition (Kuroda Normal Form) A noncontracting grammar is in Kuroda normal form if all rules are of the form AB → CD A → BC A → B A → a for variables A , B , C , D and terminals a . Theorem For every context-sensitive grammar there exists a noncontracting grammar in Kuroda normal form that produces the same language. INF2080 Lecture :: 15th February 11 / 20

  28. Linear Bounded Automata So what type of computational model accepts precisely the context-sensitive languages? INF2080 Lecture :: 15th February 12 / 20

  29. Linear Bounded Automata So what type of computational model accepts precisely the context-sensitive languages? → linear bounded automata! INF2080 Lecture :: 15th February 12 / 20

  30. Linear Bounded Automata So what type of computational model accepts precisely the context-sensitive languages? → linear bounded automata! Essentially, LBAs are NFAs with a bounded tape as memory (as opposed to the stack for PDAs) INF2080 Lecture :: 15th February 12 / 20

Recommend


More recommend