Grammars in Automata

 

What is a grammar (in automata)?

A grammar is a formal tool  that generates strings of a language by repeatedly applying production rules.

Formal definition
A grammar is a 4-tuple G=(V,Σ,R,S)G=(V,\Sigma,R,S):

  • VV: finite set of nonterminals (variables)

  • Σ\Sigma: finite set of terminals (alphabet), V∩Σ=∅V\cap\Sigma=\varnothing

  • RR: finite set of productions (rewrite rules)

  • S∈VS\in V: start symbol

Applying productions creates derivations (leftmost/rightmost), which can be shown by parse trees. The set of all terminal strings derivable from S is the languageL(G).

Why grammars matter:

  • They’re the generative counterpart to automata (the recognizers).

  • Each grammar class corresponds to a machine model: finite automata, pushdown automata, linear-bounded automata, Turing machines.


The Chomsky hierarchy (types of grammars)

Type-3: Regular Grammars (RG)

Rule shape (choose one style consistently):

  • Right-linear: A→aB∣a∣εA\to aB \mid a \mid \varepsilon

  • Left-linear: A→Ba∣a∣εA\to Ba \mid a \mid \varepsilon

Power / Automaton: exactly the regular languages ↔ DFA/NFA ↔ regular expressions.
Use: token patterns (lexical analysis).
Example: L=(ab)∗L=(ab)^*

  • S→aA∣εS\to aA \mid \varepsilon, A→bSA\to bS

Note: For some definitions, ε\varepsilon is allowed only if SS doesn’t appear on any RHS and ε∈L\varepsilon\in L.


Type-2: Context-Free Grammars (CFG)

Rule shape: A→αA\to \alpha where A∈VA\in V and α∈(V∪Σ)∗\alpha\in(V\cup\Sigma)^*

 (LHS is a single nonterminal).

Power / Automaton: context-free languages ↔ pushdown automata.
Use: programming-language syntax (nested/recursive structures), parse trees/ASTs.
Normal forms: CNF (Chomsky normal form), GNF (Greibach).
Example: L={anbn∣n≥0}L=\{a^n b^n\mid n\ge 0\}

  • S→aSb∣εS\to aSb \mid \varepsilon

Ambiguity: a CFG is ambiguous if some string has ≥2 parse trees (e.g., E→E+E∣E∗E∣idE\to E+E\mid E*E\mid id). Disambiguate by stratifying precedence:

  • E→E+T∣T,  T→T∗F∣F,  F→(E)∣idE\to E+T\mid T,\; T\to T*F\mid F,\; F\to (E)\mid id


Type-1: Context-Sensitive Grammars (CSG)

Rule shape (length nondecreasing): αAβ→α γ β\alpha A \beta \to \alpha\,\gamma\,\beta with ∣γ∣≥1|\gamma|\ge 1. (No ε\varepsilon-productions, except a special case for SS.)

Power / Automaton: context-sensitive languages ↔ linear-bounded automata (LBA).
Use: languages requiring synchronized counts beyond a single stack.
Normal form: Kuroda normal form.
Classic example: L={anbncn∣n≥1} (not CFL). One standard CSG (intuition: generate markers, then “commute” and translate):

  • S→aSBC∣abcS \to aSBC \mid abc

  • CB→BCCB \to BC

  • aB→ab,  bB→bb,  bC→bc,  cC→ccaB \to ab,\; bB \to bb,\; bC \to bc,\; cC \to cc

(High-level idea: each new aa introduces a B,CB,C pair; swap CC past BB with CB→BCCB\to BC, then rewrite B,CB,Cinto extra b,cb,c symbols, keeping counts equal.)


Type-0: Unrestricted (Phrase-Structure) Grammars

Rule shape: α→β\alpha \to \beta where α,β∈(V∪Σ)∗\alpha,\beta \in (V\cup\Sigma)^* and α\alpha contains at least one nonterminal.

Power / Automaton: recursively enumerable languages ↔ Turing machines (most general).
Use: theoretical completeness; not practical for parsing.
Note: Constructions typically simulate a TM; explicit examples are verbose.


How grammars relate to automata (at a glance)

Grammar TypeRule RestrictionLanguage ClassEquivalent MachineExample Language
Type-3 Regularlinear (left or right)RegularDFA/NFA(ab)∗(ab)^*, strings ending in 1
Type-2 Context-Freesingle nonterminal on LHSContext-freePushdown Automaton{anbn}\{a^n b^n\}, balanced parentheses
Type-1 Context-Sensitivenondecreasing length, context-limitedContext-sensitiveLinear-Bounded Automaton{anbncn}\{a^n b^n c^n\}
Type-0 Unrestrictednone (except LHS has a nonterminal)Recursively enumerableTuring Machineall TM-recognizable languages

Comments

Popular posts from this blog

Theory Of Computation PCCST302 KTU Semester 3 BTech 2024 Scheme

Motivation- Computability

Pumping Lemma For Context Free Languages - CFL