-
-
Save zehnpaard/817e0ae7eeffdec7a9d209ea86437a05 to your computer and use it in GitHub Desktop.
Minimal example using ocamllex/menhir with js_of_ocaml : compile to JS with the command `dune build ./main.bc.js`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(menhir | |
(modules parser)) | |
(ocamllex lexer) | |
(executable | |
(name main) | |
(modes js)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type t = | |
| Var of string | |
| Call of t list | |
let rec to_string = function | |
| Var s -> "Var(" ^ s ^ ")" | |
| Call xs -> | |
let s = List.map to_string xs |> String.concat " " in | |
"Call(" ^ s ^ ")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let whitespace = [' ' '\t' '\n'] | |
let char = ['a'-'z' 'A'-'Z' '+' '-' '*' '/' '<' '>' '=' '!' '?' '_'] | |
let variable = char+ | |
rule f = parse | |
| whitespace* { f lexbuf } | |
| "(" { Parser.LPAREN } | |
| ")" { Parser.RPAREN } | |
| variable as s { Parser.VAR s } | |
| eof { EOF } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="_build/default/main.bc.js"></script> | |
</head> | |
<body></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let () = | |
"(test a b c)" | |
|> Lexing.from_string | |
|> Parser.f Lexer.f | |
|> Exp.to_string | |
|> print_endline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%token <string> VAR | |
%token LPAREN | |
%token RPAREN | |
%token EOF | |
%start <Exp.t> f | |
%% | |
f : e = expr; EOF { e } | |
expr : | |
| s = VAR; { Exp.Var s } | |
| LPAREN; es = list (expr); RPAREN { Exp.Call es } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment