regular grammars

46
 Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars

Upload: hemjyotsana-parashar

Post on 16-Jul-2015

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 1/46

 

Mathematical Foundationsof Computer Science

Chapter 3: Regular Languages and

Regular Grammars

Page 2: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 2/46

 

Languages

A language (over an alphabet Σ) is any subsetof the set of all possible strings over Σ . Theset of all possible strings is written as Σ*.

Example:Σ = {a, b, c}

Σ* = { , a, b, c, ab, ac, ba, bc, ca, aaa, …}

one language might be the set of strings of length less than or equal to 2.

●  L = { , a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc}

Page 3: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 3/46

 

Regular Languages

A regular language (over an alphabet Σ) is anylanguage for which there exists a finiteautomaton that recognizes it.

Page 4: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 4/46

 

Mathematical Models of Computation

This course studies a variety of mathematicalmodels corresponding to notions of computation.

The finite automaton was our first example.

The finite automaton is an example of anautomaton model.

There are other models as well.

Page 5: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 5/46

 

Mathematical Models of Computation

Another important model is that of a grammar .

We will shortly look at regular grammars.

But first, a digression:

Page 6: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 6/46

 

Regular Expressions

A regular expression is a mathematical modelfor describing a particular type of language.

Regular expressions are kind of like arithmeticexpressions.

The regular expression is defined recursively.

Page 7: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 7/46

Regular Expressions

Given an alphabet Σ

, λ and a  Σ are all regular expressions.

If r 1 and r 2 are regular expressions, then so arer 1 + r 2,r 1 r 2 , r 1* 

and (r 1).

● Note: we usually write r 1 r 2 as r 1 r 2 .

These are the only things that are regular expressions. 

emptyset

emptystring

 

Page 8: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 8/46

Regular Expressions

Meaning:

represents the empty language

λ represents the language {λ }

a represents the language {a}

r 1 + r 2 represents the language L(r 1)   L(r 2)

r 1 r 2 represents L

(r 1) L

(r 2)r 1* represents ( L(r 1))*

 

Page 9: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 9/46

Regular Expressions

Example 1:

What does a*(a + b) represent?

It represents zero or more a's followed by either an a or a b.

{a, b, aa, ab, aaa, aab, aaaa, aaab …}

 

Page 10: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 10/46

Regular Expressions

Example 2:

What does (a + b)*(a + bb) represent?

It represents zero or more symbols, each of which can be an a or a b, followed by either  a or bb.

{a, bb, aa, abb, ba, bbb, aaa, aabb, aba, abbb,baa, babb, bba, bbbb, …}

 

Page 11: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 11/46

Regular Expressions

Example 3:

What does (aa)*(bb)*b represent?

All strings over {a, b} that start with an evennumber of a's which are then followed by an oddnumber of b's.

It's important to understand the underlyingmeaning of a regular expression.

 

Page 12: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 12/46

Regular Expressions

Example 4:

Find a regular expression for strings of 0's and 1'swhich have at least one pair of consecutive 0's.

Each such string must have a 00 somewhere in it.

It could have any string in front of it and anystring after it, as long as it's there!!!

Any string is represented by (0 + 1)*

Answer: (0 + 1)*00(0 + 1)*

 

Page 13: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 13/46

Regular Expressions

Example:

Find a regular expression for strings of 0's and1's which have no pairs of consecutive 0's.

● It's a repetition of strings that are either 1's or, if asubstring begins with 0, it must be followed by at leastone 1.

(1 + 011*)*● or equivalently, (1 + 01)*

● But such strings can't end in a 0.

 

Page 14: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 14/46

Regular Expressions

Example:

Find a regular expression for strings of 0's and 1'swhich have no pairs of consecutive 0's.

● (1 + 011*)*

● (1 + 01)*

● But such strings can't end in a 0.

● So we add (0 + λ) to the end to allow for this.

● (1 + 01)* (0 + λ)

This is only one of many possible answers.

 

Page 15: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 15/46

Regular Expressions

Why are they called regular expressions?

Because, as it turns out, the set of languages theydescribe is that of the regular languages.

That means that regular expressions are justanother model for the same thing as finiteautomata.

 

Page 16: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 16/46

Regular Expressions

Homework:

Chapter 3, Section 1

Problems 1-11, 17, 18

 

Page 17: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 17/46

Regular Expressions and RegularLanguages

As we have said, regular expressions and finiteautomata are really different ways of expressingthe same thing.

Let's see why.

Given a regular expression, how can we build anequivalent finite automaton?

(We won't bother going the other way, althoughit can be done.)

 

Page 18: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 18/46

Regular Expressions and RegularLanguages

Clearly there are simple finite automata corresponding to thesimple regular expressions:

λ  

a

λ 

a

 Note that each of these has an initial stateand one accepting state.

 

Page 19: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 19/46

Regular Expressions and RegularLanguages

On the previous slide, we saw that the simplestregular expressions can be represented by afinite automaton with an initial state (duh!) and

one isolated accepting state:

 

Page 20: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 20/46

Regular Expressions and RegularLanguages

We can build more complex automata for morecomplex regular expressions using this model:

 

Page 21: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 21/46

Regular Expressions and RegularLanguages

Here's how we build an nfa for r 1 + r 2:

λ 

λ λ 

λ 

r 1

r 2

r 1 + r 2

 

Page 22: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 22/46

Regular Expressions and RegularLanguages

Here's how we build an nfa for r 1 r 2:

r 1

r 2

λ λ 

λ 

r 1 r 2

 

Page 23: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 23/46

Regular Expressions and RegularLanguages

Here's how we build an nfa for (r 1)*:

λ λ 

λ 

λ 

1(r 1)*

λ 

 Note: the last state added is not in book. For safety, I do it

to have only one arc going into the final state.

  

Page 24: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 24/46

Building an nfa from a regular expression

Example:

Consider the regular expression (a + bb)(a+b)*(bb)

a

b b

λ 

λ 

λ 

λ 

λ 

λ 

a

bλ  λ 

λ 

λ 

λ 

λ 

λ 

λ 

b

b

sometimes we just get tired andtake an obvious shortcut

 

Page 25: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 25/46

Building regular expression from afinite automaton

The book goes on to show that it works the other way around as well: we can find a correspondingregular expression for any finite automaton.

It's fairly easy in some cases and you can "justdo it."

However, it's generally complicated and not

worth the bother studying.You are not responsible for this material

 

Page 26: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 26/46

Building regular expression from afinite automaton

The above automaton clearly corresponds to

a*(a+b)c*

a, b

ca

 

Page 27: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 27/46

Regular Expressions and nfa's

Homework:

Chapter 3, Section 2

● Problems 1-5

 

Page 28: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 28/46

Regular Grammars

Review: A grammar is a quadruple

G = (V , T , S , P ) where

V is a finite set of variables

T is a finite set of symbols, called terminals

 S is in V and is called the start   symbol 

 P is a finite set of  productions, which are rules of the form

α → β 

● where α and  β are strings consisting of terminals and variables.

 

Page 29: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 29/46

Regular Grammars

A grammar is said to be right-linear if every production in P is of the form

 A → xB or 

 A → x 

where A and B are variables (perhaps the same,

 perhaps the start symbol S ) in V and x is any string of terminal symbols(including the empty string λ )

 

Page 30: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 30/46

Regular Grammars

An alternate (and better) definition of a right-linear grammar says that every production in P isof the form

 A → aB or  A → a or 

 S → λ  (to allow λ to be in the language)

where A and B are variables (perhaps the same, but B can't be S ) in V 

and a is any terminal symbol

 

Page 31: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 31/46

Regular Grammars

The reason I prefer the second definition(although I accept the first one that happens to be used in the book) is

It's easier to work with in proving things.

It's the much more common definition.

 

Page 32: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 32/46

Regular Grammars

A grammar is said to be left-linear if every production in P is of the form

 A → Bx  or 

 A → x 

where A and B are variables (perhaps the same,

 perhaps the start symbol S ) in V and x is any string of terminal symbols(including the empty string λ )

 

Page 33: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 33/46

Regular Grammars

The alternate definition of a left-linear grammar  says that every production in P is of the form

 A → Ba or 

 A → a or 

 S → λ 

where A and B are variables (perhaps the same, but B can't be S ) in V 

and a is any terminal symbol

 

Page 34: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 34/46

Regular Grammars

Any left-linear or right-linear grammar is calleda regular grammar .

 

Page 35: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 35/46

Regular Grammars

For brevity, we often write a set of productionssuch as

 A  → x 1

 A  → x 2

 A  → x3

As A → x 1 | x 2 | x 3

 

Page 36: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 36/46

Regular Grammars

A derivation in grammar G  is any sequence of strings in V and T ,

connected with

starting with S and ending with a string containingno variables

where each subsequent string is obtained byapplying a production in P  is called a derivation.

 S    x 1  x 2  x 3  . . .  xn

abbreviated as:

S    xn*

 

Page 37: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 37/46

Regular Grammars

Ø S    x 1 x 2 x 3 . . .  xn

Ø abbreviated as:

Ø  S    xn

Ø  We say that xn is a sentence of the languagegenerated by G , L(G ).

Ø We say that the other  x 's are sentential  forms.

*

 

Page 38: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 38/46

Regular Grammars

Ø   L(G) = {w | w  T* and S    xn}

Ø  We call L(G) the language generated by G 

Ø   L(G) is the set of all sentences over grammar G 

*

 

Page 39: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 39/46

Example 1

 S   →  abS  | a is an example of a right-linear grammar.

Can you figure out what language it generates?

 L = {w  {a,b}* | w contains alternating a's

and b's , begins with an a, and ends with a b}   {a}

 L((ab)*a)

 

Page 40: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 40/46

Example 2

 S  →  Aab A → Aab | aB B → a

is an example of a left-linear grammar.Can you figure out what language it generates?

 L = {w  {a,b}* | w  is aa followed by at least

one set of alternating ab's} L(aaab(ab)*)

 

Page 41: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 41/46

Example 3

Consider the grammar 

 S  →  A A → aB | λ  

 B → Ab

This grammar is NOT regular.

 No "mixing and matching" left- and right-recursive productions. 

 

Page 42: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 42/46

Regular Grammars and nfa's

It's not hard to show that regular grammars generateand nfa's accept the same class of languages: theregular languages!

It's a long proof, where we must show thatany finite automaton has a corresponding left- or right-linear grammar,

and any regular grammar has a corresponding nfa.We won't bother with the details.

 

Page 43: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 43/46

Regular Grammars and nfa's

We get a feel for this by example.

Let S  → aA   A → abS  | b 

S A

a b

b a

 

Page 44: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 44/46

Regular Grammars and Regular Expressions

Example: L(aab*a)

We can easily construct a regular language for thisexpression:

 S  → aA A → aB

 B → bB

 B → a

 

Page 45: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 45/46

Regular Languages

regularexpressions

regulargrammars

finiteautomata

 

Page 46: Regular Grammars

5/13/2018 Regular Grammars - slidepdf.com

http://slidepdf.com/reader/full/regular-grammars-55a755f67006d 46/46

Regular Languages

Homework:

Chapter 3, Section 3

Problems