Alternation (formal language theory) explained

In formal language theory and pattern matching, alternation is the union of two sets of strings, or equivalently the logical disjunction of two patterns describing sets of strings.

Regular languages are closed under alternation, meaning that the alternation of two regular languages is again regular.[1] In implementations of regular expressions, alternation is often expressed with a vertical bar connecting the expressions for the two languages whose union is to be matched,[2] [3] while in more theoretical studies the plus sign may instead be used for this purpose.[1] The ability to construct finite automata for unions of two regular languages that are themselves defined by finite automata is central to the equivalence between regular languages defined by automata and by regular expressions.[4]

Other classes of languages that are closed under alternation include context-free languages and recursive languages.The vertical bar notation for alternation is used in the SNOBOL language and some other languages.In formal language theory, alternation is commutative and associative. This is not in general true of the form of alternation used in pattern-matching languages, because of the side-effects of performing a match in those languages.

References

  1. Book: Linz, Peter. An Introduction to Formal Languages and Automata. Jones & Bartlett Learning. 2006. 9780763737986. Theorem 4.1. 100–101. https://books.google.com/books?id=KOo4NpfTEAIC&pg=PA100.
  2. Book: Fitzgerald, Michael. Introducing Regular Expressions: Unraveling Regular Expressions, Step-by-Step. O'Reilly Media. 2012. 9781449338893. Alternation. 43–45. https://books.google.com/books?id=mzNo2e_1WD8C&pg=PA43.
  3. Web site: Alternation with The Vertical Bar. regular-expressions.info. 2021-11-11.
  4. Book: Engineering a Compiler. Keith. Cooper. Linda. Torczon. 2nd. Elsevier. 2011. 9780080916613. 41.

Bibliography