import re


def parse_cdec_sgml(sgml_str):
    """
    Parse a cdec-style sgml-formatted line.
    :param sgml_str:
    :return: dict with grammar-path, id, input and references
    """
    parts = sgml_str.split(' ||| ')
    if not parts:
        raise Exception('Missing fields' % sgml_str)
    pattern = re.compile(r'<seg grammar="([^"]+)" id="([0-9]+)">(.+)</seg>')
    match = pattern.match(parts[0])
    if match is None:
        raise SyntaxError('Bad sgml: %s' % parts[0])
    groups = match.groups()
    return {'grammar': groups[0],
            'sid': int(groups[1]),
            'src': groups[2],
            'refs': [ref.strip() for ref in parts[1:]]}