Regular Languages Programming Language Translators Prepared by Manuel
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: G 1: S → a → b. U → b. R R→ → U→ → aba. U U b S G 2: S → a → Ub → Rb R → Uaba U →b → a. S Regular? Why?
Regular Languages => Let’s devise a machine that accepts L(G 1). Observe that S => a => b. U => bb … > => b. R = b. S … => baba. U 1. 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 → b. U implies (S, bβ) ⊢ (U, β) State “sentential “moves form ends in S” 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, b S U S → b. U implies , R → U implies S → a implies R S a U , F .
Regular Languages Example: S → a → b. U → b. R R → aba. U →U S a b a F b b R ε aba U U→b →a. S
Regular Languages In general, Right-linear grammar → Transition diagram: 1. Nodes: Φ {f}, f Φ 2. A 3. A 4. S α α B F if A → B if A →
Regular Languages Example: Is “babaa” in L(G)? Node S U S F Input babaa baa aa a Derivation S => b. U => ba. S => bab. U => baba. S => 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 Σ {ε} → 2 Q 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 S a b a F b b R ε aba U is described by the FSA
Finite-State Automata TWO “SYMPTOMS” OF NON-DETERMINISM: a 1. Note: X ε 2. a F X a a is not a problem
Finite-State Automata Advantages of FSA’s: Question: What language does the following grammar generate? S → a. A →ε C → b. D A → a. B →E D → b. E Difficult to see. Try FSA. B → a. C →D E → b. S
Finite-State Automata ε F S a A E B ε ε b a b D a b C 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.
Finite-State Automata Transition Diagram (FSA) → Right-linear regular grammar 1. Φ = Q 2. A → a. B if B δ (A, a) 3. A → a if f δ (A, a), and f F 4. Start symbol = Start state
Finite-State Automata Example: A FSA: ε H RGR: A → a. B →a E→F b a B ε G B → b. D →b F → d. G b c D d E ε F D → c. E →c G→H →ε H→A Conclusion: Right-linear regular grammars and FSA’s are equivalent.
Finite-State Automata Relationship between Left-linear regular grammars and FSA’s: Derivations: Sbb. . . > = F => Ub => Rb. . . Rabab. . . Sa= => Uaa. . . > a => F → Sa → Ub => Example: U → Sb →R → Raba R → Sb S → Ua →
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. B 2. S’ α α A if A → B. A if A → , S’ is a new start state. 3. F = {S}, S is the start symbol.
Finite-State Automata Example: F → Sa → Ub R → Sb U → SB →R → Raba S → Ua →ε S’ ε S a b a F b b R ε aba U
Finite-State Automata State S’ S R U S F Input Derivation babaaa Sbabaaa Rabaaa aa Uaa a Sa F <= <=
Finite-State Automata FSA → Left-linear Regular Grammar: 1. A → B if B 2. A → if S 3. S’ → F if F α α New start symbol A A
Finite-State Automata Summarizing: Done RGR RGL RE FSA Soon Note: Beware of attempts at direct conversion between left and right-linear grammars.
- Slides: 25