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 Sensitivity Example of a CSG Informatics 2A: Lecture 26 2 Context in Programming Languages Context: Scope of variables Stuart Anderson & Bonnie Webber Overloading School of Informatics University of Edinburgh 3 Context in Natural Languages [soa,bonnie]@inf.ed.ac.uk Bounded Context Unbounded Context 20 November 2008 Recommended Reading: J&M, Chapter 16.1; Kozen, Lecture 36 Informatics 2A: Lecture 26 Context Sensitivity 1 Informatics 2A: Lecture 26 Context Sensitivity 2 Context Sensitive Grammars Context Sensitive Grammars Chomsky Hierarchy Chomsky Hierarchy Context in Programming Languages Context in Programming Languages Example of a CSG Example of a CSG Context in Natural Languages Context in Natural Languages Productions and the Chomsky Hierarchy Context Sensitivity and Complexity By virtue of this extra memory, context-sensitive grammars can We’ve spent several weeks talking about CFGs, whose productions generate more complex languages than CFGs. in A ǫ N and γ ǫ (N ∪ Σ)+ have the form: For example, we know the language a n b n can be produced by a A → γ CFG: S → aSb | ab More general are context-sensitive productions in A and γ that can But the more complex languages a n b n c n (and a n b n c n d n , etc) have the canonical, non-decreasing form: cannot. For example, the grammar α A β → αγβ S → WT where α , β ǫ (N ∪ Σ)* W → aWb | ab Here, α and β provide context to record what must precede and/or T → Tc | c follow A, if it’s to be expanded in this particular way. won’t do, because the expansion of T cannot take account of how W is expanded. Informatics 2A: Lecture 26 Context Sensitivity 3 Informatics 2A: Lecture 26 Context Sensitivity 4
Context Sensitive Grammars Context Sensitive Grammars Chomsky Hierarchy Chomsky Hierarchy Context in Programming Languages Context in Programming Languages Example of a CSG Example of a CSG Context in Natural Languages Context in Natural Languages A Context-Sensitive Grammar for a n b n c n A Context-Sensitive Grammar for a n b n c n Here is a non-canonical form CSG for producing a n b n c n This grammar exploits one element of left context in its 1. A → aABC 5. bB → bb context-sensitive rules. Why? 2. A → aBC 6. bC → bc 4. aB → ab 6. bC → bc 3. CB → BC 7. cC → cc 5. bB → bb 7. cC → cc 4. aB → ab Since B is re-written to b, and C to c, why not use the CF rules 2 4 6 abc: A → aBC → abC → abc 8. B → b aabbcc 9. C → c 1 2 3 4 5 A → aABC → aaBCBC → aaBBCC → aabBCC → With such rules, we could have derivations like 6 7 aabbCC → aabbcC → aabbcc 1 2 8 8 9 A → aABC → aaBCBC → aabCBC → aabCbC → 1 1 2 ? aaabbbccc: A → aABC → aaABCBC → aaaBCBCBC → . . . 9 aabcbC → aabcbc What else is needed? What rule applications would lead to a dead But this isn’t in the language a n b n c n end? Informatics 2A: Lecture 26 Context Sensitivity 5 Informatics 2A: Lecture 26 Context Sensitivity 6 Context Sensitive Grammars Context Sensitive Grammars Chomsky Hierarchy Context: Scope of variables Context in Programming Languages Context in Programming Languages Example of a CSG Overloading Context in Natural Languages Context in Natural Languages Context-sensitivity in Practice Programming Languages Context in programming language could be understood as a well-formed program text with a “hole” in it that needs to be Is it of any practical use to know about context-sensitivity? filled by a particular kind of thing. The context-free language that specifies the structure of the Yes programming language determines what kind of thing can appear in the hole but often there are other constraints that Context-sensitivity appears in both Programming Languages (PLs) cannot be enforced by a context-free grammar. and Natural Languages (NLs). 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 7 Informatics 2A: Lecture 26 Context Sensitivity 8
Context Sensitive Grammars Context Sensitive Grammars Context: Scope of variables Context: Scope of variables Context in Programming Languages Context in Programming Languages Overloading Overloading Context in Natural Languages Context in Natural Languages When can we use a name? Context-free Grammars are not enough Even the simplest languages that require declaration of names In programming languages we use names (and other before their use we can see that context-free grammars cannot mechanisms) to give us a convenient way to talk about the express this restriction. store of our machine. For example, consider the language L 1 generated by the The most fundamental question is: “When is it appropriate to following set of productions: use a name?” { S → D s # U s , D s → D ; D s | ε, U s → U ; U s | ε, Many programming languages enforce the disciple of requiring D → var L , U → use L , us to declare the name we intend to use in a particular context. L → a | b | a L | b L } The most common discipline is to bracket the area of the Now consider the language: program in which we can use a particular name and require that inside that bracket we must declare any name before we L 2 = { x ∈ L 1 | x = α use s ; β ⇒ x = α ′ var s ; β ′ , use it. for some s , α, α ′ , β, β ′ } Informatics 2A: Lecture 26 Context Sensitivity 9 Informatics 2A: Lecture 26 Context Sensitivity 10 Context Sensitive Grammars Context Sensitive Grammars Context: Scope of variables Context: Scope of variables Context in Programming Languages Context in Programming Languages Overloading Overloading Context in Natural Languages Context in Natural Languages Context-free grammars are not enough (ctd) An example from Java L 2 is not a CFL by use of the pumping lemma. Suppose there is a CFG G 2 with L ( G 2 ) = L 2 , let k be the public class Bicycle { constant associated with the grammar in the pumping lemma. private int cadence; Now consider the sentence: var a k b k ; # use a k b k ; ∈ L 2 private int gear; There are three cases to consider where the vwx of the private int speed; pumping lemma is: (a) entirely contained in the declaration part, (b) is entirely contained in the use part, or (c) straddles public Bicycle(int startCadence, int startSpeed, the two (i.e. uvw contains a # symbol). In each of these cases int startGear) { there is an easy contradiction. gear = startGear; This demonstrates that for issues like scope of variable or cadence = startCadence; other features that involve context, CFGs are not an adequate speed = startSpeed; 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 Informatics 2A: Lecture 26 Context Sensitivity 12
Context Sensitive Grammars Context Sensitive Grammars Context: Scope of variables Context: Scope of variables Context in Programming Languages Context in Programming Languages Overloading Overloading Context in Natural Languages Context in Natural Languages What can we refer to in an Object? Overloading public class DataArtist { Consider the program fragment: ... ... public void draw(String s) { public Bicycle mybike; ... } mybike = new Bicycle(5,21,3); public void draw(int i) { mybike.cadence = mybike.gears; ... } What would we have to change in the Class specification to public void draw(double f) { make to make this work? ... Could you imagine any way to specify this kind of constraint } using a CFG? public void draw(int i, double f) { ... } } Informatics 2A: Lecture 26 Context Sensitivity 13 Informatics 2A: Lecture 26 Context Sensitivity 14 Context Sensitive Grammars Context Sensitive Grammars Context: Scope of variables Context: Scope of variables Context in Programming Languages Context in Programming Languages Overloading Overloading Context in Natural Languages Context in Natural Languages Overloading Subtyping and “deictic” expressions public class GraphicsBox extends Box ... GraphicsBox(l,w,h,left,top) Here the term draw(x) does not refer to a unique element in { the class. super (l,w,h); Context is used to discriminate between the different choices. this.left = left; { int x; ...da.draw(x); } or String x; this.top = top; ...da.draw(x); } } 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 15 Informatics 2A: Lecture 26 Context Sensitivity 16
Recommend
More recommend