Dyck language explained

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, D1, 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*\vertallprefixesofucontainnomore]'sthan['sandthenumberof['sinuequalsthenumberof]'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, and the production:

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:

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* x N\Sigma*

and

\operatorname{delete}:\Sigma* x N\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)\inR

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.

Properties

\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

.

u=\operatorname{Cl}([)

and

v=\operatorname{Cl}(])

then

uv=\operatorname{Cl}([])=1\ne\operatorname{Cl}(][)=vu

.

uv=1

but neither

u

nor

v

are invertible in

\Sigma*/R

.

\operatorname{Cl}([)

and

\operatorname{Cl}(])

described above.

[]

) is the Narayana number

\operatorname{N}(n,k)

.

Cn

. Notice that the Dyck language of words with parentheses pairs is equal to the union, over all possible, of the Dyck languages of words of parentheses pairs with innermost pairs, as defined in the previous point. Since can range from 0 to, we obtain the following equality, which indeed holds:

Cn=

n
\sum
k=1

\operatorname{N}(n,k)

Examples

We can define an equivalence relation

L

on the Dyck language

l{D}

. For

u,v\inl{D}

we have

(u,v)\inL

if and only if

|u|=|v|

, i.e.

u

and

v

have the same length. This relation partitions the Dyck language:

l{D}/L=\{l{D}0,l{D}1,\ldots\}

. We have

l{D}=l{D}0\cupl{D}2\cupl{D}4\cup\ldots=

infty
cup
n=0

l{D}n

where

l{D}n=\{u\inl{D}\mid|u|=n\}

. Note that

l{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\inN

we define a relation

Sn

on

l{D}n

; for

u,v\inl{D}n

we have

(u,v)\inSn

if and only if

v

can be reached from

u

by a series of proper swaps. A proper swap in a word

u\inl{D}n

swaps an occurrence of '][' with '[]'.For each

n\inN

the relation

Sn

makes

l{D}n

into a partially ordered set. The relation

Sn

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

Sn

is also antisymmetric we introduce an auxiliary function

\sigman:l{D}nN

defined as a sum over all prefixes

v

of

u

:

\sigman(u)=\sumvw=u((countof['sinv)-(countof]'sinv))

The following table illustrates that

\sigman

is strictly monotonic with respect to proper swaps.
Strict monotonicity of

\sigman

partial sums of

\sigman(u)

P

P-1

P

Q

u

\ldots

] [|| <math>\ldots</math> |- ! <math>u'</math> | <math>\ldots</math> || [ || ] ||

\ldots

|-! partial sums of

\sigman(u')

|

P

||

P+1

||

P

||

Q

|-! Difference of partial sums| 0 || 2 || 0 || 0|-|}

Hence

\sigman(u')-\sigman(u)=2>0

so

\sigman(u)<\sigman(u')

when there is a proper swap that takes

u

into

u'

. Now if we assume that both

(u,v),(v,u)\inSn

and

u\nev

, then there are non-empty sequences of proper swaps such

u

is taken into

v

and vice versa. But then

\sigman(u)<\sigman(v)<\sigman(u)

which is nonsensical. Therefore, whenever both

(u,v)

and

(v,u)

are in

Sn

, we have

u=v

, hence

Sn

is antisymmetric.

The partial ordered set

D8

is shown in the illustration accompanying the introduction if we interpret a [as going up and ] as going down.

Generalizations

There exist variants of the Dyck language with multiple delimiters, e.g., D2 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).

See also

References

Notes and References

  1. Kambites, Communications in Algebra Volume 37 Issue 1 (2009) 193-208
  2. Barrington and Corbett, Information Processing Letters 32 (1989) 251-256