1(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi
2 *
3 * $Log$
4 * Revision 1.1  2006/06/22 07:40:27  michaeln
5 * Add a MoscowML compilable implementation of MLyacc, using the MLton sources
6 * as the base.
7 *
8 * Revision 1.1.1.1  1997/01/14 01:38:05  george
9 *   Version 109.24
10 *
11 * Revision 1.2  1996/02/26  15:02:31  george
12 *    print no longer overloaded.
13 *    use of makestring has been removed and replaced with Int.toString ..
14 *    use of IO replaced with TextIO
15 *
16 * Revision 1.1.1.1  1996/01/31  16:01:44  george
17 * Version 109
18 *
19 *)
20
21functor mkCore(structure IntGrammar : INTGRAMMAR) : CORE =
22	struct
23		open IntGrammar
24		open  Grammar
25		structure IntGrammar = IntGrammar
26		structure Grammar = Grammar
27
28		datatype item = ITEM of
29				{ rule : rule,
30				  dot : int,
31				  rhsAfter : symbol list
32				}
33
34		val eqItem = fn (ITEM{rule=RULE{num=n,...},dot=d,...},
35				 ITEM{rule=RULE{num=m,...},dot=e,...}) =>
36					n=m andalso d=e
37
38		val gtItem =  fn (ITEM{rule=RULE{num=n,...},dot=d,...},
39				  ITEM{rule=RULE{num=m,...},dot=e,...}) =>
40					n>m orelse (n=m andalso d>e)
41
42		structure ItemList = ListOrdSet
43			(struct
44				type elem = item
45				val eq = eqItem
46				val gt = gtItem
47			end)
48
49		open ItemList
50		datatype core = CORE of item list * int
51
52		val gtCore = fn (CORE (a,_),CORE (b,_)) => ItemList.set_gt(a,b)
53		val eqCore = fn (CORE (a,_),CORE (b,_)) => ItemList.set_eq(a,b)
54
55		(* functions for printing and debugging *)
56
57		 val prItem = fn (symbolToString,nontermToString,print) =>
58		   let val printInt = print o (Int.toString : int -> string)
59		       val prSymbol = print o symbolToString
60		       val prNonterm = print o nontermToString
61		       fun showRest nil = ()
62			 | showRest (h::t) = (prSymbol h; print " "; showRest t)
63		       fun showRhs (l,0) = (print ". "; showRest l)
64			 | showRhs (nil,_) = ()
65			 | showRhs (h::t,n) = (prSymbol h;
66					       print " ";
67					       showRhs(t,n-1))
68		   in fn (ITEM {rule=RULE {lhs,rhs,rulenum,num,...},
69				dot,rhsAfter,...}) =>
70			(prNonterm lhs; print " : "; showRhs(rhs,dot);
71		 	 case rhsAfter
72			 of nil => (print " (reduce by rule ";
73				    printInt rulenum;
74				    print ")")
75			  | _ => ();
76			  if DEBUG then
77			     (print " (num "; printInt num; print ")")
78			  else ())
79		   end
80
81	         val prCore = fn a as (_,_,print) =>
82		    let val prItem = prItem a
83		    in fn (CORE (items,state)) =>
84			  (print "state ";
85			   print (Int.toString state);
86		   	   print ":\n\n";
87		   	   app (fn i => (print "\t";
88					 prItem i; print "\n")) items;
89			   print "\n")
90		    end
91end;
92