def assign_node_ids(t, suffix=''):
    """
    makes sure that a tree, and all its terminal
    and nonterminal nodes, have a suitable xml_id
    attribute.
    """
    if hasattr(t, 'xml_id'):
        sent_id = t.xml_id
    elif hasattr(t, 'sent_no'):
        sent_id = 's%s' % (t.sent_no,)
        if sent_id[:2] == 'ss':
            sent_id = sent_id[1:]
        t.xml_id = sent_id
    elif hasattr(t, 'sent_id'):
        sent_id = str(t.sent_id)
        t.xml_id = sent_id
    for i, n in enumerate(t.terminals):
        if not hasattr(n, 'xml_id'):
            n.xml_id = '%s_%s' % (sent_id, i + 1)
    node_id = 500
    for n in t.bottomup_enumeration():
        if n.isTerminal():
            continue
        if hasattr(n, 'xml_id'):
            continue
        n.xml_id = '%s_n%s%s' % (sent_id, node_id, suffix)
        node_id += 1
