def remove_unit_rules(grammar, inplace=False):
    # type: (Grammar, bool) -> Grammar
    """
    Remove unit rules from the grammar.
    :param grammar: Grammar where remove the rules.
    :param inplace: True if transformation should be performed in place. False by default.
    :return: Grammar without unit rules.
    """
    # copy if needed
    if inplace is False:
        grammar = copy(grammar)
    # get connections
    res = find_nonterminals_reachable_by_unit_rules(grammar)
    # iterate through rules
    for rule in grammar.rules.copy():
        # delete unit rules
        if _is_unit(rule):
            grammar.rules.remove(rule)
            continue
        for nonterm in grammar.nonterminals:
            # find all nonterminals that can rewrite to current rule
            path = res.path_rules(nonterm, rule.fromSymbol)
            # get rid of cyclic paths
            if len(path) > 0 and path[0].fromSymbol != path[-1].toSymbol:
                created = _create_rule(path, rule)
                grammar.rules.add(created)
    return grammar