Regular Grammers

 

Regular Grammar

A Regular Grammar is the most restricted type of grammar in the Chomsky Hierarchy.
It generates exactly the class of regular languages (the same languages accepted by finite automata and described by regular expressions).

Formally, a grammar G=(V,Σ,R,S)G = (V, \Sigma, R, S) is regular if all productions in RR are of one of the following forms:

  1. Right-linear form:

    AaBorAaorAεA \rightarrow aB \quad \text{or} \quad A \rightarrow a \quad \text{or} \quad A \rightarrow \varepsilon

    where A,BVA, B \in V, aΣa \in \Sigma.

  2. Left-linear form:

    ABaorAaorAεA \rightarrow Ba \quad \text{or} \quad A \rightarrow a \quad \text{or} \quad A \rightarrow \varepsilon

Note: A grammar must be entirely right-linear or left-linear to be regular. Mixing both may lead to a CFG that is not regular.


Intuition

  • Regular grammars correspond to finite automata (DFA/NFA).

  • Productions represent transitions:

    • AaBA \rightarrow aB means “on reading aa, go from state AA to state BB.”

    • AaA \rightarrow a means “on reading aa, finish (accept).”

    • AεA \rightarrow \varepsilon means “accept without consuming input.”

So:

  • Right-linear → strings “grow” to the right.

  • Left-linear → strings “grow” to the left.


Examples of Regular Grammars

 Strings ending with ab

Language:

L={w{a,b}  w ends with ab}L = \{ w \in \{a,b\}^* \ | \ w \text{ ends with } ab \}

Right-linear Grammar:

SaS  bS  aAS \rightarrow aS \ | \ bS \ | \ aA
AbA \rightarrow b

Derivation:

  • For  aab:
    SaSaaSaaAaabS \Rightarrow aS \Rightarrow aaS \Rightarrow aaA \Rightarrow aab


Language aa^* (zero or more a’s)

SaS  εS \rightarrow aS \ | \ \varepsilon
  • Generates ε, a, aa, aaa, …


Language (ab)(ab)^*

SaA  εS \rightarrow aA \ | \ \varepsilon
AbSA \rightarrow bS
  • Generates ε, ab, abab, ababab, …


Left-linear version of aa^*

SSa  εS \rightarrow Sa \ | \ \varepsilon
  • Generates ε, a, aa, aaa, …

  • This is left-linear instead of right-linear.


Relation to Finite Automata

Every regular grammar corresponds to a finite automaton, and vice versa:

  • AaBA \rightarrow aB↔ transition from state A to state B on input a.

  • AaA \rightarrow a ↔ transition to a final state.

  • AεA \rightarrow \varepsilon ↔ nonterminal A is a final state.

Example:
Grammar:

SaS  bS  aS \rightarrow aS \ | \ bS \ | \ a

Generates all strings ending in a.

Equivalent NFA:

  • States: {S,F}

  • Transitions: S —a→ S, S —b→ S

  • Accept: final state F after reading a.




Key Properties of Regular Grammars

  • Generate only regular languages.

  • Strictly less powerful than context-free grammars.

  • Ambiguity is usually not an issue, because regular grammars are very restrictive.

  • Always convertible to finite automata or regular expressions.



Comments

Popular posts from this blog

Theory Of Computation PCCST302 KTU Semester 3 BTech 2024 Scheme

Non deterministic Finite Automata NFA

Example DFAs University Questions