| 
46844
 | 
     1  | 
  | 
| 
 | 
     2  | 
(******************************************************************)
  | 
| 
 | 
     3  | 
(* GENERATED FILE -- DO NOT EDIT -- GENERATED FILE -- DO NOT EDIT *)
  | 
| 
 | 
     4  | 
(* GENERATED FILE -- DO NOT EDIT -- GENERATED FILE -- DO NOT EDIT *)
  | 
| 
 | 
     5  | 
(* GENERATED FILE -- DO NOT EDIT -- GENERATED FILE -- DO NOT EDIT *)
  | 
| 
 | 
     6  | 
(******************************************************************)
  | 
| 
 | 
     7  | 
  | 
| 
 | 
     8  | 
print_depth 0;
  | 
| 
 | 
     9  | 
  | 
| 
 | 
    10  | 
(*
  | 
| 
 | 
    11  | 
  This file is generated from the contents of ML-Yacc's lib directory.
  | 
| 
 | 
    12  | 
  ML-Yacc's COPYRIGHT-file contents follow:
  | 
| 
 | 
    13  | 
  | 
| 
 | 
    14  | 
  ML-YACC COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
  | 
| 
 | 
    15  | 
  
  | 
| 
 | 
    16  | 
  Copyright 1989, 1990 by David R. Tarditi Jr. and Andrew W. Appel
  | 
| 
 | 
    17  | 
  
  | 
| 
 | 
    18  | 
  Permission to use, copy, modify, and distribute this software and its
  | 
| 
 | 
    19  | 
  documentation for any purpose and without fee is hereby granted,
  | 
| 
 | 
    20  | 
  provided that the above copyright notice appear in all copies and that
  | 
| 
 | 
    21  | 
  both the copyright notice and this permission notice and warranty
  | 
| 
 | 
    22  | 
  disclaimer appear in supporting documentation, and that the names of
  | 
| 
 | 
    23  | 
  David R. Tarditi Jr. and Andrew W. Appel not be used in advertising
  | 
| 
 | 
    24  | 
  or publicity pertaining to distribution of the software without
  | 
| 
 | 
    25  | 
  specific, written prior permission.
  | 
| 
 | 
    26  | 
  
  | 
| 
 | 
    27  | 
  David R. Tarditi Jr. and Andrew W. Appel disclaim all warranties with regard to
  | 
| 
 | 
    28  | 
  this software, including all implied warranties of merchantability and fitness.
  | 
| 
 | 
    29  | 
  In no event shall David R. Tarditi Jr. and Andrew W. Appel be liable for any
  | 
| 
 | 
    30  | 
  special, indirect or consequential damages or any damages whatsoever resulting
  | 
| 
 | 
    31  | 
  from loss of use, data or profits, whether in an action of contract, negligence
  | 
| 
 | 
    32  | 
  or other tortious action, arising out of or in connection with the use or
  | 
| 
 | 
    33  | 
  performance of this software.
  | 
| 
 | 
    34  | 
*)
  | 
| 
 | 
    35  | 
  | 
| 
 | 
    36  | 
(**** Original file: base.sig ****)
  | 
| 
 | 
    37  | 
  | 
| 
 | 
    38  | 
(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi *)
  | 
| 
 | 
    39  | 
  | 
| 
 | 
    40  | 
(* base.sig: Base signature file for SML-Yacc.  This file contains signatures
  | 
| 
 | 
    41  | 
   that must be loaded before any of the files produced by ML-Yacc are loaded
  | 
| 
 | 
    42  | 
*)
  | 
| 
 | 
    43  | 
  | 
| 
 | 
    44  | 
(* STREAM: signature for a lazy stream.*)
  | 
| 
 | 
    45  | 
  | 
| 
 | 
    46  | 
signature STREAM =
  | 
| 
 | 
    47  | 
 sig type 'xa stream
  | 
| 
 | 
    48  | 
     val streamify : (unit -> '_a) -> '_a stream
  | 
| 
 | 
    49  | 
     val cons : '_a * '_a stream -> '_a stream
  | 
| 
 | 
    50  | 
     val get : '_a stream -> '_a * '_a stream
  | 
| 
 | 
    51  | 
 end
  | 
| 
 | 
    52  | 
  | 
| 
 | 
    53  | 
(* LR_TABLE: signature for an LR Table.
  | 
| 
 | 
    54  | 
  | 
| 
 | 
    55  | 
   The list of actions and gotos passed to mkLrTable must be ordered by state
  | 
| 
 | 
    56  | 
   number. The values for state 0 are the first in the list, the values for
  | 
| 
 | 
    57  | 
    state 1 are next, etc.
  | 
| 
 | 
    58  | 
*)
  | 
| 
 | 
    59  | 
  | 
| 
 | 
    60  | 
signature LR_TABLE =
  | 
| 
 | 
    61  | 
    sig
  | 
| 
 | 
    62  | 
        datatype ('a,'b) pairlist = EMPTY | PAIR of 'a * 'b * ('a,'b) pairlist
 | 
| 
 | 
    63  | 
	datatype state = STATE of int
  | 
| 
 | 
    64  | 
	datatype term = T of int
  | 
| 
 | 
    65  | 
	datatype nonterm = NT of int
  | 
| 
 | 
    66  | 
	datatype action = SHIFT of state
  | 
| 
 | 
    67  | 
			| REDUCE of int
  | 
| 
 | 
    68  | 
			| ACCEPT
  | 
| 
 | 
    69  | 
			| ERROR
  | 
| 
 | 
    70  | 
	type table
  | 
| 
 | 
    71  | 
	
  | 
| 
 | 
    72  | 
	val numStates : table -> int
  | 
| 
 | 
    73  | 
	val numRules : table -> int
  | 
| 
 | 
    74  | 
	val describeActions : table -> state ->
  | 
| 
 | 
    75  | 
				(term,action) pairlist * action
  | 
| 
 | 
    76  | 
	val describeGoto : table -> state -> (nonterm,state) pairlist
  | 
| 
 | 
    77  | 
	val action : table -> state * term -> action
  | 
| 
 | 
    78  | 
	val goto : table -> state * nonterm -> state
  | 
| 
 | 
    79  | 
	val initialState : table -> state
  | 
| 
 | 
    80  | 
	exception Goto of state * nonterm
  | 
| 
 | 
    81  | 
  | 
| 
 | 
    82  | 
	val mkLrTable : {actions : ((term,action) pairlist * action) array,
 | 
| 
 | 
    83  | 
			 gotos : (nonterm,state) pairlist array,
  | 
| 
 | 
    84  | 
			 numStates : int, numRules : int,
  | 
| 
 | 
    85  | 
			 initialState : state} -> table
  | 
| 
 | 
    86  | 
    end
  | 
| 
 | 
    87  | 
  | 
| 
 | 
    88  | 
(* TOKEN: signature revealing the internal structure of a token. This signature
  | 
| 
 | 
    89  | 
   TOKEN distinct from the signature {parser name}_TOKENS produced by ML-Yacc.
 | 
| 
 | 
    90  | 
   The {parser name}_TOKENS structures contain some types and functions to
 | 
| 
 | 
    91  | 
    construct tokens from values and positions.
  | 
| 
 | 
    92  | 
  | 
| 
 | 
    93  | 
   The representation of token was very carefully chosen here to allow the
  | 
| 
 | 
    94  | 
   polymorphic parser to work without knowing the types of semantic values
  | 
| 
 | 
    95  | 
   or line numbers.
  | 
| 
 | 
    96  | 
  | 
| 
 | 
    97  | 
   This has had an impact on the TOKENS structure produced by SML-Yacc, which
  | 
| 
 | 
    98  | 
   is a structure parameter to lexer functors.  We would like to have some
  | 
| 
 | 
    99  | 
   type 'a token which functions to construct tokens would create.  A
  | 
| 
 | 
   100  | 
   constructor function for a integer token might be
  | 
| 
 | 
   101  | 
  | 
| 
 | 
   102  | 
	  INT: int * 'a * 'a -> 'a token.
  | 
| 
 | 
   103  | 
 
  | 
| 
 | 
   104  | 
   This is not possible because we need to have tokens with the representation
  | 
| 
 | 
   105  | 
   given below for the polymorphic parser.
  | 
| 
 | 
   106  | 
  | 
| 
 | 
   107  | 
   Thus our constructur functions for tokens have the form:
  | 
| 
 | 
   108  | 
  | 
| 
 | 
   109  | 
	  INT: int * 'a * 'a -> (svalue,'a) token
  | 
| 
 | 
   110  | 
  | 
| 
 | 
   111  | 
   This in turn has had an impact on the signature that lexers for SML-Yacc
  | 
| 
 | 
   112  | 
   must match and the types that a user must declare in the user declarations
  | 
| 
 | 
   113  | 
   section of lexers.
  | 
| 
 | 
   114  | 
*)
  | 
| 
 | 
   115  | 
  | 
| 
 | 
   116  | 
signature TOKEN =
  | 
| 
 | 
   117  | 
    sig
  | 
| 
 | 
   118  | 
	structure LrTable : LR_TABLE
  | 
| 
 | 
   119  | 
        datatype ('a,'b) token = TOKEN of LrTable.term * ('a * 'b * 'b)
 | 
| 
 | 
   120  | 
	val sameToken : ('a,'b) token * ('a,'b) token -> bool
 | 
| 
 | 
   121  | 
    end
  | 
| 
 | 
   122  | 
  | 
| 
 | 
   123  | 
(* LR_PARSER: signature for a polymorphic LR parser *)
  | 
| 
 | 
   124  | 
  | 
| 
 | 
   125  | 
signature LR_PARSER =
  | 
| 
 | 
   126  | 
    sig
  | 
| 
 | 
   127  | 
	structure Stream: STREAM
  | 
| 
 | 
   128  | 
	structure LrTable : LR_TABLE
  | 
| 
 | 
   129  | 
	structure Token : TOKEN
  | 
| 
 | 
   130  | 
  | 
| 
 | 
   131  | 
	sharing LrTable = Token.LrTable
  | 
| 
 | 
   132  | 
  | 
| 
 | 
   133  | 
	exception ParseError
  | 
| 
 | 
   134  | 
  | 
| 
 | 
   135  | 
	val parse : {table : LrTable.table,
 | 
| 
 | 
   136  | 
		     lexer : ('_b,'_c) Token.token Stream.stream,
 | 
| 
 | 
   137  | 
		     arg: 'arg,
  | 
| 
 | 
   138  | 
		     saction : int *
  | 
| 
 | 
   139  | 
			       '_c *
  | 
| 
 | 
   140  | 
				(LrTable.state * ('_b * '_c * '_c)) list * 
 | 
| 
 | 
   141  | 
				'arg ->
  | 
| 
 | 
   142  | 
				     LrTable.nonterm *
  | 
| 
 | 
   143  | 
				     ('_b * '_c * '_c) *
 | 
| 
 | 
   144  | 
				     ((LrTable.state *('_b * '_c * '_c)) list),
 | 
| 
 | 
   145  | 
		     void : '_b,
  | 
| 
 | 
   146  | 
		     ec : { is_keyword : LrTable.term -> bool,
 | 
| 
 | 
   147  | 
			    noShift : LrTable.term -> bool,
  | 
| 
 | 
   148  | 
			    preferred_change : (LrTable.term list * LrTable.term list) list,
  | 
| 
 | 
   149  | 
			    errtermvalue : LrTable.term -> '_b,
  | 
| 
 | 
   150  | 
			    showTerminal : LrTable.term -> string,
  | 
| 
 | 
   151  | 
			    terms: LrTable.term list,
  | 
| 
 | 
   152  | 
			    error : string * '_c * '_c -> unit
  | 
| 
 | 
   153  | 
			   },
  | 
| 
 | 
   154  | 
		     lookahead : int  (* max amount of lookahead used in *)
  | 
| 
 | 
   155  | 
				      (* error correction *)
  | 
| 
 | 
   156  | 
			} -> '_b *
  | 
| 
 | 
   157  | 
			     (('_b,'_c) Token.token Stream.stream)
 | 
| 
 | 
   158  | 
    end
  | 
| 
 | 
   159  | 
  | 
| 
 | 
   160  | 
(* LEXER: a signature that most lexers produced for use with SML-Yacc's
  | 
| 
 | 
   161  | 
   output will match.  The user is responsible for declaring type token,
  | 
| 
 | 
   162  | 
   type pos, and type svalue in the UserDeclarations section of a lexer.
  | 
| 
 | 
   163  | 
  | 
| 
 | 
   164  | 
   Note that type token is abstract in the lexer.  This allows SML-Yacc to
  | 
| 
 | 
   165  | 
   create a TOKENS signature for use with lexers produced by ML-Lex that
  | 
| 
 | 
   166  | 
   treats the type token abstractly.  Lexers that are functors parametrized by
  | 
| 
 | 
   167  | 
   a Tokens structure matching a TOKENS signature cannot examine the structure
  | 
| 
 | 
   168  | 
   of tokens.
  | 
| 
 | 
   169  | 
*)
  | 
| 
 | 
   170  | 
  | 
| 
 | 
   171  | 
signature LEXER =
  | 
| 
 | 
   172  | 
   sig
  | 
| 
 | 
   173  | 
       structure UserDeclarations :
  | 
| 
 | 
   174  | 
	   sig
  | 
| 
 | 
   175  | 
	        type ('a,'b) token
 | 
| 
 | 
   176  | 
		type pos
  | 
| 
 | 
   177  | 
		type svalue
  | 
| 
 | 
   178  | 
	   end
  | 
| 
 | 
   179  | 
	val makeLexer : (int -> string) -> unit -> 
  | 
| 
 | 
   180  | 
         (UserDeclarations.svalue,UserDeclarations.pos) UserDeclarations.token
  | 
| 
 | 
   181  | 
   end
  | 
| 
 | 
   182  | 
  | 
| 
 | 
   183  | 
(* ARG_LEXER: the %arg option of ML-Lex allows users to produce lexers which
  | 
| 
 | 
   184  | 
   also take an argument before yielding a function from unit to a token
  | 
| 
 | 
   185  | 
*)
  | 
| 
 | 
   186  | 
  | 
| 
 | 
   187  | 
signature ARG_LEXER =
  | 
| 
 | 
   188  | 
   sig
  | 
| 
 | 
   189  | 
       structure UserDeclarations :
  | 
| 
 | 
   190  | 
	   sig
  | 
| 
 | 
   191  | 
	        type ('a,'b) token
 | 
| 
 | 
   192  | 
		type pos
  | 
| 
 | 
   193  | 
		type svalue
  | 
| 
 | 
   194  | 
		type arg
  | 
| 
 | 
   195  | 
	   end
  | 
| 
 | 
   196  | 
	val makeLexer : (int -> string) -> UserDeclarations.arg -> unit -> 
  | 
| 
 | 
   197  | 
         (UserDeclarations.svalue,UserDeclarations.pos) UserDeclarations.token
  | 
| 
 | 
   198  | 
   end
  | 
| 
 | 
   199  | 
  | 
| 
 | 
   200  | 
(* PARSER_DATA: the signature of ParserData structures in {parser name}LrValsFun
 | 
| 
 | 
   201  | 
   produced by  SML-Yacc.  All such structures match this signature.  
  | 
| 
 | 
   202  | 
  | 
| 
 | 
   203  | 
   The {parser name}LrValsFun produces a structure which contains all the values
 | 
| 
 | 
   204  | 
   except for the lexer needed to call the polymorphic parser mentioned
  | 
| 
 | 
   205  | 
   before.
  | 
| 
 | 
   206  | 
  | 
| 
 | 
   207  | 
*)
  | 
| 
 | 
   208  | 
  | 
| 
 | 
   209  | 
signature PARSER_DATA =
  | 
| 
 | 
   210  | 
   sig
  | 
| 
 | 
   211  | 
        (* the type of line numbers *)
  | 
| 
 | 
   212  | 
  | 
| 
 | 
   213  | 
	type pos
  | 
| 
 | 
   214  | 
  | 
| 
 | 
   215  | 
	(* the type of semantic values *)
  | 
| 
 | 
   216  | 
  | 
| 
 | 
   217  | 
	type svalue
  | 
| 
 | 
   218  | 
  | 
| 
 | 
   219  | 
         (* the type of the user-supplied argument to the parser *)
  | 
| 
 | 
   220  | 
 	type arg
  | 
| 
 | 
   221  | 
 
  | 
| 
 | 
   222  | 
	(* the intended type of the result of the parser.  This value is
  | 
| 
 | 
   223  | 
	   produced by applying extract from the structure Actions to the
  | 
| 
 | 
   224  | 
	   final semantic value resultiing from a parse.
  | 
| 
 | 
   225  | 
	 *)
  | 
| 
 | 
   226  | 
  | 
| 
 | 
   227  | 
	type result
  | 
| 
 | 
   228  | 
  | 
| 
 | 
   229  | 
	structure LrTable : LR_TABLE
  | 
| 
 | 
   230  | 
	structure Token : TOKEN
  | 
| 
 | 
   231  | 
	sharing Token.LrTable = LrTable
  | 
| 
 | 
   232  | 
  | 
| 
 | 
   233  | 
	(* structure Actions contains the functions which mantain the
  | 
| 
 | 
   234  | 
	   semantic values stack in the parser.  Void is used to provide
  | 
| 
 | 
   235  | 
	   a default value for the semantic stack.
  | 
| 
 | 
   236  | 
	 *)
  | 
| 
 | 
   237  | 
  | 
| 
 | 
   238  | 
	structure Actions : 
  | 
| 
 | 
   239  | 
	  sig
  | 
| 
 | 
   240  | 
	      val actions : int * pos *
  | 
| 
 | 
   241  | 
		   (LrTable.state * (svalue * pos * pos)) list * arg->
  | 
| 
 | 
   242  | 
		         LrTable.nonterm * (svalue * pos * pos) *
  | 
| 
 | 
   243  | 
			 ((LrTable.state *(svalue * pos * pos)) list)
  | 
| 
 | 
   244  | 
	      val void : svalue
  | 
| 
 | 
   245  | 
	      val extract : svalue -> result
  | 
| 
 | 
   246  | 
	  end
  | 
| 
 | 
   247  | 
  | 
| 
 | 
   248  | 
	(* structure EC contains information used to improve error
  | 
| 
 | 
   249  | 
	   recovery in an error-correcting parser *)
  | 
| 
 | 
   250  | 
  | 
| 
 | 
   251  | 
	structure EC :
  | 
| 
 | 
   252  | 
	   sig
  | 
| 
 | 
   253  | 
	     val is_keyword : LrTable.term -> bool
  | 
| 
 | 
   254  | 
	     val noShift : LrTable.term -> bool
  | 
| 
 | 
   255  | 
 	     val preferred_change : (LrTable.term list * LrTable.term list) list
  | 
| 
 | 
   256  | 
	     val errtermvalue : LrTable.term -> svalue
  | 
| 
 | 
   257  | 
	     val showTerminal : LrTable.term -> string
  | 
| 
 | 
   258  | 
	     val terms: LrTable.term list
  | 
| 
 | 
   259  | 
	   end
  | 
| 
 | 
   260  | 
  | 
| 
 | 
   261  | 
	(* table is the LR table for the parser *)
  | 
| 
 | 
   262  | 
  | 
| 
 | 
   263  | 
	val table : LrTable.table
  | 
| 
 | 
   264  | 
    end
  | 
| 
 | 
   265  | 
  | 
| 
 | 
   266  | 
(* signature PARSER is the signature that most user parsers created by 
  | 
| 
 | 
   267  | 
   SML-Yacc will match.
  | 
| 
 | 
   268  | 
*)
  | 
| 
 | 
   269  | 
  | 
| 
 | 
   270  | 
signature PARSER =
  | 
| 
 | 
   271  | 
    sig
  | 
| 
 | 
   272  | 
        structure Token : TOKEN
  | 
| 
 | 
   273  | 
	structure Stream : STREAM
  | 
| 
 | 
   274  | 
	exception ParseError
  | 
| 
 | 
   275  | 
  | 
| 
 | 
   276  | 
	(* type pos is the type of line numbers *)
  | 
| 
 | 
   277  | 
  | 
| 
 | 
   278  | 
	type pos
  | 
| 
 | 
   279  | 
  | 
| 
 | 
   280  | 
	(* type result is the type of the result from the parser *)
  | 
| 
 | 
   281  | 
  | 
| 
 | 
   282  | 
	type result
  | 
| 
 | 
   283  | 
  | 
| 
 | 
   284  | 
         (* the type of the user-supplied argument to the parser *)
  | 
| 
 | 
   285  | 
 	type arg
  | 
| 
 | 
   286  | 
	
  | 
| 
 | 
   287  | 
	(* type svalue is the type of semantic values for the semantic value
  | 
| 
 | 
   288  | 
	   stack
  | 
| 
 | 
   289  | 
	 *)
  | 
| 
 | 
   290  | 
  | 
| 
 | 
   291  | 
	type svalue
  | 
| 
 | 
   292  | 
  | 
| 
 | 
   293  | 
	(* val makeLexer is used to create a stream of tokens for the parser *)
  | 
| 
 | 
   294  | 
  | 
| 
 | 
   295  | 
	val makeLexer : (int -> string) ->
  | 
| 
 | 
   296  | 
			 (svalue,pos) Token.token Stream.stream
  | 
| 
 | 
   297  | 
  | 
| 
 | 
   298  | 
	(* val parse takes a stream of tokens and a function to TextIO.print
  | 
| 
 | 
   299  | 
	   errors and returns a value of type result and a stream containing
  | 
| 
 | 
   300  | 
	   the unused tokens
  | 
| 
 | 
   301  | 
	 *)
  | 
| 
 | 
   302  | 
  | 
| 
 | 
   303  | 
	val parse : int * ((svalue,pos) Token.token Stream.stream) *
  | 
| 
 | 
   304  | 
		    (string * pos * pos -> unit) * arg ->
  | 
| 
 | 
   305  | 
				result * (svalue,pos) Token.token Stream.stream
  | 
| 
 | 
   306  | 
  | 
| 
 | 
   307  | 
	val sameToken : (svalue,pos) Token.token * (svalue,pos) Token.token ->
  | 
| 
 | 
   308  | 
				bool
  | 
| 
 | 
   309  | 
     end
  | 
| 
 | 
   310  | 
  | 
| 
 | 
   311  | 
(* signature ARG_PARSER is the signature that will be matched by parsers whose
  | 
| 
 | 
   312  | 
    lexer takes an additional argument.
  | 
| 
 | 
   313  | 
*)
  | 
| 
 | 
   314  | 
  | 
| 
 | 
   315  | 
signature ARG_PARSER = 
  | 
| 
 | 
   316  | 
    sig
  | 
| 
 | 
   317  | 
        structure Token : TOKEN
  | 
| 
 | 
   318  | 
	structure Stream : STREAM
  | 
| 
 | 
   319  | 
	exception ParseError
  | 
| 
 | 
   320  | 
  | 
| 
 | 
   321  | 
	type arg
  | 
| 
 | 
   322  | 
	type lexarg
  | 
| 
 | 
   323  | 
	type pos
  | 
| 
 | 
   324  | 
	type result
  | 
| 
 | 
   325  | 
	type svalue
  | 
| 
 | 
   326  | 
  | 
| 
 | 
   327  | 
	val makeLexer : (int -> string) -> lexarg ->
  | 
| 
 | 
   328  | 
			 (svalue,pos) Token.token Stream.stream
  | 
| 
 | 
   329  | 
	val parse : int * ((svalue,pos) Token.token Stream.stream) *
  | 
| 
 | 
   330  | 
		    (string * pos * pos -> unit) * arg ->
  | 
| 
 | 
   331  | 
				result * (svalue,pos) Token.token Stream.stream
  | 
| 
 | 
   332  | 
  | 
| 
 | 
   333  | 
	val sameToken : (svalue,pos) Token.token * (svalue,pos) Token.token ->
  | 
| 
 | 
   334  | 
				bool
  | 
| 
 | 
   335  | 
     end
  | 
| 
 | 
   336  | 
  | 
| 
 | 
   337  | 
  | 
| 
 | 
   338  | 
(**** Original file: join.sml ****)
  | 
| 
 | 
   339  | 
  | 
| 
 | 
   340  | 
(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi *)
  | 
| 
 | 
   341  | 
  | 
| 
 | 
   342  | 
(* functor Join creates a user parser by putting together a Lexer structure,
  | 
| 
 | 
   343  | 
   an LrValues structure, and a polymorphic parser structure.  Note that
  | 
| 
 | 
   344  | 
   the Lexer and LrValues structure must share the type pos (i.e. the type
  | 
| 
 | 
   345  | 
   of line numbers), the type svalues for semantic values, and the type
  | 
| 
 | 
   346  | 
   of tokens.
  | 
| 
 | 
   347  | 
*)
  | 
| 
 | 
   348  | 
  | 
| 
 | 
   349  | 
functor Join(structure Lex : LEXER
  | 
| 
 | 
   350  | 
	     structure ParserData: PARSER_DATA
  | 
| 
 | 
   351  | 
	     structure LrParser : LR_PARSER
  | 
| 
 | 
   352  | 
	     sharing ParserData.LrTable = LrParser.LrTable
  | 
| 
 | 
   353  | 
	     sharing ParserData.Token = LrParser.Token
  | 
| 
 | 
   354  | 
	     sharing type Lex.UserDeclarations.svalue = ParserData.svalue
  | 
| 
 | 
   355  | 
	     sharing type Lex.UserDeclarations.pos = ParserData.pos
  | 
| 
 | 
   356  | 
	     sharing type Lex.UserDeclarations.token = ParserData.Token.token)
  | 
| 
 | 
   357  | 
		 : PARSER =
  | 
| 
 | 
   358  | 
struct
  | 
| 
 | 
   359  | 
    structure Token = ParserData.Token
  | 
| 
 | 
   360  | 
    structure Stream = LrParser.Stream
  | 
| 
 | 
   361  | 
 
  | 
| 
 | 
   362  | 
    exception ParseError = LrParser.ParseError
  | 
| 
 | 
   363  | 
  | 
| 
 | 
   364  | 
    type arg = ParserData.arg
  | 
| 
 | 
   365  | 
    type pos = ParserData.pos
  | 
| 
 | 
   366  | 
    type result = ParserData.result
  | 
| 
 | 
   367  | 
    type svalue = ParserData.svalue
  | 
| 
 | 
   368  | 
    val makeLexer = LrParser.Stream.streamify o Lex.makeLexer
  | 
| 
 | 
   369  | 
    val parse = fn (lookahead,lexer,error,arg) =>
  | 
| 
 | 
   370  | 
	(fn (a,b) => (ParserData.Actions.extract a,b))
  | 
| 
 | 
   371  | 
     (LrParser.parse {table = ParserData.table,
 | 
| 
 | 
   372  | 
	        lexer=lexer,
  | 
| 
 | 
   373  | 
		lookahead=lookahead,
  | 
| 
 | 
   374  | 
		saction = ParserData.Actions.actions,
  | 
| 
 | 
   375  | 
		arg=arg,
  | 
| 
 | 
   376  | 
		void= ParserData.Actions.void,
  | 
| 
 | 
   377  | 
	        ec = {is_keyword = ParserData.EC.is_keyword,
 | 
| 
 | 
   378  | 
		      noShift = ParserData.EC.noShift,
  | 
| 
 | 
   379  | 
		      preferred_change = ParserData.EC.preferred_change,
  | 
| 
 | 
   380  | 
		      errtermvalue = ParserData.EC.errtermvalue,
  | 
| 
 | 
   381  | 
		      error=error,
  | 
| 
 | 
   382  | 
		      showTerminal = ParserData.EC.showTerminal,
  | 
| 
 | 
   383  | 
		      terms = ParserData.EC.terms}}
  | 
| 
 | 
   384  | 
      )
  | 
| 
 | 
   385  | 
     val sameToken = Token.sameToken
  | 
| 
 | 
   386  | 
end
  | 
| 
 | 
   387  | 
  | 
| 
 | 
   388  | 
(* functor JoinWithArg creates a variant of the parser structure produced 
  | 
| 
 | 
   389  | 
   above.  In this case, the makeLexer take an additional argument before
  | 
| 
 | 
   390  | 
   yielding a value of type unit -> (svalue,pos) token
  | 
| 
 | 
   391  | 
 *)
  | 
| 
 | 
   392  | 
  | 
| 
 | 
   393  | 
functor JoinWithArg(structure Lex : ARG_LEXER
  | 
| 
 | 
   394  | 
	     structure ParserData: PARSER_DATA
  | 
| 
 | 
   395  | 
	     structure LrParser : LR_PARSER
  | 
| 
 | 
   396  | 
	     sharing ParserData.LrTable = LrParser.LrTable
  | 
| 
 | 
   397  | 
	     sharing ParserData.Token = LrParser.Token
  | 
| 
 | 
   398  | 
	     sharing type Lex.UserDeclarations.svalue = ParserData.svalue
  | 
| 
 | 
   399  | 
	     sharing type Lex.UserDeclarations.pos = ParserData.pos
  | 
| 
 | 
   400  | 
	     sharing type Lex.UserDeclarations.token = ParserData.Token.token)
  | 
| 
 | 
   401  | 
		 : ARG_PARSER  =
  | 
| 
 | 
   402  | 
struct
  | 
| 
 | 
   403  | 
    structure Token = ParserData.Token
  | 
| 
 | 
   404  | 
    structure Stream = LrParser.Stream
  | 
| 
 | 
   405  | 
  | 
| 
 | 
   406  | 
    exception ParseError = LrParser.ParseError
  | 
| 
 | 
   407  | 
  | 
| 
 | 
   408  | 
    type arg = ParserData.arg
  | 
| 
 | 
   409  | 
    type lexarg = Lex.UserDeclarations.arg
  | 
| 
 | 
   410  | 
    type pos = ParserData.pos
  | 
| 
 | 
   411  | 
    type result = ParserData.result
  | 
| 
 | 
   412  | 
    type svalue = ParserData.svalue
  | 
| 
 | 
   413  | 
  | 
| 
 | 
   414  | 
    val makeLexer = fn s => fn arg =>
  | 
| 
 | 
   415  | 
		 LrParser.Stream.streamify (Lex.makeLexer s arg)
  | 
| 
 | 
   416  | 
    val parse = fn (lookahead,lexer,error,arg) =>
  | 
| 
 | 
   417  | 
	(fn (a,b) => (ParserData.Actions.extract a,b))
  | 
| 
 | 
   418  | 
     (LrParser.parse {table = ParserData.table,
 | 
| 
 | 
   419  | 
	        lexer=lexer,
  | 
| 
 | 
   420  | 
		lookahead=lookahead,
  | 
| 
 | 
   421  | 
		saction = ParserData.Actions.actions,
  | 
| 
 | 
   422  | 
		arg=arg,
  | 
| 
 | 
   423  | 
		void= ParserData.Actions.void,
  | 
| 
 | 
   424  | 
	        ec = {is_keyword = ParserData.EC.is_keyword,
 | 
| 
 | 
   425  | 
		      noShift = ParserData.EC.noShift,
  | 
| 
 | 
   426  | 
		      preferred_change = ParserData.EC.preferred_change,
  | 
| 
 | 
   427  | 
		      errtermvalue = ParserData.EC.errtermvalue,
  | 
| 
 | 
   428  | 
		      error=error,
  | 
| 
 | 
   429  | 
		      showTerminal = ParserData.EC.showTerminal,
  | 
| 
 | 
   430  | 
		      terms = ParserData.EC.terms}}
  | 
| 
 | 
   431  | 
      )
  | 
| 
 | 
   432  | 
    val sameToken = Token.sameToken
  | 
| 
 | 
   433  | 
end;
  | 
| 
 | 
   434  | 
  | 
| 
 | 
   435  | 
(**** Original file: lrtable.sml ****)
  | 
| 
 | 
   436  | 
  | 
| 
 | 
   437  | 
(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi *)
  | 
| 
 | 
   438  | 
structure LrTable : LR_TABLE = 
  | 
| 
 | 
   439  | 
    struct
  | 
| 
 | 
   440  | 
	open Array List
  | 
| 
 | 
   441  | 
	infix 9 sub
  | 
| 
 | 
   442  | 
	datatype ('a,'b) pairlist = EMPTY
 | 
| 
 | 
   443  | 
				  | PAIR of 'a * 'b * ('a,'b) pairlist
 | 
| 
 | 
   444  | 
	datatype term = T of int
  | 
| 
 | 
   445  | 
	datatype nonterm = NT of int
  | 
| 
 | 
   446  | 
	datatype state = STATE of int
  | 
| 
 | 
   447  | 
	datatype action = SHIFT of state
  | 
| 
 | 
   448  | 
			| REDUCE of int (* rulenum from grammar *)
  | 
| 
 | 
   449  | 
			| ACCEPT
  | 
| 
 | 
   450  | 
			| ERROR
  | 
| 
 | 
   451  | 
	exception Goto of state * nonterm
  | 
| 
 | 
   452  | 
	type table = {states: int, rules : int,initialState: state,
 | 
| 
 | 
   453  | 
		      action: ((term,action) pairlist * action) array,
  | 
| 
 | 
   454  | 
		      goto :  (nonterm,state) pairlist array}
  | 
| 
 | 
   455  | 
	val numStates = fn ({states,...} : table) => states
 | 
| 
 | 
   456  | 
	val numRules = fn ({rules,...} : table) => rules
 | 
| 
 | 
   457  | 
	val describeActions =
  | 
| 
 | 
   458  | 
	   fn ({action,...} : table) => 
 | 
| 
 | 
   459  | 
	           fn (STATE s) => action sub s
  | 
| 
 | 
   460  | 
	val describeGoto =
  | 
| 
 | 
   461  | 
	   fn ({goto,...} : table) =>
 | 
| 
 | 
   462  | 
	           fn (STATE s) => goto sub s
  | 
| 
 | 
   463  | 
	fun findTerm (T term,row,default) =
  | 
| 
 | 
   464  | 
	    let fun find (PAIR (T key,data,r)) =
  | 
| 
 | 
   465  | 
		       if key < term then find r
  | 
| 
 | 
   466  | 
		       else if key=term then data
  | 
| 
 | 
   467  | 
		       else default
  | 
| 
 | 
   468  | 
		   | find EMPTY = default
  | 
| 
 | 
   469  | 
	    in find row
  | 
| 
 | 
   470  | 
	    end
  | 
| 
 | 
   471  | 
	fun findNonterm (NT nt,row) =
  | 
| 
 | 
   472  | 
	    let fun find (PAIR (NT key,data,r)) =
  | 
| 
 | 
   473  | 
		       if key < nt then find r
  | 
| 
 | 
   474  | 
		       else if key=nt then SOME data
  | 
| 
 | 
   475  | 
		       else NONE
  | 
| 
 | 
   476  | 
		   | find EMPTY = NONE
  | 
| 
 | 
   477  | 
	    in find row
  | 
| 
 | 
   478  | 
	    end
  | 
| 
 | 
   479  | 
	val action = fn ({action,...} : table) =>
 | 
| 
 | 
   480  | 
		fn (STATE state,term) =>
  | 
| 
 | 
   481  | 
		  let val (row,default) = action sub state
  | 
| 
 | 
   482  | 
		  in findTerm(term,row,default)
  | 
| 
 | 
   483  | 
		  end
  | 
| 
 | 
   484  | 
	val goto = fn ({goto,...} : table) =>
 | 
| 
 | 
   485  | 
			fn (a as (STATE state,nonterm)) =>
  | 
| 
 | 
   486  | 
			  case findNonterm(nonterm,goto sub state)
  | 
| 
 | 
   487  | 
			  of SOME state => state
  | 
| 
 | 
   488  | 
			   | NONE => raise (Goto a)
  | 
| 
 | 
   489  | 
	val initialState = fn ({initialState,...} : table) => initialState
 | 
| 
 | 
   490  | 
	val mkLrTable = fn {actions,gotos,initialState,numStates,numRules} =>
 | 
| 
 | 
   491  | 
	     ({action=actions,goto=gotos,
 | 
| 
 | 
   492  | 
	       states=numStates,
  | 
| 
 | 
   493  | 
	       rules=numRules,
  | 
| 
 | 
   494  | 
               initialState=initialState} : table)
  | 
| 
 | 
   495  | 
end;
  | 
| 
 | 
   496  | 
  | 
| 
 | 
   497  | 
(**** Original file: stream.sml ****)
  | 
| 
 | 
   498  | 
  | 
| 
 | 
   499  | 
(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi *)
  | 
| 
 | 
   500  | 
  | 
| 
 | 
   501  | 
(* Stream: a structure implementing a lazy stream.  The signature STREAM
  | 
| 
 | 
   502  | 
   is found in base.sig *)
  | 
| 
 | 
   503  | 
  | 
| 
 | 
   504  | 
structure Stream :> STREAM =
  | 
| 
 | 
   505  | 
struct
  | 
| 
 | 
   506  | 
   datatype 'a str = EVAL of 'a * 'a str Unsynchronized.ref | UNEVAL of (unit->'a)
  | 
| 
 | 
   507  | 
  | 
| 
 | 
   508  | 
   type 'a stream = 'a str Unsynchronized.ref
  | 
| 
 | 
   509  | 
  | 
| 
 | 
   510  | 
   fun get(Unsynchronized.ref(EVAL t)) = t
  | 
| 
 | 
   511  | 
     | get(s as Unsynchronized.ref(UNEVAL f)) = 
  | 
| 
 | 
   512  | 
	    let val t = (f(), Unsynchronized.ref(UNEVAL f)) in s := EVAL t; t end
  | 
| 
 | 
   513  | 
  | 
| 
 | 
   514  | 
   fun streamify f = Unsynchronized.ref(UNEVAL f)
  | 
| 
 | 
   515  | 
   fun cons(a,s) = Unsynchronized.ref(EVAL(a,s))
  | 
| 
 | 
   516  | 
  | 
| 
 | 
   517  | 
end;
  | 
| 
 | 
   518  | 
  | 
| 
 | 
   519  | 
(**** Original file: parser2.sml ****)
  | 
| 
 | 
   520  | 
  | 
| 
 | 
   521  | 
(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi *)
  | 
| 
 | 
   522  | 
  | 
| 
 | 
   523  | 
(* parser.sml:  This is a parser driver for LR tables with an error-recovery
  | 
| 
 | 
   524  | 
   routine added to it.  The routine used is described in detail in this
  | 
| 
 | 
   525  | 
   article:
  | 
| 
 | 
   526  | 
  | 
| 
 | 
   527  | 
	'A Practical Method for LR and LL Syntactic Error Diagnosis and
  | 
| 
 | 
   528  | 
	 Recovery', by M. Burke and G. Fisher, ACM Transactions on
  | 
| 
 | 
   529  | 
	 Programming Langauges and Systems, Vol. 9, No. 2, April 1987,
  | 
| 
 | 
   530  | 
	 pp. 164-197.
  | 
| 
 | 
   531  | 
  | 
| 
 | 
   532  | 
    This program is an implementation is the partial, deferred method discussed
  | 
| 
 | 
   533  | 
    in the article.  The algorithm and data structures used in the program
  | 
| 
 | 
   534  | 
    are described below.  
  | 
| 
 | 
   535  | 
  | 
| 
 | 
   536  | 
    This program assumes that all semantic actions are delayed.  A semantic
  | 
| 
 | 
   537  | 
    action should produce a function from unit -> value instead of producing the
  | 
| 
 | 
   538  | 
    normal value.  The parser returns the semantic value on the top of the
  | 
| 
 | 
   539  | 
    stack when accept is encountered.  The user can deconstruct this value
  | 
| 
 | 
   540  | 
    and apply the unit -> value function in it to get the answer.
  | 
| 
 | 
   541  | 
  | 
| 
 | 
   542  | 
    It also assumes that the lexer is a lazy stream.
  | 
| 
 | 
   543  | 
  | 
| 
 | 
   544  | 
    Data Structures:
  | 
| 
 | 
   545  | 
    ----------------
  | 
| 
 | 
   546  | 
	
  | 
| 
 | 
   547  | 
	* The parser:
  | 
| 
 | 
   548  | 
  | 
| 
 | 
   549  | 
	   The state stack has the type
  | 
| 
 | 
   550  | 
  | 
| 
 | 
   551  | 
		 (state * (semantic value * line # * line #)) list
  | 
| 
 | 
   552  | 
  | 
| 
 | 
   553  | 
	   The parser keeps a queue of (state stack * lexer pair).  A lexer pair
  | 
| 
 | 
   554  | 
	 consists of a terminal * value pair and a lexer.  This allows the 
  | 
| 
 | 
   555  | 
	 parser to reconstruct the states for terminals to the left of a
  | 
| 
 | 
   556  | 
	 syntax error, and attempt to make error corrections there.
  | 
| 
 | 
   557  | 
  | 
| 
 | 
   558  | 
	   The queue consists of a pair of lists (x,y).  New additions to
  | 
| 
 | 
   559  | 
	 the queue are cons'ed onto y.  The first element of x is the top
  | 
| 
 | 
   560  | 
	 of the queue.  If x is nil, then y is reversed and used
  | 
| 
 | 
   561  | 
	 in place of x.
  | 
| 
 | 
   562  | 
  | 
| 
 | 
   563  | 
    Algorithm:
  | 
| 
 | 
   564  | 
    ----------
  | 
| 
 | 
   565  | 
  | 
| 
 | 
   566  | 
	* The steady-state parser:  
  | 
| 
 | 
   567  | 
  | 
| 
 | 
   568  | 
	    This parser keeps the length of the queue of state stacks at
  | 
| 
 | 
   569  | 
	a steady state by always removing an element from the front when
  | 
| 
 | 
   570  | 
	another element is placed on the end.
  | 
| 
 | 
   571  | 
  | 
| 
 | 
   572  | 
	    It has these arguments:
  | 
| 
 | 
   573  | 
  | 
| 
 | 
   574  | 
	   stack: current stack
  | 
| 
 | 
   575  | 
	   queue: value of the queue
  | 
| 
 | 
   576  | 
	   lexPair ((terminal,value),lex stream)
  | 
| 
 | 
   577  | 
  | 
| 
 | 
   578  | 
	When SHIFT is encountered, the state to shift to and the value are
  | 
| 
 | 
   579  | 
	are pushed onto the state stack.  The state stack and lexPair are
  | 
| 
 | 
   580  | 
	placed on the queue.  The front element of the queue is removed.
  | 
| 
 | 
   581  | 
  | 
| 
 | 
   582  | 
	When REDUCTION is encountered, the rule is applied to the current
  | 
| 
 | 
   583  | 
	stack to yield a triple (nonterm,value,new stack).  A new
  | 
| 
 | 
   584  | 
	stack is formed by adding (goto(top state of stack,nonterm),value)
  | 
| 
 | 
   585  | 
	to the stack.
  | 
| 
 | 
   586  | 
  | 
| 
 | 
   587  | 
	When ACCEPT is encountered, the top value from the stack and the
  | 
| 
 | 
   588  | 
	lexer are returned.
  | 
| 
 | 
   589  | 
  | 
| 
 | 
   590  | 
	When an ERROR is encountered, fixError is called.  FixError
  | 
| 
 | 
   591  | 
	takes the arguments to the parser, fixes the error if possible and
  | 
| 
 | 
   592  | 
        returns a new set of arguments.
  | 
| 
 | 
   593  | 
  | 
| 
 | 
   594  | 
	* The distance-parser:
  | 
| 
 | 
   595  | 
  | 
| 
 | 
   596  | 
	This parser includes an additional argument distance.  It pushes
  | 
| 
 | 
   597  | 
	elements on the queue until it has parsed distance tokens, or an
  | 
| 
 | 
   598  | 
	ACCEPT or ERROR occurs.  It returns a stack, lexer, the number of
  | 
| 
 | 
   599  | 
	tokens left unparsed, a queue, and an action option.
  | 
| 
 | 
   600  | 
*)
  | 
| 
 | 
   601  | 
  | 
| 
 | 
   602  | 
signature FIFO = 
  | 
| 
 | 
   603  | 
  sig type 'a queue
  | 
| 
 | 
   604  | 
      val empty : 'a queue
  | 
| 
 | 
   605  | 
      exception Empty
  | 
| 
 | 
   606  | 
      val get : 'a queue -> 'a * 'a queue
  | 
| 
 | 
   607  | 
      val put : 'a * 'a queue -> 'a queue
  | 
| 
 | 
   608  | 
  end
  | 
| 
 | 
   609  | 
  | 
| 
 | 
   610  | 
(* drt (12/15/89) -- the functor should be used in development work, but
  | 
| 
 | 
   611  | 
   it wastes space in the release version.
  | 
| 
 | 
   612  | 
  | 
| 
 | 
   613  | 
functor ParserGen(structure LrTable : LR_TABLE
  | 
| 
 | 
   614  | 
		  structure Stream : STREAM) : LR_PARSER =
  | 
| 
 | 
   615  | 
*)
  | 
| 
 | 
   616  | 
  | 
| 
 | 
   617  | 
structure LrParser :> LR_PARSER =
  | 
| 
 | 
   618  | 
   struct
  | 
| 
 | 
   619  | 
      structure LrTable = LrTable
  | 
| 
 | 
   620  | 
      structure Stream = Stream
  | 
| 
 | 
   621  | 
  | 
| 
 | 
   622  | 
      fun eqT (LrTable.T i, LrTable.T i') = i = i'
  | 
| 
 | 
   623  | 
  | 
| 
 | 
   624  | 
      structure Token : TOKEN =
  | 
| 
 | 
   625  | 
	struct
  | 
| 
 | 
   626  | 
	    structure LrTable = LrTable
  | 
| 
 | 
   627  | 
	    datatype ('a,'b) token = TOKEN of LrTable.term * ('a * 'b * 'b)
 | 
| 
 | 
   628  | 
	    val sameToken = fn (TOKEN(t,_),TOKEN(t',_)) => eqT (t,t')
  | 
| 
 | 
   629  | 
        end
  | 
| 
 | 
   630  | 
  | 
| 
 | 
   631  | 
      open LrTable
  | 
| 
 | 
   632  | 
      open Token
  | 
| 
 | 
   633  | 
  | 
| 
 | 
   634  | 
      val DEBUG1 = false
  | 
| 
 | 
   635  | 
      val DEBUG2 = false
  | 
| 
 | 
   636  | 
      exception ParseError
  | 
| 
 | 
   637  | 
      exception ParseImpossible of int
  | 
| 
 | 
   638  | 
  | 
| 
 | 
   639  | 
      structure Fifo :> FIFO =
  | 
| 
 | 
   640  | 
        struct
  | 
| 
 | 
   641  | 
	  type 'a queue = ('a list * 'a list)
 | 
| 
 | 
   642  | 
	  val empty = (nil,nil)
  | 
| 
 | 
   643  | 
	  exception Empty
  | 
| 
 | 
   644  | 
	  fun get(a::x, y) = (a, (x,y))
  | 
| 
 | 
   645  | 
	    | get(nil, nil) = raise Empty
  | 
| 
 | 
   646  | 
	    | get(nil, y) = get(rev y, nil)
  | 
| 
 | 
   647  | 
 	  fun put(a,(x,y)) = (x,a::y)
  | 
| 
 | 
   648  | 
        end
  | 
| 
 | 
   649  | 
  | 
| 
 | 
   650  | 
      type ('a,'b) elem = (state * ('a * 'b * 'b))
 | 
| 
 | 
   651  | 
      type ('a,'b) stack = ('a,'b) elem list
 | 
| 
 | 
   652  | 
      type ('a,'b) lexv = ('a,'b) token
 | 
| 
 | 
   653  | 
      type ('a,'b) lexpair = ('a,'b) lexv * (('a,'b) lexv Stream.stream)
 | 
| 
 | 
   654  | 
      type ('a,'b) distanceParse =
 | 
| 
 | 
   655  | 
		 ('a,'b) lexpair *
 | 
| 
 | 
   656  | 
		 ('a,'b) stack * 
 | 
| 
 | 
   657  | 
		 (('a,'b) stack * ('a,'b) lexpair) Fifo.queue *
 | 
| 
 | 
   658  | 
		 int ->
  | 
| 
 | 
   659  | 
		   ('a,'b) lexpair *
 | 
| 
 | 
   660  | 
		   ('a,'b) stack * 
 | 
| 
 | 
   661  | 
		   (('a,'b) stack * ('a,'b) lexpair) Fifo.queue *
 | 
| 
 | 
   662  | 
		   int *
  | 
| 
 | 
   663  | 
		   action option
  | 
| 
 | 
   664  | 
  | 
| 
 | 
   665  | 
      type ('a,'b) ecRecord =
 | 
| 
 | 
   666  | 
	 {is_keyword : term -> bool,
 | 
| 
 | 
   667  | 
          preferred_change : (term list * term list) list,
  | 
| 
 | 
   668  | 
	  error : string * 'b * 'b -> unit,
  | 
| 
 | 
   669  | 
	  errtermvalue : term -> 'a,
  | 
| 
 | 
   670  | 
	  terms : term list,
  | 
| 
 | 
   671  | 
	  showTerminal : term -> string,
  | 
| 
 | 
   672  | 
	  noShift : term -> bool}
  | 
| 
 | 
   673  | 
  | 
| 
 | 
   674  | 
      local 
  | 
| 
 | 
   675  | 
	 
  | 
| 
 | 
   676  | 
	 val println = fn s => (TextIO.print s; TextIO.print "\n")
  | 
| 
 | 
   677  | 
	 val showState = fn (STATE s) => "STATE " ^ (Int.toString s)
  | 
| 
 | 
   678  | 
      in
  | 
| 
 | 
   679  | 
        fun printStack(stack: ('a,'b) stack, n: int) =
 | 
| 
 | 
   680  | 
         case stack
  | 
| 
 | 
   681  | 
           of (state,_) :: rest =>
  | 
| 
 | 
   682  | 
                 (TextIO.print("\t" ^ Int.toString n ^ ": ");
 | 
| 
 | 
   683  | 
                  println(showState state);
  | 
| 
 | 
   684  | 
                  printStack(rest, n+1))
  | 
| 
 | 
   685  | 
            | nil => ()
  | 
| 
 | 
   686  | 
                
  | 
| 
 | 
   687  | 
        fun prAction showTerminal
  | 
| 
 | 
   688  | 
		 (stack as (state,_) :: _, next as (TOKEN (term,_),_), action) =
  | 
| 
 | 
   689  | 
             (println "Parse: state stack:";
  | 
| 
 | 
   690  | 
              printStack(stack, 0);
  | 
| 
 | 
   691  | 
              TextIO.print("       state="
 | 
| 
 | 
   692  | 
                         ^ showState state	
  | 
| 
 | 
   693  | 
                         ^ " next="
  | 
| 
 | 
   694  | 
                         ^ showTerminal term
  | 
| 
 | 
   695  | 
                         ^ " action="
  | 
| 
 | 
   696  | 
                        );
  | 
| 
 | 
   697  | 
              case action
  | 
| 
 | 
   698  | 
                of SHIFT state => println ("SHIFT " ^ (showState state))
 | 
| 
 | 
   699  | 
                 | REDUCE i => println ("REDUCE " ^ (Int.toString i))
 | 
| 
 | 
   700  | 
                 | ERROR => println "ERROR"
  | 
| 
 | 
   701  | 
		 | ACCEPT => println "ACCEPT")
  | 
| 
 | 
   702  | 
        | prAction _ (_,_,action) = ()
  | 
| 
 | 
   703  | 
     end
  | 
| 
 | 
   704  | 
  | 
| 
 | 
   705  | 
    (* ssParse: parser which maintains the queue of (state * lexvalues) in a
  | 
| 
 | 
   706  | 
	steady-state.  It takes a table, showTerminal function, saction
  | 
| 
 | 
   707  | 
	function, and fixError function.  It parses until an ACCEPT is
  | 
| 
 | 
   708  | 
	encountered, or an exception is raised.  When an error is encountered,
  | 
| 
 | 
   709  | 
	fixError is called with the arguments of parseStep (lexv,stack,and
  | 
| 
 | 
   710  | 
	queue).  It returns the lexv, and a new stack and queue adjusted so
  | 
| 
 | 
   711  | 
	that the lexv can be parsed *)
  | 
| 
 | 
   712  | 
	
  | 
| 
 | 
   713  | 
    val ssParse =
  | 
| 
 | 
   714  | 
      fn (table,showTerminal,saction,fixError,arg) =>
  | 
| 
 | 
   715  | 
	let val prAction = prAction showTerminal
  | 
| 
 | 
   716  | 
	    val action = LrTable.action table
  | 
| 
 | 
   717  | 
	    val goto = LrTable.goto table
  | 
| 
 | 
   718  | 
	    fun parseStep(args as
  | 
| 
 | 
   719  | 
			 (lexPair as (TOKEN (terminal, value as (_,leftPos,_)),
  | 
| 
 | 
   720  | 
				      lexer
  | 
| 
 | 
   721  | 
				      ),
  | 
| 
 | 
   722  | 
			  stack as (state,_) :: _,
  | 
| 
 | 
   723  | 
			  queue)) =
  | 
| 
 | 
   724  | 
	      let val nextAction = action (state,terminal)
  | 
| 
 | 
   725  | 
	          val _ = if DEBUG1 then prAction(stack,lexPair,nextAction)
  | 
| 
 | 
   726  | 
			  else ()
  | 
| 
 | 
   727  | 
	      in case nextAction
  | 
| 
 | 
   728  | 
		 of SHIFT s =>
  | 
| 
 | 
   729  | 
		  let val newStack = (s,value) :: stack
  | 
| 
 | 
   730  | 
		      val newLexPair = Stream.get lexer
  | 
| 
 | 
   731  | 
		      val (_,newQueue) =Fifo.get(Fifo.put((newStack,newLexPair),
  | 
| 
 | 
   732  | 
							    queue))
  | 
| 
 | 
   733  | 
		  in parseStep(newLexPair,(s,value)::stack,newQueue)
  | 
| 
 | 
   734  | 
		  end
  | 
| 
 | 
   735  | 
		 | REDUCE i =>
  | 
| 
 | 
   736  | 
		     (case saction(i,leftPos,stack,arg)
  | 
| 
 | 
   737  | 
		      of (nonterm,value,stack as (state,_) :: _) =>
  | 
| 
 | 
   738  | 
		          parseStep(lexPair,(goto(state,nonterm),value)::stack,
  | 
| 
 | 
   739  | 
				    queue)
  | 
| 
 | 
   740  | 
		       | _ => raise (ParseImpossible 197))
  | 
| 
 | 
   741  | 
		 | ERROR => parseStep(fixError args)
  | 
| 
 | 
   742  | 
		 | ACCEPT => 
  | 
| 
 | 
   743  | 
			(case stack
  | 
| 
 | 
   744  | 
			 of (_,(topvalue,_,_)) :: _ =>
  | 
| 
 | 
   745  | 
				let val (token,restLexer) = lexPair
  | 
| 
 | 
   746  | 
				in (topvalue,Stream.cons(token,restLexer))
  | 
| 
 | 
   747  | 
				end
  | 
| 
 | 
   748  | 
			  | _ => raise (ParseImpossible 202))
  | 
| 
 | 
   749  | 
	      end
  | 
| 
 | 
   750  | 
	    | parseStep _ = raise (ParseImpossible 204)
  | 
| 
 | 
   751  | 
	in parseStep
  | 
| 
 | 
   752  | 
	end
  | 
| 
 | 
   753  | 
  | 
| 
 | 
   754  | 
    (*  distanceParse: parse until n tokens are shifted, or accept or
  | 
| 
 | 
   755  | 
	error are encountered.  Takes a table, showTerminal function, and
  | 
| 
 | 
   756  | 
	semantic action function.  Returns a parser which takes a lexPair
  | 
| 
 | 
   757  | 
	(lex result * lexer), a state stack, a queue, and a distance
  | 
| 
 | 
   758  | 
	(must be > 0) to parse.  The parser returns a new lex-value, a stack
  | 
| 
 | 
   759  | 
	with the nth token shifted on top, a queue, a distance, and action
  | 
| 
 | 
   760  | 
	option. *)
  | 
| 
 | 
   761  | 
  | 
| 
 | 
   762  | 
    val distanceParse =
  | 
| 
 | 
   763  | 
      fn (table,showTerminal,saction,arg) =>
  | 
| 
 | 
   764  | 
	let val prAction = prAction showTerminal
  | 
| 
 | 
   765  | 
	    val action = LrTable.action table
  | 
| 
 | 
   766  | 
	    val goto = LrTable.goto table
  | 
| 
 | 
   767  | 
	    fun parseStep(lexPair,stack,queue,0) = (lexPair,stack,queue,0,NONE)
  | 
| 
 | 
   768  | 
	      | parseStep(lexPair as (TOKEN (terminal, value as (_,leftPos,_)),
  | 
| 
 | 
   769  | 
				      lexer
  | 
| 
 | 
   770  | 
				     ),
  | 
| 
 | 
   771  | 
			  stack as (state,_) :: _,
  | 
| 
 | 
   772  | 
			  queue,distance) =
  | 
| 
 | 
   773  | 
	      let val nextAction = action(state,terminal)
  | 
| 
 | 
   774  | 
	          val _ = if DEBUG1 then prAction(stack,lexPair,nextAction)
  | 
| 
 | 
   775  | 
			  else ()
  | 
| 
 | 
   776  | 
	      in case nextAction
  | 
| 
 | 
   777  | 
		 of SHIFT s =>
  | 
| 
 | 
   778  | 
		  let val newStack = (s,value) :: stack
  | 
| 
 | 
   779  | 
		      val newLexPair = Stream.get lexer
  | 
| 
 | 
   780  | 
		  in parseStep(newLexPair,(s,value)::stack,
  | 
| 
 | 
   781  | 
			       Fifo.put((newStack,newLexPair),queue),distance-1)
  | 
| 
 | 
   782  | 
		  end
  | 
| 
 | 
   783  | 
		 | REDUCE i =>
  | 
| 
 | 
   784  | 
		    (case saction(i,leftPos,stack,arg)
  | 
| 
 | 
   785  | 
		      of (nonterm,value,stack as (state,_) :: _) =>
  | 
| 
 | 
   786  | 
		         parseStep(lexPair,(goto(state,nonterm),value)::stack,
  | 
| 
 | 
   787  | 
				 queue,distance)
  | 
| 
 | 
   788  | 
		      | _ => raise (ParseImpossible 240))
  | 
| 
 | 
   789  | 
		 | ERROR => (lexPair,stack,queue,distance,SOME nextAction)
  | 
| 
 | 
   790  | 
		 | ACCEPT => (lexPair,stack,queue,distance,SOME nextAction)
  | 
| 
 | 
   791  | 
	      end
  | 
| 
 | 
   792  | 
	   | parseStep _ = raise (ParseImpossible 242)
  | 
| 
 | 
   793  | 
	in parseStep : ('_a,'_b) distanceParse 
 | 
| 
 | 
   794  | 
	end
  | 
| 
 | 
   795  | 
  | 
| 
 | 
   796  | 
(* mkFixError: function to create fixError function which adjusts parser state
  | 
| 
 | 
   797  | 
   so that parse may continue in the presence of an error *)
  | 
| 
 | 
   798  | 
  | 
| 
 | 
   799  | 
fun mkFixError({is_keyword,terms,errtermvalue,
 | 
| 
 | 
   800  | 
	      preferred_change,noShift,
  | 
| 
 | 
   801  | 
	      showTerminal,error,...} : ('_a,'_b) ecRecord,
 | 
| 
 | 
   802  | 
	     distanceParse : ('_a,'_b) distanceParse,
 | 
| 
 | 
   803  | 
	     minAdvance,maxAdvance) 
  | 
| 
 | 
   804  | 
  | 
| 
 | 
   805  | 
            (lexv as (TOKEN (term,value as (_,leftPos,_)),_),stack,queue) =
  | 
| 
 | 
   806  | 
    let val _ = if DEBUG2 then
  | 
| 
 | 
   807  | 
			error("syntax error found at " ^ (showTerminal term),
 | 
| 
 | 
   808  | 
			      leftPos,leftPos)
  | 
| 
 | 
   809  | 
		else ()
  | 
| 
 | 
   810  | 
  | 
| 
 | 
   811  | 
        fun tokAt(t,p) = TOKEN(t,(errtermvalue t,p,p))
  | 
| 
 | 
   812  | 
  | 
| 
 | 
   813  | 
	val minDelta = 3
  | 
| 
 | 
   814  | 
  | 
| 
 | 
   815  | 
	(* pull all the state * lexv elements from the queue *)
  | 
| 
 | 
   816  | 
  | 
| 
 | 
   817  | 
	val stateList = 
  | 
| 
 | 
   818  | 
	   let fun f q = let val (elem,newQueue) = Fifo.get q
  | 
| 
 | 
   819  | 
			 in elem :: (f newQueue)
  | 
| 
 | 
   820  | 
			 end handle Fifo.Empty => nil
  | 
| 
 | 
   821  | 
	   in f queue
  | 
| 
 | 
   822  | 
	   end
  | 
| 
 | 
   823  | 
  | 
| 
 | 
   824  | 
	(* now number elements of stateList, giving distance from
  | 
| 
 | 
   825  | 
	   error token *)
  | 
| 
 | 
   826  | 
  | 
| 
 | 
   827  | 
	val (_, numStateList) =
  | 
| 
 | 
   828  | 
	      List.foldr (fn (a,(num,r)) => (num+1,(a,num)::r)) (0, []) stateList
  | 
| 
 | 
   829  | 
  | 
| 
 | 
   830  | 
	(* Represent the set of potential changes as a linked list.
  | 
| 
 | 
   831  | 
  | 
| 
 | 
   832  | 
	   Values of datatype Change hold information about a potential change.
  | 
| 
 | 
   833  | 
  | 
| 
 | 
   834  | 
	   oper = oper to be applied
  | 
| 
 | 
   835  | 
	   pos = the # of the element in stateList that would be altered.
  | 
| 
 | 
   836  | 
	   distance = the number of tokens beyond the error token which the
  | 
| 
 | 
   837  | 
	     change allows us to parse.
  | 
| 
 | 
   838  | 
	   new = new terminal * value pair at that point
  | 
| 
 | 
   839  | 
	   orig = original terminal * value pair at the point being changed.
  | 
| 
 | 
   840  | 
	 *)
  | 
| 
 | 
   841  | 
  | 
| 
 | 
   842  | 
	datatype ('a,'b) change = CHANGE of
 | 
| 
 | 
   843  | 
	   {pos : int, distance : int, leftPos: 'b, rightPos: 'b,
 | 
| 
 | 
   844  | 
	    new : ('a,'b) lexv list, orig : ('a,'b) lexv list}
 | 
| 
 | 
   845  | 
  | 
| 
 | 
   846  | 
  | 
| 
 | 
   847  | 
         val showTerms = String.concat o map (fn TOKEN(t,_) => " " ^ showTerminal t)
  | 
| 
 | 
   848  | 
  | 
| 
 | 
   849  | 
	 val printChange = fn c =>
  | 
| 
 | 
   850  | 
	  let val CHANGE {distance,new,orig,pos,...} = c
 | 
| 
 | 
   851  | 
	  in (TextIO.print ("{distance= " ^ (Int.toString distance));
 | 
| 
 | 
   852  | 
	      TextIO.print (",orig ="); TextIO.print(showTerms orig);
 | 
| 
 | 
   853  | 
	      TextIO.print (",new ="); TextIO.print(showTerms new);
 | 
| 
 | 
   854  | 
	      TextIO.print (",pos= " ^ (Int.toString pos));
 | 
| 
 | 
   855  | 
	      TextIO.print "}\n")
  | 
| 
 | 
   856  | 
	  end
  | 
| 
 | 
   857  | 
  | 
| 
 | 
   858  | 
	val printChangeList = app printChange
  | 
| 
 | 
   859  | 
  | 
| 
 | 
   860  | 
(* parse: given a lexPair, a stack, and the distance from the error
  | 
| 
 | 
   861  | 
   token, return the distance past the error token that we are able to parse.*)
  | 
| 
 | 
   862  | 
  | 
| 
 | 
   863  | 
	fun parse (lexPair,stack,queuePos : int) =
  | 
| 
 | 
   864  | 
	    case distanceParse(lexPair,stack,Fifo.empty,queuePos+maxAdvance+1)
  | 
| 
 | 
   865  | 
             of (_,_,_,distance,SOME ACCEPT) => 
  | 
| 
 | 
   866  | 
		        if maxAdvance-distance-1 >= 0 
  | 
| 
 | 
   867  | 
			    then maxAdvance 
  | 
| 
 | 
   868  | 
			    else maxAdvance-distance-1
  | 
| 
 | 
   869  | 
	      | (_,_,_,distance,_) => maxAdvance - distance - 1
  | 
| 
 | 
   870  | 
  | 
| 
 | 
   871  | 
(* catList: concatenate results of scanning list *)
  | 
| 
 | 
   872  | 
  | 
| 
 | 
   873  | 
	fun catList l f = List.foldr (fn(a,r)=> f a @ r) [] l
  | 
| 
 | 
   874  | 
  | 
| 
 | 
   875  | 
        fun keywordsDelta new = if List.exists (fn(TOKEN(t,_))=>is_keyword t) new
  | 
| 
 | 
   876  | 
	               then minDelta else 0
  | 
| 
 | 
   877  | 
  | 
| 
 | 
   878  | 
        fun tryChange{lex,stack,pos,leftPos,rightPos,orig,new} =
 | 
| 
 | 
   879  | 
	     let val lex' = List.foldr (fn (t',p)=>(t',Stream.cons p)) lex new
  | 
| 
 | 
   880  | 
		 val distance = parse(lex',stack,pos+length new-length orig)
  | 
| 
 | 
   881  | 
	      in if distance >= minAdvance + keywordsDelta new 
  | 
| 
 | 
   882  | 
		   then [CHANGE{pos=pos,leftPos=leftPos,rightPos=rightPos,
 | 
| 
 | 
   883  | 
				distance=distance,orig=orig,new=new}] 
  | 
| 
 | 
   884  | 
		   else []
  | 
| 
 | 
   885  | 
	     end
  | 
| 
 | 
   886  | 
  | 
| 
 | 
   887  | 
  | 
| 
 | 
   888  | 
(* tryDelete: Try to delete n terminals.
  | 
| 
 | 
   889  | 
              Return single-element [success] or nil.
  | 
| 
 | 
   890  | 
	      Do not delete unshiftable terminals. *)
  | 
| 
 | 
   891  | 
  | 
| 
 | 
   892  | 
  | 
| 
 | 
   893  | 
    fun tryDelete n ((stack,lexPair as (TOKEN(term,(_,l,r)),_)),qPos) =
  | 
| 
 | 
   894  | 
	let fun del(0,accum,left,right,lexPair) =
  | 
| 
 | 
   895  | 
	          tryChange{lex=lexPair,stack=stack,
 | 
| 
 | 
   896  | 
			    pos=qPos,leftPos=left,rightPos=right,
  | 
| 
 | 
   897  | 
			    orig=rev accum, new=[]}
  | 
| 
 | 
   898  | 
	      | del(n,accum,left,right,(tok as TOKEN(term,(_,_,r)),lexer)) =
  | 
| 
 | 
   899  | 
		   if noShift term then []
  | 
| 
 | 
   900  | 
		   else del(n-1,tok::accum,left,r,Stream.get lexer)
  | 
| 
 | 
   901  | 
         in del(n,[],l,r,lexPair)
  | 
| 
 | 
   902  | 
        end
  | 
| 
 | 
   903  | 
  | 
| 
 | 
   904  | 
(* tryInsert: try to insert tokens before the current terminal;
  | 
| 
 | 
   905  | 
       return a list of the successes  *)
  | 
| 
 | 
   906  | 
  | 
| 
 | 
   907  | 
        fun tryInsert((stack,lexPair as (TOKEN(_,(_,l,_)),_)),queuePos) =
  | 
| 
 | 
   908  | 
	       catList terms (fn t =>
  | 
| 
 | 
   909  | 
		 tryChange{lex=lexPair,stack=stack,
 | 
| 
 | 
   910  | 
			   pos=queuePos,orig=[],new=[tokAt(t,l)],
  | 
| 
 | 
   911  | 
			   leftPos=l,rightPos=l})
  | 
| 
 | 
   912  | 
			      
  | 
| 
 | 
   913  | 
(* trySubst: try to substitute tokens for the current terminal;
  | 
| 
 | 
   914  | 
       return a list of the successes  *)
  | 
| 
 | 
   915  | 
  | 
| 
 | 
   916  | 
        fun trySubst ((stack,lexPair as (orig as TOKEN (term,(_,l,r)),lexer)),
  | 
| 
 | 
   917  | 
		      queuePos) =
  | 
| 
 | 
   918  | 
	      if noShift term then []
  | 
| 
 | 
   919  | 
	      else
  | 
| 
 | 
   920  | 
		  catList terms (fn t =>
  | 
| 
 | 
   921  | 
		      tryChange{lex=Stream.get lexer,stack=stack,
 | 
| 
 | 
   922  | 
				pos=queuePos,
  | 
| 
 | 
   923  | 
				leftPos=l,rightPos=r,orig=[orig],
  | 
| 
 | 
   924  | 
				new=[tokAt(t,r)]})
  | 
| 
 | 
   925  | 
  | 
| 
 | 
   926  | 
     (* do_delete(toks,lexPair) tries to delete tokens "toks" from "lexPair".
  | 
| 
 | 
   927  | 
         If it succeeds, returns SOME(toks',l,r,lp), where
  | 
| 
 | 
   928  | 
	     toks' is the actual tokens (with positions and values) deleted,
  | 
| 
 | 
   929  | 
	     (l,r) are the (leftmost,rightmost) position of toks', 
  | 
| 
 | 
   930  | 
	     lp is what remains of the stream after deletion 
  | 
| 
 | 
   931  | 
     *)
  | 
| 
 | 
   932  | 
        fun do_delete(nil,lp as (TOKEN(_,(_,l,_)),_)) = SOME(nil,l,l,lp)
  | 
| 
 | 
   933  | 
          | do_delete([t],(tok as TOKEN(t',(_,l,r)),lp')) =
  | 
| 
 | 
   934  | 
	       if eqT (t, t')
  | 
| 
 | 
   935  | 
		   then SOME([tok],l,r,Stream.get lp')
  | 
| 
 | 
   936  | 
                   else NONE
  | 
| 
 | 
   937  | 
          | do_delete(t::rest,(tok as TOKEN(t',(_,l,r)),lp')) =
  | 
| 
 | 
   938  | 
	       if eqT (t,t')
  | 
| 
 | 
   939  | 
		   then case do_delete(rest,Stream.get lp')
  | 
| 
 | 
   940  | 
                         of SOME(deleted,l',r',lp'') =>
  | 
| 
 | 
   941  | 
			       SOME(tok::deleted,l,r',lp'')
  | 
| 
 | 
   942  | 
			  | NONE => NONE
  | 
| 
 | 
   943  | 
		   else NONE
  | 
| 
 | 
   944  | 
			     
  | 
| 
 | 
   945  | 
        fun tryPreferred((stack,lexPair),queuePos) =
  | 
| 
 | 
   946  | 
	    catList preferred_change (fn (delete,insert) =>
  | 
| 
 | 
   947  | 
	       if List.exists noShift delete then [] (* should give warning at
  | 
| 
 | 
   948  | 
						 parser-generation time *)
  | 
| 
 | 
   949  | 
               else case do_delete(delete,lexPair)
  | 
| 
 | 
   950  | 
                     of SOME(deleted,l,r,lp) => 
  | 
| 
 | 
   951  | 
			    tryChange{lex=lp,stack=stack,pos=queuePos,
 | 
| 
 | 
   952  | 
				      leftPos=l,rightPos=r,orig=deleted,
  | 
| 
 | 
   953  | 
				      new=map (fn t=>(tokAt(t,r))) insert}
  | 
| 
 | 
   954  | 
		      | NONE => [])
  | 
| 
 | 
   955  | 
  | 
| 
 | 
   956  | 
	val changes = catList numStateList tryPreferred @
  | 
| 
 | 
   957  | 
	                catList numStateList tryInsert @
  | 
| 
 | 
   958  | 
			  catList numStateList trySubst @
  | 
| 
 | 
   959  | 
			    catList numStateList (tryDelete 1) @
  | 
| 
 | 
   960  | 
			      catList numStateList (tryDelete 2) @
  | 
| 
 | 
   961  | 
			        catList numStateList (tryDelete 3)
  | 
| 
 | 
   962  | 
  | 
| 
 | 
   963  | 
	val findMaxDist = fn l => 
  | 
| 
 | 
   964  | 
	  List.foldr (fn (CHANGE {distance,...},high) => Int.max(distance,high)) 0 l
 | 
| 
 | 
   965  | 
  | 
| 
 | 
   966  | 
(* maxDist: max distance past error taken that we could parse *)
  | 
| 
 | 
   967  | 
  | 
| 
 | 
   968  | 
	val maxDist = findMaxDist changes
  | 
| 
 | 
   969  | 
  | 
| 
 | 
   970  | 
(* remove changes which did not parse maxDist tokens past the error token *)
  | 
| 
 | 
   971  | 
  | 
| 
 | 
   972  | 
        val changes = catList changes 
  | 
| 
 | 
   973  | 
	      (fn(c as CHANGE{distance,...}) => 
 | 
| 
 | 
   974  | 
		  if distance=maxDist then [c] else [])
  | 
| 
 | 
   975  | 
  | 
| 
 | 
   976  | 
      in case changes 
  | 
| 
 | 
   977  | 
	  of (l as change :: _) =>
  | 
| 
 | 
   978  | 
	      let fun print_msg (CHANGE {new,orig,leftPos,rightPos,...}) =
 | 
| 
 | 
   979  | 
		  let val s = 
  | 
| 
 | 
   980  | 
		      case (orig,new)
  | 
| 
 | 
   981  | 
			  of (_::_,[]) => "deleting " ^ (showTerms orig)
  | 
| 
 | 
   982  | 
	                   | ([],_::_) => "inserting " ^ (showTerms new)
  | 
| 
 | 
   983  | 
			   | _ => "replacing " ^ (showTerms orig) ^
  | 
| 
 | 
   984  | 
				 " with " ^ (showTerms new)
  | 
| 
 | 
   985  | 
		  in error ("syntax error: " ^ s,leftPos,rightPos)
 | 
| 
 | 
   986  | 
		  end
  | 
| 
 | 
   987  | 
		   
  | 
| 
 | 
   988  | 
		  val _ = 
  | 
| 
 | 
   989  | 
		      (if length l > 1 andalso DEBUG2 then
  | 
| 
 | 
   990  | 
			   (TextIO.print "multiple fixes possible; could fix it by:\n";
  | 
| 
 | 
   991  | 
			    app print_msg l;
  | 
| 
 | 
   992  | 
			    TextIO.print "chosen correction:\n")
  | 
| 
 | 
   993  | 
		       else ();
  | 
| 
 | 
   994  | 
		       print_msg change)
  | 
| 
 | 
   995  | 
  | 
| 
 | 
   996  | 
		  (* findNth: find nth queue entry from the error
  | 
| 
 | 
   997  | 
		   entry.  Returns the Nth queue entry and the  portion of
  | 
| 
 | 
   998  | 
		   the queue from the beginning to the nth-1 entry.  The
  | 
| 
 | 
   999  | 
		   error entry is at the end of the queue.
  | 
| 
 | 
  1000  | 
  | 
| 
 | 
  1001  | 
		   Examples:
  | 
| 
 | 
  1002  | 
  | 
| 
 | 
  1003  | 
		   queue = a b c d e
  | 
| 
 | 
  1004  | 
		   findNth 0 = (e,a b c d)
  | 
| 
 | 
  1005  | 
		   findNth 1 =  (d,a b c)
  | 
| 
 | 
  1006  | 
		   *)
  | 
| 
 | 
  1007  | 
  | 
| 
 | 
  1008  | 
		  val findNth = fn n =>
  | 
| 
 | 
  1009  | 
		      let fun f (h::t,0) = (h,rev t)
  | 
| 
 | 
  1010  | 
			    | f (h::t,n) = f(t,n-1)
  | 
| 
 | 
  1011  | 
			    | f (nil,_) = let exception FindNth
  | 
| 
 | 
  1012  | 
					  in raise FindNth
  | 
| 
 | 
  1013  | 
					  end
  | 
| 
 | 
  1014  | 
		      in f (rev stateList,n)
  | 
| 
 | 
  1015  | 
		      end
  | 
| 
 | 
  1016  | 
		
  | 
| 
 | 
  1017  | 
		  val CHANGE {pos,orig,new,...} = change
 | 
| 
 | 
  1018  | 
		  val (last,queueFront) = findNth pos
  | 
| 
 | 
  1019  | 
		  val (stack,lexPair) = last
  | 
| 
 | 
  1020  | 
  | 
| 
 | 
  1021  | 
		  val lp1 = List.foldl(fn (_,(_,r)) => Stream.get r) lexPair orig
  | 
| 
 | 
  1022  | 
		  val lp2 = List.foldr(fn(t,r)=>(t,Stream.cons r)) lp1 new
  | 
| 
 | 
  1023  | 
  | 
| 
 | 
  1024  | 
		  val restQueue = 
  | 
| 
 | 
  1025  | 
		      Fifo.put((stack,lp2),
  | 
| 
 | 
  1026  | 
			       List.foldl Fifo.put Fifo.empty queueFront)
  | 
| 
 | 
  1027  | 
  | 
| 
 | 
  1028  | 
		  val (lexPair,stack,queue,_,_) =
  | 
| 
 | 
  1029  | 
		      distanceParse(lp2,stack,restQueue,pos)
  | 
| 
 | 
  1030  | 
  | 
| 
 | 
  1031  | 
	      in (lexPair,stack,queue)
  | 
| 
 | 
  1032  | 
	      end
  | 
| 
 | 
  1033  | 
	| nil => (error("syntax error found at " ^ (showTerminal term),
 | 
| 
 | 
  1034  | 
			leftPos,leftPos); raise ParseError)
  | 
| 
 | 
  1035  | 
    end
  | 
| 
 | 
  1036  | 
  | 
| 
 | 
  1037  | 
   val parse = fn {arg,table,lexer,saction,void,lookahead,
 | 
| 
 | 
  1038  | 
		   ec=ec as {showTerminal,...} : ('_a,'_b) ecRecord} =>
 | 
| 
 | 
  1039  | 
	let val distance = 15   (* defer distance tokens *)
  | 
| 
 | 
  1040  | 
	    val minAdvance = 1  (* must parse at least 1 token past error *)
  | 
| 
 | 
  1041  | 
	    val maxAdvance = Int.max(lookahead,0)(* max distance for parse check *)
  | 
| 
 | 
  1042  | 
	    val lexPair = Stream.get lexer
  | 
| 
 | 
  1043  | 
	    val (TOKEN (_,(_,leftPos,_)),_) = lexPair
  | 
| 
 | 
  1044  | 
	    val startStack = [(initialState table,(void,leftPos,leftPos))]
  | 
| 
 | 
  1045  | 
	    val startQueue = Fifo.put((startStack,lexPair),Fifo.empty)
  | 
| 
 | 
  1046  | 
	    val distanceParse = distanceParse(table,showTerminal,saction,arg)
  | 
| 
 | 
  1047  | 
	    val fixError = mkFixError(ec,distanceParse,minAdvance,maxAdvance)
  | 
| 
 | 
  1048  | 
	    val ssParse = ssParse(table,showTerminal,saction,fixError,arg)
  | 
| 
 | 
  1049  | 
	    fun loop (lexPair,stack,queue,_,SOME ACCEPT) =
  | 
| 
 | 
  1050  | 
		   ssParse(lexPair,stack,queue)
  | 
| 
 | 
  1051  | 
	      | loop (lexPair,stack,queue,0,_) = ssParse(lexPair,stack,queue)
  | 
| 
 | 
  1052  | 
	      | loop (lexPair,stack,queue,distance,SOME ERROR) =
  | 
| 
 | 
  1053  | 
		 let val (lexPair,stack,queue) = fixError(lexPair,stack,queue)
  | 
| 
 | 
  1054  | 
		 in loop (distanceParse(lexPair,stack,queue,distance))
  | 
| 
 | 
  1055  | 
		 end
  | 
| 
 | 
  1056  | 
	      | loop _ = let exception ParseInternal
  | 
| 
 | 
  1057  | 
			 in raise ParseInternal
  | 
| 
 | 
  1058  | 
			 end
  | 
| 
 | 
  1059  | 
	in loop (distanceParse(lexPair,startStack,startQueue,distance))
  | 
| 
 | 
  1060  | 
	end
  | 
| 
 | 
  1061  | 
 end;
  | 
| 
 | 
  1062  | 
  | 
| 
 | 
  1063  | 
;
  | 
| 
 | 
  1064  | 
print_depth 10;
  |