UNIQUENODE(5)

Table of Contents

Name

uniquenode searches for a unique node in a parse tree multnodes returns the first node in a list and issues a warning

Synopsis

ANY uniquenode(tree,Ntype,defalt,mult_nodes) ANY Luniquenode(tree,Ntype,defalt,mult_nodes) ANY Duniquenode(tree,Ntype,defalt,mult_nodes,depth) ANY LDuniquenode(tree,Ntype,defalt,mult_nodes,depth) ANY multnodes(tree,Ntype,defalt,firstfound)
Parameter Declarations:
ANY tree;

TYPE Ntype;
/* type of descendent node */
ANY defalt;
/* default node if none exists */ ANY (*mult_nodes)(); /* function called if more than one node */
ANY firstfound;
/* first node of the type found in tree */
int depth;
/* depth of search */

Description

These components of the run-time library are functions that search a parse tree for descendent nodes of a particular type.

uniquenode traverses tree and searches for a unique node of type Ntype. If exactly one such node is found then uniquenode returns it. If no such nodes are found then uniquenode returns defalt. If more than one such node are found, then the parameter function mult_nodes is called, and uniquenode returns the value returned by mult_nodes. The parameters to mult_nodes are the same as those to uniquenode, except for the last parameter, which instead is the first node of type Ntype found in the search. If mult_nodes is NULL, then the library function multnodes will be called if more than one node is found. This function reports a warning (see REPORTINFO(5) ) and then returns the first node.

The L and D variations of uniquenode modify the default traversal order used in the search. Luniquenode extends the traverse to include the nodes that follow tree in a node list. Duniquenode performs a depth-limited traverse; an extra depth argument specifies the depth. LDuniquenode performs a depth-limited list traverse.

Example

Given the following grammar section:
input: ( part+ )
part: ( «wheels:» wheels | «windows:» windows | «wrenches:» wrenches )

and given the function toomany which is defined as: ANY toomany(ntype,tree,defalt,firstfound) TYPE ntype; /* the type of descendent node */ ANY tree;
ANY defalt; /* the default node if none exists */ ANY firstfound;
{
reportwarning(tree, «Too many %s parts; None used.", gtypename(ntype) );
return NULL;
}
then the expression:
uniquenode(x, Wheels, def_wheels, toomany) will return one of:
a) The wheels part found in x if these is only one. b) def_wheels if there are no wheels parts found in x. c) NULL if there are multiple wheels parts (after printing a warning message).

See Also

TRAVERSE(5)
ENCLOSING(5)


© 1990 Lucent Technologies, Inc
© 1998 Harmony Software, Inc