cs-320 computer language processing · computer language processing exercise session 1 october 4,...

24
CS-320 Computer Language Processing Exercise Session 1 October 4, 2018

Upload: others

Post on 19-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

CS-320Computer Language Processing

Exercise Session 1

October 4, 2018

Page 2: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Overview

We will talk about and do exercises on the following topics:1. Regular languages,2. Finite state machines,3. how to determinize them, and4. how to minimize them.

Page 3: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Regular languages

Alphabet Σ is a set of symbols {a, b, c, . . . }.

A word w is a sequence of symbols si ∈ Σ.

We denote the empty word by ε.

A language L is a set of words.

Page 4: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Operations on regular languages

We define several operations on regular languages:I Concatenation L1 · L2,I Union L1 ∪ L2, andI Kleene closure L∗.

Other operations such as ·+, ·? can be expressed using the above.

Page 5: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Finite-state automata

A deterministic finite-state automaton (DFA) is defined by aquintuple 〈Σ,Q, s0, δ,F 〉 where

I Σ is a (finite) set of symbols called the alphabet,I Q is the finite set of states,I s0 ∈ Q is the initial state,I δ : (Q × Σ)→ Q is called the transition function, andI F ⊆ Q is the set of accepting states.

For nondeterministic finite-state automatons (NFAs) δ is notnecessarily a function, i.e., in general we only have δ ⊆ Q ×Σ×Q.

Page 6: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

A simple regular languageExercise 1

. Find a finite-state automaton that accepts the language given by(a | b)+.

q0start q1a,b

a,b

Page 7: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

A simple regular languageExercise 1

. Find a finite-state automaton that accepts the language given by(a | b)+.

q0start q1a,b

a,b

Page 8: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Even binary numbersExercise 2

. Find a finite-state automaton that accepts the even binarynumbers (e.g., 0, 10, 100, 110, . . . ).

q0start q1

01

1

0

Page 9: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Even binary numbersExercise 2

. Find a finite-state automaton that accepts the even binarynumbers (e.g., 0, 10, 100, 110, . . . ).

q0start q1

01

1

0

Page 10: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Binary numbers divisible by threeExercise 3

. Find a finite-state automaton that accepts all binary numbersdivisible by three.

q0start q1 q2

10

0

1 0

1

Page 11: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Binary numbers divisible by threeExercise 3

. Find a finite-state automaton that accepts all binary numbersdivisible by three.

q0start q1 q2

10

0

1 0

1

Page 12: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but oneExercise 4

. Find a regular expression that describes the language of all wordsover alphabet {a, b, c} which contain at most two of the threesymbols (e.g., a, acac, ccccbbbbbb, . . . ).

(a | b)∗ | (a | c)∗ | (b | c)∗

. Find an NFA which accepts the language.

Page 13: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but oneExercise 4

. Find a regular expression that describes the language of all wordsover alphabet {a, b, c} which contain at most two of the threesymbols (e.g., a, acac, ccccbbbbbb, . . . ).

(a | b)∗ | (a | c)∗ | (b | c)∗

. Find an NFA which accepts the language.

Page 14: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but oneExercise 4

. Find a regular expression that describes the language of all wordsover alphabet {a, b, c} which contain at most two of the threesymbols (e.g., a, acac, ccccbbbbbb, . . . ).

(a | b)∗ | (a | c)∗ | (b | c)∗

. Find an NFA which accepts the language.

Page 15: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but one: NFAExercise 4

(a | b)∗ | (a | c)∗ | (b | c)∗

m

0start

1

2

3

ε

ε

ε

a,b

a,c

b,c

. What does an equivalent DFA look like?

Page 16: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but one: NFAExercise 4

(a | b)∗ | (a | c)∗ | (b | c)∗

m

0start

1

2

3

ε

ε

ε

a,b

a,c

b,c

. What does an equivalent DFA look like?

Page 17: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Recap: DeterminizationFor each NFA 〈Σ,Q, q0, δ,F 〉 there isan equivalent DFA 〈Σ, 2Q, q′0, δ′,F ′〉 with

q′0 = E (q0),δ′(q′, a) =

⋃∃q1∈q′

E (δ(q1, a)), and

F ′ = {q′ | q′ ∈ 2Q ∧ q′ ∩ F 6= ∅}.

Note that for undefined transitions on symbol a in state q we get

δ′({q}, a) = ∅,

and similarly for the trap state ∅ we get

δ′(∅, a) = ∅.

Page 18: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Recap: DeterminizationFor each NFA 〈Σ,Q, q0, δ,F 〉 there isan equivalent DFA 〈Σ, 2Q, q′0, δ′,F ′〉 with

q′0 = E (q0),δ′(q′, a) =

⋃∃q1∈q′

E (δ(q1, a)), and

F ′ = {q′ | q′ ∈ 2Q ∧ q′ ∩ F 6= ∅}.

Note that for undefined transitions on symbol a in state q we get

δ′({q}, a) = ∅,

and similarly for the trap state ∅ we get

δ′(∅, a) = ∅.

Page 19: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but one: DFAExercise 4

{0, 1, 2, 3}start

{1, 2}

{1, 3}

{2, 3}

{1}

{2}

{3}

a

b

c

a

b

cb

a

c

c

a

b

a,b

ca,c

b

b,c

a

a,b,c

. What is the significance of the intermediate states?

Page 20: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

All but one: DFAExercise 4

{0, 1, 2, 3}start

{1, 2}

{1, 3}

{2, 3}

{1}

{2}

{3}

a

b

c

a

b

cb

a

c

c

a

b

a,b

ca,c

b

b,c

a

a,b,c

. What is the significance of the intermediate states?

Page 21: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Minimization

We can minimize DFAs by collapsing equivalent states.

We will consider two states s1 and s2 equivalent, if they areindistinguishable wrt. acceptance.

That is, s1 is equivalent to s2, if, for any word w , following theautomaton’s transitions from state s1, respectively s2, we end up intwo accepting or two rejecting states.

Page 22: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

Minimization

One simple algorithm for minimizing DFAs:

Use a table (with one column and one row per state) to graduallymark all non-equivalent pairs of states.

1. Initialize the table by marking all pairs of states where one isaccepting and the other is not.

2. For every symbol a and for every pair of states s1 and s2,mark the pair, if δ(s1, a) is not equivalent to δ(s2, a).

3. Repeat the second step until no more additionalnon-equivalent pairs are found.

Page 23: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

MinimizationExercise 5

. Minimize the following DFA.

q0start

q1

q4

q2 q3

q5 q6

a

b

a

b

a,ba,b

a,b

ab

a

b

Page 24: CS-320 Computer Language Processing · Computer Language Processing Exercise Session 1 October 4, 2018. Overview We will talk about and do exercises on the following topics: 1.Regular

MinimizationExercise 5

Minimized:

q0start

q{1,6}

q4

q{2,3}

q5

a

b

a

ba,b

a,ba

b