regular languages sequential machine theory prof. k. j. hintz department of electrical and computer...

46
Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications by Marek Perkowski

Post on 20-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular LanguagesRegular Languages

Sequential Machine Theory

Prof. K. J. HintzDepartment of Electrical and Computer Engineering

Lecture 3

Comments, additions and modifications by Marek Perkowski

Page 2: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

LanguagesLanguages

• Informal Languages– English– Body– Bureaucratize

• Formal Languages– Rule-based– Elements are decidable– No deeper understanding required

Page 3: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Formal LanguageFormal Language

• All the Rules of the Language Are Explicitly Stated in Terms of the Allowed Strings of Symbols, e.g.,– Programming languages, e.g., C, Lisp, Ada– Military communications (formal “informal” L)– Digital network protocols

Page 4: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

A to ...A to ...

Alphabet: a finite set of symbols, aka I, – Roman: { a, b, c, ... , z }– Binary: { 0, 1 }– Greek: { }– Cyrillic: {Ж, Й, Њ, С, Р, ... }

Page 5: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

StringString

String, word: a finite ordered sequence of symbols from the alphabet, usually written with no intervening punctuation– x1 = “ t h e “

– x2 = “ 0 1 0 1 1 0 “

– x3 = “ “

– x4 = “ Ж Й С Р “

Page 6: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

StringString

• Reverse of String– The sequence of symbols written backwards

• Reverse of Concatenation– Strings themselves must be reversed

x r1 " e h t "

RRR xyyx

Page 7: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

StringString

• Length or Size of String– The number of symbols

13

49

63

43

43

21

xx

xx

xx

Page 8: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

StringsStrings

• Null String, Empty String, e, – A string of length or size zero– The symbols e or , meant to denote the null

string, are not allowed to be part of the language

Page 9: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

SubstringSubstring

• A String, v, Is a Substring of a String, w, iff There Are Strings x and y Such That – w = x v y– x is called the prefix– y is called the suffix– x and/or y could be

Page 10: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Kleene ClosureKleene Closure

• Set of All Strings, *, I*– Order IS important– Not the same as , the powerset of the

alphabet, since order is NOT important in the powerset

P

Page 11: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Concatenation OperatorConcatenation Operator

• If x, y I*, then the concatenation of x and y is written as– z = x y– e.g., if

• x = “Red” | x | = 3

• y = “skins” | y | = 5

• z = x y = “Redskins“ | z | = 8

Page 12: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Concatenation OperatorConcatenation Operator

• Concatenation of Any String With the Null String Results in the Original String– x e = e x = x– x = x = x

• Concatenation is Associative– x = abc y=def z= ghi– ( x y ) z = x ( y z )– abcdefghi = abcdefghi

Page 13: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

LanguageLanguage

• Language, L: Any Subset of the Set of All Strings of an Alphabet

*

*

I

L

L

I*L1 L2

Page 14: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Classes of LanguagesClasses of Languages

• Enumerated Languages– Defined by a List of All Words in the Language

• Le = { “quidditch”, “nimbus 2000”, }

• not very interesting

• Rule-based– Defined by Properties or a Set of Rules

L r w w P I * : has the property

Page 15: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Rule Based LanguagesRule Based Languages

• A Test to Determine Whether a String Is a Member of a Language

• A Means of Constructing Strings That Are in the Language– Must be able to construct ALL strings in the

language– Must be able to construct ONLY strings in the

language

Page 16: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Rule-Based Language ExampleRule-Based Language Example

Let I = { a, b }

• A Language That Consists of All Two Letter Strings– L = { aa, ab, ba, bb } – is not an element of the language

Page 17: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Empty LanguageEmpty Language

• Null Language, Empty Language, : The Language With No Words in It– Not the same as – can be made into a language with words

– A language consisting only of is still a language

L

Page 18: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Kleene StarKleene Star

If is a language, then

• L* Is the Set of All Strings Obtained by Concatenating Zero or More Strings of L.

• Concatenation of Zero Strings Is • Concatenation of One String Is the String

Itself

• L+ = L* - { }

L I *

Page 19: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Kleene Closure ExampleKleene Closure Example

• L = { 0, 1}L* = { ,

0, 00, 000, ... , 0*,

1, 11, 111, ... , 1*,

01, 001, 0001, ... , 0*1,

... }

Page 20: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Kleene Closure ExamplesKleene Closure Examples

• L = { ab, f }

L* = { ,

ab, abf, fab, ffab, ffabf, ... }

• * = { }• if L = { }

then L* = { }

Page 21: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Kleene Closure ExamplesKleene Closure Examples

Let I = { a, b }

• L = Language ( ( ab )* ){, ab, abab, ababab, ... }

which is not the same as

• L = Language ( a* b* ){, a, b, ab, aab, abb, ... }

The language of all strings of a’s and b’s in which the a’s, if any, come before the b’s

Page 22: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Recursive Language DefinitionRecursive Language Definition

• Variation of Rule-Based

• Three-step Process1. Specify some basic elements of the set

2. Specify the rules for forming new elements from old elements of the set

3. Specify that elements not in 1 or 2 above are NOT elements of the set

Page 23: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Recursive ExampleRecursive Example

• Two Equivalent Recursive Definitions of Rational Numbers– Rational #1

1. Rat_1 = { -, ... -3, -2, -1, 1, 2, 3, ... , }2. if p, q Rat_1, then p/q Rat_1

3. the only rational numbers are those generated by 1 and 2 above.

Page 24: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Recursive ExampleRecursive Example

– Rational #21. Rat_2 = { -1, +1 }

2. if p, q Rat_2, p,q != 0, then (p+q)/p Rat_2

3. the only rational numbers are those generated by 1 and 2 above.

e.g.,

generates all integers

1 1

12

2 1

13

1 1

1

n

n

Page 25: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Interest in Recursive DefinitionsInterest in Recursive Definitions

• Allows Us to Prove Some Statements About What Is Computable.

• Leads to Proof by Induction

Page 26: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Principle of Mathematical Induction*

Principle of Mathematical Induction*

Let A Be a Subset of the Natural Numbers

• 0 A, and

• for each natural number, n, – if { 0, 1, ..., n } A ,– implies (n + 1) A– then A = N

* Lewis & Papadimitriou, pg. 24

Page 27: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Mathematical InductionMathematical Induction

• In practice, mathematical induction is used to prove assertions of the form

For all natural numbers, n,

property P is true

Page 28: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Mathematical Induction PracticeMathematical Induction Practice

To prove statements of the formA = { n : P is true of n }, three steps

1. Basis Step: show that 0 A,

i.e., P is true of n = 0

2. Induction Hypothesis: assume that for some arbitrary, but fixed n > 0, P holds for each natural number 0, 1, ... , n

Page 29: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Mathematical Induction PracticeMathematical Induction Practice

3. Induction Step: use the induction hypothesis (that P is true of n) to show that P is true of (n + 1)

• By the Induction Principle, Then A=N and Hence, P Is True of Every Natural Number.

Page 30: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Induction Example*Induction Example*

* Lewis & Papadimitriou, pg. 25

1. Basis Step

Show that for any n

nn n

0

1 22

2

,

00 0

2

0 0

0

2

true for n

Page 31: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Induction ExampleInduction Example

2. Induction Hypothesis

Assume that for some

when

n

mm m

m n

0

1 22

2

,

Page 32: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Induction ExampleInduction Example

3. Induction Step

1 2 1 1 2 1

1 22

21

2 2

2

2

2

2

n n n n

nn n

n nn

n n n

where is replaced by from the

induction hypothesis

Page 33: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Induction ExampleInduction Example

n n n

n n

n n

2

2

2 1 1

2

1 1

2

0 1

which shows that the hypothesis is true since if it was true

for then it must be true for any ,

Page 34: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Another Induction ExampleAnother Induction Example

• Define EVEN as

1. 0 is in EVEN

2. if x EVEN then so is x + 2

3. The only elements of EVEN are those produced by 1 & 2 above.

• Prove by induction that all of elements of EVEN end in either 0, 2, 4, 6, or 8.

Page 35: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Induction Example (cont)Induction Example (cont)

Proof1. Basis Step

0 EVEN by definition, therefore the property is true of the zero’th step since 0 { 0, 2, 4, 6, 8 }

2. Induction HypothesisAssume that the last digit of

(m+2) { 0, 2, 4, 6, 8 } for 0 < m < n

Page 36: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Induction ExampleInduction Example

3. Induction Stepn EVEN n EVEN

0 2 ...

1 4 n 2n + 2

2 6 n+1 (2n+2)+2

3 8 n+1 2(n+1)+2

4 10

ends in {0,2,4,6,8} by step 2

0+2=2, 2+2=4,4+2=66+2=8, 8+2=0 {0,2,4,6,8}

Page 37: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular ExpressionsRegular Expressions

• Shorthand Notation for Concisely Expressing Languages

• Defined Recursively

• Lead to a Definition of Regular Languages

• Provide Finite Representation of Possibly Infinite Languages

• Lead to Lexical Analyzers

Page 38: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular Expressions NotationRegular Expressions Notation

Language

with operator precedence being

highest lowest

or +

Kleene Star Concatenation Set Union

a a

a b a b

a a

a a

,

* *

*

Page 39: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular Expressions Over IRegular Expressions Over I

• and are regular expressions

• a is a regular expression for each a I

• If r and s are regular expressions, then so are r s, r s, and r*

• No other sequences of symbols are regular expressions

Page 40: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular Expressions AlternativeRegular Expressions Alternative

1. L( ) = { }

L( a ) = { a }

If p and q are regular expressions, then

2. L( pq ) = L( p ) L( q )

3. L( p q ) = L( p ) L( q )

4. L( p* ) = L( p )*

Page 41: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular Expressions ExampleRegular Expressions Example

What is L3 ( ( a b )* a ) ?

L L L

L

L

L L

3

a b a

a b a

a b a

a b a

a b a

a b a

w a b w a

*

*

*

*

*

, *

, *:

2

1

4

3

1, 1

definition

ends with

Page 42: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular ExpressionsRegular Expressions

• Boolean OR Distributes over Concatenation

– which is the language of all strings beginning with a, ending with b, and having none or more c’s in the middle, and,

– all strings beginning and ending with b and having at least one c in the middle

L

L

language

language

a bc c b

ac b bcc b

*

* *

Page 43: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular ExpressionsRegular Expressions

• The Boolean OR Operator Can Distribute When It Is Inside a Kleene Starred Expression, but Only in Certain Ways

L

language a bc b

a bc a bc a bc b

a b bc b

ab bcb

*

* *

*

Page 44: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular ExpressionsRegular Expressions

• Useful String– ( a + b )* = the set of all strings of a and b of

any length– L = Language ( ( a + b )* )– { , ab, abab, abaab, abbaab, babba, bbb, ... }

Page 45: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular LanguagesRegular Languages

• If L I* is finite, then L is regular.

• If L1 and L2 are regular, so are

– L3= L1 L2

– L4= L1 L2 = {x1 x2 | x1 L1 , x2 L2 }

• If L is regular, then so is L*, where * is the Kleene Star

Page 46: Regular Languages Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 3 Comments, additions and modifications

Regular LanguagesRegular Languages

• If L Is a Finite Language, Then L Can Be Defined by a Regular Expression.

• The Converse Is Not True. That Is, Not All Regular Expressions Represent Finite Languages.

• L = Language( ( a + b )* ) Is Infinite Yet Regular