• Keine Ergebnisse gefunden

Theory of Computer Science C1. Formal Languages and Grammars Gabriele R¨oger

N/A
N/A
Protected

Academic year: 2022

Aktie "Theory of Computer Science C1. Formal Languages and Grammars Gabriele R¨oger"

Copied!
40
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Theory of Computer Science

C1. Formal Languages and Grammars

Gabriele R¨oger

University of Basel

March 18, 2019

(2)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Introduction

(3)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Course Contents

Parts of the course:

A. backgroundX

. mathematical foundations and proof techniques B. logic(Logik)X

. How can knowledge be represented?

. How can reasoning be automated?

C. automata theory and formal languages (Automatentheorie und formale Sprachen) . What is a computation?

D. Turing computability(Turing-Berechenbarkeit) . What can be computed at all?

E. complexity theory(Komplexit¨atstheorie) . What can be computed efficiently?

F. more computability theory (mehr Berechenbarkeitheorie) . Other models of computability

(4)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Course Contents

Parts of the course:

A. backgroundX

. mathematical foundations and proof techniques B. logic(Logik)X

. How can knowledge be represented?

. How can reasoning be automated?

C. automata theory and formal languages (Automatentheorie und formale Sprachen) . What is a computation?

D. Turing computability(Turing-Berechenbarkeit) . What can be computed at all?

E. complexity theory(Komplexit¨atstheorie) . What can be computed efficiently?

F. more computability theory (mehr Berechenbarkeitheorie) . Other models of computability

(5)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Part C: Overview

Automata Theory &

Formal Languages

Languages

& Grammars

Regular Languages

Context-free Languages

Context-sensitive &

Type-0 Languages

(6)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Example: Propositional Formulas

from the logic part:

Definition (Syntax of Propositional Logic)

LetAbe a set of atomic propositions. The set of propositional formulas(overA) is inductively defined as follows:

Everyatom a∈Ais a propositional formula over A.

Ifϕis a propositional formula over A, then so is its negation ¬ϕ.

Ifϕand ψare propositional formulas over A, then so is theconjunction (ϕ∧ψ).

Ifϕand ψare propositional formulas over A, then so is thedisjunction (ϕ∨ψ).

(7)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Example: Propositional Formulas

LetSA be the set of all propositional formulas overA.

Such sets of symbol sequences (orwords) are calledlanguages.

Sought: General concepts to define such (often infinite) languages with finite descriptions.

today: grammars later: automata

(8)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Example: Propositional Formulas

Example (Grammar forS{a,b,c})

Grammar variables{F,A,N,C,D} with start variable F, terminal symbols{a,b,c,¬,∧,∨,(,)} and rules

F→A A→a N→ ¬F

F→N A→b C→(F∧F)

F→C A→c D→(F∨F)

F→D

Start with F. In each step, replace a left-hand side of a rule with its right-hand side until no more variables are left:

F⇒N⇒ ¬F⇒ ¬D⇒ ¬(F∨F)⇒ ¬(A∨F)⇒ ¬(b∨F)

F

⇒ ¬(b∨A)⇒ ¬(b∨c)

(9)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Alphabets and Formal Languages

(10)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Alphabets and Formal Languages

Definition (Alphabets, Words and Formal Languages) AnalphabetΣ is a finite non-empty set of symbols.

Aword over Σis a finite sequence of elements from Σ.

Theempty word(the empty sequence of elements) is denoted by ε. Σ denotes the set of all words over Σ.

Σ+ (= Σ\ {ε}) denotes the set of all non-empty words over Σ. We write|w|for the length of a word w.

Aformal language(over alphabet Σ) is a subset of Σ.

German: Alphabet, Zeichen/Symbole, leeres Wort, formale Sprache

Example Σ ={a,b}

Σ ={ε,a,b,aa,ab,ba,bb, . . .}

|aba|= 3,|b|= 1,|ε|= 0

(11)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Alphabets and Formal Languages

Definition (Alphabets, Words and Formal Languages) AnalphabetΣ is a finite non-empty set of symbols.

Aword over Σis a finite sequence of elements from Σ.

Theempty word(the empty sequence of elements) is denoted by ε.

Σ denotes the set of all words over Σ.

Σ+ (= Σ\ {ε}) denotes the set of all non-empty words over Σ.

We write|w|for the length of a word w.

Aformal language(over alphabet Σ) is a subset of Σ.

German: Alphabet, Zeichen/Symbole, leeres Wort, formale Sprache

Example Σ ={a,b}

Σ ={ε,a,b,aa,ab,ba,bb, . . .}

|aba|= 3,|b|= 1,|ε|= 0

(12)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Alphabets and Formal Languages

Definition (Alphabets, Words and Formal Languages) AnalphabetΣ is a finite non-empty set of symbols.

Aword over Σis a finite sequence of elements from Σ.

Theempty word(the empty sequence of elements) is denoted by ε.

Σ denotes the set of all words over Σ.

Σ+ (= Σ\ {ε}) denotes the set of all non-empty words over Σ.

We write|w|for the length of a wordw.

Aformal language(over alphabet Σ) is a subset of Σ.

German: Alphabet, Zeichen/Symbole, leeres Wort, formale Sprache

Example Σ ={a,b}

Σ ={ε,a,b,aa,ab,ba,bb, . . .}

|aba|= 3,|b|= 1,|ε|= 0

(13)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Alphabets and Formal Languages

Definition (Alphabets, Words and Formal Languages) AnalphabetΣ is a finite non-empty set of symbols.

Aword over Σis a finite sequence of elements from Σ.

Theempty word(the empty sequence of elements) is denoted by ε.

Σ denotes the set of all words over Σ.

Σ+ (= Σ\ {ε}) denotes the set of all non-empty words over Σ.

We write|w|for the length of a wordw.

Aformal language(over alphabet Σ) is a subset of Σ. German: Alphabet, Zeichen/Symbole, leeres Wort, formale Sprache

Example Σ ={a,b}

Σ ={ε,a,b,aa,ab,ba,bb, . . .}

|aba|= 3,|b|= 1,|ε|= 0

(14)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(15)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(16)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(17)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(18)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(19)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(20)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Languages: Examples

Example (Languages over Σ ={a,b}) S1 ={a,aa,aaa,aaaa, . . .}={a}+ S2 = Σ

S3 ={anbn|n ≥0}={ε,ab,aabb,aaabbb, . . .} S4 ={ε}

S5 =∅

S6 ={w ∈Σ |w contains twice as many as asbs}

S6

={ε,aab,aba,baa, . . .} S7 ={w ∈Σ | |w|= 3}

S6

={aaa,aab,aba,baa,bba,bab,abb,bbb}

(21)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Questions

Questions?

(22)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Grammars

(23)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Grammars

Definition (Grammars)

Agrammaris a 4-tuplehΣ,V,P,Si with:

1 Σ finite alphabet of terminal symbols

2 V finite set of variables(nonterminal symbols) with V ∩Σ =∅

3 P ⊆(V ∪Σ)+×(V ∪Σ) finite set ofrules(or productions)

4 S ∈V start variable

German: Grammatik, Terminalalphabet, Variablen, Regeln/Produktionen, German: Startvariable

(24)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Rule Sets

What exactly doesP ⊆(V ∪Σ)+×(V ∪Σ) mean?

(V ∪Σ): all words over (V ∪Σ)

(V ∪Σ)+: all non-empty words over (V ∪Σ) in general, for setX: X+=X\ {ε}

×: Cartesian product

(V ∪Σ)+×(V ∪Σ): set of all pairs hx,yi, wherex non-empty word over (V ∪Σ) and y word over (V ∪Σ) Instead of hx,yi we usually write rules in the formx →y.

(25)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Rules: Examples

Example

Let Σ ={a,b,c} andV ={X,Y,Z}.

Some examples of rules in (V ∪Σ)+×(V ∪Σ): X→XaY

Yb→a XY→ε XYZ→abc

abc→XYZ

(26)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Derivations

Definition (Derivations)

LethΣ,V,P,Si be a grammar. A word v∈(V ∪Σ) can be derivedfrom word u∈(V ∪Σ)+ (written as u⇒v) if

1 u =xyz,v =xy0z with x,z ∈(V ∪Σ) and

2 there is a rule y →y0 ∈P.

We write: u⇒v ifv can be derived fromu in finitely many steps (i. e., by usingn derivations forn ∈N0).

German: Ableitung

(27)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Language Generated by a Grammar

Definition (Languages)

Thelanguage generatedby a grammar G =hΣ,V,P,Si L(G) ={w ∈Σ |S ⇒w}

is the set of all words from Σ that can be derived fromS with finitely many rule applications.

German: erzeugte Sprache

(28)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Grammars

Examples: blackboard

(29)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Questions

Questions?

(30)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Chomsky Hierarchy

(31)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Chomsky Hierarchy

Grammars are organized into theChomsky hierarchy.

Definition (Chomsky Hierarchy)

Every grammar is of type 0(all rules allowed).

Grammar is oftype 1 (context-sensitive) if all rules w1 →w2 satisfy|w1| ≤ |w2|.

Grammar is oftype 2 (context-free)

if additionallyw1∈V (single variable) in all rulesw1 →w2. Grammar is oftype 3 (regular)

if additionallyw2∈Σ∪ΣV in all rulesw1→w2.

special case: rule S →εis always allowed ifS is the start variable and never occurs on the right-hand side of any rule.

German: Chomsky-Hierarchie, Typ 0, Typ 1 (kontextsensitiv), Typ 2 (kontextfrei), Typ 3 (regul¨ar)

(32)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Chomsky Hierarchy

Examples: blackboard

(33)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Chomsky Hierarchy

Definition (Type 0–3 Languages)

A languageL⊆Σ is of type 0 (type 1, type 2, type 3) if there exists a type-0 (type-1, type-2, type-3) grammarG withL(G) =L.

(34)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Type k Language: Example

Example

Consider the languageL generated by the grammar h{a,b,c,¬,∧,∨,(,)},{F,A,N,C,D},P,Fi

with the following rulesP:

F→A A→a N→ ¬F

F→N A→b C→(F∧F)

F→C A→c D→(F∨F)

F→D Questions:

Is La type-0 language?

Is La type-1 language?

Is La type-2 language?

Is La type-3 language?

(35)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Chomsky Hierarchy

regular languages (type 3) context free languages (type 2) context sensitive languages (type 1)

Type-0 languages All languages

Note: Not all languages can be described by grammars. (Proof?)

(36)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Chomsky Hierarchy

regular languages (type 3) context free languages (type 2) context sensitive languages (type 1)

Type-0 languages All languages

Note: Not all languages can be described by grammars. (Proof?)

(37)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Questions

Questions?

(38)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Summary

(39)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Summary

Languages are sets of symbol sequences.

Grammarsare one possible way to specify languages.

Language generated by a grammar is the set of all words (of terminal symbols) derivablefrom the start symbol.

Chomsky hierarchy distinguishes between languages at different levels of expressiveness.

following chapters:

more about regular languages

automata as alternative representation of languages

(40)

Introduction Alphabets and Formal Languages Grammars Chomsky Hierarchy Summary

Summary

Languages are sets of symbol sequences.

Grammarsare one possible way to specify languages.

Language generated by a grammar is the set of all words (of terminal symbols) derivablefrom the start symbol.

Chomsky hierarchy distinguishes between languages at different levels of expressiveness.

following chapters:

more about regular languages

automata as alternative representation of languages

Referenzen

ÄHNLICHE DOKUMENTE

I All usual decision problems (word problem, emptiness, finiteness, intersection, equivalence) are decidable for

GNFAs are like NFAs but the transition labels can be arbitrary regular expressions over the input alphabet. q 0

I DFAs, NFAs and regular grammars can all describe exactly the regular languages.. I Are there other concepts with the

Picture courtesy of imagerymajestic / FreeDigitalPhotos.net Gabriele R¨ oger (University of Basel) Theory of Computer Science March 24, 2021 4 / 15... Question: what if L

Similarly, there can be transitions that do not pop and/or push a symbol off/to the stack. Deterministic variants of PDAs are strictly less

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

Random-access machine (RAM): adds indirect addressing Random-access stored-program machines: related to the von Neumann architecture (very close to modern computer

C1.1 Hilbert’s 10th Problem C1.2 Church-Turing Thesis C1.3 Encoding?.