In theoretical computer science and formal language theory, a regular tree grammar is a formal grammar that describes a set of directed trees, or terms. A regular word grammar can be seen as a special kind of regular tree grammar, describing a set of single-path trees.
A regular tree grammar G is defined by the tuple G = (N, ã, Z, P), where:
The grammar G implicitly defines a set of trees: any tree that can be derived from Z using the rule set P is said to be described by G. This set of trees is known as the language of G. Formally, the relation âÂÂ<sub>G</sub> on the set T<sub>ã</sub>(N) is defined as follows:
A tree can be derived in a single step into a tree (in short: t<sub>1</sub> âÂÂ<sub>G</sub> t<sub>2</sub>), if there is a context S and a production such that:
Here, a context means a tree with exactly one hole in it; if S is such a context, S[t] denotes the result of filling the tree t into the hole of S.
The tree language generated by G is the language .
Here, T<sub>ã</sub> denotes the set of all trees composed from symbols of ã, while âÂÂ<sub>G*</sub> denotes successive applications of âÂÂ<sub>G</sub>.
A language generated by some regular tree grammar is called a regular tree language.
Let G<sub>1</sub> = (N<sub>1</sub>,ã<sub>1</sub>,Z<sub>1</sub>,P<sub>1</sub>), where
An example derivation from the grammar G<sub>1</sub> is
BList â cons(Bool,BList) â cons(false,cons(Bool,BList)) â cons(false,cons(true,nil)).
The image shows the corresponding derivation tree; it is a tree of trees (main picture), whereas a derivation tree in word grammars is a tree of strings (upper left table).
The tree language generated by G<sub>1</sub> is the set of all finite lists of boolean values, that is, L(G<sub>1</sub>) happens to equal T<sub>ã1</sub>. The grammar G<sub>1</sub> corresponds to the algebraic data type declarations (in the Standard ML programming language):
Every member of L(G<sub>1</sub>) corresponds to a Standard-ML value of type BList.
For another example, let , using the nonterminal set and the alphabet from above, but extending the production set by P<sub>2</sub>, consisting of the following productions:
The language L(G<sub>2</sub>) is the set of all finite lists of boolean values that contain true at least once. The set L(G<sub>2</sub>) has no datatype counterpart in Standard ML, nor in any other functional language. It is a proper subset of L(G<sub>1</sub>). The above example term happens to be in L(G<sub>2</sub>), too, as the following derivation shows:
BList â cons(false,BList) â cons(false,cons(true,BList)) â cons(false,cons(true,nil)).
If L<sub>1</sub>, L<sub>2</sub> both are regular tree languages, then the tree sets , and L<sub>1</sub> \ L<sub>2</sub> are also regular tree languages, and it is decidable whether , and whether L<sub>1</sub> = L<sub>2</sub>.
Applications of regular tree grammars include: