Context Sensitivity Example of a CSG Informatics 2A: Lecture 26 2 - - PowerPoint PPT Presentation

context sensitivity
SMART_READER_LITE
LIVE PREVIEW

Context Sensitivity Example of a CSG Informatics 2A: Lecture 26 2 - - PowerPoint PPT Presentation

Context Sensitive Grammars Context Sensitive Grammars Context in Programming Languages Context in Programming Languages Context in Natural Languages Context in Natural Languages 1 Context Sensitive Grammars Chomsky Hierarchy Context


slide-1
SLIDE 1 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages

Context Sensitivity

Informatics 2A: Lecture 26 Stuart Anderson & Bonnie Webber

School of Informatics University of Edinburgh [soa,bonnie]@inf.ed.ac.uk

20 November 2008

Informatics 2A: Lecture 26 Context Sensitivity 1 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages 1 Context Sensitive Grammars

Chomsky Hierarchy Example of a CSG

2 Context in Programming Languages

Context: Scope of variables Overloading

3 Context in Natural Languages

Bounded Context Unbounded Context Recommended Reading: J&M, Chapter 16.1; Kozen, Lecture 36

Informatics 2A: Lecture 26 Context Sensitivity 2 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Chomsky Hierarchy Example of a CSG

Productions and the Chomsky Hierarchy

We’ve spent several weeks talking about CFGs, whose productions in A ǫ N and γ ǫ (N ∪ Σ)+ have the form: A → γ More general are context-sensitive productions in A and γ that can have the canonical, non-decreasing form: αAβ → αγβ where α, β ǫ (N ∪ Σ)* Here, α and β provide context to record what must precede and/or follow A, if it’s to be expanded in this particular way.

Informatics 2A: Lecture 26 Context Sensitivity 3 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Chomsky Hierarchy Example of a CSG

Context Sensitivity and Complexity

By virtue of this extra memory, context-sensitive grammars can generate more complex languages than CFGs. For example, we know the language anbn can be produced by a CFG: S → aSb | ab But the more complex languages anbncn (and anbncndn, etc)

  • cannot. For example, the grammar

S → WT W → aWb | ab T → Tc | c won’t do, because the expansion of T cannot take account of how W is expanded.

Informatics 2A: Lecture 26 Context Sensitivity 4
slide-2
SLIDE 2 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Chomsky Hierarchy Example of a CSG

A Context-Sensitive Grammar for anbncn

Here is a non-canonical form CSG for producing anbncn

  • 1. A → aABC
  • 5. bB → bb
  • 2. A → aBC
  • 6. bC → bc
  • 3. CB → BC
  • 7. cC → cc
  • 4. aB → ab

abc: A

2

→ aBC

4

→ abC

6

→ abc aabbcc A

1

→ aABC

2

→ aaBCBC

3

→ aaBBCC

4

→ aabBCC

5

→ aabbCC

6

→ aabbcC

7

→ aabbcc aaabbbccc: A

1

→ aABC

1

→ aaABCBC

2

→ aaaBCBCBC

?

→ . . . What else is needed? What rule applications would lead to a dead end?

Informatics 2A: Lecture 26 Context Sensitivity 5 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Chomsky Hierarchy Example of a CSG

A Context-Sensitive Grammar for anbncn

This grammar exploits one element of left context in its context-sensitive rules. Why?

  • 4. aB → ab
  • 6. bC → bc
  • 5. bB → bb
  • 7. cC → cc

Since B is re-written to b, and C to c, why not use the CF rules

  • 8. B → b
  • 9. C → c

With such rules, we could have derivations like A

1

→ aABC

2

→ aaBCBC

8

→ aabCBC

8

→ aabCbC

9

→ aabcbC

9

→ aabcbc But this isn’t in the language anbncn

Informatics 2A: Lecture 26 Context Sensitivity 6 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Chomsky Hierarchy Example of a CSG

Context-sensitivity in Practice

Is it of any practical use to know about context-sensitivity? Yes Context-sensitivity appears in both Programming Languages (PLs) and Natural Languages (NLs).

Informatics 2A: Lecture 26 Context Sensitivity 7 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

Programming Languages

Context in programming language could be understood as a well-formed program text with a “hole” in it that needs to be filled by a particular kind of thing. The context-free language that specifies the structure of the programming language determines what kind of thing can appear in the hole but often there are other constraints that cannot be enforced by a context-free grammar. For example, if we write a hole as [. . . ] then {int i; int j; [...] = i+j} is a good example of a simple program that assigns i+j to some variable. Could we use k to fill the hole?

Informatics 2A: Lecture 26 Context Sensitivity 8
slide-3
SLIDE 3 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

When can we use a name?

In programming languages we use names (and other mechanisms) to give us a convenient way to talk about the store of our machine. The most fundamental question is: “When is it appropriate to use a name?” Many programming languages enforce the disciple of requiring us to declare the name we intend to use in a particular context. The most common discipline is to bracket the area of the program in which we can use a particular name and require that inside that bracket we must declare any name before we use it.

Informatics 2A: Lecture 26 Context Sensitivity 9 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

Context-free Grammars are not enough

Even the simplest languages that require declaration of names before their use we can see that context-free grammars cannot express this restriction. For example, consider the language L1 generated by the following set of productions: {S → Ds#Us, Ds → D;Ds | ε, Us → U;Us | ε, D → varL, U → useL, L → a | b | aL | bL} Now consider the language: L2 = {x ∈ L1 | x = α use s;β ⇒ x = α′ var s;β′, for some s, α, α′, β, β′}

Informatics 2A: Lecture 26 Context Sensitivity 10 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

Context-free grammars are not enough (ctd)

L2 is not a CFL by use of the pumping lemma. Suppose there is a CFG G2 with L(G2) = L2, let k be the constant associated with the grammar in the pumping lemma. Now consider the sentence: var akbk; # use akbk; ∈ L2 There are three cases to consider where the vwx of the pumping lemma is: (a) entirely contained in the declaration part, (b) is entirely contained in the use part, or (c) straddles the two (i.e. uvw contains a # symbol). In each of these cases there is an easy contradiction. This demonstrates that for issues like scope of variable or

  • ther features that involve context, CFGs are not an adequate

mechanism to define such languages. For many programming languages, scope rules are specified informally and implemented in compilers using symbol tables that record when a variable is in scope.

Informatics 2A: Lecture 26 Context Sensitivity 11 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

An example from Java

public class Bicycle { private int cadence; private int gear; private int speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } }

Informatics 2A: Lecture 26 Context Sensitivity 12
slide-4
SLIDE 4 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

What can we refer to in an Object?

Consider the program fragment: ... public Bicycle mybike; mybike = new Bicycle(5,21,3); mybike.cadence = mybike.gears; What would we have to change in the Class specification to make to make this work? Could you imagine any way to specify this kind of constraint using a CFG?

Informatics 2A: Lecture 26 Context Sensitivity 13 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

Overloading

public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }

Informatics 2A: Lecture 26 Context Sensitivity 14 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

Overloading

Here the term draw(x) does not refer to a unique element in the class. Context is used to discriminate between the different choices. {int x; ...da.draw(x);} or String x; ...da.draw(x);}

Informatics 2A: Lecture 26 Context Sensitivity 15 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Context: Scope of variables Overloading

Subtyping and “deictic” expressions

public class GraphicsBox extends Box ... GraphicsBox(l,w,h,left,top) { super (l,w,h); this.left = left; this.top = top; } this and super point to specific things in the context. Any object of type GraphicsBox can appear where we expect to see a Box

Informatics 2A: Lecture 26 Context Sensitivity 16
slide-5
SLIDE 5 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Bounded Context Unbounded Context

Bounded Context

What about context sensitivity in NL? We’ll look at two phenomena: agreement and reduplication. Consider the sentences:

  • Imagine a policy to insure incomes that exceeds expectations.
  • Imagine a policy to insure incomes that exceed $1m.

What exceeds expectations? What exceeds $1m? You know the answer because of subject-verb agreement in English: In the context of a singular subject, the inflected verb form must be singular. In the context of a plural subject, the inflected verb form must be plural.

Informatics 2A: Lecture 26 Context Sensitivity 17 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Bounded Context Unbounded Context

Agreement and Context Sensitivity

Even though the subject and inflected verb can be arbitrarily far away The man wants a pear. The man that Fred talked to wants a pear. The man that Sam saw Fred talk to wants a pear. The man that Harold said that Sam saw Fred talk to wants a pear. . . . the sensitivity to context can be bounded within CF rules: S → NPsg VPsg S → NPpl VPpl

Informatics 2A: Lecture 26 Context Sensitivity 18 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Bounded Context Unbounded Context

Reduplication and Context Sensitivity

While palindrome languages can be generated by a CFG – eg S → aSa | bSb | ǫ copying languages can’t. In many Natural Languages, there is a form of copying called reduplication. English: bye-bye, razzle-dazzle, criss-cross, baby-schmabby Persian: Xert-o-Pert, ˆ Carand-o-Parand, Tala-mala Swahili: pigapiga (strike repeatedly) While these look like the output of a copying grammar, reduplication is local, and so doesn’t require a CSG. Nevertheless, we’ll see in Lecture 28 that NLs require more than CF power.

Informatics 2A: Lecture 26 Context Sensitivity 19 Context Sensitive Grammars Context in Programming Languages Context in Natural Languages Bounded Context Unbounded Context

Summary

Languages exist that are more complex than can be produced by CFGs. For some of those languages, the complexity can be captured by remembering some amount of left context or right context

  • r both.

Context-sensitivity plays a practical role in both PLs and NLs. In PLs, the context is rather basic (e.g. a list of names I can use here.) As type systems get more sophisticated the type information carried by the context becomes more discriminating. In NLs, some types of context are bounded, which can be handled by multiplying the number of CFG rules or using CFG rules with features. But some amount of context-sensitivity is still needed.

Informatics 2A: Lecture 26 Context Sensitivity 20