• Keine Ergebnisse gefunden

Automatentheorie und formale Sprachen kontextfreie Sprachen

N/A
N/A
Protected

Academic year: 2022

Aktie "Automatentheorie und formale Sprachen kontextfreie Sprachen"

Copied!
16
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Automatentheorie und formale Sprachen kontextfreie Sprachen

Dozentin: Wiebke Petersen

24.6.2009

(2)

Typ 2-Sprachen: kontextfreie Grammatiken und Sprachen

Denition

Eine Grammatik(N,T,S,P)heiÿtkontextfrei, wenn alle Regeln/Produktionen die folgende Form haben:

A→α, mit A∈N undα∈(T∪N).

Eine durch eine kontextfreie Grammatik erzeugte Sprache heiÿt kontextfrei.

Theorem

Die Menge der kontextfreien Sprachen bildet eine echte Obermenge der Menge der regulären Sprachen.

Proof: Aus der Denition rechts-linearer Grammatiken folgt, dass jede reguläre Sprache kontextfrei ist.

L(anbn)ist kontextfrei, aber nicht regulär (S→aSb,S→).

(3)

Typ 2-Sprachen: kontextfreie Grammatiken und Sprachen

Denition

Eine Grammatik(N,T,S,P)heiÿtkontextfrei, wenn alle Regeln/Produktionen die folgende Form haben:

A→α, mit A∈N undα∈(T∪N).

Eine durch eine kontextfreie Grammatik erzeugte Sprache heiÿt kontextfrei.

Theorem

Die Menge der kontextfreien Sprachen bildet eine echte Obermenge der Menge der regulären Sprachen.

Proof: Aus der Denition rechts-linearer Grammatiken folgt, dass jede reguläre Sprache kontextfrei ist.

L(anbn)ist kontextfrei, aber nicht regulär (S→aSb,S→).

(4)

Typ 2-Sprachen: kontextfreie Grammatiken und Sprachen

Denition

Eine Grammatik(N,T,S,P)heiÿtkontextfrei, wenn alle Regeln/Produktionen die folgende Form haben:

A→α, mit A∈N undα∈(T∪N).

Eine durch eine kontextfreie Grammatik erzeugte Sprache heiÿt kontextfrei.

Theorem

Die Menge der kontextfreien Sprachen bildet eine echte Obermenge der Menge der regulären Sprachen.

Proof: Aus der Denition rechts-linearer Grammatiken folgt, dass jede reguläre Sprache kontextfrei ist.

L(anbn)ist kontextfrei, aber nicht regulär (S→aSb,S→).

(5)

Examples of context-free languages

L

1

= { ww

R

: w ∈ { a , b }

} L

2

= { a

i

b

j

: i ≥ j }

L

3

= { w ∈ { a , b }

: more a

0

s than b

0

s }

L

4

= { w ∈ { a , b }

: number of a

0

s equals number of b

0

s }

S → aB A → a B → b

S → bA A → aS B → bS

S → A → bAA B → aBB

(6)

Examples of context-free languages

L

1

= { ww

R

: w ∈ { a , b }

} L

2

= { a

i

b

j

: i ≥ j }

L

3

= { w ∈ { a , b }

: more a

0

s than b

0

s }

L

4

= { w ∈ { a , b }

: number of a

0

s equals number of b

0

s }

S → aB A → a B → b

S → bA A → aS B → bS

S → A → bAA B → aBB

(7)

Derivation tree

G1=h{S,NP,VP,N,V,D,N,EN},{the, cat, peter, chases},S,Pi

P=

S → NP VP VP → V NP NP → D N

NP → EN D → the N → cat

EN → peter V → chases

S

NP

D the

N cat

VP

V chases

NP EN peter

One derivation determines one derivation tree, but

the same derivation tree can result from dierent derivations.

(8)

Ambiguous grammars and ambiguous languages

Denition

Given a context-free grammar G: A derivation which always replaces the left furthest nonterminal symbol is called left-derivation

Left-derivations and derivation trees determine each other!

Denition

A context-free grammar G is ambiguous i there exists a w ∈ L ( G ) with more than one left-derivation, S →

w.

Denition

A context-free language L is inherently ambiguous i each context-free

grammar G with L ( G ) = L is ambiguous.

(9)

Ambiguous grammars and ambiguous languages

Denition

Given a context-free grammar G: A derivation which always replaces the left furthest nonterminal symbol is called left-derivation

Left-derivations and derivation trees determine each other!

Denition

A context-free grammar G is ambiguous i there exists a w ∈ L ( G ) with more than one left-derivation, S →

w.

Denition

A context-free language L is inherently ambiguous i each context-free

grammar G with L ( G ) = L is ambiguous.

(10)

Ambiguous grammars and ambiguous languages

Denition

Given a context-free grammar G: A derivation which always replaces the left furthest nonterminal symbol is called left-derivation

Left-derivations and derivation trees determine each other!

Denition

A context-free grammar G is ambiguous i there exists a w ∈ L ( G ) with more than one left-derivation, S →

w.

Denition

A context-free language L is inherently ambiguous i each context-free

grammar G with L ( G ) = L is ambiguous.

(11)

Example of an ambiguous grammar

G = (N,T,NP,P)with N ={D,N,P,NP,PP}, T ={the,cat,hat,in}, P=

NP → D N D → the N → hat

NP → NP PP N → cat P → in PP → P NP

CL Preliminaries Chomsky hierarchy Regular languages Context-free languages context-free grammars

Example of an ambiguous grammar

G= (N,T,NP,P)withN ={D,N,P,NP,PP},T ={the,cat,hat,in}, P =

NP → D N D → the N → hat NP → NP PP N → cat P → in PP → P NP

NP

NP

NP

D the

N cat

PP

P in

NP

D the

N hat

PP

P in

NP

D the

N hat

NP

NP

D the

N cat

PP

P in

NP

NP

D the

N hat

PP

P in

NP

D the

N hat

(12)

Chomsky Normal Form

Denition

A grammar is in Chomsky Normal Form (CNF) if all production rules are of the form

1

A → a

2

A → BC

with A , B , C ∈ T and a ∈ Σ (and if necessary S → in which case S may not occur in any right-hand side of a rule).

Theorem

Each context-free language is generated by a grammar in CNF.

(13)

Each context-free language is generated by a grammar in CNF

4 steps

0. Adapt the grammar such that no rules of the type A→occur (if S→, S may not occur in any right-hand-side of a rule).

1. Adapt the grammar such that terminals only occur in rules of type A→a.

2. Eliminate A→B rules.

3. Eliminate A→B1B2. . .Bn (n>2) rules.

example

G= ({S},{a,b},S,P)with P={SaSb,Sab}

1 Adapt the grammar such that terminals only occur in rules of type Aa. P0={SXaSXb,SXaXb,Xaa,Xbb}

2 Eliminate AB rules. P00=P0

3 Eliminate AB1B2. . .Bn (n>2) rules.

P000={SXaZ,ZSXb,SXaXb,Xaa,Xbb}

(14)

Each context-free language is generated by a grammar in CNF

4 steps

0. Adapt the grammar such that no rules of the type A→occur (if S→, S may not occur in any right-hand-side of a rule).

1. Adapt the grammar such that terminals only occur in rules of type A→a.

2. Eliminate A→B rules.

3. Eliminate A→B1B2. . .Bn (n>2) rules.

example

G= ({S},{a,b},S,P)with P={SaSb,Sab}

1 Adapt the grammar such that terminals only occur in rules of type Aa.

P0={SXaSXb,SXaXb,Xaa,Xbb}

2 Eliminate AB rules.

P00=P0

3 Eliminate AB1B2. . .Bn (n>2) rules.

P000 S X Z Z SX S X X X a X b

(15)

Hausaufgaben

Exercise 1

1 Geben sie eine kontextfreie Grammatik zu den folgenden Sprachen an:

L1={wwR :w ∈ {a,b}} L2={aibj :i≥j}

L3={w ∈ {a,b}:w enthält mehr a's als b's}

2 Bringen sie die folgende Grammatik in die Chomsky-Normalenform:

G = (N,T,NP,P)mit N={S, NP, VP, N, V, N, EN}, T={the, cat, dog, peter, mary chases}, und

P=

S NP VP VP V NP NP the N

NP EN N cat N dog

EN peter EN mary V chases

Bringen sie die Grammatik zur Sprache L4 von Folie 3 in Chomsky-Normalenform.

(16)

Gruppenarbeit

Gruppe 1: Führen sie das in Klabunde beschriebene Verfahren zur Konstruktion eines deterministischen endlichen Automaten aus einem nichtdeterministischen Automaten an einem kleinen Beispiel im Detail durch.

Gruppe 2: Beschreiben sie an 2-3 Beispielen, wie man ezient einen deterministischen Automaten aus einem nichtdeterministischen konstruiert.

Gruppe 3: Beschreiben sie an 2-3 Beispielen, wie man-Übergänge eliminiert.

Gruppe 4: Beschreiben sie an Beispielen, wie man endliche Automaten zur Konkatenation zweier regulärer Sprachen und zur Vereinigung und zur Schnittmenge zweier Sprachen bildet, und wie man endliche Automaten zum Kleenschen Stern einer regulären Sprache und zum Komplement einer regulären Sprache konstruiert.

Referenzen

ÄHNLICHE DOKUMENTE

Wir werden in diesem Abschnitt zuerst zeigen, dass f¨ur die im vorangegangenen Abschnitt eingef¨uhrten Typen von Grammatiken jeweils Normalformen existieren, d.h. Grammati- ken

Korrekte Sätze einer (natürlichen) Sprache sollen durch ein (endliches System) von formalen Regeln erzeugt werden. Bis heute ist

Um für dieses Konzept ein wenig mehr Intuition zu gewinnen, sei erwähnt, dass man sich eine solche Trans- formation auch wie folgt vorstellen kann: A lässt sich auf B reduzieren,

Korrekte Sätze einer (natürlichen) Sprache sollen durch ein (endliches System) von formalen Regeln erzeugt werden. Bis heute ist

Um für dieses Konzept ein wenig mehr Intuition zu gewinnen, sei erwähnt, dass man sich eine solche Transformation auch wie folgt vorstellen kann: A lässt sich auf B reduzieren, wenn

Die Chomsky Hierarchie reflektiert eine spezielle Form der Komplexität, andere Kriterien sind denkbar und führen zu anderen Hierarchien. Die Sprachklassen der Chomsky Hierarchie sind

❚ Zeichen können "hintereinandergefügt" werden und bilden durch die Reihung Worte. ❚ Der Operator hierzu

Aufbauend auf den in der „Einführung in die Computerlinguistik“ erworbenen Grundkenntnissen bietet dieses Seminar eine Einführung in die Theorie der Automaten und formalen