Commit d4c7e38b authored by POTTIER Francois's avatar POTTIER Francois

Modified [ReferenceInterpreter] to avoid an old hack based on a reference.

parent 65dbcd3e
...@@ -158,87 +158,68 @@ module T = struct ...@@ -158,87 +158,68 @@ module T = struct
next = !stack next = !stack
} }
let log = true (* The logging functions that follow are called only if [log] is [true]. *)
module Log = struct module Log = struct
open Printf open Printf
(* I use a reference as a quick and dirty form of parameter passing. *)
let log =
ref false
let maybe action =
if !log then begin
action();
prerr_newline()
end
let state s = let state s =
maybe (fun () -> fprintf stderr "State %d:" (Lr1.number s);
fprintf stderr "State %d:" (Lr1.number s) prerr_newline()
)
let shift tok s' = let shift tok s' =
maybe (fun () -> fprintf stderr "Shifting (%s) to state %d" (Terminal.print tok) (Lr1.number s');
fprintf stderr "Shifting (%s) to state %d" (Terminal.print tok) (Lr1.number s') prerr_newline()
)
let reduce_or_accept prod = let reduce_or_accept prod =
maybe (fun () -> match Production.classify prod with
match Production.classify prod with | Some _ ->
| Some _ -> fprintf stderr "Accepting";
fprintf stderr "Accepting" prerr_newline()
| None -> | None ->
fprintf stderr "Reducing production %s" (Production.print prod) fprintf stderr "Reducing production %s" (Production.print prod);
) prerr_newline()
let lookahead_token tok startp endp = let lookahead_token tok startp endp =
maybe (fun () -> fprintf stderr "Lookahead token is now %s (%d-%d)"
fprintf stderr "Lookahead token is now %s (%d-%d)" (Terminal.print tok)
(Terminal.print tok) startp.Lexing.pos_cnum
startp.Lexing.pos_cnum endp.Lexing.pos_cnum;
endp.Lexing.pos_cnum prerr_newline()
)
let initiating_error_handling () = let initiating_error_handling () =
maybe (fun () -> fprintf stderr "Initiating error handling";
fprintf stderr "Initiating error handling" prerr_newline()
)
let resuming_error_handling () = let resuming_error_handling () =
maybe (fun () -> fprintf stderr "Resuming error handling";
fprintf stderr "Resuming error handling" prerr_newline()
)
let handling_error s = let handling_error s =
maybe (fun () -> fprintf stderr "Handling error in state %d" (Lr1.number s);
fprintf stderr "Handling error in state %d" (Lr1.number s) prerr_newline()
)
end end
end end
(* Instantiate the LR engine with this information. *)
module E =
MenhirLib.Engine.Make (T)
(* Define a palatable user entry point. *) (* Define a palatable user entry point. *)
let interpret log nt lexer lexbuf = let interpret log nt lexer lexbuf =
(* Find the start state that corresponds to [nt] in the automaton. *) (* Instantiate the LR engine. *)
let module E =
MenhirLib.Engine.Make (struct
include T
let log = log
end)
in
let s = Lr1.entry_nt nt in (* Run it. *)
(* Run the engine. *)
try try
T.Log.log := log; Some (E.entry (Lr1.entry_nt nt) lexer lexbuf)
Some (E.entry s lexer lexbuf)
with T.Error -> with T.Error ->
None None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment