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