-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser.mly
75 lines (59 loc) · 1.5 KB
/
parser.mly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Compiler Construction - Minimal Lambda Language
*
* This file defined the grammar of the language, with
* the productions builtin the Abstract Syntax Tree.
*/
%{
open Ast
%}
%token <int> INT
%token <string> IDENT
%token PLUS
%token LPAREN RPAREN LBRACE RBRACE
%token FUNC
%token RETURN
%token ARROW
%token LAMBDA
%token BIND
%token COMMA
%token SEMI
%token EOF
%start program
%type <Ast.program> program
%%
program:
funcs = list(func) EOF { Array.of_list funcs }
func:
| FUNC name = IDENT;
LPAREN params = separated_list(COMMA, IDENT); RPAREN
body = func_body
{ { name; params; body; loc = $startpos } }
func_body:
| LBRACE body = statements; RBRACE { Some(body) }
| SEMI { None }
statements:
| statement statements { $1 :: $2 }
| { [] }
statement:
| RETURN expr SEMI { ReturnStmt($startpos, $2) }
| IDENT BIND expr SEMI { BindStmt($startpos, $1, $3) }
| expr SEMI { ExprStmt($startpos, $1) }
expr:
| unary_expr { $1 }
| lhs = expr; PLUS; rhs = unary_expr
{ AddExpr($startpos, lhs, rhs) }
unary_expr:
| LAMBDA
LPAREN params = separated_list(COMMA, IDENT); RPAREN
ARROW
body = postfix_expr;
{ LambdaExpr($startpos, params, body) }
| postfix_expr { $1 }
postfix_expr:
| primary_expr { $1 }
| callee = primary_expr; LPAREN args = separated_list(COMMA, expr); RPAREN
{ CallExpr($startpos, callee, args) }
primary_expr:
| LPAREN e = expr; RPAREN { e }
| name = IDENT { IdentExpr($startpos, name) }
| decimal = INT { IntExpr($startpos, decimal) }