This commit is contained in:
parent
aa2ff7b2f0
commit
2f3113f55d
11742 changed files with 1223940 additions and 0 deletions
|
|
@ -0,0 +1,3 @@
|
|||
type exp =
|
||||
| Int of int
|
||||
| Add of exp * exp
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
let rec eval = function Ast.Int n -> n | Add (a, b) -> eval a + eval b
|
||||
|
||||
let () =
|
||||
while true do
|
||||
Printf.printf ">> %!";
|
||||
let lb = Lexing.from_channel Stdlib.stdin in
|
||||
let e = Parser.main Lexer.token lb in
|
||||
Printf.printf "%d\n" (eval e)
|
||||
done
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(executable
|
||||
(public_name calc))
|
||||
|
||||
(ocamllex lexer)
|
||||
|
||||
(menhir
|
||||
(modules parser))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(lang dune 3.18)
|
||||
(using menhir 3.0)
|
||||
(package (name calc))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
let space = [' ']+
|
||||
|
||||
let digit = ['0'-'9']
|
||||
|
||||
rule token = parse
|
||||
| eof { Parser.Eof }
|
||||
| space { token lexbuf }
|
||||
| '\n' { Parser.Eof }
|
||||
| '+' { Parser.Plus }
|
||||
| digit+ { Parser.Int (int_of_string (Lexing.lexeme lexbuf)) }
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
%token Eof
|
||||
%token<int> Int
|
||||
%token Plus
|
||||
%start<Ast.exp> main
|
||||
|
||||
%left Plus
|
||||
|
||||
%{ open Ast %}
|
||||
|
||||
%%
|
||||
|
||||
main: expr Eof { $1 }
|
||||
|
||||
expr:
|
||||
| Int { Int $1 }
|
||||
| expr Plus expr { Add ($1, $3) }
|
||||
|
||||
%%
|
||||
Loading…
Add table
Add a link
Reference in a new issue