REGULAR GRAMMARS Module 04 1 COP 4020 Programming

  • Slides: 11
Download presentation
REGULAR GRAMMARS Module 04. 1 COP 4020 – Programming Language Concepts Dr. Manuel E.

REGULAR GRAMMARS Module 04. 1 COP 4020 – Programming Language Concepts Dr. Manuel E. Bermudez

TOPICS Regular Grammars Grammar to State Diagram (FSA)

TOPICS Regular Grammars Grammar to State Diagram (FSA)

REGULAR GRAMMARS Definition: A grammar G = (Φ, Σ, P, S) is regular iff

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.

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

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

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.

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

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,

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

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

SUMMARY Defined Regular Grammar Conversion from Right. Linear Grammar to Transition Diagram (Graph) Advantages of Graph: Easier to Visualize Can actually parse (nondeterministically)