parser.ml 16.9 KB
Newer Older
1 2
open Location
open Ast
3
open Ident
4
open Printf
5

6
(*
7
let ()  = Grammar.error_verbose := true
8
*)
9

10 11
exception Error of string
let error (i,j) s = Location.raise_loc i j (Error s)
12

13
let gram    = Grammar.gcreate Ulexer.lex
14

15 16
let true_atom = Atoms.V.mk_ascii "true"
let false_atom = Atoms.V.mk_ascii "false"
17 18
let true_type = Types.atom (Atoms.atom true_atom)
let false_type = Types.atom (Atoms.atom false_atom)
19

20
let parse_ident = U.mk
21

22
let id_dummy = ident (U.mk "$$$")
23

24
let label = parse_ident
25
let ident s = ident (parse_ident s)
26

27
let prog    = Grammar.Entry.create gram "prog"
28
let top_phrases   = Grammar.Entry.create gram "toplevel phrases"
29 30 31 32
let expr    = Grammar.Entry.create gram "expression"
let pat     = Grammar.Entry.create gram "type/pattern expression"
let regexp  = Grammar.Entry.create gram "type/pattern regexp"
		
33 34
let exp pos e = LocatedExpr (loc_of_pos pos,e)

35 36 37 38 39
let rec multi_prod loc = function
  | [ x ] -> x
  | x :: l -> mk loc (Prod (x, multi_prod loc l))
  | [] -> assert false
      
40
let rec tuple = function
41
  | [ x ] -> x
42
  | x :: l -> Pair (x, tuple l)
43
  | [] -> assert false
44 45

let tuple_queue = 
46
  List.fold_right (fun x q -> Pair (x, q))
47 48


49
let char = mknoloc (Internal (Types.char Chars.any))
50
let string_regexp = Star (Elem char)
51
	       
52
let cst_nil =  Atom (U.mk "nil")
53
let pat_nil = mknoloc (Internal (Sequence.nil_type))
54
		 
55
let seq_of_string s =
56 57
  let s = Encodings.Utf8.mk s in
  let rec aux i j =
58 59
    if Encodings.Utf8.equal_index i j then []
    else let (c,i) = Encodings.Utf8.next s i in c :: (aux i j)
60
  in
61
  aux (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s)
62

63

64
let parse_char loc s =
65 66
  match seq_of_string s with
    | [ c ] -> c
67
    | _ -> error loc "Character litteral must have length 1"
68

69 70
let include_stack = ref []

71 72 73 74
let protect_exn f g =
  try let x = f () in g (); x
  with e -> g (); raise e

75 76 77 78
let is_fun_decl =
   Grammar.Entry.of_parser gram "[is_fun_decl]"
     (fun strm ->
       match Stream.npeek 3 strm with
79 80
	 | [ ("", "fun"); ("IDENT", _); ("", "(") ]
	 | [ ("IDENT", _) ; ("", "(") ; _ ] -> ()
81 82 83
	 | _ -> raise Stream.Failure
     )

84
EXTEND
85
  GLOBAL: top_phrases prog expr pat regexp;
86 87

  top_phrases: [
88
    [ l = LIST0 phrase; ";;" -> List.flatten l  ]
89
  ];
90 91

  prog: [
92
    [ l = LIST0 [ p = phrase ; OPT ";;" -> p ]; EOI -> List.flatten l ]
93 94 95
  ];

  phrase: [
96 97 98 99
    [ (f,p,e) = let_binding -> 
	if f then [ mk loc (FunDecl e) ] else
	  [ mk loc (LetDecl (p,e)) ]
    | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
100
        [ mk loc (EvalStatement (exp loc (Match (e1,[p,e2])))) ]
101
    | "type"; x = IDENT; "="; t = pat -> [ mk loc (TypeDecl (ident x,t)) ]
102
    | "schema"; name = IDENT; "="; uri = STRING2 ->
103
	protect_op "schema";
104
        let schema_doc = Schema_xml.pxp_tree_of uri in
105 106
        let schema = Schema_parser.parse_schema schema_doc in
        [ mk loc (SchemaDecl (name, schema))]
107 108 109 110 111
    | (name,ns) = namespace_binding ->
	[ mk loc (Namespace (name, ns)) ]
    | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" ->
	let e = exp loc (NamespaceIn (name, ns, e2)) in
        [ mk loc (EvalStatement (exp loc e)) ]
112
    | "debug"; d = debug_directive -> [ mk loc (Debug d) ]
113 114 115
    | DIRECTIVE "#utf8" -> Ulexer.enc := Ulexing.Utf8; [ ]
    | DIRECTIVE "#latin1" -> Ulexer.enc := Ulexing.Latin1; [ ]
    | DIRECTIVE "#ascii" -> Ulexer.enc := Ulexing.Ascii; [ ]
116 117
    | DIRECTIVE "#quit" -> [ mk loc (Directive `Quit) ]
    | DIRECTIVE "#env" -> [ mk loc (Directive `Env) ]
118
    | DIRECTIVE "#reinit_ns" -> [ mk loc (Directive `Reinit_ns) ]
119
    | "include"; s = STRING2 ->
120 121 122 123
	let s = 
	  if Filename.is_relative s 
	  then Filename.concat (Location.current_dir ()) s
	  else s in
124 125
	protect_op "File inclusion";
	(* avoid looping; should issue an error ? *)
126 127
	(* it is possible to have looping with x/../x/../x/.. ....
	   Need to canonicalize filename *)
128 129 130 131
	if List.mem s !include_stack then [] 
	else (
	  include_stack := s :: !include_stack;
	  Location.push_source (`File s);
132 133
	  let saved_enc = !Ulexer.enc in
	  Ulexer.enc := Ulexing.Latin1;
134 135 136
	  protect_exn
	    (fun () ->
	       let chan = open_in s in
137 138 139 140 141
	       protect_exn
		 (fun () ->
		    let input = Stream.of_channel chan in
		    Grammar.Entry.parse prog input)
		 (fun () -> close_in chan))
142
	    (fun () ->
143
	       Ulexer.enc := saved_enc;
144 145
	       Location.pop_source ();
	       include_stack := List.tl !include_stack)
146
	)
147
    ] | 
148
    [ e = expr -> [ mk loc (EvalStatement e) ]
149 150 151 152
    ]
  ];

  debug_directive: [
153 154 155 156 157
    [ IDENT "filter"; t = pat; p = pat -> `Filter(t,p)
    | IDENT "accept"; p = pat -> `Accept p
    | IDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p)
    | IDENT "sample"; t = pat -> `Sample t
    | IDENT "subtype"; t1 = pat; t2 = pat -> `Subtype (t1,t2)
158
    | IDENT "explain"; t = pat; e = expr -> `Explain (t,e)
159
    ]
160
  ];
161

162 163 164 165 166
  keyword: [
    [ a = 
	[ "map" | "match" | "with" | "try" | "xtransform"
	| "if" | "then"	| "else"
	| "transform" | "fun" | "in"
167
	| "let" | "type" | "debug" | "include"
168
        | "and" | "validate" | "schema" | "namespace" | "ref"
169 170 171 172 173
	]
	-> a
    ]
  ];

174 175
  expr: [
    "top" RIGHTA
176 177
    [ "match"; e = SELF; "with"; b = branches -> 
	exp loc (Match (e,b))
178
    | "try"; e = SELF; "with"; b = branches -> 
179
	exp loc (Try (e,b))
180 181 182 183
    | "map"; e = SELF; "with"; b = branches -> 
	exp loc (Map (e,b))
    | "xtransform"; e = SELF; "with"; b = branches -> 
	exp loc (Xtrans (e,b))
184
    | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF ->
185 186
	let p1 = mk loc (Internal true_type)
	and p2 = mk loc (Internal false_type) in
187
	exp loc (Match (e, [p1,e1; p2,e2]))
188
    | "transform"; e = SELF; "with"; b = branches -> 
189
	exp loc (Transform (e,b))
190 191
    | "validate"; e = SELF; "with"; schema = IDENT; "#";
      typ = [ IDENT | keyword ] ->
192
        exp loc (Validate (e, schema, typ))
193
    | "fun"; (f,a,b) = fun_decl ->
194
	exp loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b })
195
    | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"->
196
        exp loc (Match (e1,[p,e2]))
197 198
    | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" ->
	exp loc (NamespaceIn (name, ns, e2))
199
    | e = expr; ":"; p = pat ->
200
	exp loc (Forget (e,p))
201 202 203 204 205 206 207 208
    | e1 = expr; ";"; e2 = expr ->
	exp loc (Match (e1, [pat_nil,e2]))
    | "ref"; p = pat; e = expr ->
	exp loc (Ref (e,p))
    ]
    |
    [ e1 = expr; ":="; e2 = expr ->
	exp loc (Apply (Dot (e1, U.mk "set"), e2))
209
    ]
210 211 212 213 214 215
    | 
    [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr -> 
	let op = match op with
	  | "<<" -> "<"
	  | ">>" -> ">"
	  | s -> s in
216
	exp loc (Op (op,[e1;e2]))
217 218
    ]

219
    | 
220
    [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> 
221
	exp loc (Op (op,[e1;e2]))
222
    | e = expr; "\\"; l = [IDENT | keyword ] -> 
223
	exp loc (RemoveField (e, label l)) 
224 225
    ]
    |
226
    [ e1 = expr; op = ["*"]; e2 = expr -> exp loc (Op (op,[e1;e2]))  
227 228 229 230 231
    | e = expr; op = "/"; p = pat ->

	let tag = mk loc (Internal (Types.atom (Atoms.any))) in
	let att = mk loc (Internal Types.Record.any) in
	let any = mk loc (Internal (Types.any)) in
232
	let re = Star(Alt(SeqCapture(id_dummy,Elem p), Elem any)) in
233 234
	let ct = mk loc (Regexp (re,any)) in
        let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in
235
	let b = (p,  Var id_dummy) in
236
	exp loc (Transform (e,[b]))
237
    ]
238
    |
239
    [ e = expr;  "."; l = [IDENT | keyword ] -> 
240
	exp loc (Dot (e, label l)) 
241
    ]  
242
 
243
    |
244 245 246 247 248 249 250 251 252 253 254
    [ op = [ IDENT "flatten"
	   | IDENT "load_xml"
	   | IDENT "load_file" | IDENT "load_file_utf8"
           | IDENT "getenv"
	   | IDENT "load_html"
	   | IDENT "print_xml" | IDENT "print_xml_utf8"
	   | IDENT "print"
	   | IDENT "int_of"
	   | IDENT "string_of"
	   | IDENT "atom_of"
	   | IDENT "raise"
255
	   ]; 
256
      e = expr -> exp loc (Op (op,[e]))
257
    | op = [ IDENT "dump_to_file" | IDENT "dump_to_file_utf8" ];
258
      e1 = expr LEVEL "no_appl"; e2 = expr -> exp loc (Op (op, [e1;e2])) 
259 260
    | e1 = SELF; IDENT "div"; e2 = expr -> exp loc (Op ("/", [e1;e2]))
    | e1 = SELF; IDENT "mod"; e2 = expr -> exp loc (Op ("mod", [e1;e2]))
261
    | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2))
262 263
    ]

264
    | "no_appl" 
265 266
    [ 
      "("; l = LIST1 expr SEP ","; ")" -> exp loc (tuple l)
267
    | "[";  l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; 
268
      loc_end = ["]" -> loc] ->
269
	let e = match e with Some e -> e | None -> cst_nil in
270 271
        let e = exp loc_end e in
        let (_,loc_end) = loc_end in
272 273 274
	let l = List.fold_right 
		  (fun x q ->
		     match x with
275
		       | `String (loc,i,j,s) -> exp loc (String (i,j,s,q))
276
		       | `Elems ((loc,_),x) -> exp (loc,loc_end) (Pair(x,q))
277 278 279 280
		       | `Explode x -> Op ("@",[x;q])
		  ) l e
	in
	exp loc l
281
    | "<"; t = [ "("; e = expr; ")" -> e
282
	       | a = tag -> exp loc a
283
	       ];
284
	a = expr_attrib_spec; ">"; c = expr ->
285
	  exp loc (Xml (t, Pair (a,c)))
286
    | "{"; r = [ expr_record_spec | -> exp loc (RecordLitt []) ]; "}" -> r
287
    | s = STRING2 ->
288
	let s = U.mk s in
289
	exp loc (String (U.start_index s, U.end_index s, s, cst_nil))
290
    | a = IDENT -> exp loc (Var (ident a))
291 292
    | "!"; e = expr ->
	exp loc (Apply (Dot (e, U.mk "get"), cst_nil))
293 294 295
    | i = INT -> exp loc (Integer (Intervals.V.mk i))
    | "`"; a = tag -> a
    | c = char -> exp loc (Char c)
296 297 298
    ]

  ];
299

300
  tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (parse_ident a)) ] ];
301 302

  tag_type: [
303
    [ IDENT "_" ->  mk loc (Internal (Types.atom Atoms.any))
304
    | a = [ IDENT | keyword ] -> mk loc (Cst (Atom (parse_ident a)))
305
    | t = ANY_IN_NS -> mk loc (NsT (parse_ident t)) 
306 307 308
    ]
  ];

309
  seq_elem: [
310
    [ x = STRING1 -> 
311
	let s = U.mk x in
312
	`String (loc, U.start_index s, U.end_index s, s)
313
    | e = expr LEVEL "no_appl" -> `Elems (loc,e)
314
    | "!"; e = expr LEVEL "no_appl" -> `Explode e
315 316
    ]
  ];
317 318 319
	
  namespace_binding: [
    [ "namespace"; 
320
	name = [ name = [ IDENT | keyword ]; "=" ->
321 322 323
		   parse_ident name
	       | -> U.mk "" ];
	uri = STRING2 ->
324
	  let ns = Ns.mk (parse_ident uri) in
325 326 327 328 329
	  (name,ns)
    ]
  ];

  
330
  let_binding: [
331 332
    [ "let"; is_fun_decl; OPT "fun"; (f,a,b) = fun_decl ->
	let f = match f with Some x -> x | None -> assert false in
333
	let p = mk loc (PatVar f) in
334
	let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in
335
        let e = exp loc (Abstraction abst) in
336
        (true,p,e)
337 338
    | "let"; p = pat; "="; e = expr -> (false,p,e)
    | "let"; p = pat; ":"; t = pat; "="; e = expr -> (false,p, Forget (e,t))
339 340 341
    ] 
  ];

342
 fun_decl_after_lparen: [
343
(* need an hack to do this, because both productions would
344
   match   [ OPT IDENT; "("; pat ] .... *)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
   [ p1 = pat LEVEL "no_arrow";
     res = [ "->"; p2 = pat;
	     a = [ ";"; a = LIST0 arrow SEP ";" -> a | -> [] ];
	     ")"; b = branches -> `Classic (p2,a,b)
	   | ":"; targ1 = pat;
	     args = LIST0 [ ","; arg = pat; ":"; targ = pat -> (arg,targ) ]; 
	     ")"; ":"; tres = pat ; 
	     "="; body = expr ->
	       `Compact (targ1,args,tres,body)
	   ] ->
       match res with
	 | `Classic (p2,a,b) -> (p1,p2)::a,b
	 | `Compact (targ1,args,tres,body) ->
	     let args = (p1,targ1) :: args in
	     let targ = multi_prod nopos (List.map snd args) in
	     let arg = multi_prod nopos (List.map fst args) in
	     let b = [arg, body] in
	     let a = [targ,tres] in
	     (a,b) ] ];


 fun_decl: [
367 368 369
   [ f = OPT IDENT; "("; (a,b) = fun_decl_after_lparen ->
       let f = match f with Some x -> Some (ident x) | None -> None in
       (f,a,b)
370
   ]
371 372
 ];

373
 arrow: [
374
    [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)]
375 376 377
  ];

  branches: [
378
    [ OPT "|"; l = LIST1 branch SEP "|" -> l ]
379 380 381
  ];

  branch: [
382
    [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ]
383 384 385 386
  ];

	  
  regexp: [ 
387 388 389 390 391
    [ x = regexp; "|"; y = regexp -> 
	match (x,y) with
	  | Elem x, Elem y -> Elem (mk loc (Or (x,y)))
	  | _ -> Alt (x,y) 
    ]
392
  | [ x = regexp; y = regexp -> Seq (x,y) ]
393
  | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ]
394 395 396 397
  | [ x = regexp; "*" -> Star x
    | x = regexp; "*?" -> WeakStar x
    | x = regexp; "+" -> Seq (x, Star x)
    | x = regexp; "+?" -> Seq (x, WeakStar x)
398
    | x = regexp; "?" ->  Alt (x, Epsilon)
399
    | x = regexp; "??" -> Alt (Epsilon, x) ]
400
  | [ "("; x = LIST1 regexp SEP ","; ")" ->
401 402 403 404 405 406 407 408 409 410 411
	(match x with
	  | [ x ] -> x
	  | _ ->
	      let x = 
		List.map 
		  (function
		     | Elem x -> x
		     | _ -> error loc 
			 "Mixing regular expressions and products")
		  x in
	      Elem (multi_prod loc x))
412 413
    | "("; a = IDENT; ":="; c = expr; ")" -> 
	Elem (mk loc (Constant ((ident a,c))))
414
    | IDENT "PCDATA" -> string_regexp
415
    | i = STRING1; "--"; j = STRING1 ->
416 417
	let i = Chars.V.mk_int (parse_char loc i)
	and j = Chars.V.mk_int (parse_char loc j) in
418
        Elem (mk loc (Internal (Types.char (Chars.char_class i j))))
419
    |  s = STRING1 ->
420
	List.fold_right
421
	  (fun c accu -> 
422
	     let c = Chars.V.mk_int c in
423
	     let c = Chars.atom c in
424 425
	     Seq (Elem (mknoloc (Internal (Types.char c))), accu))
	  (seq_of_string s)
426 427
	  Epsilon ]
    | [ e = pat LEVEL "simple" -> Elem e
428 429 430 431
    ]
  ];

  pat: [ 
432
      [ x = pat; IDENT "where"; 
433
        b = LIST1 [ a = IDENT; "="; y = pat -> (ident a,y) ] SEP "and"
434 435
            -> mk loc (Recurs (x,b)) ]
    | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ]
436
    | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] 
437
    | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) 
438
	       | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ]
439
    | 
440 441
      [ "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
442 443 444 445 446
      | "ref"; p = pat ->
	  let get_fun = mk loc (Arrow (pat_nil, p)) 
	  and set_fun = mk loc (Arrow (p, pat_nil)) in
	  let fields = [ label "get", get_fun; label "set", set_fun ] in
	  mk loc (Record (false, fields))
447
      | IDENT "_" -> mk loc (Internal Types.any)
448
      | "("; a = IDENT; ":="; c = expr; ")" -> 
449
	  mk loc (Constant (ident a,c))
450
      | schema = IDENT; "#"; typ = [ IDENT | keyword ];
451 452 453 454 455 456 457 458 459 460
        k = OPT [ "as"; k = [ "element" | "type" | "attribute" ] -> k ] ->
          let kind =
            match k with
            | None -> `Any
            | Some "element" -> `Element
            | Some "type" -> `Type
            | Some "attribute" -> `Attribute
            | _ -> assert false
          in
          mk loc (SchemaVar (kind, schema, typ))
461 462
      | a = IDENT -> 
	  mk loc (PatVar (ident a))
463
      | i = INT ; "--"; j = INT -> 
464 465
          let i =  Intervals.V.mk i 
	  and j =  Intervals.V.mk j in
466 467
          mk loc (Internal (Types.interval (Intervals.bounded i j)))
      | i = INT -> 
468
          let i =  Intervals.V.mk i  in
469
          mk loc (Internal (Types.interval (Intervals.atom i)))
470
      | "*"; "--"; j = INT ->
471
	  let j =  Intervals.V.mk j in
472
          mk loc (Internal (Types.interval (Intervals.left j)))
473
      | i = INT; "--"; "*" ->
474
	  let i = Intervals.V.mk i in
475
          mk loc (Internal (Types.interval (Intervals.right i)))
476 477
      | i = char ->
          mk loc (Internal (Types.char (Chars.char_class i i)))
478
      | i = char ; "--"; j = char ->
479
          mk loc (Internal (Types.char (Chars.char_class i j)))
480
      | "`"; c = tag_type -> c
481 482 483
      | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l
      | "["; r = [ r = regexp -> r | -> Epsilon ];
             q = [ ";"; q = pat -> q 
484
                 | -> pat_nil ]; 
485
             "]" -> mk loc (Regexp (r,q))
486
      | "<"; t =
487 488
            [ x = tag_type -> x
            | "("; t = pat; ")" -> t ];
489
	a = attrib_spec; ">"; c = pat ->
490
          mk loc (XmlT (t, multi_prod loc [a;c]))
491
      | s = STRING2 ->
492 493 494 495 496 497
	  let s = 
	    List.map 
	      (fun c -> 
		 mknoloc (Internal
			     (Types.char
				(Chars.atom
498
				   (Chars.V.mk_int c))))) 
499 500
	      (seq_of_string s) in
	  let s = s @ [mknoloc (Internal (Sequence.nil_type))] in
501
	  multi_prod loc s
502 503 504 505 506
      ]
    
  ];

  record_spec:
507
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
508
                  o = [ "?" -> true | -> false]; 
509 510
                  x = pat -> 
		    let x = if o then mk loc (Optional x) else x in
511
		    (label l, x)
512
                ] SEP ";" ->
513
	  r
514 515
      ] ];
  
516 517
  char:
    [ 
518
      [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ]
519 520 521
    ];
     

522
  attrib_spec:
523
    [ [ r = LIST0 [ l = [IDENT | keyword ]; "=";
524 525 526 527 528 529
                  o = [ "?" -> true | -> false]; 
                  x = pat; OPT ";" -> 
		    let x = if o then mk loc (Optional x) else x in
		    (label l, x)
                ] ->
	  mk loc (Record (true,r)) 
530 531 532 533
      | "("; t = pat; ")" -> t 
      | "{"; r = record_spec; "}" -> mk loc (Record (true,r))
      | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r))
      ] ];
534 535 536

  expr_record_spec:
    [ [ r = LIST1
537
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl" -> 
538
		  (label l,x) ] 
539
	       SEP ";"  ->
540
	  exp loc (RecordLitt r)
541
      ] ];
542

543
  expr_attrib_spec:
544
    [ [ r = LIST1
545
	      [ l = [IDENT | keyword ]; "="; x = expr LEVEL "no_appl"; OPT ";" -> 
546 547 548
		  (label l,x) ] ->
	  exp loc (RecordLitt r)
      ]
549
    | [ e = expr LEVEL "no_appl" -> e 
550
      | -> exp loc (RecordLitt []) 
551 552 553 554
      ] 
    ];
END

555
let pat = Grammar.Entry.parse pat
556 557
and expr = Grammar.Entry.parse expr
and prog = Grammar.Entry.parse prog
558 559
and top_phrases = Grammar.Entry.parse top_phrases

560
let sync () = 
561
  match !Ulexer.lexbuf with
562 563 564
    | None -> ()
    | Some lb ->
	let rec aux () =
565
	  match !Ulexer.last_tok with
566 567
	    | ("",";;") | ("EOI","") -> ()
	    | _ -> 
568
		Ulexer.last_tok := fst (Ulexer.token lb); 
569 570 571
		aux ()
	in
	aux ()