CDM Context-Free Grammars

Klaus Sutner Carnegie Mellon Universality

60-cont-free 2017/12/15 23:17 1 Generating Languages

 Properties of CFLs Generation vs. Recognition 3

Turing machines can be used to check membership in decidable sets. They can also be used to enumerate semidecidable sets, whence the classical notion of recursively enumerable sets.

For languages L ⊆ Σ? there is a similar notion of generation.

The idea is to set up a system of simple rules that can be used to derive all words in a particular . These systems are typically highly nondeterministic and it is not clear how to find (efficient) recognition algorithms for the corresponding languages. Noam Chomsky 4

Historically, these ideas go back to work by Chomsky in the 1950s. Chomsky was mostly interested natural languages: the goal is to develop grammars that differentiate between grammatical and and ungrammatical sentences.

1 The cat sat on the mat.

2 The mat on the sat cat.

Alas, this turns out to be inordinately difficult, syntax and semantics of natural languages are closely connected and very complicated.

But for artificial languages such as programming languages, Chomsky’s approach turned out be perfectly suited. Cat-Mat Example 5

Sentence

Noun Phrase Verb Phrase Punctuation

Determiner Noun Verb Prepositional Phrase .

The cat sat Preposition Noun Phrase

on Determiner Noun

the mat Mat-Cat Example 6

Noun Phrase

Noun Phrase Prepositional Phrase Punctuation

Determiner Noun Preposition Noun Phrase .

The mat on Determiner Adjective Noun

the sat cat Killer App: Programming Languages 7

Many programming languages have a block structure like so:

begin begin end begin begin end begin end end end

Clearly, this is not a and cannot be checked by a finite state machine. We need more compute power. Generalizing 8

We have two rather different ways of describing regular languages:

finite state machine acceptors

regular expressions

We could try to generalize either one of these.

Let’s start with the algebra angle and handle the machine model later. Grammars 9

Definition A (formal) grammar is a quadruple

G = h V, Σ, P,S i

where V and Σ are disjoint alphabets, S ∈ V , and P is a finite set of productions or rules.

the symbols of V are (syntactic) variables,

the symbols of Σ are terminals,

S is called the start symbol (or axiom).

We often write Γ = V ∪ Σ for the complete alphabet of G. Context Free Grammars 10

Definition (CFG) A context free grammar is a grammar where the productions have the form

? P ⊆ V × Γ

It is convenient to write productions in the form

π : A α  where A ∈ V and α ∈ Γ?.

The idea is that we may replace A by α. Naming Conventions 11

A, B, C . . . represent elements of V ,

S ∈ V is the start symbol,

a, b, c . . . represent elements of Σ,

X,Y,Z... represent elements of Γ,

w, x, y . . . represent elements of Σ?,

α, β, γ . . . represent elements of Γ?. Derivations 12

Given a CFG G define a one-step relation =1⇒ ⊆ Γ? × Γ? as follows:

1 αAβ =⇒ αγβ if A γ ∈ P  As usual, by induction define

α =k+1⇒ β if ∃ γ (α =k⇒ γ ∧ γ =1⇒ β)

and α =∗⇒ β if ∃ k α =k⇒ β

in which case one says that α derives or yields β. α is a sentential form if it can be derived from the start symbol S.

To keep notation simple we’ll often just write α =⇒ β. Context Free Languages 13

Definition The language of a context free grammar G is defined to be

L(G) = { x ∈ Σ? | S =∗⇒ x }

Thus L(G) is the set of all sentential forms in Σ?. We also say that G generates L(G). A language is context free (CFL) if there exists a context free grammar that generates it.

Note that in a CFG one can replace a single syntactic variable A by strings over Γ independently of were A occurs; whence the name “context free.” Later on we will generalize to replacement rules that operate on a whole block of symbols (context sensitive grammars). Example: Regular 14

Let G = h {S, A, B}, {a, b}, P,S i where the set P of productions is defined by: S aA | aB A  aA | aB B  bB | b.  A typical derivation is:

S ⇒ aA ⇒ aaA ⇒ aaaB ⇒ aaabB ⇒ aaabb

It is not hard to see that L(G) = a+b+ Not too interesting, we already know how to deal with regular languages.

Can you see the finite state machine hiding in the grammar? Is it minimal? Derivation Graph 15

Derivations of length at most 6 in this grammar. Labeled 16

S

aA aB

aaA aaB ab abB

aaaA aaaB aab aabB abb abbB

aaaaA aaaaB aaab aaabB aabb aabbB abbb abbbB

aaaaaA aaaaaB aaaab aaaabB aaabb aaabbB aabbb aabbbB abbbb abbbbB Example: Mystery 17

Let G = h {A, B}, {a, b}, P,A i where the set P of productions is defined by: A AA | AB | a B  AA | BB | b.  A typical derivation is:

A ⇒ AA ⇒ AAB ⇒ AABB ⇒ AABAA ⇒ aabaa

In this case it is not obvious what the language of G is (assuming it has some easy description, it does). More next time when we talk about . Derivation Graph 18

Derivations of length at most 3 in this grammar. Three terminal strings appear at this point. Depth 4 19 Example: Counting 20

Let G = h {S}, {a, b}, P,S i where the set P of productions is defined by:

S aSb | ε 

A typical derivation is:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb

Clearly, this grammar generates the language { aibi | i ≥ 0 }

It is easy to see that this language is not regular. Derivation Graph 21 Example: Palindromes 22

Let G = h {S}, {a, b}, P,S i where the set P of productions is defined by:

S aSa | bSb | a | b | ε  A typical derivation is:

S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aababaa

This grammar generates the language of palindromes.

Exercise Give a careful proof of this claim. Derivation Graph 23 Example: Parens 24

Let G = h {S}, {(, )}, P,S i where the set P of productions is defined by:

S SS | (S) | ε  A typical derivation is:

S ⇒ SS ⇒ (S)S ⇒ (S)(S) ⇒ (S)((S)) ⇒ ()(())

This grammar generates the language of well-formed parenthesized expressions.

Exercise Give a careful proof of this claim. Derivation Graph 25 Example: Expressions of Arithmetic 26

Let G = h {E}, {+, ∗, (, ), v}, P,E i where the set P of productions is defined by: E E + E | E ∗ E | (E) | v 

A typical derivation is:

E ⇒ E ∗ E ⇒ E ∗ (E) ⇒ E ∗ (E + E) ⇒ v ∗ (v + v)

This grammar generates a language of arithmetical expressions with plus and times. Alas, there are problems: the following derivation is slightly awkward.

E ⇒ E + E ⇒ E + (E) ⇒ E + (E ∗ E) ⇒ v + (v ∗ v)

Our grammar is symmetric in + and ∗, it knows nothing about precedence. Derivation Graph 27 Ambiguity 28

We may not worry about awkward, but the following problem is fatal:

E ⇒ E + E ⇒ E + E ∗ E ⇒ v + v ∗ v

E ⇒ E ∗ E ⇒ E + E ∗ E ⇒ v + v ∗ v

There are two derivations for the same word v + v ∗ v.

Since derivations determine the semantics of a string this is really bad news: a compiler could interpret v + v ∗ v in two different ways, producing different results. Parse Trees 29

Derivation chains are hard to read, a better representation is a tree.

Let G = hV, Σ, P,Si be a context free grammar. A parse tree of G (aka grammatical tree) is an ordered tree on nodes N, together with a labeling λ : N → V ∪ Σ such that For all interior nodes x: λ(x) ∈ V ,

If x1, . . . , xk are the children, in left-to-right order, of interior node x then λ(x) λ(x1) . . . λ(xk) is a production of G,  λ(x) = ε implies x is an only child. Derivation Trees 30

Here are the parse trees of the “expressions grammar” from above.

E E

E + E E E ∗

E E E + E ∗

Note that the trees provide a method to evaluate arithmetic expressions, so the existence of two trees becomes a nightmare. Information Hiding 31

A parse tree typically represents several derivations:

E

E E ∗

v E + E

v v

represents for example

θ1 : E ⇒ E ∗ E ⇒ E ∗ E + E ⇒ v ∗ E + E ⇒ v ∗ v + E ⇒ v ∗ v + v θ2 : E ⇒ E ∗ E ⇒ E ∗ E + E ⇒ E ∗ E + v ⇒ E ∗ v + v ⇒ v ∗ v + v θ3 : E ⇒ E ∗ E ⇒ v ∗ E ⇒ v ∗ E + E ⇒ v ∗ v + E ⇒ v ∗ v + v but not θ4 : E ⇒ E + E ⇒ E ∗ E + E ⇒ v ∗ E + E ⇒ v ∗ v + E ⇒ v ∗ v + v Leftmost Derivations 32

Let G be a grammar and assume α =1⇒ β. We call this derivation step leftmost if

α = xAα0 β = xγα0 x ∈ Σ?

A whole derivation is leftmost if it only uses leftmost steps. Thus, each replacement is made in the first possible position.

Proposition Parse trees correspond exactly to leftmost derivations. Ambiguity 33

Definition A CFG G is ambiguous if there is a word in the language of G that has two different parse trees.

Alternatively, there are two different leftmost derivations.

As the arithmetic example demonstrates, trees are connected to semantics, so ambiguity is a serious problem in a programming language. Unambiguous Arithmetic 34

For a “reasonable” context free language it is usually possible to remove ambiguity by rewriting the grammar.

For example, here is an unambiguous grammar for our arithmetic expressions.

E E + T | T T  T ∗ F | F F  (E) | v  In this grammar, v + v ∗ v has only one parse tree.

Here {E,T,F } are syntactic variables that correspond to expressions, terms and factors. Note that it is far from clear how to come up with these syntactic categories. Inherently Ambiguous Languages 35

Alas, there are CFLs where this trick will not work: every CFG for the language is already ambiguous. Here is a well-known example:

L = { aibj ck | i = j ∨ j = k; i, j, k ≥ 1 }

L consists of two parts and each part is easily unambiguous. But strings of the form aibici belong to both parts and introduce a kind of ambiguity that cannot be removed. BTW, { aibici | i ≥ 0 } is not context free.

Exercise Show that L really is inherently ambiguous.  Generating Languages

2 Properties of CFLs Regular Implies Context Free 37

Lemma Every regular language is context free.

Proof. Suppose M = h Q, Σ, δ; q0,F i is a DFA for L. Consider a CFG with V = Q and productions p a q if δ(p, a) = q p  ε if p ∈ F  Let q0 be the start symbol. 2 Substitutions 38

Definition A substitution is a map σ :Σ → P(Γ?) .

The idea is that for any word x ∈ Σ? we can define its image under σ to be language σ(x1) · σ(x2) · ... · σ(xn) S Likewise, σ(L) = x∈L σ(x).

If σ(a) = {w} then we have essentially a homomorphism. The Substitution Lemma 39

Lemma Let L ⊆ Σ? be a CFL and suppose σ :Σ → P(Γ?) is a substitution such that σ(a) is context free for every a ∈ Σ. Then the language σ(L) is also context free.

Proof.

Let G = h V, Σ, P,S i and Ga = h Va, Γ, Pa,Sa i be CFGs for the languages L and La = σ(a) respectively. We may safely assume that the corresponding sets of syntactic variables are pairwise disjoint. Define G0 as follows. Replace all terminals a on the right hand side of a production in G by the corresponding variable Sa. It is obvious that f(L(G0)) = L where f is the homomorphism defined by f(Sa) = a. Proof, cont’d 40

Now define a new grammar H as follows. S The variables of H are V ∪ a∈Σ Va, the terminals are Σ, the start symbol is S and the productions are given by

0 [ P ∪ Pa a∈Σ

Then the language generated by H is σ(L). It is clear that H derives every word in σ(L). For the opposite direction consider the parse trees in H. 2 Closure Properties 41

Corollary

? Suppose L, L1,L2 ⊆ Σ are CFLs. Then the following languages are also ∗ context free: L1 ∪ L2, L1 · L2 and L : context free languages are closed under union, concatenation and .

Proof. This follows immediately from the substitution lemma and the fact that the languages {a, b}, {ab} and {a}∗ are trivially context free. 2 Non Closure 42

Proposition CFLs are not closed under intersection and complement.

Consider

i i j i j j L1 = { a b c | i, j ≥ 0 } L2 = { a b c | i, j ≥ 0 }

i i i We will see in a moment that L1 ∩ L2 = { a b c | i ≥ 0 } fails to be context free. More Closure 43

Lemma Suppose L is a CFL and R is regular. Then L ∩ R is also context free.

Proof. This will be easy once we have a machine model for CFLs (push-down automata), more later. 2 Dyck Languages 44

One can generalize strings of balanced parentheses to strings involving multiple types of parens. To this end one uses special alphabets with paired symbols:

Γ = Σ ∪ { a | a ∈ Σ }

The Dyck language Dk is generated by the grammar

S SS | a S a | ε 

A typical derivation looks like so:

S ⇒ SS ⇒ aSaS ⇒ aaSa aS ⇒ aaSa aaSa ⇒ aaa aaa

Exercise Find an alternative definition of a Dyck language. A Characterization 45

Let us write Dk for the Dyck language with k = |Σ| kinds of parens.

For D1 there is a nice characterization via a simple counting function. Define #ax to be the number of letters a in word x.

fa(x) = #ax − #ax

Lemma ? A string x belongs to the Dyck language D1 ⊆ {a, a} iff

fa(x) = 0 and

for any prefix z of x: fa(z) ≥ 0. A Paren Mountain 46

5

4

3

2

1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Note that one can read off a proof for the correctness of the grammar S SS | a S a | ε from the picture.  k-Parens 47

k For Dk we can still count but this time we need values in N

f(x) = (fa1 (x), fa2 (x), . . . , fak (x))

Then we need f(x) = 0 and f(z) ≥ 0 for all prefixes z of x, just like for D1.

Alas, this is not enough: we also have make sure that proper nesting occurs between different types of parens.

The critical problem is that we do not want abab. Matching Pairs 48

Let x = x1x2 . . . xn.

Note that if x ∈ Dk and xi = a then there is a unique minimal j > i such that fa(x[i]) = fa(x[j]) (why?).

Intuitively, xj = a is the matching right paren for a = xi. Hence we obtain an interval [i, j] associated with the a in position i. Call the collection of all such intervals Ia, a ∈ Σ.

The critical additional condition for a balanced string is that none of the S intervals in Ia overlap, they are all nested or disjoint.

Exercise

Show that these conditions really describe the language Dk. Dyck vs. CF 49

In a strong sense, Dyck languages are the “most general” context free languages: all context free languages are built around the notion of matching parens, though this may not at all be obvious from their definitions (and, actually, not even from their grammars).

Theorem (Chomsky-Sch¨utzenberger 1963) Every context free language L ⊆ Σ? has the form L = h(D ∩ R) where D is a Dyck language, R is regular and h is a homomorphism.

The proof also relies on a machine model, more later. Parikh Vectors 50

? Suppose Σ = {a1, a2, . . . , ak}. For x ∈ Σ , the Parikh vector of x is defined by

k #x = (#a1 x, #a2 x, . . . , #ak x) ∈ N

Lift to languages via k #L = { #x | x ∈ L } ⊆ N In a sense, the Parikh vector gives the commutative version of a word: we just count all the letters, but ignore order entirely.

For example, for the Dyck language D1 over {a, a} we have #D1 = { (i, i) | i ≥ 0 }. Semi-Linear Sets 51

k A set A ⊆ N is semi-linear if it is the finite union of sets of the form X { a0 + aixi | xi ≥ 0 } i

k and ai ∈ N fixed.

In the special case k = 1, semi-linear sets are often called ultimately periodic:

A = At + (a + mN + Ap)

where At ⊆ {0, . . . , a − 1} and Ap ⊆ {0, . . . , m − 1} are the transient and periodic part, respectively.

? Observe that for any language L ⊆ {a} : L is regular iff #L ⊆ N is semi-linear. Parikh’s Theorem 52

Theorem For any context free language L, the Parikh set #L is semi-linear.

Instead of a proof, consider the example of the Dyck language D1

S SS | aSa | ε  2 Let A = #D1, then A is the least set X ⊆ N such that S → SS: X is closed under addition S → aSa: X is closed under x 7→ x + (1, 1) S → ε: X contains (0, 0) Clearly, A = { (i, i) | i ≥ 0 }. Application Parikh 53

It follows immediately that every context free language over Σ = {a} is already regular.

As a consequence, { ap | p prime } is not context free.

This type of argument also works for a slightly messier language like

L = { akb` | k > ` ∨ (k ≤ ` ∧ k prime) }

Note that in this case L and #L are essentially the same, so it all comes down to the set of primes not being semi-linear. Markings 54

Another powerful method to show that a language fails to be context free is a generalization of the infamous pumping lemma for regular languages. Alas, this time we need to build up a bit of machinery.

Definition Let w ∈ Σ?, say, n = |w|.A position in w is a number p, 1 ≤ p ≤ n. A set K ⊆ [n] of positions is called a marking of w.

A 5-factorization of w consists of 5 words x1, x2, x3, x4, x5 such that x1x2x3x4x5 = w. Given a factorization and a marking of w let K(xi) = { p | |x1 . . . xi−1| < p ≤ |x1 . . . xi| } ⊆ K

Thus K(xi) simply consists of all the marked positions in block xi. The Iteration Theorem 55

Theorem Let G = h V, Σ, P,S i be a CFG. Then there exists a number N = N(G) such that for all x ∈ L(G), K ⊆ [|x|] a marking of x of cardinality at least N:

there exists a 5-factorization x1, . . . , x5 of x such that, letting Ki = K(xi), we have:

K1,K2,K3 6= ∅ or K3,K4,K5 6= ∅

|K2 ∪ K3 ∪ K4| ≤ N.

t t  ∀ t ≥ 0 x1x2x3x4x5 ∈ L(G) .

Proof. Stare at parse trees. 2 Non-Closure 56

Lemma { aibici | i ≥ 0 } is not context free.

Proof. Recall that this shows non-closure under complements and intersections. So a CFG can count and compare two letters, as in

i i j L1 = { a b c | i, j ≥ 0 } i j j L2 = { a b c | i, j ≥ 0 }

but three letters are not manageable.

The intuition behind this will become clear next time when we introduce a machine model. Proof 57

Let N be as in the iteration theorem and set

w = aN bN cN ,

K = [N + 1, 2N] (so the b’s in the middle are marked).

Then there is a factorization x1, . . . , x5 of w such that, letting Ki = K(xi), we have:

Case 1: K1,K2,K3 6= ∅ N i j k Then x1 = a b , x2 = b , x3 = b y where j > 0.

But then x1x3x5 ∈/ L, contradiction.

Case 2: K3,K4,K5 6= ∅ i j k N Then x3 = yb , x4 = b , x5 = b c where j > 0.

Again x1x3x5 ∈/ L, contradiction. More Non-Closure 58

It follows that

∗ { x ∈ {a, b, c} | |x|a = |x|b = |x|c }

is not context free: otherwise the intersection with a?b?c? would also be context free.

Exercise Show that the copy language

? Lcopy = { x x | x ∈ Σ }

fails to be context free. Compare this to the palindrome language { x xop | x ∈ Σ? }