Regular Language

Overview

Finite automata are classified as either deterministic or nondeterministic. These two representations are equivalent.

If s is processed by finite automaton M such that M finishes in an accept state, we say M accepts s. Otherwise M rejects s. If A is the set of all strings that M accepts, we say that A is the language of machine M, denoted L(M)=A. We say that M recognizes A.

A language is called a regular language if a finite automaton recognizes it.

Automaton

Deterministic

A deterministic finite automaton (DFA) is a 5-tuple Q,Σ,δ,q0,F, where

  1. Q is a finite set called the states;
  2. Σ is a finite set called the alphabet;
  3. δ:Q×ΣQ is the transition function;
  4. q0Q is the start state; and
  5. FQ is the set of final states.

These automaton are typically denoted using a state diagram like below. The start state is indicated by an arrow pointing at it from nowhere. An accept state is denoted with a double circle.

dfa-example.png

Nondeterministic

A nondeterministic finite automaton (NFA) is a 5-tuple Q,Σ,δ,q0,F, where

  1. Q is a finite set called the states;
  2. Σ is a finite set called the alphabet;
  3. δ:Q×(Σ{ϵ})P(Q) is the transition function;
  4. q0Q is the start state; and
  5. FQ is the set of final states.

Like DFAs, these automaton are typically denoted using a state diagram. Unlike DFAs, not every state needs an exiting transition arrow for each symbol in the alphabet. Also, arrows can be labeled ϵ for the empty string.

Equivalence

Two finite automaton are equivalent if they recognize the same language. As it turns out, every NFA can be converted to an equivalent DFA.

Epsilon-Closure

Let N=Q,Σ,δ,q0,F be an NFA. Define the ϵ-closure of a set of states AQ as all states reachable from any state in A along zero or more ϵ-transitions.

More formally, define f:P(Q)P(Q) to be given by

f(R)={δ(r,ϵ)rR}.

Then the ϵ-closure of AQ using the top-down approach is

ϵ(A)={XAXP(Q)f[[X]]X}.

Using the bottom-up approach,

ϵ(A)=nNh(n)

where h:NP(Q) is defined in the usual way.

Power Set Construction

The power set construction describes the process by which an NFA can be converted into an equivalent DFA:

Let N = Q,Σ,δ,q0,F be the NFA recognizing some language A. Define the following:

  1. Q=P(Q)
  2. Σ=Σ{ϵ}
  3. δ(R,a)=rRϵ(δ(r,a))
  4. q0=ϵ({q0})
  5. F={RQR contains an accept state of N}

Then M=Q,Σ,δ,q0,F is a DFA equivalent to N, i.e. it also recognizes A.

Regular Expressions

A regular expression is a textual representation of some language. They are equivalent to finite automaton in their descriptive power. More formally, we say R is a regular expression if R is

  1. a for some a in the alphabet Σ,
  2. ϵ,
  3. ,
  4. (R1R2) where R1 and R2 are regular expressions,
  5. (R1R2) where R1 and R2 are regular expressions,
  6. (R1) where R1 is a regular expression.

Note individual symbols like aΣ or ϵ are interpreted as singleton sets.

GNFA

A generalized nondeterministic finite automaton (GNFA) is an NFA in which the transition arrows may have any regular expressions as labels, instead of only members of an alphabet (or ϵ).

These are useful for constructing a regular expression from a DFA. In particular, every DFA can be converted into a GNFA and every GNFA can be converted into a 2-state GNFA whose sole transition is labeled with the desired regular expression.

Extended

The following ERE (Extended Regular Expression) operators were defined to achieve consistency between programs like grep, sed, and awk. In POSIX, regexps are greedy.

Character Classes

ERE also introduced notation for describing a class of characters specific to a given locale/character set:

Class Similar To Meaning
[:alnum:] [A-Za-z0-9] Alphanumeric characters
[:alpha:] [A-Za-z] Alphabetic characters
[:blank:] [ \t] ' ' and TAB characters
[:cntrl:] Control characters
[:digit:] [0-9] Numeric characters
[:graph:] [^ [:cntrl:]] Printable and visible characters
[:lower:] [a-z] Lowercase alphabetic characters
[:print:] [ [:graph:]] Printable characters
[:punct:] All graphic characters except letters and digits
[:space:] [ \t\n\r\f\v] Whitespace characters
[:upper:] [A-Z] Uppercase alphabetic characters
[:xdigit:] [0-9A-Fa-f] Hexadecimal digits

Backtracking

Many popular implementations of regular expression libraries use backtracking instead of finite automaton. This implementation strategy is notorious for being exponentially slow.

The only situation in which backtracking is the best known option is in the case of backreferences, i.e. a substring that matches a previously matched portion of the "regular expression" (quoted since backreferences are not actual regular expressions in the theoretical sense).

Thompson's Algorithm

Thompson's algorithm converts regular expressions into an NFA containing a number of states equal to at most the length of the expression. The total NFA is built up from partial NFAs for each subexpression, with a different construction for each operator. Each partial NFA has dangling arrows pointing to nothing. The result is concatenated together at the end.

Once built, the NFA is simulated in a multiple-state fashion. Every possible branch of navigation is executed simultaneously.

Closure Operations

Let A and B be regular languages over alphabet Σ. Such languages are closed over the regular operations which include:

Product Construction

Let D1=Q1,Σ,δ1,q1,F1 and D2=Q2,Σ,δ2,q2,F2 be DFAs. Then the product construction of D1 and D2 is DFA D=D1×D2=Q,Σ,δ,q0,F where

The definition of F depends on the semantics desired.

Pumping Lemma

Let L be a regular language. Then there exists a pumping constant p where, if s is any string in L of length at least p, then s can be written as s=xyz such that

  1. xyizL for any integer i0,
  2. |y|>0, and
  3. |xy|p.
Powered by Forestry.md