let get_level entry position levs =
                  match position with
                  | Some First -> ([], empty_lev, levs)
                  | Some Last -> (levs, empty_lev, [])
                  | Some (Level n) ->
                      let rec get =
                        (function
                         | [] ->
                             (eprintf
                                "No level labelled \"%s\" in entry \"%s\"\n"
                                n entry.ename;
                              flush Pervasives.stderr;
                              failwith "Grammar.extend")
                         | lev :: levs ->
                             if Tools.is_level_labelled n lev
                             then ([], (change_lev entry lev n), levs)
                             else
                               (let (levs1, rlev, levs2) = get levs
                                in ((lev :: levs1), rlev, levs2)))
                      in get levs
                  | Some (Before n) ->
                      let rec get =
                        (function
                         | [] ->
                             (eprintf
                                "No level labelled \"%s\" in entry \"%s\"\n"
                                n entry.ename;
                              flush Pervasives.stderr;
                              failwith "Grammar.extend")
                         | lev :: levs ->
                             if Tools.is_level_labelled n lev
                             then ([], empty_lev, (lev :: levs))
                             else
                               (let (levs1, rlev, levs2) = get levs
                                in ((lev :: levs1), rlev, levs2)))
                      in get levs
                  | Some (After n) ->
                      let rec get =
                        (function
                         | [] ->
                             (eprintf
                                "No level labelled \"%s\" in entry \"%s\"\n"
                                n entry.ename;
                              flush Pervasives.stderr;
                              failwith "Grammar.extend")
                         | lev :: levs ->
                             if Tools.is_level_labelled n lev
                             then ([ lev ], empty_lev, levs)
                             else
                               (let (levs1, rlev, levs2) = get levs
                                in ((lev :: levs1), rlev, levs2)))
                      in get levs
                  | None ->
                      (match levs with
                       | lev :: levs ->
                           ([], (change_lev entry lev "<top>"), levs)
                       | [] -> ([], empty_lev, []))