-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.mly
More file actions
143 lines (101 loc) · 3.23 KB
/
parser.mly
File metadata and controls
143 lines (101 loc) · 3.23 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(** Parser *)
%{
open Syntax
%}
(** tokens with values *)
(** Symbol atom name *)
%token <string> VAR (** x, y, abc, ... *)
%token <string> CONSTR (** Cons, Node, ... *)
(** link name *)
%token <string> LINK (** _X, _Y, _ABC, ... *)
(** integer literal *)
%token <int> INT (** 1, 2, 3, ... *)
(** operators *)
%token DOT (** '.' *)
%token COMMA (** ',' *)
%token NU (** "nu" *)
%token NECKTIE (** "><" *)
%token CASE (** "case" *)
%token OF (** "of" *)
%token ARROW (** "->" *)
%token LAMBDA (** "\\" *)
%token OTHERWISE (** "otherwise" *)
%token VBAR (** "|" *)
%token LET (** "let" *)
%token REC (** "rec" *)
%token IN (** "in" *)
%token EQ (** "=" *)
%token PLUS (** "+" *)
%token MINUS (** "-" *)
%token TIMES (** "*" *)
(** Parentheses *)
%token LPAREN (** '(' *)
%token RPAREN (** ')' *)
%token LBRACKET (** '[' *)
%token RBRACKET (** ']' *)
%token LCBRACKET (** '{' *)
%token RCBRACKET (** '}' *)
%token LT (** '<' *)
(*
%token GT (** '>' *)
*)
(** End of file *)
%token EOF
(** Operator associativity *)
%nonassoc DOT
%left COMMA
%left EQ
%left LT
%left PLUS MINUS
%left TIMES
%nonassoc LET IN CASE ARROW
%nonassoc LPAREN LCBRACKET
%start graph_eof
%type <graph> graph_eof
%start exp_eof
%type <exp> exp_eof
%%
(** arguments of an atom separated by comma without parentheses *)
let args_inner := ~ = separated_list(COMMA, LINK); <>
(** Syntax for an atom *)
atom_name:
| CONSTR { PConstr ($1) }
| LPAREN LAMBDA ctx DOT exp RPAREN { PLam ($3, $5) }
| INT { PInt ($1) }
| MINUS INT { PInt (- $2) }
atom:
| atom_name { Atom ($1, []) } (** e.g. C *)
| atom_name LPAREN args_inner RPAREN { Atom ($1, $3) } (** e.g. C (_X1, ..., _Xm) *)
| LINK NECKTIE LINK { Atom (PConstr "><", [$1; $3]) }
ctx:
| VAR { ($1, []) } (** e.g. a *)
| VAR LBRACKET args_inner RBRACKET { ($1, $3) } (** e.g. x [_X1, ..., _Xm] *)
(** proccesses separeted by comma *)
graph:
| atom { $1 }
| ctx { let (v, args) = ($1) in Ctx (v, args) } (** e.g. x[_X1, ..., _Xm] *)
| graph COMMA graph { Mol ($1, $3) }
| NU LINK+ DOT graph
{ List.fold_right (fun x graph -> Nu (x, graph)) $2 $4 }
| LPAREN graph RPAREN { $2 }
(** the whole program *)
graph_eof: graph EOF { $1 }
exp_single:
| LCBRACKET graph RCBRACKET { Graph ($2) }
| CASE exp OF LCBRACKET graph RCBRACKET ARROW exp VBAR OTHERWISE ARROW exp
{ Case ($2, $5, $8, $12) }
| LET REC ctx ctx ctx* EQ exp IN exp
{ LetRec ($3, $4, List.fold_right (make_lambda $3) $5 $7, $9) }
| LET ctx ctx* EQ exp IN exp
{ Let ($2, List.fold_right (make_lambda $2) $3 $5, $7) }
| LPAREN exp RPAREN { $2 }
exp:
| exp exp_single { App ($1, $2) }
| exp_single { $1 }
| exp PLUS exp { BinOp (( + ), "+", $1, $3) }
| exp MINUS exp { BinOp (( - ), "-", $1, $3) }
| exp TIMES exp { BinOp (( * ), "*", $1, $3) }
| exp LT exp { RelOp (( < ), "<", $1, $3) }
| exp EQ exp { RelOp (( = ), "=", $1, $3) }
(** the whole program *)
exp_eof: exp EOF { $1 }