import random


def generate_tree(grammar, phrase):
    """Generates a sentence from the grammar and returns its parse tree.
    
    The sentence is generated in the same manner as in generate, but the
    returned value is a nested list where the first element of each sublist
    is the name of the rule generating the phrase.
    """
    if isinstance(phrase, list):
        return [generate_tree(grammar, p) for p in phrase]
    elif phrase in grammar:
        return [phrase] + generate_tree(grammar, random.choice(grammar[phrase]))
    else:
        return [phrase]