src/Pure/Syntax/parser.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 48992 0518bf89c777
child 55624 d52409077135
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
wenzelm@18
     1
(*  Title:      Pure/Syntax/parser.ML
wenzelm@2186
     2
    Author:     Carsten Clasohm, Sonia Mahjoub, and Markus Wenzel, TU Muenchen
wenzelm@18
     3
wenzelm@15979
     4
General context-free parser for the inner syntax of terms, types, etc.
wenzelm@18
     5
*)
wenzelm@18
     6
wenzelm@18
     7
signature PARSER =
wenzelm@15752
     8
sig
paulson@1507
     9
  type gram
paulson@1507
    10
  val empty_gram: gram
wenzelm@42288
    11
  val extend_gram: Syntax_Ext.xprod list -> gram -> gram
wenzelm@45632
    12
  val make_gram: Syntax_Ext.xprod list -> gram
paulson@1507
    13
  val pretty_gram: gram -> Pretty.T list
paulson@1507
    14
  datatype parsetree =
paulson@1507
    15
    Node of string * parsetree list |
paulson@1507
    16
    Tip of Lexicon.token
wenzelm@42374
    17
  exception PARSETREE of parsetree
wenzelm@42205
    18
  val pretty_parsetree: parsetree -> Pretty.T
wenzelm@45641
    19
  val parse: gram -> string -> Lexicon.token list -> parsetree list
haftmann@26678
    20
  val guess_infix_lr: gram -> string -> (string * bool * bool * int) option
wenzelm@41378
    21
  val branching_level: int Config.T
wenzelm@15752
    22
end;
paulson@1507
    23
wenzelm@15752
    24
structure Parser: PARSER =
wenzelm@18
    25
struct
wenzelm@15752
    26
wenzelm@18
    27
(** datatype gram **)
wenzelm@18
    28
wenzelm@38712
    29
(*production for the NTs are stored in a vector
wenzelm@38712
    30
  so we can identify NTs by their index*)
wenzelm@38712
    31
type nt_tag = int;
clasohm@1147
    32
wenzelm@38712
    33
datatype symb =
wenzelm@38712
    34
  Terminal of Lexicon.token
wenzelm@38712
    35
| Nonterminal of nt_tag * int;  (*(tag, precedence)*)
wenzelm@38712
    36
wenzelm@38712
    37
type nt_gram =
wenzelm@38712
    38
  ((nt_tag list * Lexicon.token list) *
wenzelm@38712
    39
    (Lexicon.token option * (symb list * string * int) list) list);
wenzelm@38712
    40
  (*(([dependent_nts], [start_tokens]), [(start_token, [(rhs, name, prio)])])*)
wenzelm@38712
    41
  (*depent_nts is a list of all NTs whose lookahead depends on this NT's lookahead*)
clasohm@1147
    42
clasohm@1147
    43
datatype gram =
wenzelm@38712
    44
  Gram of
wenzelm@38712
    45
   {nt_count: int,
wenzelm@38712
    46
    prod_count: int,
wenzelm@38712
    47
    tags: nt_tag Symtab.table,
wenzelm@38712
    48
    chains: (nt_tag * nt_tag list) list,  (*[(to, [from])]*)
wenzelm@38712
    49
    lambdas: nt_tag list,
wenzelm@38712
    50
    prods: nt_gram Vector.vector};
wenzelm@38712
    51
    (*"tags" is used to map NT names (i.e. strings) to tags;
wenzelm@38712
    52
     chain productions are not stored as normal productions
wenzelm@38712
    53
     but instead as an entry in "chains";
wenzelm@38712
    54
     lambda productions are stored as normal productions
wenzelm@38712
    55
     and also as an entry in "lambdas"*)
clasohm@1147
    56
wenzelm@42218
    57
val union_token = union Lexicon.matching_tokens;
wenzelm@42218
    58
val subtract_token = subtract Lexicon.matching_tokens;
clasohm@1147
    59
wenzelm@38712
    60
(*productions for which no starting token is
wenzelm@38712
    61
  known yet are associated with this token*)
wenzelm@38713
    62
val unknown_start = Lexicon.eof;
wenzelm@38712
    63
wenzelm@38712
    64
(*get all NTs that are connected with a list of NTs*)
wenzelm@23909
    65
fun connected_with _ ([]: nt_tag list) relatives = relatives
clasohm@1147
    66
  | connected_with chains (root :: roots) relatives =
wenzelm@38712
    67
      let val branches = subtract (op =) relatives (these (AList.lookup (op =) chains root));
wenzelm@38712
    68
      in connected_with chains (branches @ roots) (branches @ relatives) end;
clasohm@1147
    69
wenzelm@38712
    70
(*convert productions to grammar;
wenzelm@38712
    71
  N.B. that the chains parameter has the form [(from, [to])];
wenzelm@38712
    72
  prod_count is of type "int option" and is only updated if it is <> NONE*)
wenzelm@38712
    73
fun add_prods _ chains lambdas prod_count [] = (chains, lambdas, prod_count)
wenzelm@38712
    74
  | add_prods prods chains lambdas prod_count ((lhs, new_prod as (rhs, name, pri)) :: ps) =
wenzelm@38712
    75
      let
wenzelm@38712
    76
        val chain_from =
wenzelm@38712
    77
          (case (pri, rhs) of
wenzelm@38712
    78
            (~1, [Nonterminal (id, ~1)]) => SOME id
wenzelm@38712
    79
          | _ => NONE);
clasohm@1147
    80
wenzelm@38712
    81
        (*store chain if it does not already exist*)
wenzelm@38712
    82
        val (new_chain, chains') =
wenzelm@38712
    83
          (case chain_from of
wenzelm@38712
    84
            NONE => (NONE, chains)
wenzelm@38712
    85
          | SOME from =>
wenzelm@38712
    86
              let val old_tos = these (AList.lookup (op =) chains from) in
wenzelm@38712
    87
                if member (op =) old_tos lhs then (NONE, chains)
wenzelm@38712
    88
                else (SOME from, AList.update (op =) (from, insert (op =) lhs old_tos) chains)
wenzelm@38712
    89
              end);
clasohm@330
    90
wenzelm@38712
    91
        (*propagate new chain in lookahead and lambda lists;
wenzelm@38712
    92
          added_starts is used later to associate existing
wenzelm@38712
    93
          productions with new starting tokens*)
wenzelm@38712
    94
        val (added_starts, lambdas') =
wenzelm@38712
    95
          if is_none new_chain then ([], lambdas)
wenzelm@38712
    96
          else
wenzelm@38712
    97
            let (*lookahead of chain's source*)
wenzelm@38712
    98
              val ((from_nts, from_tks), _) = Array.sub (prods, the new_chain);
clasohm@1147
    99
wenzelm@38712
   100
              (*copy from's lookahead to chain's destinations*)
wenzelm@38712
   101
              fun copy_lookahead [] added = added
wenzelm@38712
   102
                | copy_lookahead (to :: tos) added =
wenzelm@38712
   103
                    let
wenzelm@38712
   104
                      val ((to_nts, to_tks), ps) = Array.sub (prods, to);
wenzelm@38712
   105
wenzelm@38712
   106
                      val new_tks = subtract (op =) to_tks from_tks;  (*added lookahead tokens*)
wenzelm@38712
   107
                      val _ = Array.update (prods, to, ((to_nts, to_tks @ new_tks), ps));
clasohm@1147
   108
                    in
wenzelm@38712
   109
                      copy_lookahead tos (if null new_tks then added else (to, new_tks) :: added)
clasohm@1147
   110
                    end;
clasohm@1147
   111
wenzelm@38712
   112
              val tos = connected_with chains' [lhs] [lhs];
wenzelm@38712
   113
            in
wenzelm@38712
   114
              (copy_lookahead tos [],
wenzelm@38712
   115
                union (op =) (if member (op =) lambdas lhs then tos else []) lambdas)
wenzelm@38712
   116
            end;
clasohm@1147
   117
wenzelm@38712
   118
        (*test if new production can produce lambda
wenzelm@38712
   119
          (rhs must either be empty or only consist of lambda NTs)*)
wenzelm@38712
   120
        val (new_lambda, lambdas') =
wenzelm@38712
   121
          if forall
wenzelm@38712
   122
            (fn Nonterminal (id, _) => member (op =) lambdas' id
wenzelm@38712
   123
              | Terminal _ => false) rhs
wenzelm@42218
   124
          then (true, union (op =) (connected_with chains' [lhs] [lhs]) lambdas')
wenzelm@38712
   125
          else (false, lambdas');
clasohm@1147
   126
wenzelm@38712
   127
        (*list optional terminal and all nonterminals on which the lookahead
wenzelm@38712
   128
          of a production depends*)
wenzelm@38712
   129
        fun lookahead_dependency _ [] nts = (NONE, nts)
wenzelm@42217
   130
          | lookahead_dependency _ (Terminal tk :: _) nts = (SOME tk, nts)
wenzelm@42217
   131
          | lookahead_dependency lambdas (Nonterminal (nt, _) :: symbs) nts =
wenzelm@38712
   132
              if member (op =) lambdas nt then
wenzelm@38712
   133
                lookahead_dependency lambdas symbs (nt :: nts)
wenzelm@38712
   134
              else (NONE, nt :: nts);
clasohm@1147
   135
wenzelm@38712
   136
        (*get all known starting tokens for a nonterminal*)
wenzelm@38712
   137
        fun starts_for_nt nt = snd (fst (Array.sub (prods, nt)));
wenzelm@38712
   138
wenzelm@38712
   139
        (*update prods, lookaheads, and lambdas according to new lambda NTs*)
wenzelm@38712
   140
        val (added_starts', lambdas') =
wenzelm@38712
   141
          let
wenzelm@38712
   142
            (*propagate added lambda NT*)
wenzelm@42217
   143
            fun propagate_lambda [] added_starts lambdas = (added_starts, lambdas)
wenzelm@38712
   144
              | propagate_lambda (l :: ls) added_starts lambdas =
wenzelm@38712
   145
                  let
wenzelm@38712
   146
                    (*get lookahead for lambda NT*)
wenzelm@38712
   147
                    val ((dependent, l_starts), _) = Array.sub (prods, l);
wenzelm@38712
   148
wenzelm@38712
   149
                    (*check productions whose lookahead may depend on lambda NT*)
wenzelm@38712
   150
                    fun examine_prods [] add_lambda nt_dependencies added_tks nt_prods =
wenzelm@38712
   151
                          (add_lambda, nt_dependencies, added_tks, nt_prods)
wenzelm@38712
   152
                      | examine_prods ((p as (rhs, _, _)) :: ps) add_lambda
wenzelm@38712
   153
                            nt_dependencies added_tks nt_prods =
wenzelm@38712
   154
                          let val (tk, nts) = lookahead_dependency lambdas rhs [] in
wenzelm@38712
   155
                            if member (op =) nts l then       (*update production's lookahead*)
wenzelm@38712
   156
                              let
wenzelm@38712
   157
                                val new_lambda = is_none tk andalso subset (op =) (nts, lambdas);
clasohm@1175
   158
wenzelm@42218
   159
                                val new_tks =
wenzelm@42218
   160
                                  (if is_some tk then [the tk] else [])
wenzelm@42218
   161
                                  |> fold (union_token o starts_for_nt) nts
wenzelm@42218
   162
                                  |> subtract (op =) l_starts;
wenzelm@38712
   163
wenzelm@42218
   164
                                val added_tks' = union_token added_tks new_tks;
wenzelm@38712
   165
wenzelm@38712
   166
                                val nt_dependencies' = union (op =) nts nt_dependencies;
clasohm@1147
   167
wenzelm@38712
   168
                                (*associate production with new starting tokens*)
wenzelm@38712
   169
                                fun copy ([]: Lexicon.token option list) nt_prods = nt_prods
wenzelm@38712
   170
                                  | copy (tk :: tks) nt_prods =
wenzelm@38712
   171
                                      let
wenzelm@38712
   172
                                        val old_prods = these (AList.lookup (op =) nt_prods tk);
wenzelm@38712
   173
                                        val prods' = p :: old_prods;
wenzelm@38712
   174
                                      in
wenzelm@38712
   175
                                        nt_prods
wenzelm@38712
   176
                                        |> AList.update (op =) (tk, prods')
wenzelm@38712
   177
                                        |> copy tks
wenzelm@38712
   178
                                      end;
clasohm@1147
   179
wenzelm@38712
   180
                                val nt_prods' =
wenzelm@38712
   181
                                  let val new_opt_tks = map SOME new_tks in
wenzelm@38712
   182
                                    copy
wenzelm@38712
   183
                                      ((if new_lambda then [NONE] else []) @ new_opt_tks) nt_prods
wenzelm@38712
   184
                                  end;
wenzelm@38712
   185
                              in
wenzelm@38712
   186
                                examine_prods ps (add_lambda orelse new_lambda)
wenzelm@38712
   187
                                  nt_dependencies' added_tks' nt_prods'
wenzelm@38712
   188
                              end
wenzelm@38712
   189
                            else (*skip production*)
wenzelm@38712
   190
                              examine_prods ps add_lambda nt_dependencies added_tks nt_prods
wenzelm@38712
   191
                          end;
clasohm@1175
   192
wenzelm@38712
   193
                    (*check each NT whose lookahead depends on new lambda NT*)
wenzelm@38712
   194
                    fun process_nts [] added_lambdas added_starts =
wenzelm@38712
   195
                          (added_lambdas, added_starts)
wenzelm@38712
   196
                      | process_nts (nt :: nts) added_lambdas added_starts =
wenzelm@38712
   197
                          let
wenzelm@38712
   198
                            val (lookahead as (old_nts, old_tks), nt_prods) = Array.sub (prods, nt);
clasohm@1147
   199
wenzelm@38712
   200
                            (*existing productions whose lookahead may depend on l*)
wenzelm@38712
   201
                            val tk_prods =
wenzelm@38712
   202
                              these
wenzelm@38712
   203
                                (AList.lookup (op =) nt_prods
wenzelm@47060
   204
                                  (SOME (hd l_starts handle List.Empty => unknown_start)));
wenzelm@38712
   205
wenzelm@38712
   206
                            (*add_lambda is true if an existing production of the nt
wenzelm@38712
   207
                              produces lambda due to the new lambda NT l*)
wenzelm@38712
   208
                            val (add_lambda, nt_dependencies, added_tks, nt_prods') =
wenzelm@38712
   209
                              examine_prods tk_prods false [] [] nt_prods;
wenzelm@38712
   210
wenzelm@38712
   211
                            val added_nts = subtract (op =) old_nts nt_dependencies;
clasohm@330
   212
wenzelm@38712
   213
                            val added_lambdas' =
wenzelm@38712
   214
                              if add_lambda then nt :: added_lambdas
wenzelm@38712
   215
                              else added_lambdas;
wenzelm@38712
   216
                            val _ =
wenzelm@38712
   217
                              Array.update
wenzelm@38712
   218
                                (prods, nt, ((added_nts @ old_nts, old_tks @ added_tks), nt_prods'));
wenzelm@38712
   219
                              (*N.B. that because the tks component
wenzelm@38712
   220
                                is used to access existing
wenzelm@38712
   221
                                productions we have to add new
wenzelm@38712
   222
                                tokens at the _end_ of the list*)
wenzelm@38712
   223
                          in
wenzelm@38712
   224
                            if null added_tks then
wenzelm@38712
   225
                              process_nts nts added_lambdas' added_starts
wenzelm@38712
   226
                            else
wenzelm@38712
   227
                              process_nts nts added_lambdas' ((nt, added_tks) :: added_starts)
wenzelm@38712
   228
                          end;
clasohm@1147
   229
wenzelm@38712
   230
                    val (added_lambdas, added_starts') = process_nts dependent [] added_starts;
wenzelm@38712
   231
                    val added_lambdas' = subtract (op =) lambdas added_lambdas;
wenzelm@38712
   232
                  in
wenzelm@38712
   233
                    propagate_lambda (ls @ added_lambdas') added_starts' (added_lambdas' @ lambdas)
wenzelm@38712
   234
                  end;
wenzelm@38712
   235
          in propagate_lambda (subtract (op =) lambdas lambdas') added_starts lambdas' end;
clasohm@330
   236
wenzelm@38712
   237
        (*insert production into grammar*)
wenzelm@38712
   238
        val (added_starts', prod_count') =
wenzelm@38712
   239
          if is_some chain_from
wenzelm@38712
   240
          then (added_starts', prod_count)  (*don't store chain production*)
wenzelm@38712
   241
          else
wenzelm@38712
   242
            let
wenzelm@38712
   243
              (*lookahead tokens of new production and on which
wenzelm@38712
   244
                NTs lookahead depends*)
wenzelm@38712
   245
              val (start_tk, start_nts) = lookahead_dependency lambdas' rhs [];
clasohm@1175
   246
wenzelm@38712
   247
              val start_tks =
wenzelm@42218
   248
                (if is_some start_tk then [the start_tk] else [])
wenzelm@42218
   249
                |> fold (union_token o starts_for_nt) start_nts;
clasohm@330
   250
wenzelm@38712
   251
              val opt_starts =
wenzelm@38712
   252
               (if new_lambda then [NONE]
wenzelm@38713
   253
                else if null start_tks then [SOME unknown_start]
wenzelm@38712
   254
                else []) @ map SOME start_tks;
wenzelm@38712
   255
wenzelm@38712
   256
              (*add lhs NT to list of dependent NTs in lookahead*)
wenzelm@38712
   257
              fun add_nts [] = ()
wenzelm@38712
   258
                | add_nts (nt :: nts) =
wenzelm@42217
   259
                    let val ((old_nts, old_tks), ps) = Array.sub (prods, nt) in
wenzelm@42217
   260
                      if member (op =) old_nts lhs then ()
wenzelm@42217
   261
                      else Array.update (prods, nt, ((lhs :: old_nts, old_tks), ps))
wenzelm@42217
   262
                    end;
wenzelm@38712
   263
wenzelm@38712
   264
              (*add new start tokens to chained NTs' lookahead list;
wenzelm@38712
   265
                also store new production for lhs NT*)
wenzelm@38712
   266
              fun add_tks [] added prod_count = (added, prod_count)
wenzelm@38712
   267
                | add_tks (nt :: nts) added prod_count =
wenzelm@38712
   268
                    let
wenzelm@38712
   269
                      val ((old_nts, old_tks), nt_prods) = Array.sub (prods, nt);
wenzelm@38712
   270
wenzelm@42218
   271
                      val new_tks = subtract_token old_tks start_tks;
wenzelm@38712
   272
wenzelm@38712
   273
                      (*store new production*)
wenzelm@38712
   274
                      fun store [] prods is_new =
wenzelm@38712
   275
                            (prods,
wenzelm@38712
   276
                              if is_some prod_count andalso is_new then
wenzelm@38712
   277
                                Option.map (fn x => x + 1) prod_count
wenzelm@38712
   278
                              else prod_count, is_new)
wenzelm@38712
   279
                        | store (tk :: tks) prods is_new =
wenzelm@38712
   280
                            let
wenzelm@38712
   281
                              val tk_prods = these (AList.lookup (op =) prods tk);
wenzelm@38712
   282
wenzelm@38712
   283
                              (*if prod_count = NONE then we can assume that
wenzelm@38712
   284
                                grammar does not contain new production already*)
wenzelm@38712
   285
                              val (tk_prods', is_new') =
wenzelm@38712
   286
                                if is_some prod_count then
wenzelm@38712
   287
                                  if member (op =) tk_prods new_prod then (tk_prods, false)
wenzelm@38712
   288
                                  else (new_prod :: tk_prods, true)
wenzelm@38712
   289
                                else (new_prod :: tk_prods, true);
clasohm@330
   290
wenzelm@42218
   291
                              val prods' =
wenzelm@42218
   292
                                if is_new' then
wenzelm@42218
   293
                                  AList.update (op =) (tk: Lexicon.token option, tk_prods') prods
wenzelm@42218
   294
                                else prods;
wenzelm@38712
   295
                            in store tks prods' (is_new orelse is_new') end;
wenzelm@38712
   296
wenzelm@38712
   297
                      val (nt_prods', prod_count', changed) =
wenzelm@38712
   298
                        if nt = lhs
wenzelm@38712
   299
                        then store opt_starts nt_prods false
wenzelm@38712
   300
                        else (nt_prods, prod_count, false);
wenzelm@38712
   301
                      val _ =
wenzelm@38712
   302
                        if not changed andalso null new_tks then ()
wenzelm@38712
   303
                        else Array.update (prods, nt, ((old_nts, old_tks @ new_tks), nt_prods'));
wenzelm@38712
   304
                    in
wenzelm@38712
   305
                      add_tks nts
wenzelm@38712
   306
                        (if null new_tks then added else (nt, new_tks) :: added) prod_count'
wenzelm@38712
   307
                    end;
wenzelm@38712
   308
              val _ = add_nts start_nts;
wenzelm@38712
   309
            in
wenzelm@38712
   310
              add_tks (connected_with chains' [lhs] [lhs]) [] prod_count
wenzelm@38712
   311
            end;
clasohm@1175
   312
wenzelm@38712
   313
        (*associate productions with new lookaheads*)
wenzelm@38712
   314
        val _ =
wenzelm@38712
   315
          let
wenzelm@38712
   316
            (*propagate added start tokens*)
wenzelm@38712
   317
            fun add_starts [] = ()
wenzelm@38712
   318
              | add_starts ((changed_nt, new_tks) :: starts) =
wenzelm@38712
   319
                  let
wenzelm@38712
   320
                    (*token under which old productions which
wenzelm@38712
   321
                      depend on changed_nt could be stored*)
wenzelm@38712
   322
                    val key =
wenzelm@38712
   323
                      (case find_first (not o member (op =) new_tks) (starts_for_nt changed_nt) of
wenzelm@38713
   324
                        NONE => SOME unknown_start
wenzelm@38712
   325
                      | t => t);
wenzelm@18
   326
wenzelm@38712
   327
                    (*copy productions whose lookahead depends on changed_nt;
wenzelm@38713
   328
                      if key = SOME unknown_start then tk_prods is used to hold
wenzelm@38712
   329
                      the productions not copied*)
wenzelm@38712
   330
                    fun update_prods [] result = result
wenzelm@38712
   331
                      | update_prods ((p as (rhs, _: string, _: nt_tag)) :: ps)
wenzelm@38712
   332
                            (tk_prods, nt_prods) =
clasohm@1147
   333
                          let
wenzelm@38712
   334
                            (*lookahead dependency for production*)
wenzelm@38712
   335
                            val (tk, depends) = lookahead_dependency lambdas' rhs [];
wenzelm@38712
   336
wenzelm@38712
   337
                            (*test if this production has to be copied*)
wenzelm@38712
   338
                            val update = member (op =) depends changed_nt;
clasohm@330
   339
wenzelm@38712
   340
                            (*test if production could already be associated with
wenzelm@38712
   341
                              a member of new_tks*)
wenzelm@38712
   342
                            val lambda =
wenzelm@38712
   343
                              length depends > 1 orelse
wenzelm@38712
   344
                              not (null depends) andalso is_some tk
wenzelm@38712
   345
                              andalso member (op =) new_tks (the tk);
wenzelm@38712
   346
wenzelm@38712
   347
                            (*associate production with new starting tokens*)
wenzelm@38712
   348
                            fun copy ([]: Lexicon.token list) nt_prods = nt_prods
wenzelm@38712
   349
                              | copy (tk :: tks) nt_prods =
wenzelm@38712
   350
                                 let
wenzelm@38712
   351
                                   val tk_prods = these (AList.lookup (op =) nt_prods (SOME tk));
clasohm@330
   352
wenzelm@38712
   353
                                   val tk_prods' =
wenzelm@38712
   354
                                     if not lambda then p :: tk_prods
wenzelm@38712
   355
                                     else insert (op =) p tk_prods;
wenzelm@38712
   356
                                     (*if production depends on lambda NT we
wenzelm@38712
   357
                                       have to look for duplicates*)
wenzelm@38712
   358
                                 in
wenzelm@38712
   359
                                   nt_prods
wenzelm@38712
   360
                                   |> AList.update (op =) (SOME tk, tk_prods')
wenzelm@38712
   361
                                   |> copy tks
wenzelm@38712
   362
                                 end;
wenzelm@38712
   363
                            val result =
wenzelm@38712
   364
                              if update then (tk_prods, copy new_tks nt_prods)
wenzelm@38713
   365
                              else if key = SOME unknown_start then (p :: tk_prods, nt_prods)
wenzelm@38712
   366
                              else (tk_prods, nt_prods);
wenzelm@38712
   367
                          in update_prods ps result end;
clasohm@377
   368
wenzelm@38712
   369
                    (*copy existing productions for new starting tokens*)
wenzelm@38712
   370
                    fun process_nts [] added = added
wenzelm@38712
   371
                      | process_nts (nt :: nts) added =
wenzelm@38712
   372
                          let
wenzelm@38712
   373
                            val (lookahead as (old_nts, old_tks), nt_prods) = Array.sub (prods, nt);
clasohm@330
   374
wenzelm@38712
   375
                            val tk_prods = these (AList.lookup (op =) nt_prods key);
wenzelm@38712
   376
wenzelm@38712
   377
                            (*associate productions with new lookahead tokens*)
wenzelm@38712
   378
                            val (tk_prods', nt_prods') = update_prods tk_prods ([], nt_prods);
wenzelm@38712
   379
wenzelm@42217
   380
                            val nt_prods'' =
wenzelm@42217
   381
                              if key = SOME unknown_start then
wenzelm@42217
   382
                                AList.update (op =) (key, tk_prods') nt_prods'
wenzelm@42217
   383
                              else nt_prods';
wenzelm@18
   384
wenzelm@42218
   385
                            val added_tks = subtract_token old_tks new_tks;
wenzelm@38712
   386
                          in
wenzelm@38712
   387
                            if null added_tks then
wenzelm@42217
   388
                              (Array.update (prods, nt, (lookahead, nt_prods''));
wenzelm@38712
   389
                                process_nts nts added)
wenzelm@38712
   390
                            else
wenzelm@42217
   391
                              (Array.update (prods, nt, ((old_nts, added_tks @ old_tks), nt_prods''));
wenzelm@38712
   392
                                process_nts nts ((nt, added_tks) :: added))
wenzelm@38712
   393
                          end;
wenzelm@18
   394
wenzelm@38712
   395
                    val ((dependent, _), _) = Array.sub (prods, changed_nt);
wenzelm@38712
   396
                  in add_starts (starts @ process_nts dependent []) end;
wenzelm@38712
   397
          in add_starts added_starts' end;
wenzelm@38712
   398
      in add_prods prods chains' lambdas' prod_count ps end;
wenzelm@237
   399
wenzelm@18
   400
wenzelm@237
   401
(* pretty_gram *)
wenzelm@18
   402
clasohm@1147
   403
fun pretty_gram (Gram {tags, prods, chains, ...}) =
wenzelm@237
   404
  let
wenzelm@237
   405
    fun pretty_name name = [Pretty.str (name ^ " =")];
wenzelm@18
   406
wenzelm@26069
   407
    val nt_name = the o Inttab.lookup (Inttab.make (map swap (Symtab.dest tags)));
clasohm@1147
   408
wenzelm@37683
   409
    fun pretty_symb (Terminal (Lexicon.Token (Lexicon.Literal, s, _))) = Pretty.quote (Pretty.str s)
wenzelm@37683
   410
      | pretty_symb (Terminal tok) = Pretty.str (Lexicon.str_of_token tok)
wenzelm@28843
   411
      | pretty_symb (Nonterminal (tag, p)) =
wenzelm@28843
   412
          Pretty.str (nt_name tag ^ "[" ^ signed_string_of_int p ^ "]");
wenzelm@18
   413
wenzelm@237
   414
    fun pretty_const "" = []
wenzelm@28843
   415
      | pretty_const c = [Pretty.str ("=> " ^ quote c)];
wenzelm@237
   416
wenzelm@28843
   417
    fun pretty_pri p = [Pretty.str ("(" ^ signed_string_of_int p ^ ")")];
wenzelm@237
   418
clasohm@1147
   419
    fun pretty_prod name (symbs, const, pri) =
wenzelm@237
   420
      Pretty.block (Pretty.breaks (pretty_name name @
wenzelm@237
   421
        map pretty_symb symbs @ pretty_const const @ pretty_pri pri));
clasohm@1147
   422
clasohm@1147
   423
    fun pretty_nt (name, tag) =
clasohm@1147
   424
      let
wenzelm@30189
   425
        fun prod_of_chain from = ([Nonterminal (from, ~1)], "", ~1);
clasohm@1147
   426
clasohm@1147
   427
        val nt_prods =
wenzelm@42218
   428
          fold (union (op =) o snd) (snd (Vector.sub (prods, tag))) [] @
wenzelm@42218
   429
            map prod_of_chain (these (AList.lookup (op =) chains tag));
clasohm@1147
   430
      in map (pretty_prod name) nt_prods end;
wenzelm@15752
   431
wenzelm@25986
   432
  in maps pretty_nt (sort_wrt fst (Symtab.dest tags)) end;
clasohm@1147
   433
clasohm@1147
   434
wenzelm@42217
   435
clasohm@1438
   436
(** Operations on gramars **)
clasohm@1147
   437
wenzelm@38712
   438
val empty_gram =
wenzelm@38712
   439
  Gram
wenzelm@38712
   440
   {nt_count = 0,
wenzelm@38712
   441
    prod_count = 0,
wenzelm@38712
   442
    tags = Symtab.empty, chains = [],
wenzelm@38712
   443
    lambdas = [],
wenzelm@38712
   444
    prods = Vector.fromList [(([], []), [])]};
clasohm@1147
   445
clasohm@1438
   446
clasohm@1438
   447
(*Invert list of chain productions*)
clasohm@1147
   448
fun inverse_chains [] result = result
wenzelm@23909
   449
  | inverse_chains ((root, branches: nt_tag list) :: cs) result =
wenzelm@38712
   450
      let
wenzelm@38712
   451
        fun add ([]: nt_tag list) result = result
clasohm@1147
   452
          | add (id :: ids) result =
wenzelm@38712
   453
              let val old = these (AList.lookup (op =) result id);
wenzelm@38712
   454
              in add ids (AList.update (op =) (id, root :: old) result) end;
wenzelm@38712
   455
      in inverse_chains cs (add branches result) end;
clasohm@1147
   456
clasohm@1438
   457
clasohm@1438
   458
(*Add productions to a grammar*)
wenzelm@37684
   459
fun extend_gram [] gram = gram
wenzelm@37684
   460
  | extend_gram xprods (Gram {nt_count, prod_count, tags, chains, lambdas, prods}) =
wenzelm@38712
   461
      let
wenzelm@38712
   462
        (*Get tag for existing nonterminal or create a new one*)
wenzelm@38712
   463
        fun get_tag nt_count tags nt =
wenzelm@38712
   464
          (case Symtab.lookup tags nt of
wenzelm@38712
   465
            SOME tag => (nt_count, tags, tag)
wenzelm@38712
   466
          | NONE => (nt_count + 1, Symtab.update_new (nt, nt_count) tags, nt_count));
clasohm@1438
   467
wenzelm@38712
   468
        (*Convert symbols to the form used by the parser;
wenzelm@38712
   469
          delimiters and predefined terms are stored as terminals,
wenzelm@38712
   470
          nonterminals are converted to integer tags*)
wenzelm@38712
   471
        fun symb_of [] nt_count tags result = (nt_count, tags, rev result)
wenzelm@42288
   472
          | symb_of (Syntax_Ext.Delim s :: ss) nt_count tags result =
wenzelm@38712
   473
              symb_of ss nt_count tags
wenzelm@38712
   474
                (Terminal (Lexicon.Token (Lexicon.Literal, s, Position.no_range)) :: result)
wenzelm@42288
   475
          | symb_of (Syntax_Ext.Argument (s, p) :: ss) nt_count tags result =
wenzelm@38712
   476
              let
wenzelm@38712
   477
                val (nt_count', tags', new_symb) =
wenzelm@38712
   478
                  (case Lexicon.predef_term s of
wenzelm@38712
   479
                    NONE =>
wenzelm@38712
   480
                      let val (nt_count', tags', s_tag) = get_tag nt_count tags s;
wenzelm@38712
   481
                      in (nt_count', tags', Nonterminal (s_tag, p)) end
wenzelm@38712
   482
                  | SOME tk => (nt_count, tags, Terminal tk));
wenzelm@38712
   483
              in symb_of ss nt_count' tags' (new_symb :: result) end
wenzelm@38712
   484
          | symb_of (_ :: ss) nt_count tags result = symb_of ss nt_count tags result;
clasohm@1147
   485
wenzelm@38712
   486
        (*Convert list of productions by invoking symb_of for each of them*)
wenzelm@38712
   487
        fun prod_of [] nt_count prod_count tags result =
wenzelm@38712
   488
              (nt_count, prod_count, tags, result)
wenzelm@42288
   489
          | prod_of (Syntax_Ext.XProd (lhs, xsymbs, const, pri) :: ps)
wenzelm@38712
   490
                nt_count prod_count tags result =
wenzelm@38712
   491
              let
wenzelm@38712
   492
                val (nt_count', tags', lhs_tag) = get_tag nt_count tags lhs;
wenzelm@38712
   493
                val (nt_count'', tags'', prods) = symb_of xsymbs nt_count' tags' [];
wenzelm@38712
   494
              in
wenzelm@38712
   495
                prod_of ps nt_count'' (prod_count + 1) tags''
wenzelm@38712
   496
                  ((lhs_tag, (prods, const, pri)) :: result)
wenzelm@38712
   497
              end;
clasohm@1147
   498
wenzelm@38712
   499
        val (nt_count', prod_count', tags', xprods') =
wenzelm@38712
   500
          prod_of xprods nt_count prod_count tags [];
clasohm@1147
   501
wenzelm@38712
   502
        (*Copy array containing productions of old grammar;
wenzelm@38712
   503
          this has to be done to preserve the old grammar while being able
wenzelm@38712
   504
          to change the array's content*)
wenzelm@38712
   505
        val prods' =
wenzelm@38712
   506
          let
wenzelm@38712
   507
            fun get_prod i =
wenzelm@38712
   508
              if i < nt_count then Vector.sub (prods, i)
wenzelm@38712
   509
              else (([], []), []);
wenzelm@38712
   510
          in Array.tabulate (nt_count', get_prod) end;
wenzelm@38712
   511
wenzelm@38712
   512
        val fromto_chains = inverse_chains chains [];
clasohm@1147
   513
wenzelm@38712
   514
        (*Add new productions to old ones*)
wenzelm@38712
   515
        val (fromto_chains', lambdas', _) =
wenzelm@38712
   516
          add_prods prods' fromto_chains lambdas NONE xprods';
clasohm@1147
   517
wenzelm@38712
   518
        val chains' = inverse_chains fromto_chains' [];
wenzelm@38712
   519
      in
wenzelm@38712
   520
        Gram
wenzelm@38712
   521
         {nt_count = nt_count',
wenzelm@38712
   522
          prod_count = prod_count',
wenzelm@38712
   523
          tags = tags',
wenzelm@38712
   524
          chains = chains',
wenzelm@38712
   525
          lambdas = lambdas',
wenzelm@38712
   526
          prods = Array.vector prods'}
wenzelm@38712
   527
      end;
wenzelm@18
   528
wenzelm@45632
   529
fun make_gram xprods = extend_gram xprods empty_gram;
clasohm@1147
   530
wenzelm@18
   531
wenzelm@42217
   532
wenzelm@42374
   533
(** parser **)
wenzelm@42374
   534
wenzelm@42374
   535
(* parsetree *)
wenzelm@18
   536
wenzelm@237
   537
datatype parsetree =
wenzelm@237
   538
  Node of string * parsetree list |
wenzelm@37683
   539
  Tip of Lexicon.token;
wenzelm@237
   540
wenzelm@42374
   541
exception PARSETREE of parsetree;
wenzelm@42374
   542
wenzelm@42374
   543
fun pretty_parsetree parsetree =
wenzelm@42374
   544
  let
wenzelm@42374
   545
    fun pretty (Node (c, pts)) =
wenzelm@42374
   546
          [Pretty.enclose "(" ")" (Pretty.breaks (Pretty.quote (Pretty.str c) :: maps pretty pts))]
wenzelm@42374
   547
      | pretty (Tip tok) =
wenzelm@42374
   548
          if Lexicon.valued_token tok then [Pretty.str (Lexicon.str_of_token tok)] else [];
wenzelm@42374
   549
  in (case pretty parsetree of [prt] => prt | _ => raise PARSETREE parsetree) end;
wenzelm@42374
   550
wenzelm@42374
   551
wenzelm@42374
   552
(* parser state *)
wenzelm@42205
   553
wenzelm@18
   554
type state =
wenzelm@38712
   555
  nt_tag * int *    (*identification and production precedence*)
wenzelm@38712
   556
  parsetree list *  (*already parsed nonterminals on rhs*)
wenzelm@38712
   557
  symb list *       (*rest of rhs*)
wenzelm@38712
   558
  string *          (*name of production*)
wenzelm@38712
   559
  int;              (*index for previous state list*)
wenzelm@18
   560
wenzelm@18
   561
wenzelm@38713
   562
(*Get all rhss with precedence >= min_prec*)
wenzelm@42217
   563
fun get_RHS min_prec = filter (fn (_, _, prec: int) => prec >= min_prec);
wenzelm@237
   564
wenzelm@38713
   565
(*Get all rhss with precedence >= min_prec and < max_prec*)
wenzelm@38713
   566
fun get_RHS' min_prec max_prec =
wenzelm@42217
   567
  filter (fn (_, _, prec: int) => prec >= min_prec andalso prec < max_prec);
wenzelm@18
   568
clasohm@330
   569
(*Make states using a list of rhss*)
wenzelm@38713
   570
fun mk_states i min_prec lhs_ID rhss =
wenzelm@38713
   571
  let fun mk_state (rhs, id, prod_prec) = (lhs_ID, prod_prec, [], rhs, id, i);
wenzelm@38713
   572
  in map mk_state rhss end;
clasohm@697
   573
wenzelm@15752
   574
(*Add parse tree to list and eliminate duplicates
clasohm@330
   575
  saving the maximum precedence*)
wenzelm@42217
   576
fun conc (t: parsetree list, prec: int) [] = (NONE, [(t, prec)])
clasohm@330
   577
  | conc (t, prec) ((t', prec') :: ts) =
clasohm@330
   578
      if t = t' then
wenzelm@38712
   579
        (SOME prec',
wenzelm@38712
   580
          if prec' >= prec then (t', prec') :: ts
wenzelm@38712
   581
          else (t, prec) :: ts)
clasohm@330
   582
      else
clasohm@330
   583
        let val (n, ts') = conc (t, prec) ts
clasohm@330
   584
        in (n, (t', prec') :: ts') end;
wenzelm@18
   585
clasohm@330
   586
(*Update entry in used*)
wenzelm@42226
   587
fun update_trees (A, t) used =
wenzelm@42226
   588
  let
wenzelm@42226
   589
    val (i, ts) = the (Inttab.lookup used A);
wenzelm@42226
   590
    val (n, ts') = conc t ts;
wenzelm@42226
   591
  in (n, Inttab.update (A, (i, ts')) used) end;
wenzelm@18
   592
clasohm@330
   593
(*Replace entry in used*)
wenzelm@42226
   594
fun update_prec (A, prec) =
wenzelm@42226
   595
  Inttab.map_entry A (fn (_, ts) => (prec, ts));
wenzelm@18
   596
wenzelm@42220
   597
fun getS A max_prec NONE Si =
wenzelm@42220
   598
      filter
wenzelm@42220
   599
        (fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec
wenzelm@42220
   600
          | _ => false) Si
wenzelm@42220
   601
  | getS A max_prec (SOME min_prec) Si =
wenzelm@42220
   602
      filter
wenzelm@42220
   603
        (fn (_, _, _, Nonterminal (B, prec) :: _, _, _) =>
wenzelm@42220
   604
            A = B andalso prec > min_prec andalso prec <= max_prec
wenzelm@42220
   605
          | _ => false) Si;
wenzelm@18
   606
wenzelm@38713
   607
fun get_states Estate i ii A max_prec =
wenzelm@33317
   608
  filter
wenzelm@38713
   609
    (fn (_, _, _, Nonterminal (B, prec) :: _, _, _) => A = B andalso prec <= max_prec
wenzelm@237
   610
      | _ => false)
wenzelm@237
   611
    (Array.sub (Estate, ii));
wenzelm@18
   612
wenzelm@18
   613
wenzelm@42219
   614
fun movedot_term c (A, j, ts, Terminal a :: sa, id, i) =
wenzelm@42282
   615
  if Lexicon.valued_token c orelse id <> ""
wenzelm@42282
   616
  then (A, j, Tip c :: ts, sa, id, i)
wenzelm@237
   617
  else (A, j, ts, sa, id, i);
wenzelm@18
   618
wenzelm@42219
   619
fun movedot_nonterm tt (A, j, ts, Nonterminal _ :: sa, id, i) =
wenzelm@42282
   620
  (A, j, tt @ ts, sa, id, i);
wenzelm@18
   621
wenzelm@42219
   622
fun movedot_lambda [] _ = []
wenzelm@42221
   623
  | movedot_lambda ((t, ki) :: ts) (state as (B, j, tss, Nonterminal (A, k) :: sa, id, i)) =
wenzelm@42282
   624
      if k <= ki then (B, j, t @ tss, sa, id, i) :: movedot_lambda ts state
wenzelm@42221
   625
      else movedot_lambda ts state;
wenzelm@18
   626
wenzelm@18
   627
wenzelm@41378
   628
(*trigger value for warnings*)
wenzelm@41378
   629
val branching_level = Config.int (Config.declare "syntax_branching_level" (fn _ => Config.Int 600));
wenzelm@18
   630
clasohm@1147
   631
(*get all productions of a NT and NTs chained to it which can
clasohm@1147
   632
  be started by specified token*)
clasohm@1147
   633
fun prods_for prods chains include_none tk nts =
wenzelm@37683
   634
  let
wenzelm@38712
   635
    fun token_assoc (list, key) =
wenzelm@38712
   636
      let
wenzelm@38712
   637
        fun assoc [] result = result
wenzelm@38712
   638
          | assoc ((keyi, pi) :: pairs) result =
wenzelm@38712
   639
              if is_some keyi andalso Lexicon.matching_tokens (the keyi, key)
wenzelm@38712
   640
                 orelse include_none andalso is_none keyi then
wenzelm@38712
   641
                assoc pairs (pi @ result)
wenzelm@38712
   642
              else assoc pairs result;
wenzelm@38712
   643
      in assoc list [] end;
clasohm@1147
   644
wenzelm@38712
   645
    fun get_prods [] result = result
wenzelm@38712
   646
      | get_prods (nt :: nts) result =
wenzelm@38711
   647
          let val nt_prods = snd (Vector.sub (prods, nt));
wenzelm@42217
   648
          in get_prods nts (token_assoc (nt_prods, tk) @ result) end;
wenzelm@37683
   649
  in get_prods (connected_with chains nts nts) [] end;
clasohm@1147
   650
clasohm@1147
   651
wenzelm@45641
   652
fun PROCESSS prods chains Estate i c states =
wenzelm@37683
   653
  let
wenzelm@37683
   654
    fun all_prods_for nt = prods_for prods chains true c [nt];
clasohm@330
   655
wenzelm@37683
   656
    fun processS used [] (Si, Sii) = (Si, Sii)
wenzelm@37683
   657
      | processS used (S :: States) (Si, Sii) =
wenzelm@37683
   658
          (case S of
wenzelm@38713
   659
            (_, _, _, Nonterminal (nt, min_prec) :: _, _, _) =>
wenzelm@38712
   660
              let (*predictor operation*)
wenzelm@37683
   661
                val (used', new_states) =
wenzelm@42226
   662
                  (case Inttab.lookup used nt of
wenzelm@38713
   663
                    SOME (used_prec, l) => (*nonterminal has been processed*)
wenzelm@38713
   664
                      if used_prec <= min_prec then
wenzelm@38712
   665
                        (*wanted precedence has been processed*)
wenzelm@42219
   666
                        (used, movedot_lambda l S)
wenzelm@38712
   667
                      else (*wanted precedence hasn't been parsed yet*)
wenzelm@37683
   668
                        let
wenzelm@37683
   669
                          val tk_prods = all_prods_for nt;
wenzelm@42217
   670
                          val States' =
wenzelm@42217
   671
                            mk_states i min_prec nt (get_RHS' min_prec used_prec tk_prods);
wenzelm@42219
   672
                        in (update_prec (nt, min_prec) used, movedot_lambda l S @ States') end
wenzelm@38712
   673
                  | NONE => (*nonterminal is parsed for the first time*)
wenzelm@38712
   674
                      let
wenzelm@38712
   675
                        val tk_prods = all_prods_for nt;
wenzelm@38713
   676
                        val States' = mk_states i min_prec nt (get_RHS min_prec tk_prods);
wenzelm@42226
   677
                      in (Inttab.update (nt, (min_prec, [])) used, States') end);
wenzelm@237
   678
              in
wenzelm@37683
   679
                processS used' (new_states @ States) (S :: Si, Sii)
wenzelm@15752
   680
              end
wenzelm@38712
   681
          | (_, _, _, Terminal a :: _, _, _) => (*scanner operation*)
wenzelm@37683
   682
              processS used States
wenzelm@37683
   683
                (S :: Si,
wenzelm@42219
   684
                  if Lexicon.matching_tokens (a, c) then movedot_term c S :: Sii else Sii)
wenzelm@38712
   685
          | (A, prec, ts, [], id, j) => (*completer operation*)
wenzelm@42222
   686
              let val tt = if id = "" then ts else [Node (id, rev ts)] in
wenzelm@38712
   687
                if j = i then (*lambda production?*)
wenzelm@37683
   688
                  let
wenzelm@42226
   689
                    val (prec', used') = update_trees (A, (tt, prec)) used;
wenzelm@42220
   690
                    val Slist = getS A prec prec' Si;
wenzelm@42220
   691
                    val States' = map (movedot_nonterm tt) Slist;
wenzelm@42220
   692
                  in processS used' (States' @ States) (S :: Si, Sii) end
wenzelm@37683
   693
                else
wenzelm@38713
   694
                  let val Slist = get_states Estate i j A prec
wenzelm@38712
   695
                  in processS used (map (movedot_nonterm tt) Slist @ States) (S :: Si, Sii) end
wenzelm@37683
   696
              end)
wenzelm@42226
   697
  in processS Inttab.empty states ([], []) end;
wenzelm@18
   698
wenzelm@18
   699
wenzelm@45641
   700
fun produce prods tags chains stateset i indata prev_token =
wenzelm@237
   701
  (case Array.sub (stateset, i) of
wenzelm@25986
   702
    [] =>
wenzelm@25986
   703
      let
wenzelm@37683
   704
        val toks = if Lexicon.is_eof prev_token then indata else prev_token :: indata;
wenzelm@48992
   705
        val pos = Position.here (Lexicon.pos_of_token prev_token);
wenzelm@27801
   706
      in
wenzelm@27801
   707
        if null toks then error ("Inner syntax error: unexpected end of input" ^ pos)
wenzelm@27801
   708
        else error (Pretty.string_of (Pretty.block
wenzelm@27807
   709
          (Pretty.str ("Inner syntax error" ^ pos ^ " at \"") ::
wenzelm@37683
   710
            Pretty.breaks (map (Pretty.str o Lexicon.str_of_token) (#1 (split_last toks))) @
wenzelm@27807
   711
            [Pretty.str "\""])))
wenzelm@27801
   712
      end
wenzelm@237
   713
  | s =>
wenzelm@38712
   714
      (case indata of
wenzelm@42218
   715
        [] => s
wenzelm@42217
   716
      | c :: cs =>
wenzelm@42217
   717
          let
wenzelm@45641
   718
            val (si, sii) = PROCESSS prods chains stateset i c s;
wenzelm@42217
   719
            val _ = Array.update (stateset, i, si);
wenzelm@42217
   720
            val _ = Array.update (stateset, i + 1, sii);
wenzelm@45641
   721
          in produce prods tags chains stateset (i + 1) cs c end));
wenzelm@18
   722
wenzelm@18
   723
wenzelm@42217
   724
fun get_trees states = map_filter (fn (_, _, [pt], _, _, _) => SOME pt | _ => NONE) states;
wenzelm@18
   725
wenzelm@45641
   726
fun earley prods tags chains startsymbol indata =
wenzelm@237
   727
  let
wenzelm@37683
   728
    val start_tag =
wenzelm@37683
   729
      (case Symtab.lookup tags startsymbol of
wenzelm@37683
   730
        SOME tag => tag
wenzelm@40959
   731
      | NONE => error ("Inner syntax: bad grammar root symbol " ^ quote startsymbol));
wenzelm@37683
   732
    val S0 = [(~1, 0, [], [Nonterminal (start_tag, 0), Terminal Lexicon.eof], "", 0)];
clasohm@330
   733
    val s = length indata + 1;
wenzelm@237
   734
    val Estate = Array.array (s, []);
wenzelm@38712
   735
    val _ = Array.update (Estate, 0, S0);
wenzelm@237
   736
  in
wenzelm@45641
   737
    get_trees (produce prods tags chains Estate 0 indata Lexicon.eof)
wenzelm@237
   738
  end;
wenzelm@18
   739
wenzelm@18
   740
wenzelm@45641
   741
fun parse (Gram {tags, prods, chains, ...}) start toks =
wenzelm@27801
   742
  let
wenzelm@27801
   743
    val end_pos =
wenzelm@27801
   744
      (case try List.last toks of
wenzelm@27801
   745
        NONE => Position.none
wenzelm@37683
   746
      | SOME (Lexicon.Token (_, _, (_, end_pos))) => end_pos);
wenzelm@27801
   747
    val r =
wenzelm@45641
   748
      (case earley prods tags chains start (toks @ [Lexicon.mk_eof end_pos]) of
wenzelm@37852
   749
        [] => raise Fail "Inner syntax: no parse trees"
wenzelm@27801
   750
      | pts => pts);
wenzelm@27801
   751
  in r end;
haftmann@26678
   752
haftmann@26678
   753
haftmann@26678
   754
fun guess_infix_lr (Gram gram) c = (*based on educated guess*)
haftmann@26678
   755
  let
wenzelm@38711
   756
    fun freeze a = map_range (curry Vector.sub a) (Vector.length a);
wenzelm@27801
   757
    val prods = maps snd (maps snd (freeze (#prods gram)));
wenzelm@37683
   758
    fun guess (SOME ([Nonterminal (_, k),
wenzelm@37683
   759
            Terminal (Lexicon.Token (Lexicon.Literal, s, _)), Nonterminal (_, l)], _, j)) =
haftmann@26678
   760
          if k = j andalso l = j + 1 then SOME (s, true, false, j)
haftmann@26678
   761
          else if k = j + 1 then if l = j then SOME (s, false, true, j)
haftmann@26678
   762
            else if l = j + 1 then SOME (s, false, false, j)
haftmann@26678
   763
            else NONE
haftmann@26678
   764
          else NONE
haftmann@26678
   765
      | guess _ = NONE;
haftmann@26678
   766
  in guess (find_first (fn (_, s, _) => s = c) prods) end;
wenzelm@18
   767
wenzelm@18
   768
end;