derivation tree of a derivation

derivation tree of a derivation

Constructing a Derivation from a Derivation Tree

Let

G

be a

context-free grammar

. Given a (finite)

derivation tree

T

of

G

, we can construct a

derivation

D

of

G

. There are two ways of doing this: top-down, or bottom-up.

The idea with the top-down approach is to build the derivation one derivation step at a time, starting from the root of the tree. During each step, a node with

children

is picked so that the next derivation step is formed, thus creating a new derivation with the new derivation step attached. Do this until all nodes of the tree are used.

To facilitate the discussion, for each node

n

of

T

, denote

[

n

]

the label of

n

.

The top-down approach works as follows:

1.

Set

X

0

=

Y

0

=

:

{

r

}

, and

D

0

the derivation

σ

=

[

r

]

, where

r

is the root of

T

.

2.

Suppose

X

i

,

Y

i

and

D

i

are defined for

i

≥

0

, and

Y

i

=

{

n

1

,

…

,

n

k

}

with

n

1

<

⋯

<

n

k

. It is easy to see that

D

i

is the derivation

σ

⇒

*

[

n

1

]

⁢

⋯

⁢

[

n

k

]

.

If none of the elements of

Y

have children, then set

D

:=

D

i

. Otherwise, pick a node

n

j

∈

Y

i

with children

n

1

′

,

…

,

n

m

′

such that

n

1

′

<

⋯

<

n

m

′

. Then set

–

X

i

+

1

:=

X

i

∪

{

n

j

}

,

–

Y

i

+

1

:=

(

Y

i

-

{

n

j

}

)

∪

{

n

1

′

,

…

,

n

m

′

}

, and

–

D

i

+

1

:=

D

i

⇒

[

n

1

]

⁢

⋯

⁢

[

n

j

-

1

]

⁢

[

n

1

′

]

⁢

⋯

⁢

[

n

m

′

]

⁢

[

n

j

+

1

]

⁢

⋯

⁢

[

n

k

]

.

Since

T

is finite,

D

can be constructed in a finite number of steps above.

Definition

. A derivation tree

T

is said to

correspond to

a derivation

D

if

D

can be constructed from

T

from the top-down.

From the construction above, one sees that every derivation tree corresponds to at least one derivation. But it may correspond to more than one derivation. If, in the second step of the construction above, one always picks the smallest node with children in each step, then the corresponding derivation is the

leftmost derivation

. Similarly, consistently picking the largest node in each step results in the rightmost derivation. This shows that a derivation tree

T

corresponds to a unique leftmost derivation (written

L

⁢

D

⁢

(

T

)

) as well as a unique rightmost derivation (written

R

⁢

D

⁢

(

T

)

).

The bottom-up approach starts out with the result

u

of the derivation tree

T

. The result corresponds to leaves of

T

.

Partition

the set of leaves into blocks, so that two leaves belonging to the same block if they have the same parent. Pick a block

B

of leaves, and form a new word

u

1

from

u

by replacing the labels of leaves in

B

by the label of their parent. Then

u

1

⇒

u

is a derivation step. Next, form a new tree

T

1

from

T

by removing the leaves of

T

in

B

. So

u

1

is now the result of

T

1

. Repeat the process just described, until all of the nodes are removed (

T

i

=

∅

for some

i

), which is possible since

T

is finite. What we end up with is a derivation. We will leave the formal detail to the reader.

Proposition 1

.

A derivation can be constructed from the top-down iff it can be constructed from the bottom-up.

Constructing a Derivation Tree from a Derivation

Conversely

, given a derivation

D

=

σ

⇒

W

1

⇒

⋯

⇒

W

n

, one can construct a derivation tree

T

such that

D

corresponds to

T

. The way this works is as follows: start with the tree with a single node whose label is

σ

. At each derivation step, additional nodes are added from the tree constructed so far, until the last derivation step is processed. The tree so constructed has a natural

linear ordering

, and hence is a derivation tree, which

D

corresponds to. This construction can be formalized as follows:

1.

Defining nodes: (The nodes of the tree

T

will be tuples of positive integers)

(a)

Set node

(

1

)

whose label is

σ

.

(b)

Suppose nodes are defined for symbols occurring in

W

i

. Look at the derivation step

W

i

⇒

W

i

+

1

. Suppose it is based on

production

P

→

Q

, and

(

p

1

,

…

,

p

m

)

is the node whose label is

P

.

*

If

Q

=

N

1

⁢

⋯

⁢

N

k

, where each

N

j

∈

Σ

, then define nodes

(

p

1

,

…

,

p

m

,

1

)

,

…

,

(

p

1

,

…

,

p

m

,

k

)

whose labels are

N

1

,

…

,

N

k

respectively.

*

If

Q

=

λ

, then define one node

(

p

1

,

…

,

p

m

,

1

)

whose label is

λ

.

(c)

Continue (b) until nodes whose labels are symbols forming word

W

n

are defined.

2.

Defining arrows: Given two nodes

p

=

(

p

1

,

…

,

p

r

)

and

q

=

(

q

1

,

…

,

q

s

)

, an arrow is constructed from

p

to

q

iff

s

=

r

+

1

and

p

1

=

q

1

,

…

,

p

r

=

q

r

.

3.

Extending the

partial order

on

T

to a linear order based on the nodes: see the entry on

ordered tree

for detail.

The

algorithm

shows that the derivation tree is uniquely constructed.

Definition

. Given a derivation

D

, the derivation tree of

D

, denoted by

T

D

, is the ordered tree constructed above from

D

.

It is easy to see that

T

D

corresponds to

D

.

Remark

. Suppose

u

is a word derived by a derivation

D

. One can find the leftmost derivation

D

′

of

u

using the methods provided above: first find

T

D

of

D

, then find

L

⁢

D

⁢

(

T

D

)

, and set

D

′

=

L

⁢

D

⁢

(

T

D

)

.

References

1

H.R. Lewis, C.H. Papadimitriou,

Elements of the Theory of Computation

. Prentice-Hall, Englewood Cliffs, New Jersey (1981).

2

A. Salomaa,

Formal Languages

, Academic Press, New York (1973).

3

J.E. Hopcroft, J.D. Ullman,

Formal Languages and Their

Relation

to

Automata

, Addison-Wesley, (1969).

Title

derivation tree of a derivation

Canonical name

DerivationTreeOfADerivation

Date of creation

2013-03-22 19:00:25

Last modified on

2013-03-22 19:00:25

Owner

CWoo (3771)

Last modified by

CWoo (3771)

Numerical id

9

Author

CWoo (3771)

Entry type

Definition

Classification

msc 68Q42

Classification

msc 68Q45

Generated on Sat Feb 10 12:43:43 2018 by

LaTeXML