43 %token <string> STRING
51 /* from lowest precedence */
57 %nonassoc EQ NEQ GT LT GE LE
61 /* to highest precedence */
63 %type <string> program
72 sprintf "program[%s]" $1
85 | MINUS exp %prec UMINUS
87 sprintf "negation[%s]" $2
89 | ID LBRACK exp RBRACK OF exp
92 let number_of_elements = $3 in
93 let initial_value = $6 in
95 "array[type[%s], size[%s], val[%s]]"
96 type_id number_of_elements initial_value
98 | ID LBRACE rec_field_assignments RBRACE
101 let rec_field_assignments = $3 in
103 "record[type[%s], rec_field_assignments[%s]]"
104 type_id rec_field_assignments
112 sprintf "assign[%s := %s]" $1 $3
116 sprintf "string[%S]" $1
121 sprintf "fun_call[%s, []]" id
123 | ID LPAREN fun_args RPAREN
127 sprintf "fun_call[%s, %s]" id fun_args
131 sprintf "op[%s %s %s]" $1 $2 $3
133 | IF exp THEN exp ELSE exp
138 sprintf "if_then_else[%s, then[%s], else[%s]]" e1 e2 e3
142 sprintf "if_then[%s, then[%s]]" $2 $4
146 sprintf "while[%s, do[%s]]" $2 $4
148 | FOR ID ASSIGN exp TO exp DO exp
154 sprintf "for[%s := %s, to[%s], do[%s]]" id e1 e2 e3
162 sprintf "exps[%s]" $2
164 | LET decs IN exps END
168 sprintf "let[decs[%s], in[exps[%s]]]" decs exps
172 (* Perhaps "void"? *)
177 rec_field_assignments:
182 sprintf "%S = %s" id exp
184 | ID EQ exp COMMA rec_field_assignments
188 let rec_field_assignments = $5 in
189 sprintf "%S = %s, %s" id exp rec_field_assignments
203 sprintf "%s; %s" exp exps
214 sprintf "%s %s" $1 $2
222 let type_id_new = $2 in
223 let type_id_orig = $4 in
224 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
226 | TYPE ID EQ LBRACE RBRACE
229 sprintf "tydec_empty_record[%s]" type_id
231 | TYPE ID EQ LBRACE tyfields RBRACE
235 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
237 | TYPE ID EQ ARRAY OF ID
240 let element_type_id = $6 in
241 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
249 sprintf "vardec[%s, exp[%s]]" id exp
251 | VAR ID COLON ID ASSIGN exp
256 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
260 | FUNCTION ID LPAREN RPAREN EQ exp
264 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
266 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
271 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
273 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
280 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
281 id tyfields type_id exp
290 sprintf "%s : %s" id_1 id_2
292 | ID COLON ID COMMA tyfields
296 let tyfield = sprintf "%s : %s" id_1 id_2 in
298 sprintf "%s, %s" tyfield tyfields
309 sprintf "%s, %s" $1 $3
333 sprintf "lvalue[%s, part[%s]]" id part
339 | lvalue_subscript {$1}
340 | lvalue_field_access {$1}
347 sprintf "subscript[%s]" exp
355 sprintf "field_access[%s]" field