1
(**************************************************************************)
5
(* Fran�ois Pottier and Yann R�gis-Gianas, INRIA Rocquencourt *)
7
(* Copyright 2005 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* under the terms of the Q Public License version 1.0, with the *)
10
(* change described in file LICENSE. *)
12
(**************************************************************************)
18
open UnparameterizedSyntax
20
let print_preludes f g =
21
List.iter (fun prelude ->
22
Printf.fprintf f "%%{%s%%}\n" prelude.stretch_raw_content
25
let print_start_symbols b g =
26
StringSet.iter (fun symbol ->
27
Printf.fprintf b "%%start %s\n" (Misc.normalize symbol)
30
let rec insert_in_partitions item m = function
34
| (m', items) :: partitions when Mark.same m m' ->
35
(m', item :: items) :: partitions
38
t :: (insert_in_partitions item m partitions)
40
let insert (undefined, partitions) = function
41
| (item, UndefinedPrecedence) ->
42
((item, 0) :: undefined, partitions)
44
| (item, PrecedenceLevel (m, v, _, _)) ->
45
(undefined, insert_in_partitions (item, v) m partitions)
47
let print_ocamltype ocamltype =
48
Printf.sprintf " <%s>" (
51
stretch.stretch_raw_content
56
let print_assoc = function
58
Printf.sprintf "%%left"
60
Printf.sprintf "%%right"
62
Printf.sprintf "%%nonassoc"
66
let print_tokens b g =
67
(* Sort tokens wrt precedence. *)
68
let undefined, partition_tokens =
69
StringMap.fold (fun token prop acu ->
70
insert acu (token, prop.tk_priority)
74
List.fold_left (fun acu (_, ms) ->
75
acu @ List.sort (fun (_, v) (_, v') -> compare v v') ms
76
) undefined partition_tokens
78
List.iter (fun (token, _) ->
79
let prop = StringMap.find token g.tokens in
80
if prop.tk_is_declared then
81
Printf.fprintf b "%%token%s %s\n"
82
(Misc.o2s prop.tk_ocamltype print_ocamltype) token
85
ignore (List.fold_left
86
(fun last_prop (token, v) ->
87
let prop = StringMap.find token g.tokens in
91
if prop.tk_associativity = UndefinedAssoc then
94
Printf.fprintf b "%s %s "
95
(print_assoc prop.tk_associativity) token;
98
| Some v' when v <> v' ->
99
if prop.tk_associativity = UndefinedAssoc then
102
Printf.fprintf b "\n%s %s "
103
(print_assoc prop.tk_associativity) token;
107
Printf.fprintf b "%s " token;
110
) None ordered_tokens);
111
Printf.fprintf b "\n"
113
let print_types b g =
114
StringMap.iter (fun symbol ty ->
115
Printf.fprintf b "%%type%s %s\n"
116
(print_ocamltype ty) (Misc.normalize symbol)
119
let string_of_producer (symbol, ido) =
120
(Misc.o2s ido (fun id -> id ^ " = ")) ^ (Misc.normalize symbol)
122
let print_branch f branch =
123
Printf.fprintf f "%s%s\n {"
124
(String.concat " " (List.map string_of_producer branch.producers))
125
(Misc.o2s branch.branch_shift_precedence (fun x -> " %prec "^x.value));
126
Action.print f branch.action;
127
Printf.fprintf f "}\n"
129
let print_trailers b g =
130
List.iter (Printf.fprintf b "%s\n") g.postludes
132
let branches_order r r' =
133
let branch_order b b' =
134
match b.branch_reduce_precedence, b'.branch_reduce_precedence with
135
| UndefinedPrecedence, _ | _, UndefinedPrecedence ->
137
| PrecedenceLevel (m, l, _, _), PrecedenceLevel (m', l', _, _) ->
138
if Mark.same m m' then
147
let rec lexical_order bs bs' =
155
| b :: bs, b' :: bs' ->
156
match branch_order b b' with
162
lexical_order r.branches r'.branches
164
let print_rules b g =
166
StringMap.fold (fun nt r acu -> (nt, r) :: acu) g.rules []
169
List.sort (fun (nt, r) (nt', r') -> branches_order r r') rules_as_list
171
List.iter (fun (nt, r) ->
172
Printf.fprintf b "\n%s:\n" (Misc.normalize nt);
174
Printf.fprintf b "| ";
181
print_start_symbols f g;
184
Printf.fprintf f "%%%%\n";
186
Printf.fprintf f "\n%%%%\n";