Automaten und formale Sprachen Notizen zu den Folien Contents 1 - - PDF document

automaten und formale sprachen
SMART_READER_LITE
LIVE PREVIEW

Automaten und formale Sprachen Notizen zu den Folien Contents 1 - - PDF document

Automaten und formale Sprachen Notizen zu den Folien Contents 1 Mathematical foundation and formal proofs 2 Venn diagrams (slide 29) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Examples to set operations (slide


slide-1
SLIDE 1

Automaten und formale Sprachen

Notizen zu den Folien

Contents

1 Mathematical foundation and formal proofs 2 Venn diagrams (slide 29) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Examples to set operations (slide 30) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Examples to the power set (slide 31) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Examples to the cross product (slide 33) . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Examples to the properties of relations (slides 35 and 37) . . . . . . . . . . . . . . . . . 3 Examples to functions (slide 38) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Truth and validity (slide 40) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Example of a proof (slide 47) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Example of a counter example (Slide 48) . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Example of a proof about sets (slide 51) . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Example of a proof by contradiction (slide 52) . . . . . . . . . . . . . . . . . . . . . . . . 6 Example of a proof by induction (slide 54) . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 Languages and grammars 7 Example derivation in a grammar (slide 67) . . . . . . . . . . . . . . . . . . . . . . . . . 7 Comment on Chomsky Hierarchy for Grammars (Slide 78) . . . . . . . . . . . . . . . . . 7 Classifying grammars according to the Chomsky-Hierarchy (slides 78, 79 and 82) . . . . 7 Comment on Chomsky Hierarchy for Languages (Slide 82) . . . . . . . . . . . . . . . . . 8 Word Problem Algorithmus for Type-1-Grammars (slide 86) . . . . . . . . . . . . . . . . 9 3 Finite Automata 9 Transition function of DFAs (slides 92 and 93) . . . . . . . . . . . . . . . . . . . . . . . 9 Solution to exercises of slide 95 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 DFA → regular grammar (slide 96) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Transition function of NFAs (slides 100 and 101) . . . . . . . . . . . . . . . . . . . . . . 11 NFA Example (slide 103) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Subset construction (slide 106) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Regular grammar → NFA (slide 110) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4 Regular Expressions 14 Precedence of the operations (slide 115) . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Examples of regular expressions (slide 117) . . . . . . . . . . . . . . . . . . . . . . . . . 14 Regular expression → NFA (slides 120–126) . . . . . . . . . . . . . . . . . . . . . . . . . 15 NFA → regular expression (slides 128–136) . . . . . . . . . . . . . . . . . . . . . . . . . 16 5 The Pumping Lemma 18 Pigeon Hole Principle (slide 144) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Pumping Lemma (slides 148 and 150) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Pumping Lemma examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 6 Minimal automata and Myhill-Nerode aquivalence 20 Equivalence classes (slides 155–159) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Acceptance equivalence example (slide 160) . . . . . . . . . . . . . . . . . . . . . . . . . 21 Acceptance equivalence and Myhill–Nerode equivalence (slide 163) . . . . . . . . . . . . 22 Examples for Myhill-Nerode equivalence (slides 164, 168) . . . . . . . . . . . . . . . . . 22 Proving Non-Regularity with the Theorem of Myhill–Nerode (slide 170) . . . . . . . . . 23 1

slide-2
SLIDE 2

7 Closure Properties and Decision Procedures 24 Construction of the Complement Automaton (slide 185) . . . . . . . . . . . . . . . . . . 24 Example Cross Product Construction (slide 188) . . . . . . . . . . . . . . . . . . . . . . 24 8 Program verification: Mutual Exclusion 25 Mutual Exclusion: First Attempt (slide 211) . . . . . . . . . . . . . . . . . . . . . . . . 25 Attempt 1: Test with Grail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Attempt 2: Test with Grail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 9 Recapitulation of Regular Languages 26 Recapitulation: Three proofs of non-regularity . . . . . . . . . . . . . . . . . . . . . . . 26 10 Context Free Grammars 27 Examples for Context Free Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Removing ǫ-productions (slides 229 and 230) . . . . . . . . . . . . . . . . . . . . . . . . 28 Ambiguous Grammar (slide 237) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Examples for Chomsky -Normal Form (slides 239–244) . . . . . . . . . . . . . . . . . . . 29 11 The CYK Algorithm 32 Notation example (slides 251/252) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 CYK Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 12 The Pumping Lemma for Context Free Languages 33 Example for the Pumping Mechanism (slide 264) . . . . . . . . . . . . . . . . . . . . . . 33 Pumping Lemma Examples (slide 269) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 13 Kellerautomaten 36 Why an Automaton Model for Context Free Languages? (slide 270) . . . . . . . . . . . 36 To slide 271 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Meaning of the Transition Function of a Push-Down Automaton (slides 277–278) . . . . 36 Notation of the Transition Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Push-Down Automaton Example 1 (slide 287) . . . . . . . . . . . . . . . . . . . . . . . . 37 To slide 273 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Example Push-Down Automata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Context Free Grammar → PDA (slide 295/296) . . . . . . . . . . . . . . . . . . . . . . . 41 PDA → Context Free Grammar (slides 298–307) . . . . . . . . . . . . . . . . . . . . . . 42 14 Closure Properties and Algorithms for Context Free Languages 43 Push-Down Automata with Final States (slide 272) . . . . . . . . . . . . . . . . . . . . . 43 Closure Under Union (slide 275) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Closure Under Product (slide 276) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Closure Under the Star Operation (slide 277) . . . . . . . . . . . . . . . . . . . . . . . . 45 Closure Under Intersection (slide 278) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Example Cross Product Construction NFA/PDA (slide 279,280) . . . . . . . . . . . . . 45 To slide 283 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 15 Generating a Parser with JavaCC 47 EBNF-Form (slides 308–309) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Invoking ANTLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2

slide-3
SLIDE 3

1 Mathematical foundation and formal proofs

Venn diagrams (slide 29)

In the top diagram of slide 29 to sets are shown: A and B. One element of A, which is not an element of B, is explicitly represented. Whether B has elements (and whether there are objects, which are both element of A and of B) is not explicitly stated. In the second diagram A is a subset of B.

Examples to set operations (slide 30)

Let A = {1, 3, 5} und B = {2, 3, 4}. It holds that A ∪ B = {1, 2, 3, 4, 5} A ∩ B = {3} A \ B = {1, 5} B \ A = {2, 4}

Examples to the power set (slide 31)

P({a, b}) =

  • ∅, {a}, {b}, {a, b}
  • P({1, 2, 3}) =
  • ∅, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
  • P(P(∅)) = P({∅}) = {∅, {∅}}

Note: for all sets A it is the case, that ∅ ⊆ A and A ⊆ A. That means, that it is always the case, that ∅ ∈ P(A) and A ∈ P(A).

Examples to the cross product (slide 33)

{1, 3} × {2, 4} = {(1, 2), (1, 4), (3, 2), (3, 4)} {a, b, c} × {1} = {(a, 1), (b, 1), (c, 1)} A × ∅ = ∅ (f¨ ur alle Mengen A)

Examples to the properties of relations (slides 35 and 37)

(a)

a b c d

(b)

a b c d

(c)

a b c d

(d)

a b c d

Relation (a) is reflexive, transitive and symmetric. It is therefore a quasi-order and an equivalence

  • relation. The relation is not anti-symmetric and therefore not an order.

Relation (b) is symmetric. (It has none of the other properties.) Relation (c) is transitive and antisymmetric. (It has none of the other properties.) Relation (d) is transitive and antisymmetric. (It has none of the other properties.) More examples for orders and equivalence relations: 3

slide-4
SLIDE 4
  • the relation ≤ is an order on the natural numbers N, because:

– it is reflexive (x ≤ x); – it is anti-symmetric (if x ≤ y and y ≤ x, then we know that x = y); – it is transitive (if x ≤ y and y ≤ z, then x ≤ z).

  • the relation = is an equivalence relation on N.

Examples to functions (slide 38)

Let A = {a, b, c} and B = {1, 2, 3}. Further more, let f1 =

  • (a, 1), (b, 1), (c, 2)
  • f2 =
  • (a, 1), (b, 2), (c, 3)
  • f3 =
  • (a, 1), (a, 2), (b, 1), (c, 2)
  • f4 =
  • (b, 1), (c, 2)
  • The relation f1 and f2 are functions from A to B; The relation f3 is not a function from A to B,

because there are two y ∈ B such that (a, y) ∈ f3 (namely 1 und 2). The relation f4 is also not a function from A to B, because there is no y ∈ B such that (a, y) ∈ f4 (one also says: h is not defined on a. Example (square function): Let the function f be defined as follows: f : Z → N, f(n) = n2 Then f is a function, because each element z ∈ Z there exists exactly one n ∈ N such that f(z) = n. For example: f(−3) = 9 f(1) = 1 · · · f(−2) = 4 f(0) = 0 f(2) = 4 · · · f(−1) = 1 f(3) = 9

Truth and validity (slide 40)

In mathematics, there is a subtle difference between truth and validity.

  • A mathematical statement is true, when it corresponds to reality. (The question what reality

is to a mathematician, will not be answered in this lecture.)

  • A mathematical statement is valid, when it can be proved.

Validity is a stronger property than truth: when a statement is valid, it is also true. Due to G¨

  • del

we know, that there are true statements which cannot be proved. Still, a mathematical statement is usually only considered to be true when it has been proved. In this lecture the difference between true and valid is not important; the two words will be used interchangeably. 4

slide-5
SLIDE 5

Example of a proof (slide 47)

  • Theorem. Let A be a set, and ⊆ A × A a quasi-order on A. We define the relation ≈ as

follows: x ≈ y falls x y und y x. Then ≈ is an equivalence relation.

  • Proof. By definition, we must show that ≈ is reflexive, symmetric and transitive.
  • Reflexive. Assume, that a ∈ A. Because is a quasi-order, is reflexive. Therefore, it holds

that a a (and the other way around). We conclude, that a ≈ a. Since a ≈ a for an arbitrary a ∈ A, we can conclude that ≈ is reflexive.

  • Symmetrisch. Nehme an, dass a, b ∈ A und a ≈ b. Nach Definition folgt aus a ≈ b, dass a b

und b a. Daraus folgt, dass b a und a b, und deswegen gilt auch b ≈ a. Weil dies gilt f¨ ur beliebige a, b ∈ A, schließen wir, dass ≈ symmetrisch ist.

  • Transitiv. Nehme an, dass a, b, c ∈ A, a ≈ b und b ≈ c. Nach Definition folgt aus a ≈ b dass

a b und b a. Außerdem folgt nach Definition aus b ≈ c dass b c und c b. Weil eine Quasi-Ordnung ist, ist sie transitiv. Deswegen folgt aus a b und b c, dass a c. Außerdem folgt aus c b und b a, dass c a. Weil a c und c a gilt nach Definition, dass a ≈ c. Dies gilt f¨ ur beliebige a, b, c ∈ A, und deswegen k¨

  • nnen wir schließen, dass ≈

transitiv ist. Because ≈ is reflexive, symmetric and transitive, it is an equivalence relations.

  • Example of a counter example (Slide 48)

Claim: Let R ∈ A × A be a binary relation. When R is symmetric and transitive, then it is also reflexive. This claim is wrong. To show this, it suffices to give a counter example. Let A = {0} and R = ∅. Then R is symmetric (because the premisse x R y is never satisfied) and transitive (because the premisses x R y and y R z are never satisfied), but it is not reflexive.

Example of a proof about sets (slide 51)

  • Theorem. For sets A, B, C it holds, that:

(A ∩ B) ∪ C = (A ∪ C) ∩ (B ∪ C)

  • Proof. We have to show two directions:

⇒ Assume that x ∈ (A ∩ B) ∪ C. By definition of ∪ it holds that x ∈ (A ∩ B) or x ∈ C.

  • Assume x ∈ (A ∩ B). Then x ∈ A and x ∈ B. From x ∈ A it follows, that x ∈ (A ∪ C).

From x ∈ B it follows, that x ∈ (B ∪ C). Because x ∈ (A ∪ C) and x ∈ (B ∪ C) it holds that x ∈ (A ∪ C) ∩ (B ∪ C).

  • Assume x ∈ C.

It holds by definition that x ∈ (A ∪ C) and x ∈ (B ∪ C). Since x ∈ (A ∪ C) and x ∈ (B ∪ C), it holds that x ∈ (A ∪ C) ∩ (B ∪ C). Because it holds in both cases that x ∈ (A ∪ C) ∩ (B ∪ C), x ∈ (A ∪ C) ∩ (B ∪ C). 5

slide-6
SLIDE 6

⇐ Assume x ∈ (A ∪ C) ∩ (B ∪ C). By definition of ∩ it holds that x ∈ (A ∪ C) and x ∈ (B ∪ C). From x ∈ (A ∪ C) it follows, that either x ∈ A or x ∈ C.

  • Assume x ∈ A. Because of x ∈ (B ∪ C) there are two possibilities:

– If x ∈ B, then together with x ∈ A it follows that x ∈ (A ∩ B). Therefore x ∈ (A ∩ B) ∪ C. – If x ∈ C, then it directly follows that x ∈ (A ∩ B) ∪ C. In both cases it holds that x ∈ (A∩B)∪C, and therefore we can conclude x ∈ (A∩B)∪C.

  • Assume x ∈ C. It directly follows that x ∈ (A ∩ B) ∪ C.

In both cases it holds that x ∈ (A∩B)∪C, and therefore we can conclude x ∈ (A∩B)∪C.

  • Example of a proof by contradiction (slide 52)

Theorem. √ 2 is irrational, that is, there are no p, q ∈ Z such that p

q =

√ 2.

  • Proof. We assume the negation of the statement and derive a contradiction. So, we assume, that

there are integers p, q such that p q = √ 2. Because we can divide p and q by common divisors, we can, without loss of generality, assume that p and q are coprime (they have no divisors in common). We multiply both sides of the equation by q: p = √ 2 · q. Then we take the square of both sides p2 = 2q2. This means, that p2 is an even number. Since the square of an odd number is always odd, it must be the case that p is even. That means, that there must be an integer a such that p = 2a From this it follows that 2q2 = p2 = (2a)2 = 4a2; dividing by 2 results in q2 = 2a2. That means that q2 and thus also q are even numbers. Since p and q are both even, they are both divisable by 2 and therefore not co-prime. This is in contradiction to the assumption. Therefore, the assumption must be false, and we conclude that there are no integers p and q such that p/q = √ 2.

  • Example of a proof by induction (slide 54)
  • Theorem. For all n > 0 it holds, that 1 + 2 + · · · + n = n · (n + 1)

2 .

  • Proof. We prove the theorem by induction.

6

slide-7
SLIDE 7
  • Base case. For n = 1 it holds:

1 · (1 + 1) 2 = 2 2 = 1 =

1

  • i=1

i (Note:

n

  • i=1

i is an abbreviation of 1 + · · · + n.)

  • Induction step. Assume, that

n

  • i=1

i = n · (n + 1) 2 . (This is the induction hypothesis.) Then:

n+1

  • i=1

i =

n

  • i=1

i + (n + 1) = n · (n + 1) 2 + (n + 1) (IH) = n · (n + 1) 2 + (2n + 2) 2 = n2 + 3n + 2 2 = (n + 1) · (n + 2) 2 The step which is marked by (IH) follows from the induction hypothesis. The theorem follows by induction.

  • 2

Languages and grammars

Example derivation in a grammar (slide 67)

A derivation of the word aabbcc in the example grammar: S ⇒ aSBC ⇒ aaBCBC ⇒ aaBBCC ⇒ aabBCC ⇒ aabbCC ⇒ aabbcC ⇒ aabbcc That means, aabbcc ∈ L(G).

Comment on Chomsky Hierarchy for Grammars (Slide 78)

All constraints are imposed additionally to the constraints mentioned before. That is, a regular grammar G = (Σ, V, P, S) must satisfy the following conditions: for all rules l → r:

  • |l| ≤ |r|;
  • l ∈ V (l is a single variable);
  • r = a or r = aB, for a ∈ Σ und B ∈ V .

The Chomsky Hierarchy for grammars is a hierarchy. That is, every Chomsky-i grammar is also a Chomsky-(i − 1) grammar (for i ∈ {1, 2, 3}). It is possible, that a language is generated by two different grammars of different Chomsky type. 7

slide-8
SLIDE 8

Classifying grammars according to the Chomsky-Hierarchy (slides 78, 79 and 82)

Examples

  • Let G0 = ({S}, {a, b}, S, P1), where P consists of the following productions:

S → aaSbb | ǫ aa → a bb → b aaa → ǫ bbb → ǫ The grammar G0 is of Type 0 but not of Type 1, because there are productions l → r with |l| > |r|.

  • Let G1 = ({S, A, B}, {a, b}, S, P), where P consists of the following productions:

S → XT | a | b | ǫ T → BT | TA | B | A BA → AB XA → aX XB → bb B → b The grammar G1 is context sensitive (Type 1), because for all productions l → r it holds that |l| ≤ |r| (S → ǫ is allowed due to the special rule for ǫ). The grammar is not context free, because three productions have left sides which consist of more than just a single variable.

  • Let G2 = ({S}, {a, b}, S, P), where P consists of the following productions:

S → T | ǫ T → aT | Tb | a | b The grammar G2 is context free (Type 2), because it is context sensitive and additionally all left sides consist of a single variable (S → ǫ is allowed due to the special rule for ǫ). It is not regular (Type 3) because the right side of the production T → Tb is not of the required form.

  • Let G3 = ({S}, {a, b}, S, P), where P consists of the following productions:

S → aA | aB | a | bB | B | ǫ A → aA | aB | a B → bB | b The grammar G3 is regular, because it is context sensitive, context free and all production have the correct form (S → ǫ is allowed due to the special rule for ǫ). It holds that L(G0) = L(G1) = L(G2) = L(G3) = {akbn | n, m ≥ 0}. The language generated by the grammars is regular, because there is a regular grammar (G3) which generates it. (The language is of course also context free and context sensitive.) 8

slide-9
SLIDE 9

Comment on Chomsky Hierarchy for Languages (Slide 82)

The following holds:

  • The language classes are contained in one another. Every Type i language is also a Type-

(i − 1) language (for i ∈ {1, 2, 3}).

  • The language classes are strictly contained in one another. For each language class Type i

(where i ∈ {0, 1, 2}) there is a language of Type i which is not of Type (i + 1).

  • There exist languages which are not generated by any grammar. (These are addressed in

the lecture “Berechenbarkeit und Komplexit¨ at”.)

Word Problem Algorithmus for Type-1-Grammars (slide 86)

Word problem for the word aabbc and the example grammar of slide 70. Start: T = {S} 1st step: T = {S, aSBC, aBC} 2nd step: T = {S, aSBC, aBC,✭✭✭✭ ✭ aaBCBC,✭✭✭✭✭ ✭ aaSBCBC, abC} 3rd step: T = {S, aSBC, aBC, abC, abc} 4th step: T = {S, aSBC, aBC, abC, abc} No changes occur anymore, so the algorithm terminates. Since aabbc / ∈ T, it holds that aabbc / ∈ L(G).

3 Finite Automata

Transition function of DFAs (slides 92 and 93)

What does the transition function look like? δ: Z × Σ → Z That is: A pair of a state and a alphabet symbol is mapped to a state. So, δ(z1, a) = z2 means, that there is an a-labelled arrow from z1 to z2. Example: the automaton from slide 89 is “textually” represented as follows: M = ({z1, z2}, {a, b}, δ, z1, {z2}), where: δ(z1, a) = z1 δ(z2, a) = z2 δ(z1, b) = z2 δ(z2, b) = z1 Extension to words ˆ δ is the extension of δ from single symbols to words. Example (see the automaton of slide 89): ˆ δ(z1, ab) = ˆ δ(δ(z1, a), b) = ˆ δ(z1, b) = ˆ δ(δ(z1, b), ǫ) = ˆ δ(z2, ǫ) = z2 That means that the automaton will go from z1 to z2 when reading the word ab. 9

slide-10
SLIDE 10

Solution to exercises of slide 95

  • Answer of the first exercise:

L = {x ∈ Σ∗ | x contains exactly 1 b}

  • Answer to the second exercise:

M = (Z, Σ, δ, z0, E), mit Z = {1, 2, 3, 4}, Σ = {a, b}, z0 = 1, E = {4}: 1 2 3 4 a b b a a b a, b The states of a DFA always have a meaning. In this case:

  • 1. When the automaton is in state 1, it just started and has not yet read any symbol.
  • 2. When the automaton is in state 2, the first symbol was an a, and the symbol which

was read in the previous step was also an a.

  • 3. When the automaton is in state 3, the first symbol was an a, and the symbol which

was read in the last step was a b. (In this state the word can be accepted.)

  • 4. Wheb the automaton is in state 4, the first symbol was not an a. This means that the

word cannot be in the language. Therefore, no final state is reachable from this state.

DFA → regular grammar (slide 96)

  • Theorem. Every language which is accepted by a deterministic finite automaton is regular.
  • Proof. By definition a language is regular, when there is a regular grammar which generates it.

Let M = (Z, Σ, δ, z0, E) be a DFA. We have to show, that there exists a regular grammar G with L(G) = T(M). We construct the grammar G = (V, Σ, P, S), where V = Z, S = z0 and P contains the following productions:

  • When ǫ ∈ T(M), P contains a production S → ǫ. (In this case it may be necessary to

transform the grammar, such that the special rule for ǫ is satisfied.)

  • For all z1 ∈ Z and a ∈ Σ:

– When δ(z1, a) = z2, then (z1 → az2) ∈ P. – When also z2 ∈ E, then (z1 → a) ∈ P. It remain to show that T(M) = L(G). There are two directions:

  • Assume a1 . . . an ∈ T(M).

Then there are states q0, . . . , qn, such that q0 = z0, qn ∈ E and qi = δ(qi−1, ai), f¨ ur i ∈ {1, . . . , n}. By construction it holds that (qi−1 → aiqi) ∈ P, for i ∈ {1, . . . , n}. Since it additionally holds that qn ∈ E, we have (qn−1 → an) ∈ P. Therefore z0 ⇒ a1q1 ⇒ a1a2q2 ⇒ · · · ⇒ a1 . . . an−1qn−1 ⇒ a1 . . . an is a derivation in G, and a1 . . . an ∈ L(G). 10

slide-11
SLIDE 11
  • Assume a1 . . . an ∈ L(G).

By definition there is a derivation z0 ⇒ a1q1 ⇒ a1a2q2 ⇒ · · · ⇒ a1 . . . an−1qn−1 ⇒ a1 . . . an. By construction this means that (qi−1 → aiqi) ∈ P, for i ∈ {1, . . . , n}. Thus (qi−1 → aiqi) ∈ P, for i ∈ {1, . . . , n}. By construction qi = δ(qi−1, ai). Because also (qn−1 → an) ∈ P, there is a qn ∈ E such that qn = δ(qn−1, an). From this, it follows that a1 . . . an ∈ T(M).

  • Remark: The above proof is constructive. That means that it contains an algorithm to transform

a DFA into an equivalent regular grammar. Example for the transformation When we apply the algorithm from the proof to the DFA from slide 89, we obtain the following regular grammar. z1 → az1 | bz2 | b z2 → az2 | bz1 | a

Transition function of NFAs (slides 100 and 101)

What does the transition function look like? δ: Z × Σ → P(Z) That means that each pair of a state and an alphabet symbol is mapped to a set of states. Example: the automaton on slide 99 is repesented “textually” as follows: M = ({z0, z1, z2, z3}, {a, b}, δ, {z0, z3}, {z3}), where: δ(z0, a) = {z1} δ(z1, a) = ∅ δ(z2, a) = {z3} δ(z3, a) = {z0} δ(z0, b) = {z0, z2} δ(z1, b) = {z3} δ(z2, b) = ∅ δ(z3, b) = ∅ Extension to words Like for DFA, ˆ δ is the extension of δ from alphabet symbols to words. Let Z′ = {z1, . . . , zn}. Then

  • z∈Z′

ˆ δ(δ(z, a), x) means the same as. ˆ δ(δ(z1, a), x) ∪ · · · ∪ ˆ δ(δ(zn, a), x) For example (for the automaton on slide 99): ˆ δ({z0}, ba) = ˆ δ(δ(z0, b), a) = ˆ δ({z0, z2}, a) = ˆ δ(δ(z0, a), ǫ) ∪ ˆ δ(δ(z2, a), ǫ) = ˆ δ({z1}, ǫ) ∪ ˆ δ({z3}, ǫ) = {z1} ∪ {z3} = {z1, z3} This means, that when reading the word ba the automaton goes from state z0 either to state z1

  • r to state z3.

11

slide-12
SLIDE 12

NFA Example (slide 103)

  • The language accepted by the NFA is:

L′ = {x | the 3rd last symbol of x is a}

  • The following NFA accepts the language L = {x | x f¨

angt mit a an und endet mit b}: z0 z1 z2 a b a, b

Subset construction (slide 106)

We apply the subset construction to transform the following NFA (over the alphabet Σ = {a, b} to a DFA: z1 z2 z3 z4 a a a b a b a Sometimes it is helpful, to use a table. We start with the set of initial states ({z1, z4}) and look which sets of states can be reached after the reading of a single alphabet symbol. The states which are reached are entered in the table: a b {z1, z4} {z2, z3} {z3} {z2, z3} {z3} For each of the state sets (which are states of the DFA) we look which states are reached when a single alphabet symbol is read. The reached states are again entered in the table (if they are not already there). In the end the table look as follows: a b {z1, z4} {z2, z3} {z3} → {z2, z3} {z2, z3} {z3} → {z3} ∅ {z3} ∅ ∅ ∅ 12

slide-13
SLIDE 13

In this way only the reachable states will be in the table. (Note, that the complete subset automa- ton would have 24 = 16 states, of which we only wrote down 4.) The states {z2, z3} and {z3} are final states of the DFA, because they contain a final state of the NFA (namely z3). The DFA which correspond to the table is the following: {z1, z4} {z2, z3} {z3} ∅ a b a b a b a, b Note that the states of the constructed DFA correspond to sets of states of the original NFA. Remark: Although using a table can sometimes be helpful, this is not necessary. It is also possible to write down the states in the graphical representation of the DFA immediately (in particular this is also allowed in the exercises and the exam). With the subset construction we transform the NFA of page 12 to the following NFA. (Unreachable state, like for example {z2, z3}, are not given.) {z0} {z1} {z1, z2} ∅ a b b a a b a, b

Regular grammar → NFA (slide 110)

Let G = ({S, U}, {a, b}, P, S) be a regular grammar, where P contains the following productions: S → aS | bU | a U → aU | bS | b We transform this grammar to an NFA M = (Z, {a, b}, δ, S, E). We obtain: Z = {S, U, X} E = {X} and δ as follows: 13

slide-14
SLIDE 14

S U X a a b b a b

4 Regular Expressions

Precedence of the operations (slide 115)

The ”‘∗”’ operator has the highest precedence, then the concatenation operator, and the ”‘|”’

  • perator has the lowest precedence.

That means: ab | cd∗ ≡ ((ab) | (c(d∗)))

Examples of regular expressions (slide 117)

Let Σ = {a, b, c}. The language of a regular expression:

  • α1 = (ab | ba)

L(α1) = L(ab) ∪ L(ba) = {ab, ba}

  • α2 = (ab | ba)∗

L(α2) = L(α1)∗ = {ab, ba}∗ = {ǫ, ab, ba, abab, abba, baab, baba, . . . , }

  • α3 = (ab | ba)c∗

L(α3) = L((ab | ba)c∗) = L(α1)L(c∗) = {ab, ba, abc, bac, abcc, bacc, abccc, baccc, . . .}

  • α4 = (a | b | c)∗abc(a | b | c)∗

L(a | b | c) = {a, b, c} L(abc) = {abc} L(α4) = {x | x enth¨ alt abc} Specifying regular expressions:

  • 1. Language of al words that start with a and end with bb.

β1 = a(a | b | c)∗bb

  • 2. Language of al words that contain an even number of a’s.

β2 =

  • (b | c)∗a(b | c)∗a(b | c)∗∗
  • der

β′

2 =

  • (b | c) | a(b | c)∗a

∗ There always (infitely) many regular expression which generate a certain regular language. 14

slide-15
SLIDE 15

Regular expression → NFA (slides 120–126)

Let α = (a | b)∗c. We want to transform α, with the help of the construction from the lecture, to an NFA which accepts the same language.

  • 1. NFAs for the atomic regular expressions:

Ma = za z′

a

a Mb = zb z′

b

b Mc = zc z′

c

c

  • 2. We construct Ma|b from Ma and Mb as follows, such that T(Ma|b) = L(a | b).

Ma|b = za z′

a

zb z′

b

a b

  • 3. From Ma|b we construct the NFA M ∗

(a|b) which accepts the same language as (a | b)∗.

M(a|b)∗ = za z′

a

zb z′

b

x a b a b a b Because ǫ / ∈ L(a | b), we have to add an additional state which is both an initial and a final state (otherwise the automaton wouldn’t accept the empty word).

  • 4. Finally, we combine the automata M(a|b)∗ and Mc to form the automaton Mα, such that

T(Mα) = L(α). M(a|b)∗ = za z′

a

zb z′

b

zc z′

c

x a b c a b a b a b Note that the final states of M(a|b)∗ are not final states in the combined automaton. The state z′

a, z′ b and x have no function anymore, because there is no final state which can be

15

slide-16
SLIDE 16

reached from them. When we apply the construction precisely, however, these states are not removed. Because M(a|b)∗ accepts the empty word, the initial states of Mc are also initial states in the combined automaton. The NFA generated by the construction is usually not the smallest NFA which accepts the correct

  • language. In this case, for example, the following NFA also accepts the same language as the

regular expression (a | b)∗c: zc z′

c

c a, b The construction is only used to show that for each regular expression there exists at least one equivalent NFA.

NFA → regular expression (slides 128–136)

Correctness of the algorithm The algorithm which transforms an NFA into a regular expression is correct, because:

  • The rules of the algorithm preserve the language of the automaton. That is, the orinial

automaton accepts the same language as the one which is generated after the application of a rule.

  • In each step the automaton becomes smaller, because either one state is removed, or the

number of state remains equal and a transition is removed. This means that the algorithm terminates. Example We transform the following NFA M into a regular expression: M = z1 z2 z3 z4 z5 a b c d b a

  • 1. First, we add a new initial and a new final state.

s e z1 z2 z3 z4 z5 ǫ ǫ a b c d b a ǫ

  • 2. By applying rule E twice, we can remove the states z1 and z2.

16

slide-17
SLIDE 17

s e z3 z4 z5 ǫa ǫb c d b a ǫ

  • 3. We remove the state z5 by applying rule E again:

s e z3 z4 ǫa ǫb cǫ dǫ b a

  • 4. We remove the state z4 with rule E:

s e z3 ǫa ǫba ǫbdǫ ba cǫ bdǫ

  • 5. With rule V we remove the parallel arrows.

s e z3 ǫa | ǫba ǫbdǫ ba cǫ | bdǫ

  • 6. Now, we remove the loop with the help of rule S:

s e z3 ǫa | ǫba ǫbdǫ (ba)∗(cǫ | bdǫ)

  • 7. Nun kann z3 einfach entfernt werden (mit der Regel E):

s e ǫbdǫ (ǫa | ǫba)(ba)∗(cǫ | bdǫ)

  • 8. Finally, we obtain the end result by applying rule V:

s e (ǫbdǫ) | (ǫa | ǫba)(ba)∗(cǫ | bdǫ)

  • 9. Optinally removing the ǫ’s result in the sought after regular expression:

bd | (a | ba)(ba∗)(c | bd) 17

slide-18
SLIDE 18

5 The Pumping Lemma

Pigeon Hole Principle (slide 144)

In the block “The pigeon hole principle for finite automata” it says m ≥ n (instead of m > n because the length of a path corresponds to the number of transitions, not the number of states. Thus, a path of length m contains m + 1 states.

Pumping Lemma (slides 148 and 150)

Because the Pumping Lemma makes no assertions about automata anymore, it can be applied (in the reverse direction) on non-regular languages (for which there are no automata). The negation

  • f

”‘There is a number n, such that all words x ∈ L with |x| ≥ n can be decomposed in x = uvw, such that |v| ≥ 1, |uv| ≤ n and for all i = 0, 1, 2, . . . : uviw ∈ L.”’ is: ”‘For all numbers n there is a word x ∈ L with |x| ≥ n, such that for all decompositions x = uvw with |v| ≥ 1 and |uv| ≤ n, there is a number i, such that uviw ∈ L.”’ In this way we can use the Pumping Lemma to show that a language is not regular. However, there are also languages that satisfy the conditions of the Pumping Lemma but are not

  • regular. This means that the Pumping Lemma can only be used to show that a language is not

regular, not to show that a language is regular.

Pumping Lemma examples

The Step n -boxes in the following proofs refer to the steps of the cooking recipe (slide 151 and 152); they do not belong to the proof text.

  • Theorem. Let Σ = {a, b}. The language L1 = {akbk ∈ Σ∗ | k ∈ N0} is not regular.

Proof.

Step 1

Let n be an arbitrary natural number.

Step 2

We choose the word x = anbn. Clearly, x ∈ L1 and |x| ≥ n.

Step 3 The word x can be decomposed in x = uvw, where |uv| ≤ n

and |v| ≥ 1, in the following way:

  • u = aℓ, v = am, w = an−(ℓ+m)bn, where m ≥ 1. (Because of |uv| ≤ n we know that u and v

both consist of only a’s.)

Step 4 For this decomposition we choose i = 2. We have

uviw = aℓ+2m+(n−(ℓ+m))bn = an+mbn. Because of m ≥ 1, it holds that uviw / ∈ L1. By the Pumping Lemma L1 is not regular.

  • Remark: we could have also chosen i = 0. It holds that

uv0w = aℓ+n−(ℓ+m)bn = an−mbn. Because m ≥ 1 it holds that uv0w / ∈ L1.

  • Theorem. Let Σ = {a, b}. The language L2 = {wwR | w ∈ Σ∗} is not regular.

18

slide-19
SLIDE 19

(Here wR represents the word w in reverse order, for example abbR = bba.) Proof.

Step 1

Let n be an arbitrary number.

Step 2

We choose the word x = anbban. Clearly x ∈ L2 and |x| ≥ n.

Step 3

We can decompose x in the following ways in x = uvw, such that |uv| ≤ n and |v| ≥ 1:

  • u = aℓ, v = am, w = an−(ℓ+m)bban, where m ≥ 1. (Because of |uv| ≤ n we know that u and

v both consist of only a’s.)

Step 4 For this decomposition we choose i = 2. We have

uviw = aℓ+2m+(n−(ℓ+m))bban = an+mbban. Because of m ≥ 1 it holds that uviw / ∈ L2. By the Pumping Lemma L1 is not regular.

  • Theorem. Let Σ = {a, b, c}. The language L3 = {akbℓcm ∈| k ≥ 1, ℓ ≤ m} is not regular.

(The language L3 consists of words z ∈ L(a∗b∗c∗) that contain at least one a and in which the number of b’s is smaller than or equal to the number of c’s.) Note, that the variables which are used inside the set description of languages are in general local

  • variables. In particular, the ℓ and m which are used above are not the same as the ℓ and m used

below. Proof.

Step 1

Let n be an arbitrary number.

Step 2

We choose the word x = abncn. Clearly x ∈ L2 and |x| ≥ n.

Step 3

We can decompose x in the following ways in x = uvw, such that |uv| ≤ n and |v| ≥ 1:

  • 1. u = ǫ, v = abℓ, w = bn−ℓcn, wobei m ≥ 1.

(Note that when ℓ = 0 the subword v consists of only the a. In this case the condition that |v| ≥ 0 still holds.)

  • 2. u = abℓ, v = bm, w = brcn, wobei ℓ + m + r = n und m ≥ 1.

(De difference between the two cases, is whether the a lies in v or not.)

Step 4 For both decom-

positions, there is an i such that uviw / ∈ L3:

  • 1. Let i = 0. Since u = v0 = ǫ, gilt uviw = w = bn−ℓcn. It could be that ℓ = 0. Thus the

number of b’s and c’s do not have to be different. However, the word does not start with an a, and therefore uviw / ∈ L3.

  • 2. Sei i = 2: Not it holds that uviw = abℓ+2m+rcn. Because n = ℓ + m + r and m ≥ 1 it holds

that ℓ + 2m + r > n. Thus uviw / ∈ L3. By the Pumping Lemma L3 is not regular.

  • Theorem. Let Σ = {a}. The language L4 = {a2k | k ∈ N} is not regular. (See slide 153.)
  • Proof. Let n be an arbitrary number.

Step 2

We choose the word x = a2n. Clearly x ∈ L2 and |x| ≥ n.

Step 3

We can decompose x in the following ways in x = uvw, such that |uv| ≤ n and |v| ≥ 1:

  • u = ap, v = aq, w = a2n−p−q, where p + q ≤ n und q ≥ 1.

Step 4 We choose i = 2; then uviw = a2n+q. Because 2n > n (for all n), it holds that p + q < 2n

and therefore that 0 < q < 2n. This means, that 2n < 2n + q < 2n + 2n = 2 · 2n = 2n+1. From this it follows that 2n + q is not a power of 2, and thus that uviw = uv2w / ∈ L4. By the Pumping Lemma L4 is not regular.

  • 19
slide-20
SLIDE 20

Hints for using the Pumping Lemma The most important part of a proof that uses the Pumping Lemma is the choice of the word with a length ≥ n. Some hints about choosing this word:

  • The word must be longer than n. There is, however, no upper limit. It is not bad if the

word is much longer than n.

  • Choose a word that is as simple as possible. The only condition is that the word is in the

language (and long enough).

  • Because of the condition, |uv| ≤ n, the “pumping” occurs only in the first part of the word.

Additionally to the above hint, choose a word in which the structure of the first n letters is a simple as possible. (For example, in two of the above example the word was chosen in such a way that the first n letters were all the same.)

6 Minimal automata and Myhill-Nerode aquivalence

Equivalence classes (slides 155–159)

Example 1. Let the set M = {a, b, c, d} and the relation R ⊆ M × M =

  • (a, a), (a, b), (a, c), (b, a), (b, b), (b, c), (c, a), (c, b), (c, c), (d, d)
  • be given. This relation can be graphically represented as follows:

a b c d The relation R is an equivalence ation because it is reflexive, symmetrical and transitive (see slide 2Equivalence Relations and Minimal AutomataDoc-Start).

  • Reflexive. For all elements x of M it holds that (x, x) ∈ R.

For example: (a, a) ∈ R and (d, d) ∈ R.

  • Symmetric. For all pairs of x and y, such that x ∈ M, y ∈ M and (x, y) ∈ R, it also holds

that (y, x) ∈ R. For example: (a, b) ∈ R and also (b, a) ∈ R.

  • Transitive. For all x, y, z such that x ∈ M, y ∈ M, z ∈ M, (x, y) ∈ R and (y, z) ∈ R, also

(x, z) ∈ R. For example: (a, b) ∈ R and (b, c) ∈ R. It must hold that(a, c) ∈ R, which it does. (a, b) ∈ R and (b, a) ∈ R. It must holds that (a, a) ∈ R, which it does. The equivalence classes of the relation are: [a]R = {a, b, c} [b]R = {a, b, c} [c]R = {a, b, c} [d]R = {d} 20

slide-21
SLIDE 21

Because [a]R = [b]R = [c]R the relation R has two equivalence classes, namely {a, b, c} and {d}. Example 2. Let the set M = {a, b, c, d, e} and the relation R ⊆ M × M =

  • (a, a), (a, b), (b, a), (b, b), (c, c), (d, d), (d, e), (e, d), (e, e)
  • .

be given. This relation can be graphically represented as follows: a b c d e The relation R is an equivalence relation, because it is reflexive, symmetric and transitive. The equivalence classes of R are: [a]R = {a, b} [c]R = {c} [d]R = {d, e} [b]R = {a, b} [e]R = {d, e} Because [a]R = [b]R and [d]R = [e]R, this relation has three equivalence classes, namely {a, b}, {c} and {d, e}. Example 3. Let the alphabet Σ = {a, b} and the relation L ⊆ Σ∗ × Σ∗ on Σ∗ be given, which is defined as follows: (x, y) ∈ L iff |x| = |y|, where |w| is the length of the word w. That means, two words are in the relation L when they have the same length. Now it holds for a word x that [x]L = {y | y has the same length as x}. Because there are infinitely many possible lengths, this relation has infinitely many equivalence classes.

Acceptance equivalence example (slide 160)

We consider the following DFA: 1 2 3 4 5 6 b a a b a b b a b a a, b Observation: for the states 4, 5 it holds that:

  • With a word which contains a we always reach a final state.

21

slide-22
SLIDE 22
  • With a word which does not contain an a we alway reach a non-final state (4 or 5).

From this it follows that 4 und 5 are acceptance equivalent and can be merged. The same holds for states 2 and 3:

  • With a word which starts with b and contains an a, we always reach a final state.
  • With a word which doesn’t start with b or doesn’t contain an a, we always a non-final state.

Also the states 2 and 3 are acceptance equivalent. This means, that the states 4/5 and 2/3 can be merged. The result is the following smaller DFA: 1 2/3 4/5 6 a, b a b b a a, b No more states can be merged, so this automaton is actually the smallest one.

Acceptance equivalence and Myhill–Nerode equivalence (slide 163)

Each x ∈ Σ∗ corresponds to a unique state in a deterministic finite automaton, namely z = ˆ δ(z0, x). Therefore the definition of acceptance equivalence can be extanded to words from Σ∗ and languages (instead of automata). This is called the Myhill–Nerode equivalence.

Examples for Myhill-Nerode equivalence (slides 164, 168)

For L = {akbk | k ∈ N} it holds that:

  • Let x = a4b3 and y = a3b2. Then x ≡L y, because xz ∈ L iff z = b iff yz ∈ L.
  • Let x = a2b2 and y = a3b2. Then x ≡L y, because for z = ǫ we have xz = x = a2b2 ∈ L but

yz = y = a3b2 / ∈ L.

  • Let x = a4b2 und y = a3b2. Then x ≡L y, because for z = b we have xz = a4b2b = a4b3 /

∈ L but yz = a3b2b = a3b3 ∈ L. Specifying Myhill–Nerode equivalence classes:

  • L1 = {w ∈ {a, b}∗ | #a(w) gerade}

This language has the following Myhill–Nerode equivalence classes: – [ǫ] = {w ∈ {a, b}∗ | #a(w) gerade} = L1 (equivalence class of ǫ) – [a] = {w ∈ {a, b}∗ | #a(w) ungerade} = {a, b}∗ \ L1 (equivalence class of a) Example: ǫ and aa are equivalent, because – When we add a word with an even number of a’s to it, the result is in the language in both cases. 22

slide-23
SLIDE 23

– When we add a word with an odd number of a’s to it, the result is not in the language is both cases. Clearly, every word is either equivalent to ǫ or to a. The Myhill–Nerode equivalence classes correspond to the states of an automaton which accepts the language: z1 z2 b b a a Here z1 corresponds to the equivalence class [ǫ] and z2 to the equivalence class [a].

  • L2 = {w ∈ {a, b, c}∗ | w does not contain the subword abc}

This language has the following equivalence classes: – [ǫ] = {w ∈ {a, b, c}∗ | w doesn’t end on a or ab and doesn’t contain abc} – [a] = {w ∈ {a, b, c}∗ | w ends on a and doesn’t contain abc} – [ab] = {w ∈ {a, b, c}∗ | w ends on ab and doesn’t contain abc} – [abc] = {w ∈ {a, b, c}∗ | w contains abc} The words a and ab are not equivalent, because when we add a c to them, ac is still in the language, but abc isn’t. The Myhill–Nerode equivalence classes correspond to the states of an automaton which accepts the language: z0 z1 z2 zE a b, c a b c a b c a, b, c Here: – z0 corresponds to [ǫ] – z1 corresponds to [a] – z2 corresponds to [ab] und – z3 corresponds to [abc].

Proving Non-Regularity with the Theorem of Myhill–Nerode (slide 170)

  • Theorem. Let Σ = {a, b}. The language L1 = {akbk | k ≥ 0} is not regular.
  • Proof. We consider the words ǫ, a, aa, aaa, . . . , ai, . . . (for i ≥ 0). It holds that

ai ≡L1 aj f¨ ur i = j. 23

slide-24
SLIDE 24

because aibi ∈ L1 but ajbi / ∈ L1. This means, that L1 has infinitely many Myhill–Nerode equivalence classes, and therefore, that L1 is not regular.

  • Theorem. Let Σ = {a, b, c}. The language L2 = {anbmcm | n, m ≤ 1} ∪ {bmcn | n, m ≤ 1} is not

regular. Proof. Let xp be the word abp. Then xi ≡L2 xj when i = j, because for z = bi we have xiz = abici ∈ L2 but xjz = abjci / ∈ L2. This means that [xi] = [xj] (for i = j), and therefore L2 has infinitely many Myhill–Nerode equivalence classes. Thus, L2 is not regular. However, note that L2 satisfies the condition of the pumping lemma. Let n be the constant from the pumping lemma, and let x ∈ L2 be a word of the language with |x| > n. There are two cases:

  • x = apbmcm f¨

ur p, m ≥ 1. In this case we take the decomposition u = ǫ, v = a, w = ap−1bmcm. But then uviw ∈ L2 for all i ∈ N (because the number of a’s at the start of the word is arbitrary).

  • x = bmcp, f¨

ur p, m ≥ 1. Now, we take the decomposition u = ǫ, v = b, w = bm−1cp. Now jetzt gilt dass uviw ∈ L2 for all i ∈ N (because without an a at the start of the word the number of b’s and c’s is arbitrary.

7 Closure Properties and Decision Procedures

Construction of the Complement Automaton (slide 185)

The construction of the complement automaton only works for DFAs. To apply the construction

  • n NFAs, we first have to transform the NFA into a DFA. This can lead to an exponential growth
  • f the automaton.

Example Cross Product Construction (slide 188)

In the cross product construction two automata are operated in parallel. The states of the new automaton specify, in which state both original automata are; that means, that the states of the new automaton are pairs of states of the original automata. For DFAs We consider the following DFAs over the alphabet Σ = {a, b}: M1 = z1 z2 b b a a M2 = y1 y2 b a a b 24

slide-25
SLIDE 25

The automaton M (such that T(M) = T(M1)∩T(M2)), generated by the cross product construc- tion, looks as follows: (z1, y1) (z1, y2) (z2, y1) (z2, y2) a b a b a b a b For NFAs Consider the following NFAs over the alphabet Σ = {a, b}: N1 = z1 z2 a a a, b N2 = y1 y2 b a, b The NFA generated by the cross product construction is the following: (z1, y1) (z1, y2) (z2, y1) (z2, y2) a a a, b b

8 Program verification: Mutual Exclusion

Mutual Exclusion: First Attempt (slide 211)

  • Meaning of the states 1, 2, 3, 4, 5: these correspond to the lines of the program number

correspondingly

  • Meaning of the loops labelled with ∆i: because we will construct the cross product later,

to synchronize the automata, transitions of other automata, which have nothing to do with process i, cannot be left out. They are simply “monitored” and have no influence on the transitions.

  • all states are end states. The program runs forever (because of the while true do and the

automaton accepts all finite prefixes of the infinite runs. 25

slide-26
SLIDE 26

Attempt 1: Test with Grail

For the tool Grail we use the encoding from slide 214. We use the Grail-tools as follows: $ fmcross p1.aut < p2.aut > psynch.aut $ fmcross f.aut < psynch.aut > sys.aut $ fmcment wa.aut > wa-cment.aut $ fmcross sys.aut < wa-cment.aut > errors.aut $ fmenum errors.aut DdAXax DdAaXx DdAaxX DdaAXx . . .

Attempt 2: Test with Grail

$ fmcross p1.aut < p2.aut > psynch.aut $ fmcross f1.aut < psynch.aut > psynch1.aut $ fmcross f2.aut < psynch1.aut > sys.aut $ fmcment wa.aut > wa-cment.aut $ fmcross sys.aut < wa-cment.aut > errors.aut $ fmenum errors.aut (keine Ausgabe) There are no words in the intersection of complement of the specification and the system behaviour. Therefore, the algorithm is correct.

9 Recapitulation of Regular Languages

Recapitulation: Three proofs of non-regularity

  • Theorem. Let Σ = {a, b}. The language L = {w ∈ Σ∗ | #a(w) = #b(w)} is not regular.

Proof with the Pumping Lemma. Let n be an arbitrary natural number. We choose the word x = anbn!+n, where n! is the factorial function, that is n! = n · (n − 1) · (n − 2) · . . . · 1. Clearly, x ∈ L und |x| ≥ n. All decompositions of x in x = uvw, where |uv| ≤ n und |v| ≥ 1, have the following form:

  • u = ap, v = aq, w = an−p−qbn!+n.

Now we choose i = n!

q + 1. Note that n! has q as a product, so n! is divisable by q.

Now, uviw = ambn!+n, where m = p + i · q + n − p − q. However: p + q(n! q + 1) + n − p − q = q(n! q + 1) + n − q = n! + q + n − q = n! + n. This means, that uviw / ∈ L. Therefore, by the Pumping Lemma, L is not regular.

  • Proof with the Myhill–Nerode theorem. Let the following set of words M ⊆ Σ∗ be given: M =

{ak | k ∈ N}. Let x = ai and y = bj, where i = j. Then for z = bi it holds that xz / ∈ L and yz ∈ L. This means, that x and y are not Myhill-Nerode equivalent w.r.t. the language L. Since 26

slide-27
SLIDE 27

M contains infinitely many words, L has infinitely many Myhill–Nerode equivalence classes and therefore, by the Myhill–Nerode theorem, is not regular. Proof by closure properties. Assume, that L is regular. Because regular languages are closed under complement, also L = Σ∗ \ L = {w ∈ Σ∗ | #a(w) = #b(w)} is regular. Since {anbm | n, m ≥ 0} is a regular language (because the regular expression a∗b∗ generates it), also L ∩ {anbm | n, m ≥ 0} = {anbn | n ≥ 0} would be regular. This is, however, in contradiction with the proof of page ?? (and in the cooking recipe on slides 151–152). We have to retract the assumption that L is regular. Therefore, L is not regular.

10 Context Free Grammars

Examples for Context Free Languages

Let Σ = {a, b}. Example 1 (Slide 228, above) Give a context free grammar G1, such that L(G1) = {anbn | n ≥ 0}. Solution: G1 = (V, Σ, P, S), where V = {S, X} and P contains the following productions: S → X | ǫ X → aXb | ab Example 2 (Slide 228, below) Give a context free grammar G2, such that LG2 = {akbnambn | n, m, k ≥ 1}. Note that the two sequences of b’s have the same length, but the two sequence of a’s don’t. Solution: G2 = (V, Σ, P, S), where V = {S, A, X} and P contains the following productions: S → AX A → aA | a X → bXb | bAb Explanation:

  • A generates a-sequenzes of arbitrary length (that contain at least one a).
  • X words of the form bnambn (where n, m ≥ 1). Here, A is used to generate the central part
  • f the word.
  • S composes the words generated by A and X in order to generate words from the language.

Beispiel 3: Give a context free grammar G3, such that L(G3) = {akb | w ∈ Σ∗}, where wR denotes the reverse

  • f w.

Solution: G3 = (V, Σ, P, S), where V = {S, X} and P contains the following productions: S → ǫ | X X → aXa | bXb | aa | bb 27

slide-28
SLIDE 28

Removing ǫ-productions (slides 229 and 230)

This theorem means, that ǫ-productions may be used arbitrarily. They do not change the expres- sive power of context free grammars. Example 1: (see slide 231) Let G = (V, Σ, P, S), where V = {S, X, Y, Z}, Σ = {a, b} and P containts the following produc- tions: S → XZ X → aY b | ǫ Y → bXa | bb Z → ǫ | aSa The set of variables from which the empty words can be derived is V1 = {S, X, Z}. Everywhere where a variable from this set occurs on the right side, we also must allow the empty word. We

  • btain the following grammar:

S → XZ | X | Z | ǫ X → aY b | ǫ Y → bXa | bb | ba Z → ǫ | aSa | aa We remove all productions of the form Q → ǫ: S → XZ | X | Z X → aY b Y → bXa | bb | ba Z → aSa | aa Since ǫ is generated by the original grammar, we have to add a new start variable S′. The final grammar is G′ = (V ′, Σ, P ′, S′), where V ′ = {S, X, Y, Z, S′} and P ′ consists of the following productions: S′ → S | ǫ S → XZ | X | Z X → aY b Y → bXa | bb | ba Z → aSa | aa Example 2: Let Σ = {a, b, c}. Let the grammar G = (V, Σ, P, S), be given, where V = {S, C} and P contains the following productions: S → CaSbC C → cC | ǫ The set of variables from which the empty words can be derived is V1 = {C}. Everywhere where C occurs on the right side of a production, we also have to allow the empty words. We obtain a grammar which contains the following productions: S → CaSbC | CaSb | aSbC | aSb C → c | cC 28

slide-29
SLIDE 29

Example 3: Let Σ = {<, >}. Let the grammar G = ({S}, Σ, P, S) be given, where P consists of the following productions: S → <S>S | ǫ From S the empty word can be derived. Everywhere, where S occurs on the right side, we also have to allow the empty words to be generated. Additionally, we make sure that the new grammar generates the empty word by adding a new start variable S′. The final grammar is: G′ = ({S, S′}, Σ, P ′, S′), where P consists of the following productions: S′ → S | ǫ S → <S>S | <>S | <S> | <>

Ambiguous Grammar (slide 237)

An example of an ambiguous grammar is G = ({S}, {a, b}, P, S), where P consists of the following productions: S → a | b | SS. The following are two different syntax trees for the same word (aba): S S S S S a b a S S S S S a b a There is a non-ambiguous grammar which is equivalent to the above grammar: S → aX | bXX → aX | bX | ǫ In general this is not the case however: there exist context free languages for which no non- ambiguous grammars exist.

Examples for Chomsky -Normal Form (slides 239–244)

Example 1: Let G = ({S, A}, {a, b, c}, P, S), where P consists of the following productions: S → aAb A → S | aaSc | B | ǫ B → A | b After each step of the transformation procudure the productions are given (bold symbols denote the changes). Step 1: Removing ǫ-productions We use the procedure from slides 229/230: S → aAb | ab A → S | aaSc | B B → A | b 29

slide-30
SLIDE 30

Step 2: Removing chain productions There is a cycle A → B → A. We replace A and B by the single variable A and remove the chain productions A → B and B → A: S → aAb | ab A → S | aaSc | b Now, there is still one chain production A → S. This is removed and for each production S → w we add new production A → w: S → aAb | ab A → aAb | ab | aaSc | b Step 3: Removing alphabet symbols from the right sides When a right side contains two or more symbols the alphabet symbols are replaced by variables: S → U aAU b | U aU b A → U aAU b | U aU b | U aU aSU c | b U a → a U b → b U c → c Step 4: Splitting long right sides S → UaC | UaUb C → AU b A → UaC | UaUb | UaD | b D → U aE E → SU c Ua → a Ub → b Uc → c Example 2: Let G = (V, Σ, P, S) where V = {S, A, B, C, D, E, F}, Σ = {a, b, c} and P contains the following productions: S → ABC D → E | a A → B | DAD E → F | bb B → C | EBE F → bFc | aa C → A | FCF After each step in the transformation procudure the productions are: Step 1: Removing ǫ-productions There are no ǫ-production so the grammar doesn’t change in this step. Step 2: Removing chain productions There are two chains: the cycle A → B → C → A and the cycle-free chain D → E → F. 30

slide-31
SLIDE 31

The cycle is removed by replacing all variables of the cycle by the new variable G and removing the chain productions: S → GGG D → E | a G → DGD E → F | bb G → EGE F → bFc | aa G → FGF In order to remove the cycle-free chain, we remove the last production of the chain (E → F) and add a production E → w for each production F → w: S → GGG D → E | a G → DGD E → bFc | aa | bb G → EGE F → bFc | aa G → FGF We do the same, from the end to the start of the chain, for all productions of the chain. In this case, we only have the first production, D → E: S → GGG D → bFc | aa | bb | a G → DGD E → bFc | aa | bb G → EGE F → bFc | aa G → FGF Step 3: Removing alphabet symbols from the right sides S → GGG D → U bFU c | U aU a | U bU b | a G → DGD E → U bFU c | U aU a | U bU b G → EGE F → U bFU c | U aU a G → FGF U a → a U b → b U c → c Step 4: Splitting long right sides S → GW D → UbV | UaUa | UbUb | a W → GG E → UbV | UaUa | UbUb G → DX F → UbV | UaUa X → GD V → FU c G → EY Y → GE Ua → a G → FZ Ub → b Z → GF Uc → c 31

slide-32
SLIDE 32

11 The CYK Algorithm

Notation example (slides 251/252)

Let Σ = {a, b}. We consider the grammar G = (V, Σ, P, S) where V = {S, A, B, C, D, E} and P consists of the following productions: S → AC | a C → SB A → a D → b B → DE E → c Let x = aabcbc. We have: x1,1 = a T1,1 = {S, A} x1,2 = aa T1,2 = ∅ x1,4 = aabc T1,4 = {S} x5,2 = bc T5,2 = {B}

CYK Examples

Example 1 (slide 257) The grammar is already in Chomsky normal form. We apply the CYK algorithm and the result is the following table: a a b c b c j = 1 A, F A, F B, G C B, G C j = 2 F S E, D G E, D j = 3 S S, H G j = 4 H S j = 5 S D, H j = 6 S, H The start symbol S occurs in the bottom box, so the word aabcbc is in the language of the grammar. Example 2 (slide 258) The grammar is not yet in Chomsky normal form. So we have to transform it into Chomsky normal form first. S → AB Ua → a A → UaUb | UaA′ Ub → b A′ → AUb Uc → c B → c | UcB When we apply the CYK algorithm to the word aaabbbcc, we obtain the following table: 32

slide-33
SLIDE 33

a a a b b b c c j = 1 Ua Ua Ua Ub Ub Ub B, Uc B, Uc j = 2 A B j = 3 A′ j = 4 A j = 5 A′ j = 6 A j = 7 S j = 8 S The start symbol S appears in the bottom box, which means that the word aaabbbcc is generated by the grammar.

12 The Pumping Lemma for Context Free Languages

Example for the Pumping Mechanism (slide 264)

Let Σ = {<, >}. We consider the grammar G = (V, Σ, P, S), where V = {S, T, O, C} and P consists of the following productions: S → OT | SS | OC T → SC O → < C → > The grammar G is in Chomsky normal form and generates the language of correctly parenthesized expressions, for example <<><>> ∈ L(G) but <<> / ∈ L(G).) Consider the following syntax tree for the word <<><>>. S O < T S S O < C > S O < C > C > There are two equal variables on the same path. The two S-nodes, which are marked in the above figure, produce the following decomposition of the word: u = <, v = ǫ, w = <>, x = <>, y = >. We can now “pump up” and “pump down” the word, by replacing the subtree of the top S with the subtree of the bottom S and vice versa. 33

slide-34
SLIDE 34

S O < T S S S O < C > S O < C > S O < C > C > S O < T S O < C > C > The left syntax tree corresponds to the word uv2wx2y = <<><><>>, the right one to the word uv0wx0y = <<>>. Note, that x0 = ǫ, also if |x| > 0.

Pumping Lemma Examples (slide 269)

  • Theorem. The language L1 = {ambm2 | m ≥ 1} is not context free.
  • Proof. Let n be an arbitrary natural number. We choose the word z = anbn2. Clearly, z ∈ L1 and

|z| ≥ n. The word z can be decomposed in z = uvwxy in such a way that |vwx| ≤ n and |vx| ≥ 1 in the following ways:

  • 1. vwx consists only of a’s:

u = ak, v = aℓ, w = ap, x = aq, y = arbn2, where n = k + ℓ + p + q + r und ℓ + q ≥ 1.

  • 2. vwx consists only of b’s:

u = anbk, v = bℓ, w = bp, x = bq, y = br, where n2 = k + ℓ + p + q + r and ℓ + q ≥ 1.

  • 3. v consists of both a’s and b’s:

u = an−k, v = akbℓ, w = bp, x = bq, y = br, where k > 0, ℓ > 0 (because v consists of both a’s and b’s) and n2 = ℓ + p + q + r.

  • 4. x consists of both a’s and b’s:

u = ak, v = aℓ, w = ap, x = aqbr, y = bn2−r, where p > 0, q > 0 (because x consists of both a’s and b’s) and n = k + ℓ + p + q.

  • 5. v consists only of a’s and x consists only of b’s:

u = ak, v = aℓ, w = apbq, x = br, y = bh, where n = k + ℓ + p, n2 = q + r + h and ℓ + r ≥ 1. In all cases we can choose an i ≥ 0 such that uviwxiy / ∈ L1.

  • 1. We choose i = 2. Then uviwxiy = an+ℓ+qbn2.

Since ℓ + q ≥ 1, it holds that n + ℓ + q = n and therefore uviwxiy / ∈ L1.

  • 2. We choose i = 2. Then uviwxiy = anbn2+ℓ+q.

Since ℓ + q ≥ 1, it holds that n + ℓ + q = n and therefore uviwxiy / ∈ L1.

  • 3. We choose i = 2. Then uviwxiy = anbℓakbn2.

Since k, ℓ > 0 in this word a’s and b’s occur in the wrong order, and therefore uviwxiy / ∈ L1. 34

slide-35
SLIDE 35
  • 4. We choose i = 2. Then uviwxiy = anbraqbn2.

Since p, q > 0 in this word a’s and b’s occur in the wrong order, and therefore uviwxiy / ∈ L1.

  • 5. We show that uv2wx2y /

∈ L1. uv2wx2y = an+ℓbn2+r. So we must show, that (n + ℓ)2 = n2 + 2nℓ + ℓ2 = n2 + r. This means, that r = 2nℓ + ℓ2. When ℓ = 0, then r = 0 because of the condition that |vx| ≥ 1 (ℓ + r ≥ 1), and therefore r = 2nℓ + ℓ2. When ℓ > 0, then 2nℓ + ℓ2 > n. Because of the condition that |vwx| ≤ n, then it also must hold that r = 2nℓ + ℓ2. In both cases it holds that r = 2nℓ+ℓ2. Therefore we can conclude, that dass uv2wx2y / ∈ L1. Because there existst such an index for each decomposition, by the pumping lemma L1 is not context free.

  • Theorem. The language L2 = {ambmcm | m > 0} is not context free.
  • Proof. Let n be an arbitrary number. We choose the word z = anbncn. Clearly, z ∈ L und |z| > n.

The word z can be decomposed in z = uvwxy, in such a way that |vwx| ≤ n and |vx| ≥ 1, in the following ways:

  • 1. vwx is completely in the a-part of z:

u = ak, v = aℓ, w = ap, x = aq, y = arbncn, where k + ℓ + p + q + r = n and ℓ + q ≥ 1.

  • 2. x conists of a’s as well as b’s:

u = ak, v = aℓ, w = ap, x = aqbr, y = bn−rcn, where k + ℓ + p + q = n and ℓ + q + r ≥ 1.

  • 3. v consists only of a’s and x consists only of b’s:

u = ak, v = aℓ, w = an−k−ℓbn−p−q, x = bp, y = bq, where ℓ + p ≥ 1.

  • 4. v consists of a’s as well as b’s:

u = an−k, v = akbℓ, w = bp, x = bq, y = brcn, where ℓ + p + q + r = n and k + ℓ + q ≥ 1.

  • 5. vwx consists only of b’s:

u = anbk, v = bℓ, w = bp, x = bq, y = brcn, where k + ℓ + p + q + r = n and ℓ + q ≥ 1.

  • 6. x consists of both b’s and c’s:

u = anbk, v = bℓ, w = bp, x = bqcr, y = cn−r, where k + ℓ + p + q = n and ℓ + q + r ≥ 1.

  • 7. v consists only of b’s and x consists only of c’s:

u = anbk, v = bℓ, w = bn−k−ℓcn−p−q, x = cp, y = cq, where ℓ + p ≥ 1.

  • 8. v consists of b’s as well as c’s:

u = anbn−k, v = bkcℓ, w = cp, x = cq, y = cr, where ℓ + p + q + r = n und k + ℓ + q ≥ 1.

  • 9. uvw consists only of c’s:

u = anbnck, v = cℓ, w = cp, x = cq, y = cr, where k + ℓ + p + q + r = n und ℓ + q ≥ 1. Because of the condition |vwx| ≤ n, there is always at least on symbol of the alphabet (a, b or c) which is not in uvw and thus does not participate in the pumping of the word. This means that the pumping changes the number of at least one alphabet symbol while it leave the number of at least one other symbol as is. Therefore the result cannot be in the language. For example, we can choose i = 0 and show that uv0wx0y / ∈ L in case 2: 35

slide-36
SLIDE 36
  • 2. uv0wx0y = ak+pbn−rcn

This word lies in the language, when k + p = n − r = n. Because of ℓ + q + r ≥ 1 we know, that ℓ ≥ 1 or q ≥ 1 or r ≥ 1 (or more of the above). When ℓ ≥ 1 or q ≥ 1, it follows from k + ℓ + p + q = n, that k + p = n. When r ≥ 1 it follows from k + ℓ + p + q = n, that n − r = n. (In the other cases, we can also choose i = 0.) Because there existst such an index for each decomposition, by the pumping lemma L1 is not context free.

  • 13

Kellerautomaten

Why an Automaton Model for Context Free Languages? (slide 270)

Some construction and procedure are more efficient when we apply then to automata instead of

  • grammars. For example:
  • the word problem (we will see that the word problem can sometimes be solved more efficiently

than in cubic time)

  • closure properties (closure of context free languages under intersection with regular languages

is for example more easily shown with the help of push-down automata)

To slide 271

A finite automaton cannot recognize this language, because it cannot remember words a1a2 . . . an

  • f arbitrary length. In order to check whether the second part of the word corresponds with the

first part, it would however have to remember arbitrary long words).

Meaning of the Transition Function of a Push-Down Automaton (slides 277– 278)

  • Σ ∪ {ǫ} is the alphabet together with an additional symbol ǫ.
  • Z × (Σ ∪ {ǫ}) × Γ is the set of 3-tuples which consist of a state, an alphabet symbol or ǫ,

and of a stack symbol.

  • Z × Γ∗ is the set of all pairs which consist of a state and a sequence of stack symbols.
  • This means, δ is a function, which takes a state (the active state), an alphabet symbol or ǫ

(the input symbol, where ǫ represents the case where nothing is read) and a stack symbol (the top stack symbol) as input, and produces a pair of state (the successor state) and a sequence of stack symbols (the stack symbols with which the top stack symbol is replaced) as output.

Notation of the Transition Function

Let M = (Z, Σ, Γ, δ, z0, #) be a push-down automaton. For z ∈ Z, a ∈ Σ ∪ {ǫ} and A ∈ Γ, δ(z, a, A) is a finite set of pairs. 36

slide-37
SLIDE 37

For example, let Z = {z0, z1}, Σ = {a, b} and Γ = {A, #}. Then δ could be defined as follows: δ(z0, a, #) = {(z0, #), (z0, A#)} δ(z1, a, #) = ∅ δ(z0, a, A) = {(z0, A), (z0, AA)} δ(z1, a, A) = ∅ δ(z0, b, #) = ∅ δ(z1, b, #) = ∅ δ(z0, b, A) = {(z1, ǫ)} δ(z1, b, A) = {(z1, ǫ)} δ(z0, ǫ, #) = ∅ δ(z1, ǫ, #) = {(z1, ǫ)} δ(z0, ǫ, A) = ∅ δ(z1, ǫ, A) = ∅ Note, that δ is a total function, and therefore it must be defined for all combination of state, alphabet symbol (oder ǫ) and stack symbol. For clearity, we will also denote the transition function as follows: δ(z0, a, #) ∋ (z0, #) δ(z1, b, A) ∋ (z1, ǫ) δ(z0, a, #) ∋ (z0, A#) δ(z1, ǫ, #) ∋ (z1, ǫ) δ(z0, a, A) ∋ (z0, A) δ(z0, a, A) ∋ (z0, AA) δ(z0, b, A) ∋ (z1, ǫ)

  • r even shorter as follows:

(z0, a, #) → (z0, #) (z1, b, A) → (z1, ǫ) (z0, a, #) → (z0, A#) (z1, ǫ, #) → (z1, ǫ) (z0, a, A) → (z0, A) (z0, a, A) → (z0, AA) (z0, b, A) → (z1, ǫ). Hereby we will assume that when δ(z, a, A) ∋ (z′, γ) or (z, a, A) → (z′, γ) is not specified (for z ∈ Z, a ∈ Σ ∪ {ǫ}, γ ∈ Γ∗), then it is meant that (z′, γ) / ∈ δ(z, a, A). Sometimes we will also consider a graphical representation of push-down automata. A transition (z, a, A) ∋ (z′, B1 . . . Bn) is then drawn as follows: z z′

a, A → B1 . . . Bn

Push-Down Automaton Example 1 (slide 287)

Give a push-down automaton M for the language L(M) = {w$wR | w ∈ {a, b}∗} Solution: M = ({z1, z2}, {a, b, $}, {#, A, B}, δ, z1, #), where δ is defined a follows (we write (z, a, A) → (z′, x) for (z′, x) ∈ δ(z, a, A)). (z1, a, #) → (z1, A#) (z1, a, A) → (z1, AA) (z1, a, B) → (z1, AB) (z1, b, #) → (z1, B#) (z1, b, A) → (z1, BA) (z1, b, B) → (z1, BB) (z1, $, #) → (z2, #) (z1, $, A) → (z2, A) (z1, $, B) → (z2, B) (z2, a, A) → (z2, ǫ) (z2, b, B) → (z2, ǫ) (z2, ǫ, #) → (z2, ǫ) Graphically, the push-down automaton looks as follows: 37

slide-38
SLIDE 38

z1 z2

$, #→#, $, A→A, $, B→B a, #→A#, b, #→B#, a, A→AA, b, A→BA, a, B→AB, b, B→BB a, A→ǫ, b, B→ǫ, ǫ, #→ǫ

When an arrow has two labels (for example ”‘$, #→#, $, A→A, $, B→B”’) this means, that it represents two transitions (we also did this with finite automata).

To slide 273

⊢∗ denotes the reflexiv and transitive closure of ⊢. That means, that (z, w, γ) ⊢∗ (z′, w′, γ′) when (z′, w′, γ′) can be reached from (z, w, γ) in zero, one or more steps. In other words, (z, w, γ) ⊢∗ (z′, w′, γ′) if and only if there are configurations k1, . . . , kn such that k1 = (z, w, γ), kn = (z′, w′, γ′) and ki ⊢ ki+1, for 0 < i < n.

Example Push-Down Automata

Example 1. (slide 291) Exercise: Let Σ = {a, b} be the alphabet. Give a push-down automaton which accepts the following language: L = {wwR | w ∈ {a, b}∗}. Solution: M = ({z1, z2}, {a, b}, {#, A, B}, δ, z1, #), where δ is defined as follows: (z1, a, #) → (z1, A#) (z1, a, A) → (z1, AA) (z1, a, B) → (z1, AB) (z1, b, #) → (z1, B#) (z1, b, A) → (z1, BA) (z1, b, B) → (z1, BB) (z1, ǫ, #) → (z2, #) (z1, ǫ, A) → (z2, A) (z1, ǫ, B) → (z2, B) (z2, a, A) → (z2, ǫ) (z2, b, B) → (z2, ǫ) (z2, ǫ, #) → (z2, ǫ) A run of this push-down automaton, in which the automaton ends with an empty stack, is the following: (z1, aabbaa, #) ⊢ (z1, abbaa, A#) ⊢ (z1, bbaa, AA#) ⊢ (z1, baa, BAA#) ⊢ (z2, baa, BAA#) ⊢ (z2, aa, AA#) ⊢ (z2, a, A#) ⊢ (z2, ǫ, #) ⊢ (z2, ǫ, ǫ) However, there are also runs that do not end in an empty stack: (z1, aabbaa, #) ⊢ (z1, abbaa, A#) ⊢ (z1, bbaa, AA#) ⊢ (z1, baa, BAA#) ⊢ (z1, aa, BBAA#) ⊢ (z1, a, ABBAA#) ⊢ (z2, a, ABBAA#) ⊢ (z2, ǫ, BBAA#) A word is accepted by the push-down automaton when there exists at least one run that ends with the empty stack (and read the entire input word). Thus, in this case aabbaa is accepted. Example 2. (slide 292, above) Exercise: Let Σ = {a, b} be the alphabet. Give a push-down automaton which accepts the following language: L1 = {anbm | 1 ≤ n ≤ m} Solution: We need to states: 38

slide-39
SLIDE 39
  • z1: Reading the a’s (Initial state)

In this state the automaton reads in the a’s and stores the number of a’s on the stack (in unary representation, that is, the number of symbols of the stack is the numer of a’s read).

  • z2: Reading the b’s

In this state the automaton reads in the b’s while deconstructing the stack. For each symbol

  • n the stack one b must be read. Because it only required that the number of b is not smaller

than the number of a’s, it is also possible to read a b without removing a symbol from the stack. Formally, the push-down automaton is defined as follows: M1 = (Z, Σ, Γ, δ, z1, #), where Z = {z1, z2}, Σ = {a, b}, Γ = {A, #} and δ is defined by (z1, a, #) → (z1, A#) when the input symbol is an a (z1, a, A) → (z1, AA) push A on the stack (z1, ǫ, #) → (z2, #)

  • r jump non-deterministically to

(z1, ǫ, A) → (z2, A) state z2 (z2, b, #) → (z2, ǫ) read a b, and optionally pop (z2, b, #) → (z2, #) the top stack symbol (A or #) (z2, b, A) → (z2, ǫ) (a a cannot be read in z2) (z2, b, A) → (z2, A) (z2, ǫ, #) → (z2, ǫ) make sure that the automaton can stop when n ≤ m Example 3. (slide 292, below) Exercise: Let Σ = {a, b, $} be the alphabet. Give a push-down automaton which accepts the following language: L2 = {x$y | x ∈ {a, b}∗, y ∈ {a, b}∗, x = y} Solution: The language consists of words of the form x$y where x and y are not equal. Since we can only observe the top symbol on the stack, it is not sufficient to store the first half of the word

  • n the stack: it is then on the stack in reversed order. So we must think of something else to find
  • ut if the first half is different from the second. There are two possible reasons why two words x

and y can be different: first, x and y can have a different length, and second, the is a position i such that the ith symbol of x is different from the ith symbol of y. We now build a push-down automaton which non-deterministically choose a position to check for inequality, or check whether the lengths are different. In the first case a position is stored on the stack, in the second case the length of x. Our automaton has the following states:

  • z1: Choose position of x or length

When an a is read, the automaton can non-deterministically decide to jump to z3 to check whether the corresponding position of y is different (that is, a b). When an b is read, the automaton can non-deterministically decide to jump to z5 to check whether the corresponding position of y is different (that is, an a). In both cases it is also possible to stay in z1 and to push a 1 on the stack, so that a different position or the length can be chosen. When $ is read, the automaton jumps to z2 to check the length. 39

slide-40
SLIDE 40
  • z2: Check length of y

This state check, whether y has a different number of symbols as there symbols on the stack. A word is accepted, as soon as either the stack is empty but the input isn’t (in this case the automaton jumps to z7 to read the rest of the input and accept) or the input is empty but the stack isn’t (in this case the automaton jumps to z8 to empty the stack).

  • z3: Read rest of x 1

Together with z4 this state checks, whether the corresponding position of y contains a b. In particular, this state reads the rest of x, without changing the stack, and then jumps to z4.

  • z4: Check position 1

When there are still symbols on the stack, a symbol is read from the input and one symbol is popped from the stack. Otherwise, b is read and the automaton changes to accept state z7. (There are no transitions in which an a is read.)

  • z5 and z6: anologous to z3 und z4, but it is checked whether the corresponding position

contains an a.

  • z7: Accept state 1

The rest of the input is read and after that the stack bottom symbol is removed from the stack.

  • z8: Accept state 2

The stack is empties, but no part of the input is read. Formally, the automaton M2 is defined as follows: M2 = (Z, Σ, Γ, δ, z1, #) where Z = {z1, z2, z3, z4, z5, z6, z7}, Σ = {a, b, $}, Γ = {1, #} and δ contains the following transi- 40

slide-41
SLIDE 41

tions: Zustand z1 Zustand z2 (z1, a, #) → (z1, 1#) (z2, a, 1) → (z2, ǫ) (z1, a, 1) → (z1, 11) (z2, b, 1) → (z2, ǫ) (z1, a, #) → (z3, #) (z2, a, #) → (z7, #) (z1, a, 1) → (z3, 1) (z2, b, #) → (z7, #) (z1, b, #) → (z1, 1#) (z2, ǫ, 1) → (z8, ǫ) (z1, b, 1) → (z1, 11) (z1, b, #) → (z5, #) (z1, b, 1) → (z5, 1) (z1, $, #) → (z2, #) (z1, $, 1) → (z2, 1) Zustand z3 Zustand z5 (z3, a, #) → (z3, #) (z5, a, #) → (z5, #) (z3, a, 1) → (z3, 1) (z5, a, 1) → (z5, 1) (z3, b, #) → (z3, #) (z5, b, #) → (z5, #) (z3, b, 1) → (z3, 1) (z5, b, 1) → (z5, 1) (z3, $, #) → (z4, #) (z5, $, #) → (z6, #) (z3, $, 1) → (z4, 1) (z5, $, 1) → (z6, 1) Zustand z4 Zustand z6 (z4, a, 1) → (z4, ǫ) (z6, a, 1) → (z6, ǫ) (z4, b, 1) → (z4, ǫ) (z6, b, 1) → (z6, ǫ) (z4, b, #) → (z7, #) (z6, a, #) → (z7, #) Zustand z7 Zustand z8 (z7, a, #) → (z7, #) (z8, ǫ, 1) → (z8, ǫ) (z7, b, #) → (z7, #) (z8, ǫ, #) → (z8, ǫ) (z7, ǫ, #) → (z7, ǫ)

Context Free Grammar → PDA (slide 295/296)

In a transition between configurations, a push-down automaton can replace a single symbol on the stack by a sequence of symbol. Thus, we can use the stack to simulate the derivation of a word in the grammar. Since we consider context free grammars, terminal symbols cannot be replaced by anything else anymore (only single variables can be replaced). This means, that terminal symbols that are left

  • f the first variable (which are above the first variable on the stack) and correspond to the input

can be removed from the stack, so that a variable becomes the top stack symbol again. Example of slide 296: When we apply the construction on the example grammar, we obtain the following push-down automaton: M = ({z}, {[, ]}, {S, [, ]}, δ, z, S), 41

slide-42
SLIDE 42

where δ is given as follows: (z, [, [) → (z, ǫ) (z, ], ]) → (z, ǫ) (z, ǫ, S) → (z, [S]S) (z, ǫ, S) → (z, ǫ)

PDA → Context Free Grammar (slides 298–307)

Forms of Productions:

  • S → (z0, #, z) f¨

ur alle z ∈ Z. From (z0, #, z) we can derive (by the proof idea) all words that the push-down automaton can read in, when it is in the initial state, removes a # from the stack, and ends in z. That means, that from S all words of the language can be derived, as required.

  • (z, A, z′) → a falls (z′, ǫ) ∈ δ(z, a, A).

When it is in state z the push-down automaton can change to z′ and remove an a from the stack.

  • (z, A, z′) → a(z1, B1, z2)(z2, B2, z3) . . . (zk, Bk, z′) when (z1, B1 . . . Bk) ∈ δ(z, a, A), where

z2, . . . , zk ∈ Z. The stack automaton can, while reading a, go from state z to state z1, by replacing the stack symbol A by B1 . . . Bn. In order to make the stack one symbol shorter, these symbols must be removed as well. We have the following situation: A z B1 B2 Bk z1 B2 Bk z2 Bk zk z′ a x1 x2 . . . xk−1 xk When we assume that, for 1 ≤ i ≤ k, (zi, Bi, zi+1) ⇒∗ xi (with z′ = zk), then the push-down automaton can, while reading ax1 . . . xk and removing A from the stack, go from state z to state z′. Example of slide 306: When we apply the construction on the example automaton, we obtain the following context free grammar G: G = (V, Σ, P, S) where V = {S}∪Z ×Γ×Z (that means, that V consists of S and 3-tuples of a state, stack symbol and a state) and P consists of the following productions: Productions of the first form: S → (z1, #, z1) S → (z1, #, z2) 42

slide-43
SLIDE 43

Productions of the second form: (z1, A, z2) → b (z2, A, z2) → b (z2, #, z2) → ǫ Productions of the third form: (z1, #, z1) → a(z1, A, z1)(z1, #, z1) (z1, #, z2) → a(z1, A, z1)(z1, #, z2) (z1, #, z1) → a(z1, A, z2)(z2, #, z1) (z1, #, z2) → a(z1, A, z2)(z2, #, z2) (z1, A, z1) → a(z1, A, z1)(z1, A, z1) (z1, A, z2) → a(z1, A, z1)(z1, A, z2) (z1, A, z1) → a(z1, A, z2)(z2, A, z1) (z1, A, z2) → a(z1, A, z2)(z2, A, z2) Here is a syntax tree for the word aabb: S (z1, #, z2) a (z1, A, z2) a (z1, A, z2) b (z2, A, z2) b (z2, #, z2) ǫ

14 Closure Properties and Algorithms for Context Free Languages

Push-Down Automata with Final States (slide 272)

  • Theorem. A language is accepted by a push-down automaton with final states if and only if it

is accepted by a push-down automaton. Why does this theorem hold? ⇒ We transform a push-down automaton with final states in a push-down automaton by adding a new accept state in which the stack is emptied. The final state can be reached with ǫ-transitions from all original final states. ⇐ We transform a push-down automaton to a push-down automaton with final states, by in- troducing a final state zE and for each transition in which the stack bottom symbol is removed, adding a transition to zE. (This only works when it always the case that the stack bottom symbol is removed last. We can always construct such a push-down automaton.) 43

slide-44
SLIDE 44

Closure Under Union (slide 275)

The context free languages are closed under union. Why is this the case? Let two context free grammars G1 = (V1, Σ, P1, S1) and G2 = (V2, Σ, P2, S2) over the same al- phabet be given. We can assume that the two grammars do not have any variables in common, because, if this would not be the case, we could rename the variables of one grammar. Now we can construct a context free grammar G with L(G) = L(G1) ∪ L(G2) by adding a new start variable S (which does not occur in one of the grammars G1 or G2 and adding the following two productions: S → S1 S → S2 When we want to derive a word from S, we must replace S by S1 or by S2. Depending on the choice, we can then derive the word in G1 or G2. So it clearly is the case that L(G) = L(G1) ∪ L(G2). Consider as an example the following two grammars over the alphabet Σ = {a, b, c}: G1 = ({S1, Y, A}, Σ, P1, S1), where P1 consists of: S1 → AY Y → bY c | ǫ A → aA | ǫ G2 = ({S2, X, C}, Σ, P2, S2), where P2 consists of: S2 → XC X → aXb | ǫ C → cC | ǫ It holds that L(G1) = {ajbkcK | j, k ≥ 0} and L(G2) = {akbkcj | j, k ≥ 0}. Note that G1 and G2 use different variables. A context free grammar G with L(G) = L(G1) ∪ L(G2) is: G = ({S1, A, Y, S2, X, C, S}, Σ, P, S), where P consists of the following productions: S → S1 | S2 S1 → AY S2 → XC Y → bY c | ǫ X → aXb | ǫ A → aA | ǫ C → cC | ǫ

Closure Under Product (slide 276)

The context free languages are closed under product. Why is this the case? Let two context free grammars G1 = (V1, Σ, P1, S1) and G2 = (V2, Σ, P2, S2) over the same alpha- bet be given. Again we assume that the two grammars do not have any variables in common. Now, we can construct a context free grammar G with L(G) = L(G1)L(G2) by adding a new start variable (which does not occur in either G1 or G2) and adding the production S → S1S2. When we derive a word from S, in the first step we must replace S by S1S2. From S1 a word in w1 ∈ L(G1) is derived, from S2 a word w2 ∈ L(G2). The language L(G) consists of the concatenations of word which can be derived from S1 and S2. 44

slide-45
SLIDE 45

More formally, S ⇒ S1S2 ⇒∗ w1w2 if and only if S1 ⇒∗ w1 and S2 ⇒∗ w2. Deswegen gilt L(G) = L(G1)L(G2) = {w1w2 | w1 ∈ L(G1), w2 ∈ L(G2)}. As an example we consider the context free grammars G1 and G2 from page 44. The following context free grammar G generates the product of L(G1) and L(G2): G = ({S1, A, Y, S2, X, C, S}, Σ, P, S), where P consists of the following productions: S → S1S2 S1 → AY S2 → XC Y → bY c | ǫ X → aXb | ǫ A → aA | ǫ C → cC | ǫ

Closure Under the Star Operation (slide 277)

The context free languages are closed under the star operation. Why is this the case? The the grammar G1 = (V1, Σ, P1, S1) be given. We can now construct a grammar G with L(G) = L(G1)∗ by adding a new start variable S (which does not occur in G1) and adding the following productions: S → S1S | ǫ. With the new productions we can generate a sequence of S1’s of arbitrary length. From every S1

  • f the sequence a word of the language L(G1) can be derived.

For words w1, . . . , wn ∈ Σ∗ it holds, that S ⇒∗ S1 · · · S1

  • n Mal

⇒∗ w1 · · · wn iff S1 ⇒ w1, . . . , S1 ⇒ wn That is, w1 · · · wn ∈ L(G) iff w1 ∈ L(G1), . . . , wn ∈ L(G1). Consider as an example the grammar G1 of page 44. A context free grammar which generates L(G1)∗ is: G = ({S1, A, Y, S}, Σ, P, S) where P consists of the following productions: S → ǫ | S1S S1 → AY Y → bY c | ǫ A → aA | ǫ

Closure Under Intersection (slide 278)

The languages L1 and L2 of slide 278 are generated by the context free grammars G1 and G2 of page 44, in particular L1 = L(G1) and L2 = L(G2). This means, that L1 and L2 are context free

  • languages. However, the intersection of the two languages is L1 ∩ L2 = {akbkck | k ≥ 0}, and that

language is not context free (see page ??).

Example Cross Product Construction NFA/PDA (slide 279,280)

Let Σ = {<, >}. Let M be the following push-down automaton (with final states): M = ({z0, zE}, Σ, {A, #}, δM, z0, {zE}), 45

slide-46
SLIDE 46

where δM is given by: δM(z0, <, #) ∋ (z0, A#) δM(z0, <, A) ∋ (z0, AA) δM(z0, >, A) ∋ (z0, ǫ) δM(z0, ǫ, #) ∋ (zE, #) M accepts the language of correctly parenthesized expressions. Let A be the following non-deterministic finite automaton: A = ({z1, z2}, Σ, δA, {z1}, {z2}), where δA is given by δ(z1, <) = {z1} δ(z1, >) = {z2} δ(z2, <) = ∅ δ(z2, >) = {z2} A can be graphically represented as follows: z1 z2 > < > A accepts the language L((<)∗(>)∗). The following automaton accepts the language N(M) ∩ T(A): M ′ = (Z, Σ, Γ, δ, (z0, z1), #, E) where

  • Z = {z0, zE} × {z1, z2} = {(z0, z1), (z0, z2), (zE, z1), (zE, z2)}
  • E = {zE} × {z1, z2} = {(zE, z1), (zE, z2)}

and δ is defined as follows: δ((z0, z1), <, #) ∋ ((z0, z1), A#) δ((z0, z1), <, A) ∋ ((z0, z1), AA) δ((z0, z1), >, A) ∋ ((z0, z2), ǫ) δ((z0, z1), ǫ, #) ∋ ((zE, z1), #) δ((z0, z2), ǫ, #) ∋ ((zE, z2), #) We see that the automaton accepts the language N(M ′) = {<n>n | n ≥ 0} = N(M) ∩ T(A).

To slide 283

A problem is decidable when there is an algorithm which solves the problem. Like in the case of algorithms for regular languages, we assume in all cases, that context free languages are given in the form of a grammar or a push-down automaton. 46

slide-47
SLIDE 47

15 Generating a Parser with JavaCC

EBNF-Form (slides 308–309)

In praxis a different syntax is used (non-terminals are written in pointed parenthesis, terminals are put in quotation marks and instead of “→” people write “::=” or “=”); we, however, use the syntax that we used in the rest of the lecture.

Invoking ANTLR

We invoke ANTLR on the example grammar Expression.g: $ antlr4 -visitor Expression.g ANTLR generates the following files:

  • ExpressionLexer.java – Java class which implements the lexical analyser of the Expression

parser

  • ExpressionParser.java – Java class which implements the Expression parser
  • ExpressionLexer.tokens und ExpressionParser.tokens – token lists for the lexical anal-

yser and for the parser

  • ExpressionListener.java und ExpressionBaseListener.java – interface for the “tree

walker” idiom for the processing of the syntax tree (this was not used in the lecture)

  • ExpressionVisitor.java und ExpressionBaseVisitor.java – interface for the “visitor”

idiom for the processing of the syntax tree (this variant was used in the lecture) We compile all Java classes: $ javac *.java Now we can use the Grun tool to try out the parser and show a syntax tree. We want to use the grammar Expression, the start variable is program and for the input we take the example input prog. $ grun Expression program -gui prog The option -gui makes sure that the syntax tree is shown in a graphical user interface. The Java class Evaluator implements a simple “programming” language which can evaluate arith- metical expressions and store auxiliary results in variables. $ java Evaluator prog 42.0 47