Substitution in C
/* Substitute new for old in tree */
TOKEN subst (TOKEN new, TOKEN old, TOKEN tree)
{ TOKEN tok, last, opnd, ptr;
if (tree == NULL) return (tree);
if (tree->tokentype == OPERATOR)
{ last = NULL;
ptr = tree->operands;
tok = copytok(tree);
while ( ptr != NULL )
{ opnd = subst (new, old, ptr);
if (last == NULL)
tok->operands = opnd;
else last->link = opnd;
last = opnd;
ptr = ptr->link;
}
return (tok) ;
}
else if (tree->tokentype == IDENTIFIERTOK
&& strcmp(tree->stringval,
old->stringval) == 0)
return ( copytok(new) );
else return ( copytok(tree) );
}