src/Pure/Syntax/parser.ML
author wenzelm
Wed, 19 Jan 1994 14:21:26 +0100
changeset 237 a7d3e712767a
parent 46 f0f4978af183
child 258 e540b7d4ecb1
permissions -rw-r--r--
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables now much leaner (eliminated gramgraph, all data except tables of old parser are shared); simplified the internal interfaces for syntax extension;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Syntax/parser.ML
46
f0f4978af183 *** empty log message ***
wenzelm
parents: 18
diff changeset
     2
    ID:         $Id$
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     3
    Author:     Sonia Mahjoub and Markus Wenzel, TU Muenchen
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     4
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     5
Isabelle's main parser (used for terms and typs).
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     6
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     7
TODO:
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
     8
  fix ambiguous grammar problem
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     9
  ~1 --> chain_pri
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    10
  improve syntax error
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    11
  extend_gram: remove 'roots' arg
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    12
*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    13
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    14
signature PARSER =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    15
sig
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    16
  structure Lexicon: LEXICON
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    17
  structure SynExt: SYN_EXT
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    18
  local open Lexicon SynExt SynExt.Ast in
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    19
    type gram
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    20
    val empty_gram: gram
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    21
    val extend_gram: gram -> string list -> xprod list -> gram
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    22
    val merge_grams: gram -> gram -> gram
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    23
    val pretty_gram: gram -> Pretty.T list
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    24
    datatype parsetree =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    25
      Node of string * parsetree list |
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    26
      Tip of token
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    27
    val parse: gram -> string -> token list -> parsetree
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    28
  end
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    29
end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    30
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    31
functor ParserFun(structure Symtab: SYMTAB and Lexicon: LEXICON
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    32
  and SynExt: SYN_EXT)(*: PARSER *) =  (* FIXME *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    33
struct
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    34
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    35
structure Pretty = SynExt.Ast.Pretty;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    36
structure Lexicon = Lexicon;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    37
structure SynExt = SynExt;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    38
open Lexicon SynExt;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    39
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    40
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    41
(** datatype gram **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    42
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    43
datatype symb =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    44
  Terminal of token |
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    45
  Nonterminal of string * int;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    46
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    47
datatype gram =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    48
  Gram of (string * (symb list * string * int)) list
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    49
    * (symb list * string * int) list Symtab.table;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    50
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    51
fun mk_gram prods = Gram (prods, Symtab.make_multi prods);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    52
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    53
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    54
(* empty, extend, merge grams *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    55
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    56
val empty_gram = mk_gram [];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    57
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    58
fun extend_gram (gram1 as Gram (prods1, _)) _ xprods2 =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    59
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    60
    fun symb_of (Delim s) = Some (Terminal (Token s))
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    61
      | symb_of (Argument (s, p)) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    62
          (case predef_term s of
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    63
            None => Some (Nonterminal (s, p))
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    64
          | Some tk => Some (Terminal tk))
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    65
      | symb_of _ = None;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    66
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    67
    fun prod_of (XProd (lhs, xsymbs, const, pri)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    68
      (lhs, (mapfilter symb_of xsymbs, const, pri));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    69
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    70
    val prods2 = distinct (map prod_of xprods2);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    71
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    72
    if prods2 subset prods1 then gram1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    73
    else mk_gram (extend_list prods1 prods2)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    74
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    75
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    76
fun merge_grams (gram1 as Gram (prods1, _)) (gram2 as Gram (prods2, _)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    77
  if prods2 subset prods1 then gram1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    78
  else if prods1 subset prods2 then gram2
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    79
  else mk_gram (merge_lists prods1 prods2);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    80
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    81
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    82
(* pretty_gram *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    83
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    84
fun pretty_gram (Gram (prods, _)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    85
  let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    86
    fun pretty_name name = [Pretty.str (name ^ " =")];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    87
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    88
    fun pretty_symb (Terminal (Token s)) = Pretty.str (quote s)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    89
      | pretty_symb (Terminal tok) = Pretty.str (str_of_token tok)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    90
      | pretty_symb (Nonterminal (s, p)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    91
          Pretty.str (s ^ "[" ^ string_of_int p ^ "]");
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    92
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    93
    fun pretty_const "" = []
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    94
      | pretty_const c = [Pretty.str ("=> " ^ quote c)];
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    95
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    96
    fun pretty_pri p = [Pretty.str ("(" ^ string_of_int p ^ ")")];
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    97
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    98
    fun pretty_prod (name, (symbs, const, pri)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    99
      Pretty.block (Pretty.breaks (pretty_name name @
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   100
        map pretty_symb symbs @ pretty_const const @ pretty_pri pri));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   101
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   102
    map pretty_prod prods
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   103
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   104
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   105
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   106
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   107
(** parse **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   108
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   109
datatype parsetree =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   110
  Node of string * parsetree list |
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   111
  Tip of token;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   112
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   113
type state =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   114
  string * int
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   115
    * parsetree list    (*before point*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   116
    * symb list         (*after point*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   117
    * string * int;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   118
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   119
type earleystate = state list Array.array;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   120
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   121
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   122
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   123
fun get_prods tab lhs pred =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   124
  filter pred (Symtab.lookup_multi (tab, lhs));
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   125
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   126
fun getProductions tab A i =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   127
  get_prods tab A (fn (_, _, j) => j >= i orelse j = ~1);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   128
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   129
fun getProductions' tab A i k =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   130
  get_prods tab A (fn (_, _, j) => (j >= i andalso j < k) orelse j = ~1);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   131
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   132
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   133
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   134
fun mkState i jj A ([Nonterminal (B, ~1)], id, ~1) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   135
      (A, max_pri, [], [Nonterminal (B, jj)], id, i)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   136
  | mkState i jj A (ss, id, j) = (A, j, [], ss, id, i);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   137
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   138
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   139
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   140
fun conc (t, k:int) [] = (None, [(t, k)])
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   141
  | conc (t, k) ((t', k') :: ts) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   142
      if t = t' then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   143
        (Some k', if k' >= k then (t', k') :: ts else (t, k) :: ts)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   144
      else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   145
        let val (n, ts') = conc (t, k) ts
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   146
        in (n, (t', k') :: ts') end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   147
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   148
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   149
fun update_tree ((B, (i, ts)) :: used) (A, t) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   150
  if A = B then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   151
    let val (n, ts') = conc t ts
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   152
    in ((A, (i, ts')) :: used, n) end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   153
  else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   154
    let val (used', n) = update_tree used (A, t)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   155
    in ((B, (i, ts)) :: used', n) end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   156
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   157
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   158
fun update_index ((B, (i, ts)) :: used) (A, j) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   159
  if A = B then (A, (j, ts)) :: used
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   160
  else (B, (i, ts)) :: (update_index used (A, j));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   161
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   162
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   163
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   164
fun getS A i Si =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   165
  filter
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   166
    (fn (_, _, _, Nonterminal (B, j) :: _, _, _) => A = B andalso j <= i
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   167
      | _ => false) Si;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   168
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   169
fun getS' A k n Si =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   170
  filter
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   171
    (fn (_, _, _, Nonterminal (B, j) :: _, _, _) => A = B andalso j <= k andalso j > n
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   172
      | _ => false) Si;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   173
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   174
fun getStates Estate i ii A k =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   175
  filter
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   176
    (fn (_, _, _, Nonterminal (B, j) :: _, _, _) => A = B andalso j <= k
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   177
      | _ => false)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   178
    (Array.sub (Estate, ii));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   179
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   180
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   181
(* MMW *)(* FIXME *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   182
fun movedot_term (A, j, ts, Terminal a :: sa, id, i) c =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   183
  if valued_token c then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   184
    (A, j, (ts @ [Tip c]), sa, id, i)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   185
  else (A, j, ts, sa, id, i);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   186
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   187
fun movedot_nonterm ts (A, j, tss, Nonterminal (B, k) :: sa, id, i) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   188
  (A, j, tss @ ts, sa, id, i);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   189
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   190
fun movedot_lambda _ [] = []
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   191
  | movedot_lambda (B, j, tss, Nonterminal (A, k) :: sa, id, i) ((t, ki) :: ts) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   192
      if k <= ki then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   193
        (B, j, tss @ t, sa, id, i) ::
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   194
          movedot_lambda (B, j, tss, Nonterminal (A, k) :: sa, id, i) ts
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   195
      else movedot_lambda (B, j, tss, Nonterminal (A, k) :: sa, id, i) ts;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   196
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   197
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   198
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   199
fun PROCESSS Estate grammar i c states =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   200
let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   201
fun processS used [] (Si, Sii) = (Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   202
  | processS used (S :: States) (Si, Sii) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   203
      (case S of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   204
        (_, _, _, Nonterminal (A, j) :: _, _, _) =>
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   205
          let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   206
            val (used_neu, States_neu) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   207
              (case assoc (used, A) of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   208
                Some (k, l) =>      (*A gehoert zu used*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   209
                  if k <= j         (*Prioritaet wurde behandelt*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   210
                  then (used, movedot_lambda S l)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   211
                  else              (*Prioritaet wurde noch nicht behandelt*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   212
                    let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   213
                      val L = getProductions' grammar A j k;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   214
                      val States' = map (mkState i j A) L;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   215
                    in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   216
                      (update_index used (A, j), States' @ movedot_lambda S l)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   217
                    end
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   218
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   219
              | None =>             (*A gehoert nicht zu used*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   220
                  let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   221
                    val L = getProductions grammar A j;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   222
                    val States' = map (mkState i j A) L;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   223
                  in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   224
                    ((A, (j, [])) :: used, States')
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   225
                  end)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   226
          in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   227
            processS used_neu (States @ States_neu) (S :: Si, Sii)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   228
          end
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   229
      | (_, _, _, Terminal a :: _, _, _) =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   230
          processS used States
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   231
            (S :: Si,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   232
              if matching_tokens (a, c) then movedot_term S c :: Sii else Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   233
                                          (* MMW *)(* FIXME *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   234
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   235
      | (A, k, ts, [], id, j) =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   236
          let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   237
            val tt = if id = "" then ts else [Node (id, ts)]
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   238
          in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   239
            if j = i then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   240
              let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   241
                val (used', O) = update_tree used (A, (tt, k));
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   242
              in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   243
                (case O of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   244
                  None =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   245
                    let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   246
                      val Slist = getS A k Si;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   247
                      val States' = map (movedot_nonterm tt) Slist;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   248
                    in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   249
                      processS used' (States @ States') (S :: Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   250
                    end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   251
                | Some n =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   252
                    if n >= k then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   253
                      processS used' States (S :: Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   254
                    else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   255
                      let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   256
                        val Slist = getS' A k n Si;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   257
                        val States' = map (movedot_nonterm tt) Slist;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   258
                      in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   259
                        processS used' (States @ States') (S :: Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   260
                      end)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   261
              end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   262
            else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   263
              processS used
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   264
                (States @ map (movedot_nonterm tt) (getStates Estate i j A k))
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   265
                (S :: Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   266
          end)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   267
in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   268
  processS [] states ([], [])
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   269
end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   270
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   271
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   272
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   273
fun syntax_error toks =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   274
  error ("Syntax error at: " ^ space_implode " " (map str_of_token toks));
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   275
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   276
fun produce grammar stateset i indata =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   277
  (case Array.sub (stateset, i) of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   278
    [] => syntax_error indata (* MMW *)(* FIXME *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   279
  | s =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   280
    (case indata of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   281
      [] => Array.sub (stateset, i)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   282
    | c :: cs =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   283
      let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   284
        val (si, sii) = PROCESSS stateset grammar i c s;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   285
      in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   286
        Array.update (stateset, i, si);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   287
        Array.update (stateset, i + 1, sii);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   288
        produce grammar stateset (i + 1) cs
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   289
      end));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   290
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   291
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   292
(*(* FIXME new *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   293
val get_trees = mapfilter (fn (_, _, [pt], _, _, _) => Some pt | _ => None);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   294
*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   295
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   296
fun get_trees [] = []
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   297
  | get_trees ((_, _, pt_lst, _, _, _) :: stateset) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   298
      let val l = get_trees stateset
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   299
      in case pt_lst of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   300
           [ptree] => ptree :: l
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   301
         |   _     => l
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   302
      end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   303
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   304
fun earley grammar startsymbol indata =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   305
  let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   306
    val S0 = [("S'", 0, [], [Nonterminal (startsymbol, 0), Terminal EndToken], "", 0)];
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   307
    val s = length indata + 1;     (* MMW *)(* FIXME was .. + 2 *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   308
    val Estate = Array.array (s, []);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   309
  in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   310
    Array.update (Estate, 0, S0);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   311
    let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   312
      val l = produce grammar Estate 0 indata;    (* MMW *)(* FIXME was .. @ [EndToken] *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   313
      val p_trees = get_trees l;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   314
    in p_trees end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   315
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   316
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   317
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   318
(* FIXME demo *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   319
fun parse (Gram (_, prod_tab)) start toks =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   320
  (case earley prod_tab start toks of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   321
    [] => sys_error "parse: no parse trees"
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   322
  | pt :: _ => pt);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   323
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   324
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   325
end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   326