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

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

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

Page 1: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

Regular Languages

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

Associate ProfessorUniversity of Florida

Programming Language Translators

Page 2: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University 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

Page 3: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

Page 4: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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.

=>=>

=>=>=>

Page 6: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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”

Page 7: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

Page 8: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

→ bU → U →aS → bR

S

U

RF

aba

ε

b

b

a

b

a

Page 9: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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α

α

Page 10: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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.

Page 11: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

Page 13: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

Page 14: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

Note: is not a problem

Fa

X

a

a X

ε

a1. 2.

Page 15: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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.

Page 21: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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α

α

Page 22: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

Finite-State AutomataExample:

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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

Page 24: Regular Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

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 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.

Finite-State AutomataSummarizing:

RGR RGL

RE FSA

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

Done

Soon