REGULAR GRAMMARS Module 04 1 COP 4020 Programming
- Slides: 11
REGULAR GRAMMARS Module 04. 1 COP 4020 – Programming Language Concepts Dr. Manuel E. Bermudez
TOPICS Regular Grammars Grammar to State Diagram (FSA)
REGULAR GRAMMARS 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 GRAMMARS Examples: G 1: S → a R → aba. U → b. U →U → b. R U→b Regular? Why? → a. S G 2: S → a → Ub → Rb R → Uaba U →b → a. S Regular? Why?
REGULAR GRAMMARS Let’s devise a machine that accepts L(G 1). S => a => => b. U => bb b. R => => ba. S … G 1: S → a → b. U → b. R => R → → U → → aba. U U b a. S baba. U 1. All sentential forms (except sentences) have ONE nonterminal. 2. The nonterminal occurs in the right-most position. 3. Applicable productions depend only on that nonterminal.
REGULAR GRAMMARS Encode possible derivation sequences with a relation ⊢ (moves-to) 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 GRAMMARS Define a graph, one node per nonterminal, actions on each sentential form. S → b. U implies R → U implies S → a implies S R S b a U , and F .
REGULAR GRAMMAR TO TRANSITION DIAGRAM G 1: S → a R → aba. U → b. R S a b a F b b R ε aba U →U U →b → a. S Advantage of diagram: Easier to visualize
REGULAR GRAMMAR TO TRANSITION DIAGRAM In general, conversion from right-linear grammar G=(Φ, Σ, P, S) to transition diagram: 1. Nodes: Φ {F}, F Φ 2. A 3. A 4. S α α B if A → B F if A →
ADVANTAGE OF TRANSITION DIAGRAM: GOOD FOR PARSING Example: Is “babaa” in L(G)? Node Input Derivation S babaa S => U abaa b. U => S baa ba. S => U aa bab. U => S a baba. S => F babaa Note: Use of graph is non-deterministic. Will fix later.
SUMMARY Defined Regular Grammar Conversion from Right. Linear Grammar to Transition Diagram (Graph) Advantages of Graph: Easier to Visualize Can actually parse (nondeterministically)
- Regular grammar generates regular language
- Cop 1 cop 2
- Good cop bad cop interrogation
- Useless variables in context-free grammars
- Type 0 grammar is called unrestricted grammar
- Unrestricted grammar examples
- Handling questions in context-free grammars
- Center for army analysis
- Maksud lpc gaji
- As/nzs 4020
- C device module module 1
- Greedy programming vs dynamic programming