liveBook · Manning

Regular grammar

As explained in Natural Language Processing in Action, Second Edition

A regular grammar is a formal grammar that generates exactly the regular languages through restricted production rules.

A regular grammar describes character patterns with predictable, finite-state behavior. Matching a known sequence can trigger a scripted response or extract information, making the approach useful for pattern-based dialog systems and chatbots. section 1.4.2

The underlying machine can be modeled as a finite-state machine or deterministic finite automaton. A true finite-state matcher processes input step by step, returns a match or nonmatch, and eventually halts. Python and POSIX regular expressions add lookahead and lookback features, so they are not strictly true regular grammars and may lose those guarantees. section 1.4.2 section 11.2.2

Regular grammars support several practical pattern-processing tasks:

Sentence segmentation

A pattern such as `re.split(r'[!.?]+[\s$]+', text)` separates ordinary sentences by punctuation followed by whitespace. More careful patterns avoid splitting decimal numbers, while spaCy provides a learned alternative whose output can be compared with the regex result. section 11.2.2

Figure 1.4 Kinds of automata
figure

Use the automata hierarchy to place regular grammars and finite-state machines within related formal-language models. section 1.4.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains recognition, generation, deterministic processing, halting, and regex caveats without developing full finite-state-machine theory.
Examples
Uses chatbot triggers, Python regex sentence splitting, decimal-number protection, and spaCy sentence recognition as concrete examples.
What you can do after
Apply it via regular expressions to recognize patterns, extract text, segment sentences, or trigger scripted responses.
How it connects
Connects regular grammars with regular expressions, finite-state machines, deterministic finite automata, formal languages, chatbots, and sentence segmentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.