• Keine Ergebnisse gefunden

Theory of Computer Science C5. Context-free Languages: Normal Form and PDA Gabriele R¨oger

N/A
N/A
Protected

Academic year: 2022

Aktie "Theory of Computer Science C5. Context-free Languages: Normal Form and PDA Gabriele R¨oger"

Copied!
34
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Theory of Computer Science

C5. Context-free Languages: Normal Form and PDA

Gabriele R¨ oger

University of Basel

April 1, 2019

(2)

Theory of Computer Science

April 1, 2019 — C5. Context-free Languages: Normal Form and PDA

C5.1 Context-free Grammars and ε-Rules C5.2 Chomsky Normal Form

C5.3 Push-Down Automata

C5.4 Summary

(3)

Overview

Automata &

Formal Languages

Languages

& Grammars

Regular Languages

Context-free Languages

ε-rules

Chomsky Normal Form

PDAs

Pumping Lemma Closure Properties

Decidability Context-sensitive &

Type-0 Languages

(4)

C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules

C5.1 Context-free Grammars and

ε-Rules

(5)

C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules

Repetition: Context-free Grammars

Definition (Context-free Grammar)

A context-free grammar is a 4-tuple hΣ, V , P , S i with

1

Σ finite alphabet of terminal symbols,

2

V finite set of variables (with V ∩ Σ = ∅),

3

P ⊆ (V × (V ∪ Σ) + ) ∪ {hS, εi} finite set of rules,

4

If S → ε ∈ P , then all other rules in V × ((V \ {S}) ∪ Σ) + .

5

S ∈ V start variable.

Rule X → ε is only allowed if X = S and S never occurs on a right-hand side.

With regular grammars, this restriction could be lifted.

How about context-free grammars?

(6)

C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules

Overview

Automata &

Formal Languages

Languages

& Grammars

Regular Languages

Context-free Languages

ε-rules

Chomsky Normal Form

PDAs

Pumping Lemma Closure Properties

Decidability Context-sensitive &

Type-0 Languages

(7)

C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules

ε-Rules

Theorem

For every grammar G with rules P ⊆ V × (V ∪ Σ) there is a context-free grammar G 0 with L(G ) = L(G 0 ).

Proof.

Let G = hΣ, V , P , S i be a grammar with P ⊆ V × (V ∪ Σ) . Let V ε = {A ∈ V | A ⇒ ε}. We can find this set V ε by first collecting all variables A with rule A → ε ∈ P and then successively adding additional variables B if there is a rule B → A 1 A 2 . . . A k ∈ P and the variables A i are already in the set

for all 1 ≤ i ≤ k. . . .

(8)

C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules

ε-Rules

Theorem

For every grammar G with rules P ⊆ V × (V ∪ Σ) there is a context-free grammar G 0 with L(G ) = L(G 0 ).

Proof (continued).

Let P 0 be the rule set that is constructed from P by

I adding rules that obviate the need for A → ε rules:

for every existing rule B → w with B ∈ V , w ∈ (V ∪ Σ) + , let I ε be the set of positions where w contains a variable A ∈ V ε . For every non-empty set I 0 ⊆ I ε , add a new rule B → w 0 , where w 0 is constructed from w by removing the variables at all positions in I 0 .

I removing all rules of the form A → ε (after the previous step).

. . .

(9)

C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules

ε-Rules

Theorem

For every grammar G with rules P ⊆ V × (V ∪ Σ) there is a context-free grammar G 0 with L(G ) = L(G 0 ).

Proof (continued).

Then L(G ) \ {ε} = L(hΣ, V , P 0 , Si) and P 0 contains no rule A → ε. If the start variable S of G is not in V ε , we are done.

Otherwise, let S 0 be a new variable and construct P 00 from P 0 by

1

replacing all occurrences of S on the right-hand side of rules with S 0 ,

2

adding the rule S 0 → w for every rule S → w , and

3

adding the rule S → ε.

Then L(G ) = L(hΣ, V ∪ {S 0 }, P 00 , S i).

(10)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

C5.2 Chomsky Normal Form

(11)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Overview

Automata &

Formal Languages

Languages

& Grammars

Regular Languages

Context-free Languages

ε-rules

Chomsky Normal Form

PDAs

Pumping Lemma Closure Properties

Decidability Context-sensitive &

Type-0 Languages

(12)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Chomsky Normal Form: Motivation

As in logical formulas (and other kinds of structured objects), normal forms for grammars are useful:

I they show which aspects are critical for defining grammars and which ones are just syntactic sugar

I they allow proofs and algorithms to be restricted

to a limited set of grammars (inputs): those in normal form

Hence we now consider a normal form for context-free grammars.

(13)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Chomsky Normal Form: Definition

Definition (Chomsky Normal Form)

A context-free grammar G is in Chomsky normal form (CNF) if all rules have one of the following three forms:

I A → BC with variables A, B, C , or

I A → a with variable A, terminal symbol a, or

I S → ε with start variable S . German: Chomsky-Normalform

in short: rule set P ⊆ (V × (VV ∪ Σ)) ∪ {hS, εi}

(14)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Chomsky Normal Form: Theorem

Theorem

For every context-free grammar G there is a context-free grammar G 0 in Chomsky normal form with L(G) = L(G 0 ).

Proof.

The following algorithm converts the rule set of G into CNF:

Step 1: Eliminate rules of the form A → B with variables A, B . If there are sets of variables {B 1 , . . . , B k } with rules

B 1 → B 2 , B 2 → B 3 , . . . , B k−1 → B k , B k → B 1 , then replace these variables by a new variable B.

Define a strict total order < on the variables such that A → B ∈ P

implies that A < B. Iterate from the largest to the smallest

variable A and eliminate all rules of the form A → B while adding

rules A → w for every rule B → w with w ∈ (V ∪ Σ) + . . . .

(15)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Chomsky Normal Form: Theorem

Theorem

For every context-free grammar G there is a context-free grammar G 0 in Chomsky normal form with L(G) = L(G 0 ).

Proof (continued).

Step 2: Eliminate rules with terminal symbols on the

Step 2:

right-hand side that do not have the form A → a.

For every terminal symbol a ∈ Σ add a new variable A a and the rule A a → a.

Replace all terminal symbols in all rules that do not have

the form A → a with the corresponding newly added variables. . . .

(16)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Chomsky Normal Form: Theorem

Theorem

For every context-free grammar G there is a context-free grammar G 0 in Chomsky normal form with L(G) = L(G 0 ).

Proof (continued).

Step 3: Eliminate rules of the form A → B 1 B 2 . . . B k with k > 2 For every rule of the form A → B 1 B 2 . . . B k with k > 2, add new variables C 2 , . . . , C k−1 and replace the rule with

A → B 1 C 2 C 2 → B 2 C 3

.. .

C k−1 → B k−1 B k

(17)

C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form

Chomsky Normal Form: Length of Derivations

Observation

Let G be a grammar in Chomsky normal form,

and let w ∈ L(G ) be a non-empty word generated by G .

Then all derivations of w have exactly 2|w | − 1 derivation steps.

Proof.

Exercises

(18)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

C5.3 Push-Down Automata

(19)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Overview

Automata &

Formal Languages

Languages

& Grammars

Regular Languages

Context-free Languages

ε-rules

Chomsky Normal Form

PDAs

Pumping Lemma Closure Properties

Decidability Context-sensitive &

Type-0 Languages

(20)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Limitations of Finite Automata

q

0

0 q

1

q

2

0,1

0

I Language L is regular.

⇐⇒ There is a finite automaton that accepts L.

I What information can a finite automaton “store”

about the already read part of the word?

I Infinite memory would be required for

L = {x 1 x 2 . . . x n x n . . . x 2 x 1 | n > 0, x i ∈ {a, b}}.

I therefore: extension of the automata model with memory

(21)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Stack

A stack is a data structure following the last-in-first-out (LIFO) principle supporting the following operations:

I push: puts an object on top of the stack

I pop: removes the object at the top of the stack

I peek: returns the top object without removing it

Push Pop

German: Keller, Stapel

(22)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Visually

Input tape

I n p u t

Read head

Push-down automaton

Stack access

Stack

German: Kellerautomat, Eingabeband, Lesekopf, Kellerzugriff

(23)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Definition

Definition (Push-down Automaton)

A push-down automaton (PDA) is a 6-tuple M = hQ , Σ, Γ, δ, q 0 , #i with

I Q finite set of states

I Σ the input alphabet

I Γ the stack alphabet

I δ : Q × (Σ ∪ {ε}) × Γ → P f (Q × Γ ) the transition function (where P f is the set of all finite subsets)

I q 0 ∈ Q the start state

I # ∈ Γ the bottommost stack symbol

German: Kellerautomat, Eingabealphabet, Kelleralphabet,

German: Uberf¨ ¨ uhrungsfunktion

(24)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Transition Function

Let M = hQ, Σ, Γ, δ, q 0 , #i be a push-down automaton.

What is the Intuitive Meaning of the Transition Function δ?

I hq 0 , B 1 . . . B k i ∈ δ(q, a, A): If M is in state q, reads symbol a and has A as the topmost stack symbol,

then M can transition to q 0 in the next step while replacing A with B 1 . . . B k (afterwards B 1 is the topmost stack symbol)

q a, A → B

1

. . . B

k

q

0

I special case a = ε is allowed (spontaneous transition)

(25)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Example

q q

0

a, A → AA a, B → AB a, # → A#

b, A → BA b, B → BB b, # → B#

a, A → ε b, B → ε

a,A → ε b,B → ε ε, # → ε

M = h{q, q 0 }, { a, b }, { A, B, # }, δ, q, # i with

δ(q, a, A) = {hq, AAi, hq

0

, εi} δ(q, b, A) = {hq, BAi} δ(q, ε, A) = ∅ δ(q, a, B) = {hq, ABi} δ(q, b, B) = {hq, BBi, hq

0

, εi} δ(q, ε, B) = ∅ δ(q, a, #) = {hq, A#i} δ(q, b, #) = {hq, B#i} δ(q, ε, #) = ∅ δ(q

0

, a, A) = {hq

0

, εi} δ(q

0

, b, A) = ∅ δ(q

0

, ε, A) = ∅ δ(q

0

, a, B) = ∅ δ(q

0

, b, B) = {hq

0

, εi} δ(q

0

, ε, B) = ∅

δ(q

0

, a, #) = ∅ δ(q

0

, b, #) = ∅ δ(q

0

, ε, #) = {hq

0

, εi}

(26)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Configuration

Definition (Configuration of a Push-down Automaton)

A configuration of a push-down automaton M = hQ, Σ, Γ, δ, q 0 , #i is given by a triple c ∈ Q × Σ × Γ .

German: Konfiguration Example

I n p u t

q

Configuration

hq, ut, BAC#i.

(27)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Steps

Definition (Transition/Step of a Push-down Automaton)

We write c ` M c 0 if a push-down automaton M = hQ, Σ, Γ, δ, q 0 , #i can transition from configuration c to configuration c 0 in one step.

Exactly the following transitions are possible:

hq, a 1 . . . a n , A 1 . . . A m i ` M

 

 

 

 

hq 0 , a 2 . . . a n , B 1 . . . B k A 2 . . . A m i if hq 0 , B 1 . . . B k i ∈ δ(q, a 1 , A 1 ) hq 0 , a 1 a 2 . . . a n , B 1 . . . B k A 2 . . . A m i

if hq 0 , B 1 . . . B k i ∈ δ(q, ε, A 1 ) German: Ubergang ¨

If M is clear from context, we only write c ` c 0 .

(28)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Reachability of Configurations

Definition (Reachable Configuration)

Configuration c 0 is reachable from configuration c in PDA M (c ` M c 0 ) if there are configurations c 0 , . . . , c n (n ≥ 0) where

I c 0 = c ,

I c i ` M c i+1 for all i ∈ {0, . . . , n − 1}, and

I c n = c 0 .

German: c

0

ist in M von c erreichbar

(29)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Recognized Words

Definition (Recognized Word of a Push-down Automaton) PDA M = hQ, Σ, Γ, δ, q 0 , #i recognizes the word w = a 1 . . . a n

iff the configuration hq, ε, εi (word processed and stack empty) for some q ∈ Q is reachable from the start configuration hq 0 , w , # i.

M recognizes w iff hq 0 , w , #i ` M hq, ε, εi for some q ∈ Q.

German: M erkennt w , Startkonfiguration

(30)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Recognized Word Example

q q

0

a, A → AA a, B → AB a, # → A#

b, A → BA b, B → BB b, # → B#

a, A → ε b, B → ε

a,A → ε b,B → ε ε, # → ε

example: this PDA recognizes bbabbabb blackboard

(31)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

Push-down Automata: Accepted Language

Definition (Accepted Language of a Push-down Automaton) Let M be a push-down automaton with input alphabet Σ.

The language accepted by M is defined as

L(M ) = {w ∈ Σ | M recognizes w }.

example: blackboard

(32)

C5. Context-free Languages: Normal Form and PDA Push-Down Automata

PDAs Accept Exactly the Context-free Languages

Theorem

A language L is context-free if and only if

L is accepted by a push-down automaton.

(33)

C5. Context-free Languages: Normal Form and PDA Summary

C5.4 Summary

(34)

C5. Context-free Languages: Normal Form and PDA Summary

Summary

I Every context-free language has a grammar in Chomsky normal form. All rules have form

I

A → BC with variables A, B, C, or

I

A → a with variable A, terminal symbol a, or

I

S → ε with start variable S.

I Push-down automata (PDAs) extend NFAs with memory.

I PDAs accept not with end states but with an empty stack.

I The languages accepted by PDAs are exactly

the context-free languages.

Referenzen

ÄHNLICHE DOKUMENTE

B7.1 Context-free Grammars B7.2 Chomsky Normal Form B7.3 Push-Down Automata B7.4 Summary... B7.1

Picture courtesy of imagerymajestic / FreeDigitalPhotos.net Gabriele R¨ oger (University of Basel) Theory of Computer Science March 31, 2021 4 / 26... Closure under

If w does not encode such a sequence: enter an infinite loop?. If w ∈ PCP then the procedure terminates with “yes”, otherwise it does

Gabriele R¨ oger (University of Basel) Theory of Computer Science April 25/27, 2021 2 /

Question: Is there a sequence of dominos such that the upper and lower row match (= are equal). 1 011

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy

The word problem, emptiness problem and finiteness problem for the class of context-free languages are decidable. The equivalence problem and intersection problem for the class

Picture courtesy of imagerymajestic / FreeDigitalPhotos.net Gabriele R¨ oger (University of Basel) Theory of Computer Science April 3, 2019 5 /