src/Pure/Syntax/parser.ML
author clasohm
Tue, 26 Apr 1994 14:48:41 +0200
changeset 345 7007562172b1
parent 330 2fda15dd1e0f
child 362 6bea8fdc0e70
permissions -rw-r--r--
made a few cosmetic changes
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$
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
     3
    Author:     Sonia Mahjoub, Markus Wenzel and Carsten Clasohm, TU Muenchen
18
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:
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
     8
  improve syntax error
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
     9
  extend_gram: remove 'roots' arg
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    10
*)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    11
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    12
signature PARSER =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    13
sig
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    14
  structure Lexicon: LEXICON
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    15
  structure SynExt: SYN_EXT
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    16
  local open Lexicon SynExt SynExt.Ast in
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    17
    type gram
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    18
    val empty_gram: gram
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    19
    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
    20
    val merge_grams: gram -> gram -> gram
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    21
    val pretty_gram: gram -> Pretty.T list
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    22
    datatype parsetree =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    23
      Node of string * parsetree list |
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    24
      Tip of token
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    25
    val parse: gram -> string -> token list -> parsetree list
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    26
  end
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    27
end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    28
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    29
functor ParserFun(structure Symtab: SYMTAB and Lexicon: LEXICON
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    30
  and SynExt: SYN_EXT) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    31
struct
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    32
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    33
structure Pretty = SynExt.Ast.Pretty;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    34
structure Lexicon = Lexicon;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    35
structure SynExt = SynExt;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    36
open Lexicon SynExt;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    37
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    38
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    39
(** datatype gram **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    40
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    41
datatype symb =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    42
  Terminal of token |
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
    43
  Nonterminal of string * int;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
    44
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    45
datatype refsymb = Term of token | Nonterm of rhss_ref * int
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    46
                                (*reference to production list instead of name*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    47
and gram = Gram of (string * (symb list * string * int)) list *
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    48
                   (string * rhss_ref) list
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    49
withtype rhss_ref = (token option * (refsymb list * string * int) list) list ref
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    50
                                      (*lookahead table: token and productions*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    51
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    52
(* convert productions to reference grammar with lookaheads and eliminate
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    53
   chain productions *)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    54
fun mk_gram prods = 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    55
  let (*get reference on list of all possible rhss for nonterminal lhs
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    56
        (if it doesn't exist a new one is created and added to the nonterminal
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    57
         list)*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    58
      fun get_rhss ref_prods lhs =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    59
        case assoc (ref_prods, lhs) of
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    60
            None =>
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    61
              let val l = ref [(None, [])]
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    62
              in (l, (lhs, l) :: ref_prods) end
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    63
          | Some l => (l, ref_prods);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    64
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    65
      (*convert symb list to refsymb list*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    66
      fun mk_refsymbs ref_prods [] rs = (rs, ref_prods)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    67
        | mk_refsymbs ref_prods (Terminal tk :: symbs) rs =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    68
            mk_refsymbs ref_prods symbs (rs @ [Term tk])
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    69
        | mk_refsymbs ref_prods (Nonterminal (name, prec) :: symbs) rs =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    70
            let val (rhss, ref_prods') = get_rhss ref_prods name
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    71
            in mk_refsymbs ref_prods' symbs (rs @ [Nonterm (rhss, prec)])
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    72
            end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    73
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    74
      (*convert prod list to (string * rhss_ref) list
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    75
        without computing lookaheads*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    76
      fun mk_ref_gram [] ref_prods = ref_prods
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    77
        | mk_ref_gram ((lhs, (rhs, name, prec)) :: ps) ref_prods =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    78
            let val (rhs', ref_prods') = get_rhss ref_prods lhs;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    79
                val (dummy, rhss) = hd (!rhs');
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    80
                val (ref_symbs, ref_prods'') = mk_refsymbs ref_prods' rhs [];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    81
            in rhs' := [(dummy, (ref_symbs, name, prec) :: rhss)];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    82
               mk_ref_gram ps ref_prods''
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    83
            end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    84
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    85
      (*eliminate chain productions*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    86
      fun elim_chain ref_gram =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    87
        let (*make a list of pairs representing chain productions and delete
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    88
              these productions*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    89
            fun list_chain [] = []
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    90
              | list_chain ((_, rhss_ref) :: ps) =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    91
                  let fun lists [] new_rhss chains = (new_rhss, chains)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    92
                        | lists (([Nonterm (id2, ~1)], _, ~1) :: rs) 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    93
                                new_rhss chains =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    94
                            lists rs new_rhss ((rhss_ref, id2) :: chains)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    95
                        | lists (rhs :: rs) new_rhss chains = 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    96
                            lists rs (rhs :: new_rhss) chains;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    97
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    98
                      val (dummy, rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
    99
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   100
                      val (new_rhss, chains) = lists rhss [] [];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   101
                  in rhss_ref := [(dummy, new_rhss)];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   102
                     chains @ (list_chain ps)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   103
                  end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   104
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   105
            (*convert a list of pairs to an association list
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   106
              by using the first element as the key*)
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   107
            fun mk_assoc pairs =
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   108
              let fun doit [] result = result
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   109
                    | doit ((id1, id2) :: ps) result =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   110
                        doit ps 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   111
                        (overwrite (result, (id1, id2 :: (assocs result id1))));
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   112
              in doit pairs [] end;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   113
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   114
            (*replace reference by list of rhss in chain pairs*)
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   115
            fun deref (id1, ids) =
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   116
              let fun deref1 [] = []
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   117
                    | deref1 (id :: ids) =
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   118
                        let val (_, rhss) = hd (!id);
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   119
                        in rhss @ (deref1 ids) end;
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   120
              in (id1, deref1 ids) end;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   121
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   122
            val chain_pairs = 
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   123
               map deref (transitive_closure (mk_assoc (list_chain ref_gram)));
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   124
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   125
            (*add new rhss to productions*)
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   126
            fun elim (rhss_ref, rhss) =
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   127
              let val (dummy, old_rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   128
              in rhss_ref := [(dummy, old_rhss @ rhss)] end;
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   129
        in map elim chain_pairs;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   130
           ref_gram
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   131
        end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   132
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   133
      val ref_gram = elim_chain (mk_ref_gram prods []);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   134
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   135
      (*make a list of all lambda NTs 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   136
        (i.e. nonterminals that can produce lambda*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   137
      val lambdas =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   138
        let fun lambda [] result = result
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   139
              | lambda ((_, rhss_ref) :: nts) result =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   140
                  if rhss_ref mem result then
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   141
                    lambda nts result
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   142
                  else
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   143
                    let (*list all NTs that can be produced by a rhs
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   144
                          containing only lambda NTs*)
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   145
                        fun only_lambdas [] result = result
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   146
                          | only_lambdas ((_, rhss_ref) :: ps) result =
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   147
                              let fun only (symbs, _, _) =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   148
                                   forall (fn (Nonterm (id, _)) => id mem result
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   149
                                            | (Term _)          => false) symbs;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   150
                          
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   151
                                  val (_, rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   152
                              in if not (rhss_ref mem result) andalso
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   153
                                    exists only rhss then
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   154
                                   only_lambdas ref_gram (rhss_ref :: result)
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   155
                                 else
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   156
                                   only_lambdas ps result
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   157
                              end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   158
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   159
                        val (_, rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   160
                    in if exists (fn (symbs, _, _) => null symbs) rhss
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   161
                       then lambda nts
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   162
                              (only_lambdas ref_gram (rhss_ref :: result))
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   163
                       else lambda nts result
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   164
                    end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   165
         in lambda ref_gram [] end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   166
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   167
      (*list all nonterminals on which the lookahead depends (due to lambda 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   168
        NTs this can be more than one)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   169
        and report if there is a terminal at the 'start'*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   170
      fun rhss_start [] skipped = (None, skipped)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   171
        | rhss_start (Term tk :: _) skipped = (Some tk, skipped)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   172
        | rhss_start (Nonterm (rhss_ref, _) :: rest) skipped =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   173
            if rhss_ref mem lambdas then 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   174
              rhss_start rest (rhss_ref ins skipped)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   175
            else
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   176
              (None, rhss_ref ins skipped);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   177
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   178
      (*list all terminals that can start the given rhss*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   179
      fun look_rhss starts rhss_ref =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   180
        let fun look [] _ = []
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   181
              | look ((symbs, _, _) :: todos) done =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   182
                  let val (start_token, skipped) = rhss_start symbs [];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   183
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   184
                      (*process all nonterminals on which the lookahead
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   185
                        depends and build the new todo and done lists for
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   186
                        the look function*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   187
                      fun look2 [] todos = look todos (done union skipped)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   188
                        | look2 (rhss_ref :: ls) todos =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   189
                            if rhss_ref mem done then look2 ls todos
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   190
                            else case assoc (starts, rhss_ref) of
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   191
                                Some tks => tks union (look2 ls todos)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   192
                              | None => let val (_, rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   193
                                        in look2 ls (rhss union todos) end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   194
                  in case start_token of
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   195
                         Some tk => start_token ins (look2 skipped todos)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   196
                       | None => look2 skipped todos
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   197
                  end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   198
 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   199
            val (_, rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   200
        in look rhss [rhss_ref] end;                       
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   201
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   202
      (*make a table that contains all possible starting terminals
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   203
        for each nonterminal*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   204
      fun mk_starts [] starts = starts
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   205
        | mk_starts ((_, rhss_ref) :: ps) starts =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   206
            mk_starts ps ((rhss_ref, look_rhss starts rhss_ref) :: starts);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   207
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   208
      val starts = mk_starts ref_gram [];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   209
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   210
      (*add list of allowed starting tokens to productions*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   211
      fun mk_lookahead (_, rhss_ref) =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   212
        let (*add item to lookahead list (a list containing pairs of token and 
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   213
              rhss that can be started with it*)
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   214
            fun add_start new_rhs tokens table =
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   215
                  let fun add [] [] = []
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   216
                        | add (tk :: tks) [] =
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   217
                            (tk, [new_rhs]) :: (add tks [])
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   218
                        | add tokens ((tk, rhss) :: ss) =
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   219
                            if tk mem tokens then 
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   220
                              (tk, new_rhs :: rhss) :: (add (tokens \ tk) ss)
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   221
                            else
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   222
                              (tk, rhss) :: (add tokens ss);
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   223
                  in add tokens table end;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   224
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   225
            (*combine all lookaheads of a list of nonterminals*)
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   226
            fun combine_starts rhss_refs =
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   227
              foldr (op union) 
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   228
              ((map (fn rhss_ref => let val Some tks = assoc (starts, rhss_ref)
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   229
                                    in tks end) rhss_refs), []);
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   230
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   231
            (*get lookahead for a rhs and update lhs' lookahead list*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   232
            fun look_rhss [] table = table
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   233
              | look_rhss ((rhs as (symbs, id, prec)) :: rs) table =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   234
                  let val (start_token, skipped) = rhss_start symbs [];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   235
                      val starts = case start_token of
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   236
                                     Some tk => Some tk 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   237
                                                ins (combine_starts skipped)
345
7007562172b1 made a few cosmetic changes
clasohm
parents: 330
diff changeset
   238
                                   | None => if skipped subset lambdas then
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   239
                                               [None]
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   240
                                             else
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   241
                                               combine_starts skipped;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   242
                  in look_rhss rs (add_start rhs starts table) end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   243
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   244
             val (_, rhss) = hd (!rhss_ref);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   245
        in rhss_ref := look_rhss rhss [] end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   246
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   247
  in map mk_lookahead ref_gram;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   248
     Gram (prods, ref_gram)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   249
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   250
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   251
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   252
(* empty, extend, merge grams *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   253
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   254
val empty_gram = mk_gram [];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   255
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   256
fun extend_gram (gram1 as Gram (prods1, _)) _ xprods2 =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   257
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   258
    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
   259
      | symb_of (Argument (s, p)) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   260
          (case predef_term s of
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   261
            None => Some (Nonterminal (s, p))
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   262
          | Some tk => Some (Terminal tk))
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   263
      | symb_of _ = None;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   264
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   265
    fun prod_of (XProd (lhs, xsymbs, const, pri)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   266
      (lhs, (mapfilter symb_of xsymbs, const, pri));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   267
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   268
    val prods2 = distinct (map prod_of xprods2);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   269
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   270
    if prods2 subset prods1 then gram1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   271
    else mk_gram (extend_list prods1 prods2)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   272
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   273
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   274
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
   275
  if prods2 subset prods1 then gram1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   276
  else if prods1 subset prods2 then gram2
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   277
  else mk_gram (merge_lists prods1 prods2);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   278
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   279
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   280
(* pretty_gram *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   281
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   282
fun pretty_gram (Gram (prods, _)) =
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
    fun pretty_name name = [Pretty.str (name ^ " =")];
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   285
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   286
    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
   287
      | 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
   288
      | pretty_symb (Nonterminal (s, p)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   289
          Pretty.str (s ^ "[" ^ string_of_int p ^ "]");
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   290
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   291
    fun pretty_const "" = []
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   292
      | pretty_const c = [Pretty.str ("=> " ^ quote c)];
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   293
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   294
    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
   295
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   296
    fun pretty_prod (name, (symbs, const, pri)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   297
      Pretty.block (Pretty.breaks (pretty_name name @
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   298
        map pretty_symb symbs @ pretty_const const @ pretty_pri pri));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   299
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   300
    map pretty_prod prods
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   301
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   302
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   303
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   304
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   305
(** parse **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   306
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   307
datatype parsetree =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   308
  Node of string * parsetree list |
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   309
  Tip of token;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   310
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   311
type state =
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   312
  rhss_ref * int      (*lhs: identification and production precedence*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   313
  * parsetree list    (*already parsed nonterminals on rhs*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   314
  * refsymb list      (*rest of rhs*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   315
  * string            (*name of production*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   316
  * int;              (*index for previous state list*)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   317
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   318
type earleystate = state list Array.array;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   319
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   320
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   321
(*Get all rhss with precedence >= minPrec*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   322
fun getRHS minPrec = filter (fn (_, _, prec:int) => prec >= minPrec);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   323
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   324
(*Get all rhss with precedence >= minPrec and < maxPrec*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   325
fun getRHS' minPrec maxPrec =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   326
  filter (fn (_, _, prec:int) => prec >= minPrec andalso prec < maxPrec);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   327
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   328
(*Make states using a list of rhss*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   329
fun mkStates i minPrec lhsID rhss =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   330
  let fun mkState (rhs, id, prodPrec) = (lhsID, prodPrec, [], rhs, id, i);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   331
  in map mkState rhss end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   332
	
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   333
(*Add parse tree to list and eliminate duplicates 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   334
  saving the maximum precedence*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   335
fun conc (t, prec:int) [] = (None, [(t, prec)])
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   336
  | conc (t, prec) ((t', prec') :: ts) =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   337
      if t = t' then
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   338
        (Some prec', if prec' >= prec then (t', prec') :: ts 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   339
                     else (t, prec) :: ts)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   340
      else
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   341
        let val (n, ts') = conc (t, prec) ts
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   342
        in (n, (t', prec') :: ts') end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   343
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   344
(*Update entry in used*)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   345
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
   346
  if A = B then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   347
    let val (n, ts') = conc t ts
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   348
    in ((A, (i, ts')) :: used, n) end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   349
  else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   350
    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
   351
    in ((B, (i, ts)) :: used', n) end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   352
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   353
(*Replace entry in used*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   354
fun update_index (A, prec) used =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   355
  let fun update((hd as (B, (_, ts))) :: used, used') =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   356
        if A = B
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   357
        then used' @ ((A, (prec, ts)) :: used)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   358
        else update (used, hd :: used')
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   359
  in update (used, []) end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   360
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   361
fun getS A maxPrec Si =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   362
  filter
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   363
    (fn (_, _, _, Nonterm (B, prec) :: _, _, _)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   364
          => A = B andalso prec <= maxPrec
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   365
      | _ => false) Si;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   366
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   367
fun getS' A maxPrec minPrec Si =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   368
  filter
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   369
    (fn (_, _, _, Nonterm (B, prec) :: _, _, _)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   370
          => A = B andalso prec > minPrec andalso prec <= maxPrec
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   371
      | _ => false) Si;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   372
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   373
fun getStates Estate i ii A maxPrec =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   374
  filter
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   375
    (fn (_, _, _, Nonterm (B, prec) :: _, _, _)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   376
          => A = B andalso prec <= maxPrec
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   377
      | _ => false)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   378
    (Array.sub (Estate, ii));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   379
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   380
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   381
fun movedot_term (A, j, ts, Term a :: sa, id, i) c =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   382
  if valued_token c then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   383
    (A, j, (ts @ [Tip c]), sa, id, i)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   384
  else (A, j, ts, sa, id, i);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   385
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   386
fun movedot_nonterm ts (A, j, tss, Nonterm _ :: sa, id, i) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   387
  (A, j, tss @ ts, sa, id, i);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   388
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   389
fun movedot_lambda _ [] = []
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   390
  | movedot_lambda (B, j, tss, Nonterm (A, k) :: sa, id, i) ((t, ki) :: ts) =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   391
      if k <= ki then
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   392
        (B, j, tss @ t, sa, id, i) ::
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   393
          movedot_lambda (B, j, tss, Nonterm (A, k) :: sa, id, i) ts
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   394
      else movedot_lambda (B, j, tss, Nonterm (A, k) :: sa, id, i) ts;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   395
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   396
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   397
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   398
fun PROCESSS Estate i c states =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   399
let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   400
fun get_lookahead rhss_ref = token_assoc (!rhss_ref, c);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   401
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   402
fun processS used [] (Si, Sii) = (Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   403
  | processS used (S :: States) (Si, Sii) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   404
      (case S of
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   405
        (_, _, _, Nonterm (rhss_ref, minPrec) :: _, _, _) =>
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   406
          let                                       (*predictor operation*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   407
            val (used_new, States_new) =
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   408
              (case assoc (used, rhss_ref) of
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   409
                Some (usedPrec, l) =>       (*nonterminal has been processed*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   410
                  if usedPrec <= minPrec then
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   411
                                      (*wanted precedence has been processed*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   412
                    (used, movedot_lambda S l)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   413
                  else            (*wanted precedence hasn't been parsed yet*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   414
                    let val rhss = get_lookahead rhss_ref;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   415
                      val States' = mkStates i minPrec rhss_ref
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   416
                                      (getRHS' minPrec usedPrec rhss);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   417
                    in (update_index (rhss_ref, minPrec) used, 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   418
                        movedot_lambda S l @ States')
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   419
                    end
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   420
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   421
              | None =>           (*nonterminal is parsed for the first time*)
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   422
                  let val rhss = get_lookahead rhss_ref;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   423
                      val States' = mkStates i minPrec rhss_ref
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   424
                                      (getRHS minPrec rhss);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   425
                  in ((rhss_ref, (minPrec, [])) :: used, States') end)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   426
          in
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   427
            processS used_new (States_new @ States) (S :: Si, Sii)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   428
          end
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   429
      | (_, _, _, Term a :: _, _, _) =>               (*scanner operation*)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   430
          processS used States
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   431
            (S :: Si,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   432
              if matching_tokens (a, c) then movedot_term S c :: Sii else Sii)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   433
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   434
      | (A, prec, ts, [], id, j) =>                   (*completer operation*)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   435
          let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   436
            val tt = if id = "" then ts else [Node (id, ts)]
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   437
          in
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   438
            if j = i then                             (*lambda production?*)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   439
              let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   440
                val (used', O) = update_tree used (A, (tt, prec));
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   441
              in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   442
                (case O of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   443
                  None =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   444
                    let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   445
                      val Slist = getS A prec Si;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   446
                      val States' = map (movedot_nonterm tt) Slist;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   447
                    in
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   448
                      processS used' (States' @ States) (S :: Si, Sii)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   449
                    end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   450
                | Some n =>
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   451
                    if n >= prec then
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   452
                      processS used' States (S :: Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   453
                    else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   454
                      let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   455
                        val Slist = getS' A prec n Si;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   456
                        val States' = map (movedot_nonterm tt) Slist;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   457
                      in
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   458
                        processS used' (States' @ States) (S :: Si, Sii)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   459
                      end)
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   460
              end 
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   461
            else
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   462
              processS used
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   463
                (map (movedot_nonterm tt) (getStates Estate i j A prec) @ States)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   464
                (S :: Si, Sii)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   465
          end)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   466
in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   467
  processS [] states ([], [])
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   468
end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   469
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   470
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   471
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   472
fun syntax_error toks =
258
e540b7d4ecb1 minor internal changes;
wenzelm
parents: 237
diff changeset
   473
  error ("Syntax error at: " ^ quote (space_implode " " (map str_of_token toks)));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   474
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   475
fun produce stateset i indata =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   476
  (case Array.sub (stateset, i) of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   477
    [] => syntax_error indata (* MMW *)(* FIXME *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   478
  | s =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   479
    (case indata of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   480
      [] => Array.sub (stateset, i)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   481
    | c :: cs =>
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   482
      let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   483
        val (si, sii) = PROCESSS stateset i c s;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   484
      in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   485
        Array.update (stateset, i, si);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   486
        Array.update (stateset, i + 1, sii);
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   487
        produce stateset (i + 1) cs
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   488
      end));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   489
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   490
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   491
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
   492
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   493
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   494
fun earley grammar startsymbol indata =
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   495
  let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   496
    val rhss_ref = case assoc (grammar, startsymbol) of
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   497
                       Some r => r
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   498
                     | None => error ("parse: Unknown startsymbol " ^ 
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   499
                                      quote startsymbol);
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   500
    val S0 = [(ref [], 0, [], [Nonterm (rhss_ref, 0), Term EndToken], "", 0)];
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   501
    val s = length indata + 1;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   502
    val Estate = Array.array (s, []);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   503
  in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   504
    Array.update (Estate, 0, S0);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   505
    let
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   506
      val l = produce Estate 0 indata;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   507
      val p_trees = get_trees l;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   508
    in p_trees end
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   509
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   510
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   511
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   512
fun parse (Gram (_, prod_tab)) start toks =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   513
  (case earley prod_tab start toks of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 46
diff changeset
   514
    [] => sys_error "parse: no parse trees"
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   515
  | pts => pts);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   516
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   517
end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents:
diff changeset
   518