43 %token <string> STRING
51 /* from lowest precedence */
54 %nonassoc EQ NEQ GT LT GE LE
58 /* to highest precedence */
60 %type <string> program
69 sprintf "program[%s]" $1
82 | MINUS exp %prec UMINUS
84 sprintf "negation[%s]" $2
86 | ID LBRACK exp RBRACK OF exp
89 let number_of_elements = $3 in
90 let initial_value = $6 in
92 "array[type[%s], size[%s], val[%s]]"
93 type_id number_of_elements initial_value
95 | ID LBRACE rec_field_assignments RBRACE
98 let rec_field_assignments = $3 in
100 "record[type[%s], rec_field_assignments[%s]]"
101 type_id rec_field_assignments
109 sprintf "assign[%s := %s]" $1 $3
113 sprintf "string[%S]" $1
118 sprintf "fun_call[%s, []]" id
120 | ID LPAREN fun_args RPAREN
124 sprintf "fun_call[%s, %s]" id fun_args
128 sprintf "op[%s %s %s]" $1 $2 $3
130 | IF exp THEN exp ELSE exp
135 sprintf "if_then_else[%s, then[%s], else[%s]]" e1 e2 e3
139 sprintf "if_then[%s, then[%s]]" $2 $4
143 sprintf "while[%s, do[%s]]" $2 $4
145 | FOR ID ASSIGN exp TO exp DO exp
151 sprintf "for[%s := %s, to[%s], do[%s]]" id e1 e2 e3
159 sprintf "exps[%s]" $2
161 | LET decs IN exps END
165 sprintf "let[decs[%s], in[exps[%s]]]" decs exps
169 (* Perhaps "void"? *)
174 rec_field_assignments:
179 sprintf "%S = %s" id exp
181 | ID EQ exp COMMA rec_field_assignments
185 let rec_field_assignments = $5 in
186 sprintf "%S = %s, %s" id exp rec_field_assignments
200 sprintf "%s; %s" exp exps
211 sprintf "%s %s" $1 $2
219 let type_id_new = $2 in
220 let type_id_orig = $4 in
221 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
223 | TYPE ID EQ LBRACE RBRACE
226 sprintf "tydec_empty_record[%s]" type_id
228 | TYPE ID EQ LBRACE tyfields RBRACE
232 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
234 | TYPE ID EQ ARRAY OF ID
237 let element_type_id = $6 in
238 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
246 sprintf "vardec[%s, exp[%s]]" id exp
248 | VAR ID COLON ID ASSIGN exp
253 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
257 | FUNCTION ID LPAREN RPAREN EQ exp
261 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
263 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
268 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
270 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
277 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
278 id tyfields type_id exp
287 sprintf "%s : %s" id_1 id_2
289 | ID COLON ID COMMA tyfields
293 let tyfield = sprintf "%s : %s" id_1 id_2 in
295 sprintf "%s, %s" tyfield tyfields
306 sprintf "%s, %s" $1 $3
330 sprintf "lvalue[%s, part[%s]]" id part
336 | lvalue_subscript {$1}
337 | lvalue_field_access {$1}
344 sprintf "subscript[%s]" exp
352 sprintf "field_access[%s]" field