# Dyck language

In the theory of formal languages of computer science, mathematics, and linguistics, a Dyck word is a balanced string of brackets. The set of Dyck words forms a Dyck language. The simplest, Dyck-1, uses just two matching brackets, e.g. ( and ).
Dyck words and language are named after the mathematician Walther von Dyck. They have applications in the parsing of expressions that must have a correctly nested sequence of brackets, such as arithmetic or algebraic expressions.

## Formal definition

Let $\Sigma =\{[,]\}$ be the alphabet consisting of the symbols [ and ]. Let $\Sigma ^{*}$ denote its Kleene closure. The Dyck language is defined as:
    $\{u\in \Sigma ^{*}\vert {\text{ all prefixes of }}u{\text{ contain no more ]'s than ['s}}{\text{ and the number of ['s in }}u{\text{ equals the number of ]'s}}\}.$

### Context-free grammar

It may be helpful to define the Dyck language via a context-free grammar in some situations. The Dyck language is generated by the context-free grammar with a single non-terminal S, and the production:
    S → ε | "[" S "]" S
That is, S is either the empty string (ε) or is "[", an element of the Dyck language, the matching "]", and an element of the Dyck language.
An alternative context-free grammar for the Dyck language is given by the production:
    S → ("[" S "]")$^*$
That is, S is zero or more occurrences of the combination of "[", an element of the Dyck language, and a matching "]", where multiple elements of the Dyck language on the right side of the production are free to differ from each other.

### Alternative definition

In yet other contexts it may instead be helpful to define the Dyck language by splitting $\Sigma ^{*}$ into equivalence classes, as follows. For any element $u\in \Sigma ^{*}$ of length $|u|$, we define partial functions $\operatorname {insert} :\Sigma ^{*}\times \mathbb {N} \rightarrow \Sigma ^{*}$ and $\operatorname {delete} :\Sigma ^{*}\times \mathbb {N} \rightarrow \Sigma ^{*}$ by
    $\operatorname {insert} (u,j)$ is $u$ with "$$" inserted into the $j$th position
    $\operatorname {delete} (u,j)$ is $u$ with "$$" deleted from the $j$th position
with the understanding that $\operatorname {insert} (u,j)$ is undefined for $j>|u|$ and $\operatorname {delete} (u,j)$ is undefined if $j>|u|-2$. We define an equivalence relation $R$ on $\Sigma ^{*}$ as follows: for elements $a,b\in \Sigma ^{*}$ we have $(a,b)\in R$ if and only if there exists a sequence of zero or more applications of the $\operatorname {insert}$ and $\operatorname {delete}$ functions starting with $a$ and ending with $b$. That the sequence of zero operations is allowed accounts for the reflexivity of $R$. Symmetry follows from the observation that any finite sequence of applications of $\operatorname {insert}$ to a string can be undone with a finite sequence of applications of $\operatorname {delete}$. Transitivity is clear from the definition.
The equivalence relation partitions the language $\Sigma ^{*}$ into equivalence classes. If we take $\epsilon$ to denote the empty string, then the language corresponding to the equivalence class $\operatorname {Cl} (\epsilon )$ is called the Dyck language.

## Generalizations
### Typed Dyck language

There exist variants of the Dyck language with multiple delimiters, e.g., Dyck-2 on the alphabet "(", ")", "[", and "]". The words of such a language are the ones which are well-parenthesized for all delimiters, i.e., one can read the word from left to right, push every opening delimiter on the stack, and whenever we reach a closing delimiter then we must be able to pop the matching opening delimiter from the top of the stack. (The counting algorithm above does not generalise).
For example, the following is a valid sentence in Dyck-3 (with matching delimiters colored the same):

### Finite depth

A Dyck language sentence can be pictured as a descent and ascent through the levels of nested brackets. As one reads along a Dyck sentence, each opening bracket increases the nesting depth by 1, and each closing bracket decreases by 1. The depth of a sentence is the maximal depth reached within the sentence.
For example, we can annotate the following sentence with the depth at each step:
   0 ( 1 [ 2 [ 3 ] 2 { 3 } 2 ] 1 ( 2 ) 1 { 2 ( 3 ) 2 } 1 ) 0 [ 1 ] 0
and the entire sentence has depth 3.
We define Dyck-(k, m) as the language with k types of brackets and maximal depth m. This has applications in the formal theory of recurrent neural networks.

## Properties

* The Dyck language is closed under the operation of concatenation.
* By treating $\Sigma ^{*}$ as an algebraic monoid under concatenation we see that the monoid structure transfers onto the quotient $\Sigma ^{*}/R$, resulting in the syntactic monoid of the Dyck language. The class $\operatorname {Cl} (\epsilon )$ will be denoted $1$.
* The syntactic monoid of the Dyck language is not commutative: if $u=\operatorname {Cl} ($ and $v=\operatorname {Cl})$ then $uv=\operatorname {Cl} ()=1\neq \operatorname {Cl}=vu$.
* With the notation above, $uv=1$ but neither $u$ nor $v$ are invertible in $\Sigma ^{*}/R$.
* The syntactic monoid of the Dyck language is isomorphic to the bicyclic semigroup by virtue of the properties of $\operatorname {Cl} ($ and $\operatorname {Cl})$ described above.
* By the Chomsky–Schützenberger representation theorem, any context-free language is a homomorphic image of the intersection of some regular language with a Dyck language on one or more kinds of bracket pairs.
* The Dyck language with two distinct types of brackets can be recognized in the complexity class $TC^{0}$.
* The number of distinct Dyck words with exactly n pairs of parentheses and k innermost pairs (viz. the substring $[\ ]$) is the Narayana number $\operatorname {N} (n,k)$.
* The number of distinct Dyck words with exactly n pairs of parentheses is the n-th Catalan number $C_{n}$. Notice that the Dyck language of words with n parentheses pairs is equal to the union, over all possible k, of the Dyck languages of words of n parentheses pairs with k innermost pairs, as defined in the previous point. Since k can range from 0 to n, we obtain the following equality, which indeed holds:
       $C_{n}=\sum _{k=1}^{n}\operatorname {N} (n,k)$

## Examples

We can define an equivalence relation $L$ on the Dyck language ${\mathcal {D}}$. For $u,v\in {\mathcal {D}}$ we have $(u,v)\in L$ if and only if $|u|=|v|$, i.e. $u$ and $v$ have the same length. This relation partitions the Dyck language: ${\mathcal {D}}/L=\{{\mathcal {D}}_{0},{\mathcal {D}}_{1},\ldots \}$. We have ${\mathcal {D}}={\mathcal {D}}_{0}\cup {\mathcal {D}}_{2}\cup {\mathcal {D}}_{4}\cup \ldots =\bigcup _{n=0}^{\infty }{\mathcal {D}}_{n}$ where ${\mathcal {D}}_{n}=\{u\in {\mathcal {D}}\mid |u|=n\}$. Note that ${\mathcal {D}}_{n}$ is empty for odd $n$.
Having introduced the Dyck words of length $n$, we can introduce a relationship on them. For every $n\in \mathbb {N}$ we define a relation $S_{n}$ on ${\mathcal {D}}_{n}$; for $u,v\in {\mathcal {D}}_{n}$ we have $(u,v)\in S_{n}$ if and only if $v$ can be reached from $u$ by a series of proper swaps. A proper swap in a word $u\in {\mathcal {D}}_{n}$ swaps an occurrence of '][' with ''. For each $n\in \mathbb {N}$ the relation $S_{n}$ makes ${\mathcal {D}}_{n}$ into a partially ordered set. The relation $S_{n}$ is reflexive because an empty sequence of proper swaps takes $u$ to $u$. Transitivity follows because we can extend a sequence of proper swaps that takes $u$ to $v$ by concatenating it with a sequence of proper swaps that takes $v$ to $w$ forming a sequence that takes $u$ into $w$. To see that $S_{n}$ is also antisymmetric we introduce an auxiliary function $\sigma _{n}:{\mathcal {D}}_{n}\rightarrow \mathbb {N}$ defined as a sum over all prefixes $v$ of $u$:
    $\sigma _{n}(u)=\sum _{vw=u}{\Big({\text{count of ['s in }}v)-({\text{count of ]'s in }}v){\Big )}$
The following table illustrates that $\sigma _{n}$ is strictly monotonic with respect to proper swaps.
Strict monotonicity of $\sigma _{n}$ :
| partial sums of $\sigma _{n}(u)$ | $P$ | $P-1$ | $P$ | $Q$ |
| $u$ | $\ldots$ | ] | [ | $\ldots$ |
| $u'$ | $\ldots$ | [ | ] | $\ldots$ |
| partial sums of $\sigma _{n}(u')$ | $P$ | $P+1$ | $P$ | $Q$ |
| Difference of partial sums | 0 | 2 | 0 | 0 |
Hence $\sigma _{n}(u')-\sigma _{n}(u)=2>0$ so $\sigma _{n}(u)<\sigma _{n}(u')$ when there is a proper swap that takes $u$ into $u'$. Now if we assume that both $(u,v),(v,u)\in S_{n}$ and $u\neq v$, then there are non-empty sequences of proper swaps such $u$ is taken into $v$ and vice versa. But then $\sigma _{n}(u)<\sigma _{n}(v)<\sigma _{n}(u)$ which is nonsensical. Therefore, whenever both $(u,v)$ and $(v,u)$ are in $S_{n}$, we have $u=v$, hence $S_{n}$ is antisymmetric.
The partial ordered set $D_{8}$ is shown in the illustration accompanying the introduction if we interpret a [ as going up and ] as going down.