Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/branches/SMLNJ/src/ml-yacc/src/grammar.sml
ViewVC logotype

Annotation of /sml/branches/SMLNJ/src/ml-yacc/src/grammar.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (view) (download)

1 : monnier 2 (* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi
2 :     *
3 :     * $Log$
4 : monnier 113 * Revision 1.1.1.4 1998/06/05 19:39:59 monnier
5 :     * 110.7
6 : monnier 2 *
7 : monnier 8 * Revision 1.1.1.1 1997/01/14 01:38:05 george
8 :     * Version 109.24
9 :     *
10 : monnier 2 * Revision 1.2 1996/02/26 15:02:33 george
11 :     * print no longer overloaded.
12 :     * use of makestring has been removed and replaced with Int.toString ..
13 :     * use of IO replaced with TextIO
14 :     *
15 :     * Revision 1.1.1.1 1996/01/31 16:01:45 george
16 :     * Version 109
17 :     *
18 :     *)
19 :    
20 :     structure Grammar : GRAMMAR =
21 :     struct
22 :    
23 :     (* define types term and nonterm using those in LrTable
24 :     datatype term = T of int
25 :     datatype nonterm = NT of int *)
26 :    
27 :     open LrTable
28 :     datatype symbol = TERM of term | NONTERM of nonterm
29 :     datatype grammar = GRAMMAR of
30 :     {rules: {lhs: nonterm,
31 :     rhs: symbol list,
32 :     precedence: int option,
33 :     rulenum: int} list,
34 :     noshift : term list,
35 :     eop : term list,
36 :     terms: int,
37 :     nonterms: int,
38 :     start : nonterm,
39 :     precedence : term -> int option,
40 :     termToString : term -> string,
41 :     nontermToString : nonterm -> string}
42 :     end;
43 :    
44 :     structure IntGrammar : INTGRAMMAR =
45 :     struct
46 :     structure Grammar = Grammar
47 :     open Grammar
48 :    
49 :     datatype rule = RULE of
50 :     {lhs: nonterm,
51 :     rhs: symbol list,
52 :     num: int,(* internal # assigned by coreutils *)
53 :     rulenum: int,
54 :     precedence: int option}
55 :    
56 :     val eqTerm = (op =)
57 :     val gtTerm = fn (T i,T j) => i>j
58 :    
59 :     val eqNonterm = (op =)
60 :     val gtNonterm = fn (NT i,NT j) => i>j
61 :    
62 :     val eqSymbol = (op =)
63 :     val gtSymbol = fn (TERM (T i),TERM (T j)) => i>j
64 :     | (NONTERM (NT i),NONTERM (NT j)) => i>j
65 :     | (TERM _,NONTERM _) => false
66 :     | (NONTERM _,TERM _) => true
67 :    
68 :    
69 :     structure SymbolAssoc = Table(type key = symbol
70 :     val gt = gtSymbol)
71 :    
72 :     structure NontermAssoc = Table(type key = nonterm
73 :     val gt = gtNonterm)
74 :    
75 :     val DEBUG = false
76 :    
77 :     val prRule = fn (a as symbolToString,nontermToString,print) =>
78 :     let val printSymbol = print o symbolToString
79 :     fun printRhs (h::t) = (printSymbol h; print " ";
80 :     printRhs t)
81 :     | printRhs nil = ()
82 :     in fn (RULE {lhs,rhs,num,rulenum,precedence,...}) =>
83 :     ((print o nontermToString) lhs; print " : ";
84 :     printRhs rhs;
85 :     if DEBUG then (print " num = ";
86 :     print (Int.toString num);
87 :     print " rulenum = ";
88 :     print (Int.toString rulenum);
89 :     print " precedence = ";
90 :     case precedence
91 :     of NONE => print " none"
92 :     | (SOME i) =>
93 :     print (Int.toString i);
94 :     ())
95 :     else ())
96 :     end
97 :    
98 :     val prGrammar =
99 :     fn (a as (symbolToString,nontermToString,print)) =>
100 :     fn (GRAMMAR {rules,terms,nonterms,start,...}) =>
101 :     let val printRule =
102 :     let val prRule = prRule a
103 :     in fn {lhs,rhs,precedence,rulenum} =>
104 :     (prRule (RULE {lhs=lhs,rhs=rhs,num=0,
105 :     rulenum=rulenum, precedence=precedence});
106 :     print "\n")
107 :     end
108 :     in print "grammar = \n";
109 :     List.app printRule rules;
110 :     print "\n";
111 :     print (" terms = " ^ (Int.toString terms) ^
112 :     " nonterms = " ^ (Int.toString nonterms) ^
113 :     " start = ");
114 :     (print o nontermToString) start;
115 :     ()
116 :     end
117 :     end;

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0