regular languages

25
Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 4

Upload: odell

Post on 11-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Regular Languages. Programming Language Concepts Lecture 4. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. Regular Languages. We will study: Regular grammars Relation to finite-state automata Regular expressions Equivalence among representations - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Regular Languages

Regular Languages

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 4

Page 2: Regular Languages

Regular Languages

We will study:• Regular grammars• Relation to finite-state automata• Regular expressions• Equivalence among representations• Elimination on non-determinism• State minimization

Page 3: Regular Languages

Regular Languages

Definition: A grammar G = (Φ, Σ, P, S) is regular iff either (but not both):Every production is of the form

A → or A → B (right linear)

Every production is of the form A → or A → B (left linear),where Σ*, and A, B Φ.

Page 4: Regular Languages

Regular LanguagesExamples:

G1: S → a R → abaU → bU → U Regular? Why?

→ bR U → b → S

G2: S → a R → Uaba → Ub U → b Regular? Why?

→ Rb → aS

Page 5: Regular Languages

Regular Languages

Let’s devise a machine that accepts L(G1).Observe that

S => a bU => bb …bR bS …

babaU1. Every sentential form (except sentences) has exactly one

nonterminal.2. The nonterminal occurs in the right-most position.3. Applicable productions depend only on that nonterminal.

=>

=>

=>=>=>

Page 6: Regular Languages

Regular Languages

Encode possible derivation sequences with a relation ⊢ on pairs of the form (q, ), where

q – current state – remaining string to accept

So, S → bU implies

(S, bβ) ⊢ (U, β)

State “sentential

form ends in S”

“moves

to”

state “sentential form ends in U”

Page 7: Regular Languages

Regular Languages

Define a graph, one node per nonterminal, describing the possible actions on each sentential form. So,

S → bU implies ,

R → U implies ,

S → a implies .

S U

R U

S F

b

a

Page 8: Regular Languages

Regular Languages

Example: S → a R → abaU U → b → bU → U →aS

→ bR

S

U

RF

aba

ε

b

b

a

b

a

Page 9: Regular Languages

Regular Languages

In general, Right-linear grammar → Transition diagram:

1. Nodes: Φ {f}, f Φ

2. if A → B

3. if A →

4.

A B

S

A Fα

α

Page 10: Regular Languages

Regular Languages

Example: Is “babaa” in L(G)?

Node Input DerivationS babaa S =>U abaa bU =>S baa baS =>U aa babU =>S a babaS =>F babaa Yes.

Page 11: Regular Languages

Finite-State Automata

Definition: A (non-deterministic) finite-state automaton is a 5-tuple M = (Q, Σ, δ, s, F), where

Q is a finite set of states,

Σ is a finite set of transition symbols,

δ: Q x Σ {ε} → 2Q is a partial function called the transition function,

s Q is called the start state, and

F Q is the set of final states.

Page 12: Regular Languages

Finite-State Automata

An FSA is the formal accepting mechanism of a regular language. It requires that each transition be labeled by a string of length < 1.

The state diagram

is described by the FSA

S

U

RF

aba

ε

b

b

a

b

a

Page 13: Regular Languages

Finite-State Automata

({S, R, U, F, X, Y}, {a, b}, δ, S, {F}), whereδ (S, a) = {F}δ (S, b) = {U, R} δ (R, ε) = {U}δ (R, a) = {X}δ (U, a) = {S}δ (U, b) = {F}δ (X, b) = {Y}δ (Y, a) = {U}

R X

X Y

Y U

a

b

a

Page 14: Regular Languages

Finite-State Automata

TWO “SYMPTOMS” OF NON-DETERMINISM:

Note: is not a problem

Fa

X

a

a X

ε

a1. 2.

Page 15: Regular Languages

Finite-State AutomataAdvantages of FSA’s:

Question: What language does the following grammar generate?

S → aA A → aB B → aC → ε → E → DC → bD D → bE E → bS

Difficult to see. Try FSA.

Page 16: Regular Languages

Finite-State Automata

Answer: L*, where L = {ab, aabb, aaabbb}

Summary: FSA’s are as powerful as right-linear regular grammars.

Are they more powerful? No. Can transform FSA → RGR.

S A

E D C

B

b

εb

aa

ε

ba

Page 17: Regular Languages

Finite-State Automata

Transition Diagram (FSA) → Right-linear regular grammar

1. Φ = Q

2. A → aB if B δ (A, a)

3. A → a if f δ (A, a), and f F

4. Start symbol = Start state

Page 18: Regular Languages

Finite-State AutomataExample:

FSA:

RGR: A → aBB → bBD → cE → a → bD → c

→ bE → F F → dGG → H

→ εH → A

Conclusion: Right-linear regular grammars and FSA’s are equivalent.

A

G Fε

ε

ba

dH

B D E

bc

ε

Page 19: Regular Languages

Finite-State AutomataRelationship between Left-linear regular grammars and FSA’s:

Example: F → Sa U → Sb R → Sb → Ub → R S → Ua

→ Raba →

Derivations: Sbb ...F => Ub => Rb ...

Rabab ... Sa => Uaa ...

a

=>

=>

=>

=>

Page 20: Regular Languages

Finite-State Automata

Similarities with right-linear grammars:1. Sentential forms have at most one nonterminal.2. Sentences have none.3. Applicable productions depend only on the one

nonterminal.

Differences with right-linear grammars:1. Nonterminals appears on left-most position.2. String generated right-to-left, versus left-to-right for right-linear grammars.

Page 21: Regular Languages

Finite-State Automata

Left-linear Regular Grammar → FSA

1. if A → B .

2. if A → α, S’ is a new start state.

3. F = {S}, S is the start symbol.

B A

S’ Aα

α

Page 22: Regular Languages

Finite-State Automata

Example: F → Sa U → SB S → Ua

→ Ub → R → εR → Sb → Raba

S

U

RF

aba

ε

b

b

a

b

a

S’

Page 23: Regular Languages

Finite-State Automata

State Input DerivationS’ babaaa babaaaS babaaa Sbabaaa <=R abaaa Rabaaa <=U aa Uaa <=S a Sa <=F F

Page 24: Regular Languages

Finite-State Automata

FSA → Left-linear Regular Grammar:

1. A → B if

2. A → if

3. S’ → F if

B A

S’ Aα

α

F

New start symbol

Page 25: Regular Languages

Finite-State AutomataSummarizing:

RGR RGL

RE FSA

Note: Beware of attempts at diirect conversion between left and right-linear grammars.

Done

Soon