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 139 - (view) (download)

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

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