Skip to content

Instantly share code, notes, and snippets.

@toots
Created April 25, 2019 17:48
Show Gist options
  • Save toots/c0a082da9e92ebdae0025da49bfb52d8 to your computer and use it in GitHub Desktop.
Save toots/c0a082da9e92ebdae0025da49bfb52d8 to your computer and use it in GitHub Desktop.
open Parser
let rec tokenizer lexbuf =
match%sedlex lexbuf with
| '\n' -> tokenizer lexbuf
| Plus('0'..'9') -> NUMBER (Sedlexing.Utf8.lexeme lexbuf)
| ';', ';' -> SEQSEQ
| eof -> EOF
| _ -> failwith "Parse error!"
let interactive =
MenhirLib.Convert.Simplified.traditional2revised Parser.interactive
let empty_p = {
Lexing.
pos_fname = "";
pos_lnum = 1;
pos_bol = 0;
pos_cnum = 0;
}
let get_token lexbuf =
let tokenizer () =
(Lexer.tokenizer lexbuf, empty_p, empty_p)
in
interactive tokenizer
let () =
let lexbuf = Sedlexing.Utf8.from_channel stdin in
let rec f () =
Printf.printf "Got number from stdin: %d\n%!" (get_token lexbuf);
f ()
in
f ()
.PHONY: clean all
x := cmx
i := cmi
V := @
OCAMLOPT := ocamlfind ocamlopt
OCAMLDEP := ocamlfind ocamldep
OCAMLFLAGS := -g -linkpkg -package sedlex -package sedlex.ppx -package menhirlib
SOURCES := parser.mli parser.ml lexer.ml main.ml
all: test
parser.mli parser.ml: parser.mly
menhir --explain $(<)
.depend: $(SOURCES)
$(V)echo OCAMLDEP
$(V)$(OCAMLDEP) $(^) > $(@)
%.$(i): %.mli
$(V)echo OCAMLOPT -c $(<)
$(V)$(OCAMLOPT) $(OCAMLFLAGS) -c $(<)
%.$(x): %.ml
$(V)echo OCAMLOPT -c $(<)
$(V)$(OCAMLOPT) $(OCAMLFLAGS) -c $(<)
test: $(SOURCES:.ml=.$(x))
$(V)echo OCAMLOPT -o $(@)
$(V)$(OCAMLOPT) -linkpkg -o $(@) $(^) $(OCAMLFLAGS)
clean:
rm -f .depend parser.mli parser.ml test *.o *.conflicts *.cm*
-include .depend
%token <string> NUMBER
%token SEQSEQ EOF
%start interactive
%type <int> interactive
%%
exprs:
| NUMBER { int_of_string $1 }
interactive:
| exprs SEQSEQ { $1 }
| EOF { raise End_of_file }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment