COMP20121 The Implementation and Power of Computer Languages Power - - PowerPoint PPT Presentation

comp20121 the implementation and power of computer
SMART_READER_LITE
LIVE PREVIEW

COMP20121 The Implementation and Power of Computer Languages Power - - PowerPoint PPT Presentation

COMP20121 The Implementation and Power of Computer Languages Power Part http://www.cs.man.ac.uk/ petera/2121/index.html . Peter Aczel room: CS2.52, tel: 56155 petera@cs.man.ac.uk email: Department of Computer Science, University of


slide-1
SLIDE 1

COMP20121 The Implementation and Power of Computer Languages ‘Power’ Part

http://www.cs.man.ac.uk/∼petera/2121/index.html .

Peter Aczel room: CS2.52, tel: 56155 email:

petera@cs.man.ac.uk

Department of Computer Science, University of Manchester

COMP20121 - Section 0 – p.1/307

slide-2
SLIDE 2

COMP20121, ‘power’ part: section 1

lectures 2,3: Finite automata for regular languages

LECTURE FOUR

  • Some properties of Regular Languages
  • Start of section 2: Context-free Grammars

COMP20121 - Section 1 – p.74/307

slide-3
SLIDE 3

Properties of regular languages

Proposition

  • Every finite language is regular.

COMP20121 - Section 1 – p.75/307

slide-4
SLIDE 4

Properties of regular languages

Proposition

  • Every finite language is regular.
  • If L is a regular language then so are

Ln (for all n ∈ N), L∗, L and LR.

COMP20121 - Section 1 – p.75/307

slide-5
SLIDE 5

Properties of regular languages

Proposition

  • Every finite language is regular.
  • If L is a regular language then so are

Ln (for all n ∈ N), L∗, L and LR.

  • If L and L′ are regular languages then

so are L ∪ L′, L ∩ L′ and L · L′.

COMP20121 - Section 1 – p.75/307

slide-6
SLIDE 6

Proofs—I

We first show that every finite language is regular:

COMP20121 - Section 1 – p.76/307

slide-7
SLIDE 7

Proofs—I

We first show that every finite language is regular: If {α1, α2, . . . , αn} is a finite language then α1|α2| · · · |αn is a pattern for the language.

COMP20121 - Section 1 – p.76/307

slide-8
SLIDE 8

Proofs—II

If L is regular then so are Ln and L∗:

COMP20121 - Section 1 – p.77/307

slide-9
SLIDE 9

Proofs—II

If L is regular then so is Ln: For if p is a pattern for L then ? is a pattern for Ln,

COMP20121 - Section 1 – p.77/307

slide-10
SLIDE 10

Proofs—II

If L is regular then so is Ln: For if p is a pattern for L then pp · · · p

n

is a pattern for Ln, and

COMP20121 - Section 1 – p.77/307

slide-11
SLIDE 11

Proofs—II

If L is regular then so is Ln: For if p is a pattern for L then pp · · · p

n

is a pattern for Ln, and ? is one for L∗.

COMP20121 - Section 1 – p.77/307

slide-12
SLIDE 12

Proofs—II

If L is regular then so is Ln: For if p is a pattern for L then pp · · · p

n

is a pattern for Ln, and p∗ is one for L∗.

COMP20121 - Section 1 – p.77/307

slide-13
SLIDE 13

Proofs—II

If L is regular then so is its complement, L. This follows from Assessed Exercise 1(b) and Theorem 1.5.

COMP20121 - Section 1 – p.77/307

slide-14
SLIDE 14

Proofs—II

If L is regular then so is LR. This is an exercise.

COMP20121 - Section 1 – p.77/307

slide-15
SLIDE 15

Proofs—III

If L and L′ are regular languages then so is L ∪ L′ :

COMP20121 - Section 1 – p.78/307

slide-16
SLIDE 16

Proofs—III

If L and L′ are regular languages then so is L ∪ L′ : If p is a pattern for L and p′ one for L′ then

? is a pattern for L ∪ L′.

COMP20121 - Section 1 – p.78/307

slide-17
SLIDE 17

Proofs—III

If L and L′ are regular languages then so is L ∪ L′ : If p is a pattern for L and p′ one for L′ then

p|p′ is a pattern for L ∪ L′.

COMP20121 - Section 1 – p.78/307

slide-18
SLIDE 18

Proofs—III

If L and L′ are regular languages then so is L · L′ :

COMP20121 - Section 1 – p.78/307

slide-19
SLIDE 19

Proofs—III

If L and L′ are regular languages then so is L · L′ : If p is a pattern for L and p′ one for L′ then

? is a pattern for L · L′.

COMP20121 - Section 1 – p.78/307

slide-20
SLIDE 20

Proofs—III

If L and L′ are regular languages then so is L · L′ : If p is a pattern for L and p′ one for L′ then

pp′ is a pattern for L · L′.

COMP20121 - Section 1 – p.78/307

slide-21
SLIDE 21

Proofs—III

If L and L′ are regular languages then so is L ∩ L′. This is again an exercise.

COMP20121 - Section 1 – p.78/307

slide-22
SLIDE 22

Long words in regular languages

Assume that a DFA has n states. Then every word of length at least n will go through (at least) one state twice.

COMP20121 - Section 1 – p.79/307

slide-23
SLIDE 23

Long words in regular languages

Assume that a DFA has n states. Then every word of length at least n will go through (at least) one state twice.

. . . . . .

Very long word

x1 xn

state 1 state n + 1

COMP20121 - Section 1 – p.79/307

slide-24
SLIDE 24

Long words in regular languages

Assume that a DFA has n states. Then every word of length at least n will go through (at least) one state twice.

. . . i . . . i . . .

Length of word ≥ n, where n is the number of states

COMP20121 - Section 1 – p.79/307

slide-25
SLIDE 25

Long words in regular languages

Assume that a DFA has n states. Then every word of length at least n will go through (at least) one state twice.

i . . .

Length of word ≥ n, where n is the number of states

. . . . . . . . .

COMP20121 - Section 1 – p.79/307

slide-26
SLIDE 26

The Pumping Lemma

Lemma (The Pumping Lemma) Let L be an infinite regular language. Then there exists a natural number n > 0 such that every word α of L consisting of at least

n characters contains a ‘pumping section’ in

the following sense.

COMP20121 - Section 1 – p.80/307

slide-27
SLIDE 27

The Pumping Lemma

The word α can be split up into three parts, say λ, µ, and ν such that

  • α = λµν and µ = ǫ;
  • the length of λµ is at most n

and so that all words of the form λµkν, for

k ∈ N, belong to L.

COMP20121 - Section 1 – p.81/307

slide-28
SLIDE 28

The Pumping Lemma–II

The processing of α ends The processing of α begins

COMP20121 - Section 1 – p.82/307

slide-29
SLIDE 29

The Pumping Lemma–II

p

The processing of α ends The processing of α begins

COMP20121 - Section 1 – p.82/307

slide-30
SLIDE 30

The Pumping Lemma–II

p

The processing of α ends The processing of α begins

λ is processed

COMP20121 - Section 1 – p.82/307

slide-31
SLIDE 31

The Pumping Lemma–II

p

The processing of α ends The processing of α begins

µ is processed λ is processed

COMP20121 - Section 1 – p.82/307

slide-32
SLIDE 32

The Pumping Lemma–II

p

The processing of α ends The processing of α begins

µ is processed ν is processed λ is processed

COMP20121 - Section 1 – p.82/307

slide-33
SLIDE 33

Processing λν

p

The processing of α ends

µ is processed ν is processed

The processing of λν begins

λ is processed

COMP20121 - Section 1 – p.83/307

slide-34
SLIDE 34

Processing λν

p

The processing of α ends

µ is processed

The processing of λν begins

ν is processed λ is processed

COMP20121 - Section 1 – p.83/307

slide-35
SLIDE 35

Processing λµ2ν

p

The processing of α ends

µ is processed ν is processed

The processing of λν begins

λ is processed

COMP20121 - Section 1 – p.84/307

slide-36
SLIDE 36

Processing λµ2ν

p

The processing of α ends The processing of α begins

ν is processed λ is processed µ is processed

COMP20121 - Section 1 – p.84/307

slide-37
SLIDE 37

Processing λµ2ν

p

The processing of α ends The processing of α begins

ν is processed λ is processed µ is processed

COMP20121 - Section 1 – p.84/307

slide-38
SLIDE 38

Processing λµ2ν

p

The processing of α ends

µ is processed

The processing of λν begins

ν is processed λ is processed

COMP20121 - Section 1 – p.84/307

slide-39
SLIDE 39

Pumping Lemma: Example

The language L = {0i1i | i ∈ N} is not regular.

  • Assume n is as in the Pumping Lemma.

COMP20121 - Section 1 – p.85/307

slide-40
SLIDE 40

Pumping Lemma: Example

The language L = {0i1i | i ∈ N} is not regular.

  • Assume n is as in the Pumping Lemma.
  • Consider 0n1n; assume λ, µ, ν as in the

Pumping Lemma.

COMP20121 - Section 1 – p.85/307

slide-41
SLIDE 41

Pumping Lemma: Example

  • Since the length of λµ is at most n and

µ = ǫ, we know that

n

  • 00 · · ·

λ

· · ·

  • µ

00

n

1 · · · 1 .

COMP20121 - Section 1 – p.86/307

slide-42
SLIDE 42

Pumping Lemma: Example

  • Since the length of λµ is at most n and

µ = ǫ, we know that

n

  • 00 · · ·

λ

· · ·

  • µ

00

n

1 · · · 1 .

  • But then λν = λµ0ν is not in L:

<n

00 · · ·

λ n

1 · · · 1 .

COMP20121 - Section 1 – p.86/307

slide-43
SLIDE 43

Section 1 summary

  • Regular languages are describable via

patterns (regular expressions).

COMP20121 - Section 1 – p.87/307

slide-44
SLIDE 44

Section 1 summary

  • Regular languages are describable via

patterns (regular expressions).

  • Regular languages are precisely the

languages accepted by finite deterministic automata and

COMP20121 - Section 1 – p.87/307

slide-45
SLIDE 45

Section 1 summary

  • Regular languages are describable via

patterns (regular expressions).

  • Regular languages are precisely the

languages accepted by finite deterministic automata and

  • these are precisely the languages

accepted by finite non-deterministic automata.

COMP20121 - Section 1 – p.87/307

slide-46
SLIDE 46

Section 1 summary

  • Matching a word to a pattern is a

recursive process, while checking acceptance with a deterministic automaton is straightforward.

COMP20121 - Section 1 – p.88/307

slide-47
SLIDE 47

Section 1 summary

  • Matching a word to a pattern is a

recursive process, while checking acceptance with a deterministic automaton is straightforward.

  • The Pumping Lemma says that infinite

regular languages contain strings where a ‘pumping section’ can be repeated arbitrarily often.

COMP20121 - Section 1 – p.88/307

slide-48
SLIDE 48

Section 1 summary

  • Regular languages are not powerful

enough for the parsing of programming languages.

COMP20121 - Section 1 – p.89/307

slide-49
SLIDE 49

COMP20121, Context Free Grammars LECTURE FOUR (ctd)

Section 2: Context Free Grammars

COMP20121 - Section 2 – p.90/307

slide-50
SLIDE 50

Context-free grammars

Definition A context-free grammar is given by the following:

  • an alphabet Σ of terminal symbols,

also called the object alphabet;

COMP20121 - Section 2 – p.91/307

slide-51
SLIDE 51

Context-free grammars

Definition A context-free grammar is given by the following:

  • an alphabet Σ of terminal symbols,

also called the object alphabet;

  • an alphabet N of non-terminal

symbols, the elements of which are also referred to as auxiliary characters, placeholders or, in some books, variables, where N ∩ Σ = ∅;

COMP20121 - Section 2 – p.91/307

slide-52
SLIDE 52

Context-free grammars

  • a special non-terminal symbol S ∈ N

called the start symbol;

COMP20121 - Section 2 – p.92/307

slide-53
SLIDE 53

Context-free grammars

  • a special non-terminal symbol S ∈ N

called the start symbol;

  • a finite set of production rules, i.e.

strings of the form R → Γ where

R ∈ N is a non-terminal symbol and Γ ∈ (Σ ∪ N)∗ is an arbitrary string of

terminal and non-terminal symbols. The rule can be read as ‘R can be replaced by Γ’.

COMP20121 - Section 2 – p.92/307

slide-54
SLIDE 54

Context-free grammars: example

Σ = {(, ), +, −, ×, /, 0, 1, 2, . . .} N = {S, B}

COMP20121 - Section 2 – p.93/307

slide-55
SLIDE 55

Context-free grammars: example

Σ = {(, ), +, −, ×, /, 0, 1, 2, . . .} N = {S, B}

B → 0 S → B S → (S) B → 1 S → S + S S → S − S B → 2 S → S × S S → S/S

. . .

COMP20121 - Section 2 – p.93/307

slide-56
SLIDE 56

Context-free grammars: example

Σ = {(, ), +, −, ×, /, 0, 1, 2, . . .} N = {S, B}

Or, shorter:

B → 0 | 1 | 2 | . . . S → B | (S) | S + S | S − S | S × S | S/S

COMP20121 - Section 2 – p.93/307

slide-57
SLIDE 57

Generating strings: example

Generating strings:

COMP20121 - Section 2 – p.94/307

slide-58
SLIDE 58

Generating strings: example

Generating strings:

S

COMP20121 - Section 2 – p.94/307

slide-59
SLIDE 59

Generating strings: example

Generating strings:

S ⇒ S × S

COMP20121 - Section 2 – p.94/307

slide-60
SLIDE 60

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S)

COMP20121 - Section 2 – p.94/307

slide-61
SLIDE 61

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S)

COMP20121 - Section 2 – p.94/307

slide-62
SLIDE 62

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S) ⇒ B × (S + S)

COMP20121 - Section 2 – p.94/307

slide-63
SLIDE 63

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S) ⇒ B × (S + S) ⇒ B × (B + S)

COMP20121 - Section 2 – p.94/307

slide-64
SLIDE 64

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S) ⇒ B × (S + S) ⇒ B × (B + S) ⇒ 2 × (B + S)

COMP20121 - Section 2 – p.94/307

slide-65
SLIDE 65

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S) ⇒ B × (S + S) ⇒ B × (B + S) ⇒ 2 × (B + S) ⇒ 2 × (3 + S)

COMP20121 - Section 2 – p.94/307

slide-66
SLIDE 66

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S) ⇒ B × (S + S) ⇒ B × (B + S) ⇒ 2 × (B + S) ⇒ 2 × (3 + S) ⇒ 2 × (3 + B)

COMP20121 - Section 2 – p.94/307

slide-67
SLIDE 67

Generating strings: example

Generating strings:

S ⇒ S × S ⇒ S × (S) ⇒ B × (S) ⇒ B × (S + S) ⇒ B × (B + S) ⇒ 2 × (B + S) ⇒ 2 × (3 + S) ⇒ 2 × (3 + B) ⇒ 2 × (3 + 4)

COMP20121 - Section 2 – p.94/307

slide-68
SLIDE 68

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4)

COMP20121 - Section 2 – p.95/307

slide-69
SLIDE 69

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S

COMP20121 - Section 2 – p.95/307

slide-70
SLIDE 70

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S ×

COMP20121 - Section 2 – p.95/307

slide-71
SLIDE 71

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S S × ( )

COMP20121 - Section 2 – p.95/307

slide-72
SLIDE 72

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S × ( )

COMP20121 - Section 2 – p.95/307

slide-73
SLIDE 73

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S S S × ( + )

COMP20121 - Section 2 – p.95/307

slide-74
SLIDE 74

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S S S B × ( + )

COMP20121 - Section 2 – p.95/307

slide-75
SLIDE 75

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S S S B 2 × ( + )

COMP20121 - Section 2 – p.95/307

slide-76
SLIDE 76

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S S S B 2 × ( 3 + )

COMP20121 - Section 2 – p.95/307

slide-77
SLIDE 77

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S S S B B 2 × ( 3 + )

COMP20121 - Section 2 – p.95/307

slide-78
SLIDE 78

Generating strings: Parse tree

S ⇒ · · · ⇒ 2 × (3 + 4) S S S B S S S B B 2 × ( 3 + 4 )

COMP20121 - Section 2 – p.95/307

slide-79
SLIDE 79

Generating strings: formally

Definition A string Γ ∈ (Σ ∪ N)∗ is generated by a grammar G if there is a sequence of strings

S = Γ0 ⇒ Γ1 ⇒ · · · ⇒ Γn = Γ

such that each step Γi ⇒ Γi+1 is

  • btained by the application of one of G’s

production rules to a non-terminal

  • ccurring in Γi as follows.

COMP20121 - Section 2 – p.96/307

slide-80
SLIDE 80

Generating strings: formally

Let A ∈ N occur in Γi and assume that there is a production rule

A → ∆.

Then Γi+1 is obtained from Γi by replacing

  • ne occurrence of A in Γi by the string ∆.

COMP20121 - Section 2 – p.97/307

slide-81
SLIDE 81

Generating strings: formally

If A → ∆ is a production rule, so that

A ∈ N, ∆ ∈ (Σ ∪ N)∗,

and Γi is · · · A · · · then we get

Γi ⇒ Γi+1

where Γi+1 is · · · ∆ · · · .

COMP20121 - Section 2 – p.98/307

slide-82
SLIDE 82

Generated Language

Definition 11 The language generated by a grammar G is the set of all strings

α ∈ Σ∗ which are generated by G.

Languages generated by context-free grammars are called context-free.

COMP20121 - Section 2 – p.99/307

slide-83
SLIDE 83

Regular languages are context-free

Proposition: Every regular language is generated by a context-free grammar.

COMP20121 - Section 2 – p.100/307

slide-84
SLIDE 84

Regular languages are context-free

Proposition: Every regular language is generated by a context-free grammar. We build a grammar from the automaton

(Q, q•, F, δ).

COMP20121 - Section 2 – p.100/307

slide-85
SLIDE 85

Regular languages are context-free

Proposition: Every regular language is generated by a context-free grammar. We build a grammar from the automaton

(Q, q•, F, δ).

We put N = Q, S = q•

COMP20121 - Section 2 – p.100/307

slide-86
SLIDE 86

Regular languages are context-free

Proposition: Every regular language is generated by a context-free grammar. We build a grammar from the automaton

(Q, q•, F, δ).

We put N = Q, S = q• and have a rule

q → xq′ whenever q

x

→ q′ and a rule q → ǫ whenever q ∈ F .

COMP20121 - Section 2 – p.100/307

slide-87
SLIDE 87

Regular languages are context-free

Then

q•

x1

− → q1

x2

− → · · ·

xn

− → qn ∈ F

if and only if

COMP20121 - Section 2 – p.101/307

slide-88
SLIDE 88

Regular languages are context-free

Then

q•

x1

− → q1

x2

− → · · ·

xn

− → qn ∈ F

if and only if

S = q• ⇒ x1q1 ⇒ x1x2q2 ⇒ · · · ⇒ x1x2 · · · xnqn ⇒ x1x2 · · · xn

COMP20121 - Section 2 – p.101/307

slide-89
SLIDE 89

Grammars for regular languages

Proposition A language is regular if and only if it is generated by a context-free grammar which is subject to the restriction that all production rules have one of the forms

R → xR′ or R → x or R → ǫ

where R, R′ ∈ N and x ∈ Σ.

COMP20121 - Section 2 – p.102/307

slide-90
SLIDE 90

Grammars for regular languages

  • ‘If’: see previous Proposition.
  • ‘Only if’: Build an automaton from the

grammar:

COMP20121 - Section 2 – p.103/307

slide-91
SLIDE 91

Grammars for regular languages

  • ‘If’: see previous Proposition.
  • ‘Only if’: Build an automaton from the

grammar:

  • Q = N + {Z}, q• = S;

COMP20121 - Section 2 – p.103/307

slide-92
SLIDE 92

Grammars for regular languages

  • ‘If’: see previous Proposition.
  • ‘Only if’: Build an automaton from the

grammar:

  • Q = N + {Z}, q• = S;
  • R ∈ F if R → ǫ or R = Z;

COMP20121 - Section 2 – p.103/307

slide-93
SLIDE 93

Grammars for regular languages

  • ‘If’: see previous Proposition.
  • ‘Only if’: Build an automaton from the

grammar:

  • Q = N + {Z}, q• = S;
  • R ∈ F if R → ǫ or R = Z;
  • R

x

− → R′ if R → xR′, R

x

− → Z if R → x.

COMP20121 - Section 2 – p.103/307

slide-94
SLIDE 94

Grammars for regular languages

An accepting path, labelled with

x1 · · · xn, through the automaton is

either q•

x1

→ R1

x2

→ · · ·

xn

→ Rn ∈ F or

else is q•

x1

→ R1

x2

→ · · ·

xn−1

→ Rn−1

xn

→ Z ∈ F and these correspond in the

grammar to either S ⇒ x1R1 ⇒

x1x2R2 ⇒ · · · ⇒ x1 · · · xnRn ⇒ x1 · · · xn or else S ⇒ x1R1 ⇒ x1x2R2 ⇒ · · · ⇒ x1 · · · xn−1Rn−1 ⇒ x1 · · · xn

COMP20121 - Section 2 – p.104/307