This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,3 @@
type exp =
| Int of int
| Add of exp * exp

View file

@ -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

View file

@ -0,0 +1,7 @@
(executable
(public_name calc))
(ocamllex lexer)
(menhir
(modules parser))

View file

@ -0,0 +1,3 @@
(lang dune 3.18)
(using menhir 3.0)
(package (name calc))

View file

@ -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)) }

View file

@ -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) }
%%