You are here
HomeDyck language
Primary tabs
Dyck language
The importance of using parentheses can be illustrated by looking at the following expression:
$((21)\cdot((1+2))\cdot 4)\div((1+2)\cdot 2)$ 
There is no ambiguity in computing the result, which is $2$. If we remove all the parentheses in the expression, we get
$21\cdot1+2\cdot 4\div 1+2\cdot 2$ 
which does not make much sense, unless we know the order of arithmetic operations in advance. In addition, without using parentheses, the result will differ depending on how the order of operations is assigned.
Now, if we remove all the symbols in the first expression above except the parentheses, we get
$(()(()))(())$ 
an expression known as a word of “wellbalanced” parentheses.
Formally, let $\Sigma_{1}=\{(,)\}$ be an alphabet consisting of the left and right parentheses. Given word $u$ over $\Sigma_{1}$, let $D_{1}(u)$ be the number of occurrences of the left parentheses in $u$ minus the number of occurrences of the right parentheses in $u$.
Definition. A word $u$ over $\Sigma_{1}$ is said to be a word of wellbalanced parentheses, if
1. $D_{1}(u)=0$, and
2. $D_{1}(v)\geq 0$ for any prefix $v$ of $u$.
For simplicity, we also say that $u$ is a wellbalanced word over $\Sigma_{1}$.
Given this definition, the word above is wellbalanced, but $()(()))$ and $)())(($ are not.
Definition. The set of wellbalanced words over $\Sigma_{1}$ is called the parenthesis language or Dyck language over $\Sigma_{1}$, and is denoted by $\boldsymbol{\operatorname{Paren}_{1}}$.
By induction, it is not hard to see that $\boldsymbol{\operatorname{Paren}_{1}}$ can be generated by the following grammar:
1. terminal set is $\Sigma_{1}$,
2. nonterminal set is the singleton consisting of the start symbol $\sigma$,
3. productions are $\sigma\to\lambda$ (the empty word), $\sigma\to\sigma\sigma$, and $\sigma\to(\sigma)$.
As a result, $\boldsymbol{\operatorname{Paren}_{1}}$ is contextfree. Furthermore, $\boldsymbol{\operatorname{Paren}_{1}}$ is a deterministic language, and hence unambiguous.
More generally, one can consider expressions involving more than one type of parentheses, such as [], $\{\}$, and $\langle\rangle$.
Definition. Let $\Sigma_{n}=\{(_{1},)_{1},\ldots,(_{n},)_{n}\}$ be an alphabet consisting of $n$ types of parentheses, a left and a right one for each type. The Dyck language over $\Sigma_{n}$, written $\boldsymbol{\operatorname{Paren}_{n}}$, is the language generated by the following grammar:
1. terminal set is $\Sigma_{n}$,
2. nonterminal set is the singleton consisting of the start symbol $\sigma$,
3. productions are $\sigma\to\lambda$ (the empty word), $\sigma\to\sigma\sigma$, and $\sigma\to(_{i}\sigma)_{i}$ for each $i$ in $\{1,\ldots,n\}$.
As before, $\boldsymbol{\operatorname{Paren}_{n}}$ is contextfree, and deterministic in particular, and hence unambiguous.
Words in $\boldsymbol{\operatorname{Paren}_{n}}$ are also called wellbalanced. However, it is a little more complicated to characterize what a wellbalanced word is. The two criteria above for the case $n=1$, while necessary, are not sufficient enough to describe the “wellnestedness” of parentheses when $n>1$. For example, if $n=2$, and the parentheses considered are $\{\}$ and $[]$, then the word $[\{]\}$ satisfy both criteria above, but fail to be wellnested.
In order to fully characterize a wellbalanced word over $\Sigma_{n}$, we first define, for each $i=1,\ldots,n$, the function $D_{i}$ much the same way as $D_{1}$: so that $D_{i}(u)$ is the number of left parentheses $(_{i}$, minus the number of right parentheses $)_{i}$. Call a word $u$ partially balanced if, for every $i=1,\ldots,n$:
1. $D_{i}(u)=0$, and
2. $D_{i}(v)\geq 0$ for every prefix $v$ of $u$.
Next, write $u=u_{1}\cdots u_{m}$, where each $u_{k}$ is a symbol in $\Sigma_{n}$. Let $u(j)$ be the prefix $u_{1}\cdots u_{j}$. Given a position $j$ in $u$, if $u_{j}$ is a left parenthesis, say $(_{i}$, then there is a corresponding right parenthesis $)_{i}$ in $u$ to the right of $u_{j}$, positioned at, say $k$, satisfying the equation $D_{i}(u(j))=D_{i}(u(k))+1$. This is a straightforward result of the two criteria above. Let $j^{+}$ be the least such position such that the equation holds. Now, if $u_{j}$ is right parenthesis, then for some position $k<j$, we have $k^{+}=j$. This means that, given any position $j$ in $u$, there is a unique pair of positions $(j_{0},j_{1})$, such that

either $j=j_{0}$ or $j=j_{1}$, and

$j_{0}^{+}=j_{1}$.
Define, for each $j$, the word $u[j]$ to be the subword of $u$ with starting position $j_{0}$ and ending position $j_{1}$. Now, we are ready to state the last criterion in order that $u$ be wellbalanced:
 3.
for each position $j$ in $u$, the word $u[j]$ is partially balanced.
It can be shown, the set of words satisfying all three criteria above is $\boldsymbol{\operatorname{Paren}_{n}}$. Furthermore, if $n=1$, the third criterion can be derived from the first two criteria.
Other than being deterministic, some basic properties of $\boldsymbol{\operatorname{Paren}_{n}}$:

$\boldsymbol{\operatorname{Paren}_{m}}\subseteq\boldsymbol{\operatorname{Paren}% _{n}}$, for any $m\leq n$.

More generally, $\boldsymbol{\operatorname{Paren}_{n}}$ is insertion closed: if $u,vw\in\boldsymbol{\operatorname{Paren}_{n}}$, then $vuw\in\boldsymbol{\operatorname{Paren}_{n}}$.

$\boldsymbol{\operatorname{Paren}_{n}}$ is also deletion closed: if $u_{1}vu_{2},v\in\boldsymbol{\operatorname{Paren}_{n}}$, then $u_{1}u_{2}\in\boldsymbol{\operatorname{Paren}_{n}}$.

$\boldsymbol{\operatorname{Paren}_{n}}$ is not prefixfree.

Suppose $f:\Sigma_{n}\to\Sigma_{m}^{*}$ is a function, such that for each $i=1,\ldots,n$, $f$ maps $(_{i}$ and $)_{i}$ to some $u_{i}$ and $v_{i}$ respectively such that $u_{i}v_{i}\in\boldsymbol{\operatorname{Paren}_{m}}$. Then the extension $f^{*}:\Sigma_{n}^{*}\to\Sigma_{m}^{*}$, when restricted to $\boldsymbol{\operatorname{Paren}_{n}}$, is a language homomorphism from $\boldsymbol{\operatorname{Paren}_{n}}$ to $\boldsymbol{\operatorname{Paren}_{m}}$.
Remark. It can be shown that the number of words of length $2n$ in $\boldsymbol{\operatorname{Paren}_{1}}$ is the $n$th Catalan number. For a proof of this, see this entry. The idea is to visualize a word in $\boldsymbol{\operatorname{Paren}_{1}}$ as a path in a twodimensional lattice, which can be generated as follows: given a word $u$ of length $2n$, the path $p(u)$ starts from $(0,0)$ (which corresponds to the first symbol of $u$). If point $(i,j)$ is on the path, then the next point on the path is $(i+1,k)$, where $k=j+1$ if the $i$th symbol of $u$ is $($, otherwise $k=j1$. So the increase from one point to the next is either $(1,1)$, or $(1,1)$. As a result, the path $P(u)$ has the property that it never dips below the $x$axis, and it ends at $(2n,0)$. Paths defined this way are also known as Dyck paths.
References
 1 D. C. Kozen, Automata and Computability, Springer, New York (1997).
 2 J.E. Hopcroft, J.D. Ullman, Formal Languages and Their Relation to Automata, AddisonWesley, (1969).
Mathematics Subject Classification
68Q45 no label found68Q42 no label found Forums
 Planetary Bugs
 HS/Secondary
 University/Tertiary
 Graduate/Advanced
 Industry/Practice
 Research Topics
 LaTeX help
 Math Comptetitions
 Math History
 Math Humor
 PlanetMath Comments
 PlanetMath System Updates and News
 PlanetMath help
 PlanetMath.ORG
 Strategic Communications Development
 The Math Pub
 Testing messages (ignore)
 Other useful stuff
 Corrections