fundamentele informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · an unrestricted...

26
Fundamentele Informatica 3 voorjaar 2019 http://www.liacs.leidenuniv.nl/ ~ vlietrvan1/fi3/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 5, 4 maart 2019 8. Recursively Enumerable Languages 8.3. More General Grammars 1

Upload: others

Post on 06-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Fundamentele Informatica 3

voorjaar 2019

http://www.liacs.leidenuniv.nl/~vlietrvan1/fi3/

Rudy van Vliet

kamer 140 Snellius, tel. 071-527 2876

rvvliet(at)liacs(dot)nl

college 5, 4 maart 2019

8. Recursively Enumerable Languages

8.3. More General Grammars

1

Page 2: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Huiswerkopgave

Voor 0.4pt

Inleveren: maandag 25 maart 2019, 11:05 uur

2

Page 3: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

8.3. More General Grammars

reg. languages FA reg. grammar reg. expression

determ. cf. languages DPDA

cf. languages PDA cf. grammar

cs. languages LBA cs. grammar

re. languages TM unrestr. grammar

3

Page 4: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

A slide from lecture 1

FI2: Pumping Lemma for CFLs

✖✕✗✔✁✁❅❅❅

S

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

✁✁

✁✁

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

v z

✖✕✗✔

❆❆�

��

A

✁✁

✁✁

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

✁✁✁

✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆

w y

✖✕✗✔A

✁✁✁

✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆

x

4

Page 5: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

A slide from lecture 1FI2: Pumping Lemma for CFLs

✖✕✗✔✁✁❅❅❅

S

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

v z

✖✕✗✔A

✁✁✁

✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆

x

✖✕✗✔✁✁❅❅❅

S

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

v z

✖✕✗✔

❆❆�

��

A

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁

❆❆❆❆❆❆❆❆❆❆❆❆

w y

✖✕✗✔

❆❆�

��

A

✁✁

✁✁

✁✁

✁✁

✁✁

✁✁✁

✁✁

✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆❆

✁✁

✁✁

✁✁

✁✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆

w y

✖✕✗✔A

✁✁

✁✁

✁✁

✁✁✁

✁✁✁

❆❆❆❆❆❆❆❆❆❆❆❆

x

5

Page 6: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Definition 8.10. Unrestricted grammars

An unrestricted grammar is a 4-tuple G = (V,Σ, S, P ), where V

and Σ are disjoint sets of variables and terminals, respectively,

S is an element of V called the start symbol, and P is a set of

productions of the form

α → β

where α, β ∈ (V ∪Σ)∗ and α contains at least one variable.

6

Page 7: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Notation as for CFGs:

α ⇒∗G β

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

but. . .

7

Page 8: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example 8.12. A Grammar Generating {anbncn | n ≥ 1}

8

Page 9: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example 8.12. A Grammar Generating {anbncn | n ≥ 1}

S → SABC | LABC

BA → AB CB → BC CA → AC

LA → a aA → aa aB → ab bB → bb bC → bc cC → cc

9

Page 10: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example 8.11. A Grammar Generating {a2k| k ∈ N}

{a, a2, a4, a8, a16, . . .} = {a, aa, aaaa, aaaaaaaa, aaaaaaaaaaaaaaaa, . . .}

10

Page 11: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example 8.11. A Grammar Generating {a2k| k ∈ N}

{a, a2, a4, a8, a16, . . .} = {a, aa, aaaa, aaaaaaaa, aaaaaaaaaaaaaaaa, . . .}

S → LaR

L → LD Da → aaD DR → R

L → Λ R → Λ

11

Page 12: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example.

An Unrestricted Grammar Generating XX = {xx | x ∈ {a, b}∗}

First a CFG for Pal = {x ∈ {a, b}∗ | x = xr}:

S → aSa | bSb | a | b | Λ

12

Page 13: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example.

An Unrestricted Grammar Generating XX = {xx | x ∈ {a, b}∗}

S → aAS | bBS | M

Aa → aA Ab → bA Ba → aB Bb → bB

AM → Ma BM → Mb M → Λ

13

Page 14: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Theorem 8.13.

For every unrestricted grammar G, there is a Turing machine T

with L(T ) = L(G).

Proof.

1. Move past input

2. Simulate derivation in G on the tape of a Turing machine

3. Equal

14

Page 15: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Theorem 8.13.

For every unrestricted grammar G, there is a Turing machine T

with L(T ) = L(G).

Proof.

1. Move past input

2. Simulate derivation in G on the tape of a Turing machine:

Write S on tape

Repeat

a. Select production α → β

b. Select occurrence of α (if there is one)

c. Replace occurrence of α by β

until b. fails (caused by . . . )

3. Equal

15

Page 16: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

A slide from lecture 4

Theorem 7.31.

For every nondeterministic TM T = (Q,Σ,Γ, q0, δ),

there is an ordinary (deterministic) TM T1 = (Q1,Σ,Γ1, q1, δ1)

with L(T1) = L(T ).

Moreover, if there is no input on which T can loop forever,

then T1 also halts on every input.

The proof of this result does not have to be known for the exam.

16

Page 17: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Example.

(The second part of) the construction from Theorem 8.13 to

obtain a TM simulating a derivation in the unrestricted grammar

with productions

S → aBS | Λ aB → Ba Ba → aB B → b

See next slide

N.B.:

In next slide, we simulate application of arbitrary production by

• first moving to arbitrary position in current string (at q2)

• only then selecting (and applying) a possible production

This implementation of the construction must be known for the

exam

17

Page 18: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✲ ✲ ✲

✂✂✂✂✂✂✂✂✂✂✂✂✂✂✂✂✂✍

❇❇❇❇❇❇❇❇❇❇❇❇❇❇❇❇❇◆

✟✟✟✟✟✟✟✟✟✟✯ ❍❍❍❍❍❍❍❍❍❍❥✲ ✲

q0 q1 q2

q3 q4

q5

q6 q7

q8ha

∆/∆,R ∆/S,S

S/a,R

∆/B,R

∆/S,L

a/B,R B/a,L

B/a,R a/B,L

∆/∆,L

∆/∆,S

S/∆,L

B/b,L

∆/∆,R

✓✏S/S,R

B/B,R

b/b,Ra/a,R

✒✑✻a/a,L

b/b,LB/B,L

S/S,L

✏✑✛

a/a,Lb/b,LB/B,L

S/S,L

18

Page 19: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Theorem 8.14.

For every Turing machine T with input alphabet Σ,

there is an unrestricted grammar G

generating the language L(T ) ⊆ Σ∗.

Proof.

1. Generate (every possible) input string for T .

2. Simulate computation of T for this input string as derivation

in grammar.

3. If T reaches accept state, reconstruct original input string.

19

Page 20: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

(Part of) a slide from lecture 2

Notation:

description of tape contents: xσy or xy

configuration xqy = xqy∆ = xqy∆∆

initial configuration corresponding to input x: q0∆x

20

Page 21: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✲ ✲ ✲ ✲ ✲q0 q1 q2 q3 ha∆/∆,R a/$,L $/$,R ∆/∆,S

✓✏b/b,R

✓✏b, b,R

Computation for x = ba.

21

Page 22: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Theorem 8.14.

For every Turing machine T with input alphabet Σ,

there is an unrestricted grammar G

generating the language L(T ) ⊆ Σ∗.

Proof.

1. Generate (every possible) input string for T (two copies),

with additional (∆∆)’s and state.

2. Simulate computation of T for this input string as derivation

in grammar (on second copy).

3. If T reaches accept state, reconstruct original input string.

22

Page 23: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✫✪✬✩

✲ ✲ ✲ ✲ ✲q0 q1 q2 q3 ha∆/∆,R a/$,L $/$,R ∆/∆,S

✓✏b/b,R

✓✏b, b,R

23

Page 24: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

3. If T reaches accept state, reconstruct original input string. . .

24

Page 25: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Theorem 8.14.

For every Turing machine T with input alphabet Σ,

there is an unrestricted grammar Ggenerating the language L(T ) ⊆ Σ∗.

Proof.

1. Generate (every possible) input string for T (two copies),

with additional (∆∆)’s and state.

2. Simulate computation of T for this input string as derivation

in grammar (on second copy).

3. If T reaches accept state, reconstruct original input string.

Ad 2. Move δ(p, a) = (q, b, R) of Tyields production p(σ1a) → (σ1b)q

Ad 3. Propagate ha all over the string

ha(σ1σ2) → σ1, for σ1 ∈ Σ

ha(∆σ2) → Λ

25

Page 26: Fundamentele Informatica 3liacs.leidenuniv.nl/.../fi3/voorjaar2019/slides/... · An unrestricted grammar is a 4-tuple G = (V,Σ,S,P), where V and Σ are disjoint sets of variables

Wat?

• De Nationale Studenten Enquête

Waarom?

• Omdat je graag je mening wilt geven & wilt meehelpen je opleiding te verbeteren

• Omdat bij 25% respons studenten koeken krijgen

• Omdat er per ingevulde enquête 25 cent wordt gedoneerd aan stichting vluchteling � student (UAF)• Omdat de studievereniging met de hoogste respons een gratis sportactiviteit mag organiseren

Hoe?

• Via de persoonlijke link in de uitnodigingsmail

• Link kwijt? Vul je uMail � ibox e � mailadres in op www.nse.nl

Gemakkelijk via je telefoon!

26