-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml-intermediate.y
218 lines (168 loc) · 5.79 KB
/
ml-intermediate.y
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lexer.h" // Include the lexer header file
%}
%union {
char *str;
}
%token <str> IDENTIFIER INTEGER_LITERAL OCT_INTEGER_LITERAL BIN_INTEGER_LITERAL HEX_INTEGER_LITERAL FLOAT_LITERAL BOOL_LITERAL CHAR_LITERAL STRING_LITERAL
%token PLUS MINUS TIMES DIVIDE EQ NEQ LT GT LE GE AND OR NOT INC DEC
%token LPAREN RPAREN LBRACE RBRACE LBRACKET RBRACKET COMMA COLON SEMICOLON ASSIGN
%token FUN VAL TYPE RECORD OF
%token FOR IN DO DONE IF THEN ELSE FI LOCAL END LET MATCH WITH
%type <str> program declaration functionDeclaration variableDeclaration parameterList
%type <str> defineRecord record recordPairList recordPair recordInstPair
%type <str> defineType typeVariantList typeVariant typeList type
%type <str> forExpression ifExpression expression literal octintegerLiteral binintegerLiteral hexintegerLiteral
%type <str> integerLiteral floatLiteral boolLiteral charLiteral stringLiteral
%type <str> binaryOperator unaryOperator argumentList identifier letter lowercase uppercase any-char
%type <str> bin-digit oct-digit hex-digit digit match patternMatches patternMatch pattern
%type <str> literalPattern identifierPattern
%%
program : declaration
| program declaration
;
declaration : functionDeclaration
| variableDeclaration
| defineType
| defineRecord
;
functionDeclaration : FUN IDENTIFIER LPAREN parameterList RPAREN COLON type ASSIGN expression
;
variableDeclaration : VAL IDENTIFIER COLON type ASSIGN expression
;
parameterList : IDENTIFIER
| parameterList COMMA IDENTIFIER
;
defineRecord : RECORD IDENTIFIER ASSIGN record
;
record : LBRACE recordPairList RBRACE
;
recordPairList : recordPair
| recordPairList SEMICOLON recordPair
;
recordPair : IDENTIFIER COLON type
;
recordInstPair : IDENTIFIER ASSIGN (identifierPattern | literalPattern)
;
defineType : TYPE UserDefinedType ASSIGN typeVariantList
;
typeVariantList : typeVariant
| typeVariantList '|' typeVariant
;
typeVariant : IDENTIFIER OF typeList
| IDENTIFIER
;
typeList : type
| typeList '*' type
;
type : INTEGER_LITERAL
| FLOAT_LITERAL
| BOOL_LITERAL
| CHAR_LITERAL
| STRING_LITERAL
| UserDefinedType
;
forExpression : FOR expression IN expression DO expression DONE
;
ifExpression : IF expression THEN expression ELSE expression FI
;
expression : literal
| IDENTIFIER
| LPAREN expression RPAREN
| IDENTIFIER LPAREN (identifierPattern | literalPattern) RPAREN
| recordInstPair
| expression binaryOperator expression
| unaryOperator expression
| MATCH expression WITH patternMatches
| ifExpression
| forExpression
| localExpression
| assignmentExpression
;
localExpression : LOCAL expression_list IN expression_list END
;
expression_list : expression
| expression_list expression
;
assignmentExpression : LET IDENTIFIER ASSIGN expression IN expression
;
literal : integerLiteral
| octintegerLiteral
| binintegerLiteral
| hexintegerLiteral
| floatLiteral
| boolLiteral
| charLiteral
| stringLiteral
;
octintegerLiteral : OCT_INTEGER_LITERAL
;
binintegerLiteral : BIN_INTEGER_LITERAL
;
hexintegerLiteral : HEX_INTEGER_LITERAL
;
integerLiteral : INTEGER_LITERAL
;
floatLiteral : FLOAT_LITERAL
;
boolLiteral : BOOL_LITERAL
;
charLiteral : CHAR_LITERAL
;
stringLiteral : STRING_LITERAL
;
binaryOperator : PLUS
| MINUS
| TIMES
| DIVIDE
| EQ
| NEQ
| LT
| GT
| LE
| GE
| AND
| OR
;
unaryOperator : MINUS
| NOT
| INC
| DEC
;
argumentList : expression
| argumentList COMMA expression
;
identifier : IDENTIFIER
;
letter : lowercase
| uppercase
;
match : MATCH expression WITH patternMatches
;
patternMatches : patternMatch
| patternMatches '|' patternMatch
;
patternMatch : pattern ARROW expression
;
pattern : literalPattern
| identifierPattern
;
literalPattern : integerLiteral
| floatLiteral
| boolLiteral
| charLiteral
| stringLiteral
;
identifierPattern : IDENTIFIER LPAREN literalPattern RPAREN
;
%%
int main() {
yyparse();
return 0;
}
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}