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
81 | MINUS exp %prec UMINUS
83 sprintf "negation[%s]" $2
85 | ID LBRACK exp RBRACK OF exp
90 sprintf "array[type[%s], size[%s], val[%s]]" type_id exp_1 exp_2
92 | ID LBRACE rec_field_assignments RBRACE
95 let rec_field_assignments = $3 in
97 "record[type[%s], rec_field_assignments[%s]]"
98 type_id rec_field_assignments
106 sprintf "assign[%s := %s]" $1 $3
110 sprintf "string[%S]" $1
115 sprintf "fun_call[%s, []]" id
117 | ID LPAREN fun_args RPAREN
121 sprintf "fun_call[%s, %s]" id fun_args
125 sprintf "op[%s %s %s]" $1 $2 $3
127 | IF exp THEN exp ELSE exp
132 sprintf "if_then_else[%s, then[%s], else[%s]]" e1 e2 e3
136 sprintf "if_then[%s, then[%s]]" $2 $4
140 sprintf "while[%s, do[%s]]" $2 $4
142 | FOR ID ASSIGN exp TO exp DO exp
148 sprintf "for[%s := %s, to[%s], do[%s]]" id e1 e2 e3
156 sprintf "exps[%s]" $2
158 | LET decs IN exps END
162 sprintf "let[decs[%s], in[exps[%s]]]" decs exps
166 (* Perhaps "void"? *)
177 sprintf "%s; %s" $1 $3
187 sprintf "%s %s" $1 $2
196 | FUNCTION ID LPAREN RPAREN EQ exp
200 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
202 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
207 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
209 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
216 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
217 id tyfields type_id exp
225 sprintf "vardec[%s, exp[%s]]" id exp
227 | VAR ID COLON ID ASSIGN exp
232 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
238 let type_id_new = $2 in
239 let type_id_orig = $4 in
240 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
242 | TYPE ID EQ LBRACE RBRACE
245 sprintf "tydec_empty_record[%s]" type_id
247 | TYPE ID EQ LBRACE tyfields RBRACE
251 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
253 | TYPE ID EQ ARRAY OF ID
256 let element_type_id = $6 in
257 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
264 | tyfield COMMA tyfields
268 sprintf "%s, %s" tyfield tyfields
276 sprintf "tyfield[%s, %s]" id type_id
279 rec_field_assignments:
284 sprintf "%S = %s" id exp
286 | ID EQ exp COMMA rec_field_assignments
290 let rec_field_assignments = $5 in
291 sprintf "%S = %s, %s" id exp rec_field_assignments
301 sprintf "%s, %s" $1 $3
322 sprintf "lvalue[%s]" id
328 sprintf "get_record_field[%s, %s]" record field
330 | lvalue LBRACK exp RBRACK
333 let subscript = $3 in
334 sprintf "get_array_subscript[%s, %s]" array subscript