250 likes | 601 Views
Regular Languages. Programming Language Translators. 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
E N D
Regular Languages Programming Language Translators 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 • 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 Languages Examples: 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 … babaU • Every sentential form (except sentences) has exactly one nonterminal. • The nonterminal occurs in the right-most position. • 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 . b S U R U a S F
Regular Languages Example: S → a R → abaU U → b → bU → U →aS → bR S b a R b ε a F aba b U
Regular Languages In general, Right-linear grammar → Transition diagram: • Nodes: Φ {f}, f Φ • if A → B • if A → α A B α A F S
Regular Languages Example: Is “babaa” in L(G)? Node Input Derivation S 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 transitionsymbols, δ: Q x Σ {ε} → 2Q is a partial function called the transitionfunction, s Q is called the startstate, and F Q is the set of finalstates.
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 b a R b a ε F aba b U
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} a R X b X Y a Y U
Finite-State Automata TWO“SYMPTOMS”OFNON-DETERMINISM: Note: is not a problem a ε 1. X 2. a X a a F
Finite-State Automata Advantages of FSA’s: Question: What language does the following grammar generate? S → aA A → aB B → aC → ε → E → D C → 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. a a F S A B ε ε a b b b E D C
Finite-State Automata Transition Diagram (FSA) → Right-linear regular grammar • Φ = Q • A → aB if B δ (A, a) • A → a if f δ (A, a), and f F • Start symbol = Start state
Finite-State Automata b a b c Example: FSA: RGR: A → aB B → bB D → cE → a → bD → c → b E → F F → dG G → H → ε H → A Conclusion: Right-linear regular grammars and FSA’s areequivalent. A B D E ε ε ε d H G F
Finite-State Automata Relationship 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: • Sentential forms have at most one nonterminal. • Sentences have none. • Applicable productions depend only on the one nonterminal. Differences with right-linear grammars: • Nonterminals appears on left-most position. • String generated right-to-left, versus left-to-right for right-linear grammars.
Finite-State Automata Left-linear Regular Grammar → FSA • if A → B. • if A → , S’ is a new start state. • 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’ ε S b a R b a ε F aba b U
Finite-State Automata State Input Derivation S’ babaaa babaaa S babaaa Sbabaaa <= R abaaa Rabaaa <= U aa Uaa <= S a Sa <= F F
Finite-State Automata FSA → Left-linear Regular Grammar: • A → B if • A → if • S’ → F if α B A α S A F New start symbol
Finite-State Automata Summarizing: RGR RGL RE FSA Note: Beware of attempts at direct conversion between left and right-linear grammars. Done Soon