Matrix grammar

A matrix grammar is a formal grammar in which instead of single productions, productions are grouped together into finite sequences. A production cannot be applied separately, it must be applied in sequence. In the application of such a sequence of productions, the rewriting is done in accordance to each production in sequence, the first one, second one etc. till the last production has been used for rewriting. The sequences are referred to as matrices.

Matrix grammar is an extension of context-free grammar, and one instance of a controlled grammar.

Formal definition
A matrix grammar is an ordered quadruple $$G = (V_N, V_T, X_0, M)$$ where $$\quad P \in V^* V_N V^*, \quad Q \in V^*, \quad V = V_N \cup V_T.$$
 * $$V_N$$ is a finite set of non-terminals
 * $$V_T$$ is a finite set of terminals
 * $$X_0$$ is a special element of $$V_N$$, viz. the starting symbol
 * $$M$$ is a finite set of non-empty sequences whose elements are ordered pairs $$(P, Q)$$ where

The pairs are called productions, written as $$P \to Q$$. The sequences are called matrices and can be written as

$$m = [P_1 \to Q_1, \ldots, P_r \to Q_r].$$

Let $$F$$ be the set of all productions appearing in the matrices $$m$$ of a matrix grammar $$G$$. Then the matrix grammar $$G$$ is of type-$$i, i = 0, 1, 2, 3$$, length-increasing, linear, $$\lambda$$-free, context-free or context-sensitive if and only if the grammar $$G_1 = (V_N, V_T, X_0, F)$$ has the following property.

For a matrix grammar $$G$$, a binary relation $$\Rightarrow_G$$ is defined; also represented as $$\Rightarrow$$. For any $$P, Q \in V^*$$, $$P \Rightarrow Q$$ holds if and only if there exists an integer $$r \ge 1$$ such that the words

$$\alpha_1,, \ldots, \alpha_{r + 1}, \quad P_1, \ldots, P_r, \quad Q_1, \ldots, Q_r, \quad R_1, \ldots, R_r, \quad, R^1, \ldots, R^r$$

over V exist and
 * $$\alpha_1 = P$$ and $$\alpha_{r + 1} = Q$$
 * $$[P_1 \to Q_1, \ldots, P_r \to Q_r]$$ is one of the matrices of $$G$$
 * $$\alpha_i = R_i P_i R^i$$ and $$\alpha_{i + 1} = R_i Q_i R^i$$ for all $$i$$ such that $$1 \leq i \leq r.$$

Let $$\Rightarrow^{*}$$ be the reflexive transitive closure of the relation $$\Rightarrow$$. Then, the language generated by the matrix grammar $$G$$ is given by


 * $$L(G) = \{P \in {V_T}^* | X_0 \Rightarrow^{*} P\}.$$

Examples
Consider the matrix grammar

$$G = (\{S, X, Y\}, \{a, b, c\}, S, M)$$

where $$M$$ is a collection containing the following matrices:

$$ m_0 : [S \rightarrow XY], \quad m_1 : [X \rightarrow aXb, Y \rightarrow cY], \quad m_2 : [X \rightarrow ab, Y \rightarrow c] $$

These matrices, which contain only context-free rules, generate the context-sensitive language

$$L = \{a^nb^nc^n|n \ge 1\}.$$ The associate word of $$a^nb^nc^n $$ is $$Aw(a^nb^nc^n) = m_0m^{n-2}_1m_2, \forall n \geq 2$$ and $$ Aw(abc) = m_0m_2 $$.

This example can be found on pages 8 and 9 of in the following form: Consider the matrix grammar

$$G = (\{S, X, Y, Z\}, \{a, b, c\}, S, M)$$

where $$M$$ is a collection containing the following matrices:

$$ m_0 : [S \rightarrow abc], \quad m_1 : [S \rightarrow aXbYcZ], \quad m_2 : [X \rightarrow aX, Y \rightarrow bY, Z \rightarrow cZ], \quad m_3 : [X \rightarrow ab, Y \rightarrow b, Z \rightarrow c] $$

These matrices, which contain only context-regular rules, generate the context-sensitive language

$$L = \{a^nb^nc^n|n \ge 1\}.$$

The associate word of $$a^nb^nc^n $$ is $$Aw(a^nb^nc^n) = m_1m^{n-2}_2m_3, \forall n \geq 2$$ and $$ Aw(abc) = m_0 $$.

Properties
Let be the class of languages produced by matrix grammars, and $MAT$ the class of languages produced by $$\lambda$$-free matrix grammars.
 * Trivially, $MAT$ is included in.
 * All context-free languages are in $MAT$, and all languages in are recursively enumerable.
 * $MAT$ is closed under union, concatenation, intersection with regular languages and permutation.
 * All languages in $MAT$ can be produced by a context-sensitive grammar.
 * There exists a context-sensitive language which does not belong to.
 * Each language produced by a matrix grammar with only one terminal symbol is regular.

Open problems
It is not known whether there exist languages in which are not in $MAT$, and it is neither known whether  contains languages which are not context-sensitive.