Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida...

Post on 18-Jan-2018

243 views 0 download

description

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  Φ.

Transcript of Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida...

Regular Languages

Prepared byManuel E. Bermúdez, Ph.D.

Associate ProfessorUniversity of Florida

Programming Language Translators

Regular Languages

We will study:

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

Regular LanguagesDefinition: 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 Φ.

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

Regular LanguagesLet’s devise a machine that accepts L(G1).

Observe thatS => 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.

=>=>

=>=>=>

Regular LanguagesEncode possible derivation sequences with a relation ⊢

on pairs of the form (q, ), whereq – current state – remaining string to accept

So, S → bU implies

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

State “sentential form ends in S”

“movesto”

state “sentential form ends in U”

Regular LanguagesDefine 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

Regular LanguagesExample: S → a R → abaU U → b

→ bU → U →aS → bR

S

U

RF

aba

ε

b

b

a

b

a

Regular LanguagesIn general, Right-linear grammar → Transition diagram:

1. Nodes: Φ {f}, f Φ

2. if A → B

3. if A →

4.

A B

S

A Fα

α

Regular LanguagesExample: 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.

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.

Finite-State AutomataAn 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

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 YY U

a

ba

Finite-State AutomataTWO “SYMPTOMS” OF NON-DETERMINISM:

Note: is not a problem

Fa

X

a

a X

ε

a1. 2.

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.

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

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

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

ε

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

=>

=>

=>

=>

Finite-State AutomataSimilarities 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.

Finite-State AutomataLeft-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α

α

Finite-State AutomataExample:

F → Sa U → SB S → Ua → Ub → R → ε

R → Sb → Raba

S

U

RF

aba

ε

b

b

a

b

a

S’ε

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

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

Finite-State AutomataSummarizing:

RGR RGL

RE FSA

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

Done

Soon