Extra: Unrestricted Grammars

We defined regular grammars by adding additional restrictions to the definition of a context-free grammar. If we instead loosen restrictions, we get what is called an Unrestricted Grammar

Unrestricted grammars aren’t used much in practice because they are hard to work with (and no parsing algorithm exists). However, it is interesting to know that they exist.

Definition

An Unrestricted Grammar is specified by a 4-tuple (Σ,V,S,R)(\Sigma, {\cal V}, S, {\cal R}), where

  • Σ\Sigma is an alphabet; the members of Σ\Sigma are called terminals.
  • V{\cal V} is a (disjoint!) alphabet; the members of V\cal V are called nonterminals.
  • SVS\in {\cal V} is called the start symbol
  • R\cal R is a set of rules of the form LRL\to R where L,R(ΣV)L,R\in(\Sigma\cup {\cal V})^{*}.

In other words, an unrestricted grammar is just like a context-free grammar except the left-hand-side doesn’t have to be a single nonterminal.

Example

The following grammar produces the (non-context-free!) language { anbncn   n1 }\{\ \mathtt{a}^n\mathtt{b}^n\mathtt{c}^n\ \ |\ n\geq 1\ \} :

SabcSaSQbQcbbcccQQc\begin{array}{lcl} S & \to & \mathtt{abc}\\ S & \to & \mathtt{a}SQ\\ \mathtt{b}Q\mathtt{c} & \to & \mathtt{bbcc}\\ \mathtt{c}Q & \to & Q\mathtt{c}\\ \end{array}

For example,

SaSQaabcQaabQcaabbcc.S \Rightarrow \mathtt{a}SQ \Rightarrow \mathtt{aabc}Q \Rightarrow \mathtt{aab}Q\mathtt{c} \Rightarrow \mathtt{aabbcc}.

where we replaced cQ\mathtt{c}Q by QcQ\mathtt{c} in the third step and replaced bQc\mathtt{b}Q\mathtt{c} by bbcc\mathtt{bbcc} in the fourth step.

We can read the rule

bQcbbcc\mathtt{b}Q\mathtt{c} \to \mathtt{bbcc}

as saying “we can replace QQ by bc, but only if that QQ appears in-between b and c.” In other words, whether we can replace QQ by bc depends on the context in which QQ appears; the rule is context-sensitive.

In contrast, the rule

QbcQ \to \mathtt{bc}

lets us replace QQ by bc no matter where QQ appears, i.e., no matter what context QQ appears in. Such rules are therefore called context-free, and this is where the terminology of CFLs and CFGs comes from.

Example

Any language we can imagine computing can be described by some unrestricted grammar. For example, the following grammar produces all sequences of a’s whose length is composite (i.e., whose length is not a prime number):

SHaCCaCCaTTZUAZZAaZZAaHZHYYAAYYaaYYUTTXaXXaaAXXaHXϵ\begin{array}{lcl} S & \to & H\mathtt{a}C \\ C & \to & \mathtt{a}C \\ C & \to & \mathtt{a}T \\ T & \to & ZU \\ AZ & \to & ZA \\ \mathtt{a}Z & \to & ZA\mathtt{a} \\ HZ & \to & HY\\ YA & \to & AY \\ Y\mathtt{a} & \to & \mathtt{a}Y \\ YU & \to & T \\ T & \to & X \\ \mathtt{a}X & \to & X\mathtt{a}\mathtt{a} \\ AX & \to & X\mathtt{a} \\ HX & \to & \epsilon\\ \end{array}