def process_preamble(tok):
    """ process_preamble : list -> dict

        process_preamble(l) goes over the list l searching for the POMDP prb information and fills in a dictionary
        structure with that info. Preamble lines are removed from l (destructively). The returned dictionary
        includes the fields:

        . states       -> a list of entities corresponding to the POMDP states.
        . actions      -> a list of entities corresponding to the POMDP actions.
        . observations -> a list of entities corresponding to the POMDP observations.
        . discount     -> the value of the discount for the POMDP.
        . cost         -> +1 or -1, depending on whether the POMDP is defined as rewards or costs.

        If any of the fields is not provided, the function raises an exception. """

    fields = ['states', 'actions', 'observations', 'discount', 'cost']
    prb = dict()

    i = 0
    while i < len(tok):
        line = tok[i]

        if line[0] == 'states:':
            if len(line) == 1:
                line = line + tok[i + 1]
                del (tok[i + 1])

            if len(line) > 2:
                prb['states'] = line[1:]
            elif line[1].isdigit():
                prb['states'] = list(range(int(line[1])))
            else:
                raise ValueError('process_preamble -> invalid specification')
            del (tok[i])

        elif line[0] == 'actions:':
            if len(line) > 2 or len(line) == 1:
                if len(line) == 1:
                    line = line + tok[i + 1]
                    del (tok[i + 1])
                prb['actions'] = line[1:]
            elif line[1].isdigit():
                prb['actions'] = list(range(int(line[1])))
            else:
                raise ValueError('process_preamble -> invalid specification')
            del (tok[i])

        elif line[0] == 'observations:':
            if len(line) > 2 or len(line) == 1:
                if len(line) == 1:
                    line = line + tok[i + 1]
                    del (tok[i + 1])
                prb['observations'] = line[1:]
            elif line[1].isdigit():
                prb['observations'] = list(range(int(line[1])))
            else:
                raise ValueError('process_preamble -> invalid specification')
            del (tok[i])

        elif line[0] == 'discount:':
            if len(line) > 2:
                raise ValueError('process_preamble -> invalid specification')

            prb['discount'] = float(line[1])
            del (tok[i])

        elif line[0] == 'values:':
            if len(line) > 2:
                raise ValueError('process_preamble -> invalid specification')

            if line[1] == 'reward':
                prb['cost'] = -1
            elif line[1] == 'cost':
                prb['cost'] = 1
            else:
                raise ValueError('process_preamble -> invalid specification')
            del (tok[i])

        else:
            i += 1

    for fld in fields:
        if fld not in prb:
            raise ValueError('process_preamble -> incomplete specification (missing %s)' % fld)

    #print('Preamble: processed successfully.')

    return prb