-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
164 lines (133 loc) · 3.58 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
%{
open Ast;;
%}
/* Declare your tokens here. */
%token EOF
%token <Range.t * int32> INT
%token <Range.t * string> IDENT
%token <Range.t> SEMI /* ; */
%token <Range.t> COMMA /* , */
%token <Range.t> LBRACE /* { */
%token <Range.t> RBRACE /* } */
%token <Range.t> IF /* if */
%token <Range.t> ELSE /* else */
%token <Range.t> WHILE /* while */
%token <Range.t> FOR /* for */
%token <Range.t> RETURN /* return */
%token <Range.t> TINT /* int */
%token <Range.t> PLUS /* + */
%token <Range.t> DASH /* - */
%token <Range.t> STAR /* * */
%token <Range.t> SLASH /* / */
%token <Range.t> PERCENT /* % */
%token <Range.t> GT /* > */
%token <Range.t> GTEQ /* >= */
%token <Range.t> LT /* < */
%token <Range.t> LTEQ /* <= */
%token <Range.t> EQEQ /* == */
%token <Range.t> EQ /* = */
%token <Range.t> BANG /* ! */
%token <Range.t> BANGEQ /* != */
%token <Range.t> BAR /* | */
%token <Range.t> AMPER /* & */
%token <Range.t> LPAREN /* ( */
%token <Range.t> RPAREN /* ) */
%token <Range.t> TILDE /* ~ */
%token <Range.t> LTLT /* << */
%token <Range.t> GTGT /* >> */
%token <Range.t> GTGTGT /* >>> */
/* ---------------------------------------------------------------------- */
%start toplevel
%type <Ast.prog> toplevel
%type <Ast.exp> exp
%type <Ast.block> block
%type <Ast.typ> typ
%type <Ast.stmt> stmt
%%
toplevel:
| prog EOF { $1 }
prog:
| block RETURN exp SEMI { ($1, $3) }
block:
| vdecls stmts { ($1, $2) }
vdecls:
| vdecl SEMI vdecls { $1::$3 }
| /* empty */ { [] }
vdecl:
| typ IDENT EQ exp { {v_ty=$1; v_id=snd($2); v_init=$4 } }
typ:
| TINT { TInt }
/* int x = 3, int y = 4, int z = 5 */
vdecllist:
| /* empty */ { [] }
| vdeclplus { $1 }
vdeclplus:
| vdecl { [$1] }
| vdecl COMMA vdeclplus { $1::$3 }
lhs:
| IDENT { $1 }
stmts:
| stmt stmts { $1::$2 }
| /* empty */ { [] }
stmtOPT:
| /* empty */ { None }
| stmt { Some $1 }
expOPT:
| /* empty */ { None }
| exp { Some $1 }
exp:
| E4 { $1 }
E4:
| E4 BAR E5 { Binop (Or, $1, $3) }
| E5 { $1 }
E5:
| E5 AMPER E6 { Binop (And, $1, $3) }
| E6 { $1 }
E6:
| E6 EQEQ E7 { Binop (Eq, $1, $3) }
| E6 BANGEQ E7 { Binop (Neq, $1, $3) }
| E7 { $1 }
E7:
| E7 LT E8 { Binop (Lt, $1, $3) }
| E7 LTEQ E8 { Binop (Lte, $1, $3) }
| E7 GT E8 { Binop (Gt, $1, $3) }
| E7 GTEQ E8 { Binop (Gte, $1, $3) }
| E8 { $1 }
E8:
| E8 LTLT E9 { Binop (Shl, $1, $3) }
| E8 GTGTGT E9 { Binop (Shr, $1, $3) }
| E8 GTGT E9 { Binop (Sar, $1, $3) }
| E9 { $1 }
E9:
| E9 PLUS E10 { Binop (Plus, $1, $3) }
| E9 DASH E10 { Binop (Minus, $1, $3) }
| E10 { $1 }
E10:
| E10 STAR E11 { Binop (Times, $1, $3) }
| E11 { $1 }
E11:
| DASH E11 { Unop (Neg, $2) }
| BANG E11 { Unop (Lognot, $2) }
| TILDE E11 { Unop (Not, $2) }
| E12 { $1 }
E12:
| INT { Cint (snd $1) }
| LPAREN exp RPAREN { $2 }
| IDENT { Id (snd $1) }
stmt:
| unmatchedif {$1}
| matchedif {$1}
unmatchedif:
| IF LPAREN exp RPAREN unmatchedstmt {If($3,$5,None)}
| IF LPAREN exp RPAREN matchedif ELSE unmatchedif {If($3,$5,Some $7)}
matchedif:
| IF LPAREN exp RPAREN matchedif ELSE matchedif {If($3,$5,Some $7)}
| matchedstmt {$1}
unmatchedstmt:
| WHILE LPAREN exp RPAREN unmatchedstmt {While($3,$5)}
| FOR LPAREN vdecllist SEMI expOPT SEMI stmtOPT RPAREN unmatchedstmt {For($3,$5,$7,$9)}
matchedstmt:
| lhs EQ exp SEMI { Assign(Var(snd($1)), $3) }
| LBRACE block RBRACE {Block($2)}
| WHILE LPAREN exp RPAREN matchedstmt {While($3,$5)}
| FOR LPAREN vdecllist SEMI expOPT SEMI stmtOPT RPAREN matchedstmt {For($3,$5,$7,$9)}