7.1.6 Cloning Nonterminal Symbols

A nonterminal symbol assignment using ‘=’ creates a shallow copy of a nonterminal symbol:

NewNonterminalSymbol = ExistingNonterminalSymbol ;

This assignment is equivalent to defining a nonterminal symbol NewNonterminalSymbol with the right-hand side of a nonterminal symbol ExistingNonterminalSymbol. The grammar must contain the definition of ExistingNonterminalSymbol earlier in its text.

Example

The grammar fragment

A: "c" B
 | C C
;

E = A ;

is equivalent to the fragment

A: "c" B
 | C C
;

E: "c" B
 | C C
;

A nonterminal symbol assignment using ‘=*=’ creates a deep copy of a nonterminal symbol:

NewNonterminalSymbol =*= ExistingNonterminalSymbol ;

The grammar must contain the definition of ExistingNonterminalSymbol earlier in its text.

The deep copy operation first collects a set of nonterminal symbols in a right-hand side defined for ExistingNonterminalSymbol and right-hand sides of all nonterminal symbols referenced from the right-hand side recursively. For every nonterminal symbol in the set, the operation generates a unique nonterminal symbol name by appending a suffix ‘__NUMBER’ to a source nonterminal symbol name or changing an already existing suffix. The operation then defines right-hand sides for the new nonterminal symbols as right-hand sides of corresponding source nonterminal symbols with changing all nonterminal symbols in the right-hand sides to corresponding new nonterminal symbols.

Example

In the grammar

S: A E ;

A: . B
 | C C
;

B: C D D ;
C: . D ;

D: . B
 | .
;

E =*= A ;

the assignment ‘E =*= A ;’ is equivalent to the productions

E: . B__0
 | C__0 C__0
;

B__0: C__0 D__0 D__0 ;
C__0: . D__0 ;

D__0: . B__0
    | .
;

Adding the assignment ‘F =*= A ;’ or ‘F =*= E ;’ is equivalent to adding the productions

F: . B__1
 | C__1 C__1
;

B__1: C__1 D__1 D__1 ;
C__1: . D__1 ;

D__1: . B__1
    | .
;