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"? *)
170 rec_field_assignments:
175 sprintf "%S = %s" id exp
177 | ID EQ exp COMMA rec_field_assignments
181 let rec_field_assignments = $5 in
182 sprintf "%S = %s, %s" id exp rec_field_assignments
192 sprintf "%s; %s" $1 $3
202 sprintf "%s %s" $1 $2
209 let type_id_new = $2 in
210 let type_id_orig = $4 in
211 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
213 | TYPE ID EQ LBRACE RBRACE
216 sprintf "tydec_empty_record[%s]" type_id
218 | TYPE ID EQ LBRACE tyfields RBRACE
222 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
224 | TYPE ID EQ ARRAY OF ID
227 let element_type_id = $6 in
228 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
236 sprintf "vardec[%s, exp[%s]]" id exp
238 | VAR ID COLON ID ASSIGN exp
243 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
247 | FUNCTION ID LPAREN RPAREN EQ exp
251 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
253 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
258 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
260 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
267 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
268 id tyfields type_id exp
276 sprintf "%s : %s" id_1 id_2
278 | ID COLON ID COMMA tyfields
282 let tyfield = sprintf "%s : %s" id_1 id_2 in
284 sprintf "%s, %s" tyfield tyfields
294 sprintf "%s, %s" $1 $3
315 sprintf "lvalue[%s]" id
321 sprintf "get_record_field[%s, %s]" record field
323 | lvalue LBRACK exp RBRACK
326 let subscript = $3 in
327 sprintf "get_array_subscript[%s, %s]" array subscript