Regular Languages
description
Transcript of Regular Languages
Regular Languages
Prepared by
Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida
Programming Language ConceptsLecture 4
Regular Languages
We will study:• Regular grammars• Relation to finite-state automata• Regular expressions• Equivalence among representations• Elimination on non-determinism• State minimization
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 Φ.
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 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.
=>
=>
=>=>=>
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”
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
Regular Languages
Example: S → a R → abaU U → b → bU → U →aS
→ bR
S
U
RF
aba
ε
b
b
a
b
a
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α
α
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.
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 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
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
Finite-State Automata
TWO “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.
Fε
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 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.
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α
α
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’
Finite-State Automata
State 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 diirect conversion between left and right-linear grammars.
Done
Soon