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 2

lecture 5: Pushdown Automata

LECTURE SIX

From Grammars to PDAs

COMP20121 - Section 2 – p.123/307

slide-3
SLIDE 3

From grammars to PDAs

Given a grammar, create a PDA:

COMP20121 - Section 2 – p.124/307

slide-4
SLIDE 4

From grammars to PDAs

Given a grammar, create a PDA:

  • Three states, 0 (start state), 1 and

2 (accepting state), with transitions

COMP20121 - Section 2 – p.124/307

slide-5
SLIDE 5

From grammars to PDAs

Given a grammar, create a PDA:

  • Three states, 0 (start state), 1 and

2 (accepting state), with transitions

( , )→push(S)

→ 1,

  • 1

(EOF,EOS)→

→ 2,

COMP20121 - Section 2 – p.124/307

slide-6
SLIDE 6

From grammars to PDAs

Given a grammar, create a PDA:

  • Three states, 0 (start state), 1 and

2 (accepting state), with transitions

( , )→push(S)

→ 1,

  • 1

(EOF,EOS)→

→ 2,

  • 1

( ,R)→pop;push(Xn)···push(X1)

→ 1 for every

production R → X1X2 · · · Xn.

COMP20121 - Section 2 – p.124/307

slide-7
SLIDE 7

From grammars to PDAs

Given a grammar, create a PDA:

  • Three states, 0 (start state), 1 and

2 (accepting state), with transitions

( , )→push(S)

→ 1,

  • 1

(EOF,EOS)→

→ 2,

  • 1

( ,R)→pop;push(Xn)···push(X1)

→ 1 for every

production R → X1X2 · · · Xn.

  • 1

(x,x)→pop;advance

→ 1, for each x ∈ Σ.

COMP20121 - Section 2 – p.124/307

slide-8
SLIDE 8

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ;

COMP20121 - Section 2 – p.125/307

slide-9
SLIDE 9

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: Three states

1 2

COMP20121 - Section 2 – p.125/307

slide-10
SLIDE 10

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: Getting started.

1 2 ( , ) → push(S)

COMP20121 - Section 2 – p.125/307

slide-11
SLIDE 11

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: When finished.

1 (EOF, EOS) → 2 ( , ) → push(S)

COMP20121 - Section 2 – p.125/307

slide-12
SLIDE 12

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: S → aSb.

1 (EOF, EOS) → 2 ( , S) → pop; push(b); push(S); push(a) ( , ) → push(S)

COMP20121 - Section 2 – p.125/307

slide-13
SLIDE 13

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: S → ǫ.

1 (EOF, EOS) → 2 ( , S) → pop; push(b); push(S); push(a) ( , ) → push(S) ( , S) → pop

COMP20121 - Section 2 – p.125/307

slide-14
SLIDE 14

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: Recognize a.

1 (EOF, EOS) → 2 ( , S) → pop; push(b); push(S); push(a) ( , S) → pop (a, a) → pop; advance ( , ) → push(S)

COMP20121 - Section 2 – p.125/307

slide-15
SLIDE 15

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Build a PDA: Recognize b.

1 (EOF, EOS) → 2 ( , S) → pop; push(b); push(S); push(a) (b, b) → pop; advance ( , S) → pop (a, a) → pop; advance ( , ) → push(S)

COMP20121 - Section 2 – p.125/307

slide-16
SLIDE 16

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; Finished PDA

1 (EOF, EOS) → 2 ( , S) → pop; push(b); push(S); push(a) (b, b) → pop; advance ( , S) → pop (a, a) → pop; advance ( , ) → push(S)

COMP20121 - Section 2 – p.125/307

slide-17
SLIDE 17

Example: From grammar to PDA

Consider the grammar S → aSb|ǫ; which (with exception of the empty word) accepts the same language as the PDA

2 3 ( , S) → pop (a, a) → pop; advance ( , S) → ( , ) → push(S) (b, b) → pop; advance ( , S) → pop; push(b); push(S); push(a) ( , a) → ( , b) → (EOF, EOS) → 1

COMP20121 - Section 2 – p.125/307

slide-18
SLIDE 18

From PDAs to grammars

  • Given a (non-deterministic) PDA, it is

possible to devise a context-free grammar such that the language generated by the grammar is the language accepted by the automaton.

COMP20121 - Section 2 – p.126/307

slide-19
SLIDE 19

From PDAs to grammars

  • Given a (non-deterministic) PDA, it is

possible to devise a context-free grammar such that the language generated by the grammar is the language accepted by the automaton.

  • We will not prove this statement here.

See any of the recommended books.

COMP20121 - Section 2 – p.126/307

slide-20
SLIDE 20

Context-free languages and PDAs

The following result connects context-free languages and PDAs. Theorem 2.1 A language is context-free if and only if there is a non-deterministic pushdown automaton that accepts it.

COMP20121 - Section 2 – p.127/307

slide-21
SLIDE 21

Determinism versus non-determinism

  • Non-deterministic PDAs are more

powerful than deterministic PDAs.

COMP20121 - Section 2 – p.128/307

slide-22
SLIDE 22

Determinism versus non-determinism

  • Non-deterministic PDAs are more

powerful than deterministic PDAs.

  • There are context-free grammars for

which there is no deterministic pushdown

  • automaton. See Exercise 19 for an

example.

COMP20121 - Section 2 – p.128/307

slide-23
SLIDE 23

Determinism versus non-determinism

  • Non-deterministic PDAs are more

powerful than deterministic PDAs.

  • There are context-free grammars for

which there is no deterministic pushdown

  • automaton. See Exercise 19 for an

example. (This example is not examinable.)

COMP20121 - Section 2 – p.128/307

slide-24
SLIDE 24

Long words in context-free languages

If α is a ‘long enough’ word then in the parse tree for this word, at least one of the non-terminal symbols, say R, will appear twice.

COMP20121 - Section 2 – p.129/307

slide-25
SLIDE 25

Long words in context-free languages

If α is a ‘long enough’ word then in the parse tree for this word, at least one of the non-terminal symbols, say R, will appear twice. It is not easy to define what ‘long enough’ means this time.

COMP20121 - Section 2 – p.129/307

slide-26
SLIDE 26

Long words in context-free languages

Parse tree for α:

R S R

COMP20121 - Section 2 – p.130/307

slide-27
SLIDE 27

Long words in context-free languages

Breaking up α:

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.130/307

slide-28
SLIDE 28

Leaving out parts of α

Given a parse tree for α, with α broken up as before,

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.131/307

slide-29
SLIDE 29

Leaving out parts of α

we can replace the big parse tree at R

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.131/307

slide-30
SLIDE 30

Leaving out parts of α

we can replace the big parse tree at R

R S R κ λ µ ν ξ

by the small parse tree at R

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.131/307

slide-31
SLIDE 31

Leaving out parts of α

we can replace the big parse tree at R

R S R κ λ µ ν ξ

by the small parse tree at R

R S R κ λ µ ν ξ

to get the parse tree for κµξ

S R κ µ ξ

COMP20121 - Section 2 – p.131/307

slide-32
SLIDE 32

Repeating parts of α

Given a parse tree for α, with α broken up as before,

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.132/307

slide-33
SLIDE 33

Repeating parts of α

we can replace the small parse tree at R

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.132/307

slide-34
SLIDE 34

Repeating parts of α

we can replace the small parse tree at R

R S R κ λ µ ν ξ

by the big parse tree at R

R S R κ λ µ ν ξ

COMP20121 - Section 2 – p.132/307

slide-35
SLIDE 35

Repeating parts of α

to get the parse tree for κλλµννξ

S R R R κ λ λ µ ν ν ξ

COMP20121 - Section 2 – p.133/307

slide-36
SLIDE 36

The Pumping Lemma II

We can formalize this observation: Lemma 2.2 If L is a context-free language then there is a number n such that any word α of L that has length at least n can be divided into five pieces

COMP20121 - Section 2 – p.134/307

slide-37
SLIDE 37

The Pumping Lemma II

α = κλµνξ such that

  • the length of λν is greater than 0;
  • the length of λµν is at most n;
  • all words of the form κλkµνkξ, where

k ∈ N, belong to L.

COMP20121 - Section 2 – p.134/307

slide-38
SLIDE 38

The Pumping Lemma II

α = κλµνξ such that

  • the length of λν is greater than 0;
  • the length of λµν is at most n;
  • all words of the form κλkµνkξ, where

k ∈ N, belong to L.

Note that if L is finite, it may not have any words of length n or longer!

COMP20121 - Section 2 – p.134/307

slide-39
SLIDE 39

Applying Pumping: An example

  • The language L = {aibici | i ∈ N} is

not context-free.

COMP20121 - Section 2 – p.135/307

slide-40
SLIDE 40

Applying Pumping: An example

  • The language L = {aibici | i ∈ N} is

not context-free.

  • To see this, suppose it were!

Choose n by the Pumping Lemma. Let α = anbncn. Write α = κλµνξ as in the lemma.

COMP20121 - Section 2 – p.135/307

slide-41
SLIDE 41

Applying Pumping: An example

Then one of λ, ν is non-empty. Case 1: Each only contain one kind of character. Then κµξ ∈ L.

COMP20121 - Section 2 – p.136/307

slide-42
SLIDE 42

Applying Pumping: An example

Then one of λ, ν is non-empty. Case 1: Each only contain one kind of character. Then κµξ ∈ L. Case 2: One of λ, ν contains more than

  • ne kind of character.

Then κλ2µν2ξ ∈ L.

COMP20121 - Section 2 – p.136/307

slide-43
SLIDE 43

Section 2 summary

  • Context-free languages are generated

by context-free grammars.

COMP20121 - Section 2 – p.137/307

slide-44
SLIDE 44

Section 2 summary

  • Context-free languages are generated

by context-free grammars.

  • Context-free languages are more

powerful than regular ones.

COMP20121 - Section 2 – p.137/307

slide-45
SLIDE 45

Section 2 summary

  • Context-free languages are generated

by context-free grammars.

  • Context-free languages are more

powerful than regular ones.

  • Context-free languages correspond

precisely to those accepted by non-deterministic pushdown automata.

COMP20121 - Section 2 – p.137/307

slide-46
SLIDE 46

Section 2 summary (ctd)

  • Deterministic PDAs are less powerful

than (non-deterministic) PDAs.

COMP20121 - Section 2 – p.138/307

slide-47
SLIDE 47

Section 2 summary (ctd)

  • Deterministic PDAs are less powerful

than (non-deterministic) PDAs.

  • There is a Pumping Lemma for

context-free languages.

COMP20121 - Section 2 – p.138/307

slide-48
SLIDE 48

Section 2 summary (ctd)

  • Deterministic PDAs are less powerful

than (non-deterministic) PDAs.

  • There is a Pumping Lemma for

context-free languages.

  • Context-free languages are sufficient

to parse programming languages but

COMP20121 - Section 2 – p.138/307

slide-49
SLIDE 49

Section 2 summary (ctd)

  • Deterministic PDAs are less powerful

than (non-deterministic) PDAs.

  • There is a Pumping Lemma for

context-free languages.

  • Context-free languages are sufficient

to parse programming languages but

  • a compiler needs a proper

random-access memory.

COMP20121 - Section 2 – p.138/307