43 %token <string> STRING
51 /* from lowest precedence */
56 %nonassoc EQ NEQ GT LT GE LE
60 /* to highest precedence */
62 %type <string> program
71 sprintf "program[%s]" $1
84 | MINUS exp %prec UMINUS
86 sprintf "negation[%s]" $2
88 | ID LBRACK exp RBRACK OF exp
91 let number_of_elements = $3 in
92 let initial_value = $6 in
94 "array[type[%s], size[%s], val[%s]]"
95 type_id number_of_elements initial_value
97 | ID LBRACE rec_field_assignments RBRACE
100 let rec_field_assignments = $3 in
102 "record[type[%s], rec_field_assignments[%s]]"
103 type_id rec_field_assignments
111 sprintf "assign[%s := %s]" $1 $3
115 sprintf "string[%S]" $1
120 sprintf "fun_call[%s, []]" id
122 | ID LPAREN fun_args RPAREN
126 sprintf "fun_call[%s, %s]" id fun_args
130 sprintf "op[%s %s %s]" $1 $2 $3
132 | IF exp THEN exp ELSE exp
137 sprintf "if_then_else[%s, then[%s], else[%s]]" e1 e2 e3
141 sprintf "if_then[%s, then[%s]]" $2 $4
145 sprintf "while[%s, do[%s]]" $2 $4
147 | FOR ID ASSIGN exp TO exp DO exp
153 sprintf "for[%s := %s, to[%s], do[%s]]" id e1 e2 e3
161 sprintf "exps[%s]" $2
163 | LET decs IN exps END
167 sprintf "let[decs[%s], in[exps[%s]]]" decs exps
171 (* Perhaps "void"? *)
176 rec_field_assignments:
181 sprintf "%S = %s" id exp
183 | ID EQ exp COMMA rec_field_assignments
187 let rec_field_assignments = $5 in
188 sprintf "%S = %s, %s" id exp rec_field_assignments
202 sprintf "%s; %s" exp exps
213 sprintf "%s %s" $1 $2
221 let type_id_new = $2 in
222 let type_id_orig = $4 in
223 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
225 | TYPE ID EQ LBRACE RBRACE
228 sprintf "tydec_empty_record[%s]" type_id
230 | TYPE ID EQ LBRACE tyfields RBRACE
234 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
236 | TYPE ID EQ ARRAY OF ID
239 let element_type_id = $6 in
240 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
248 sprintf "vardec[%s, exp[%s]]" id exp
250 | VAR ID COLON ID ASSIGN exp
255 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
259 | FUNCTION ID LPAREN RPAREN EQ exp
263 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
265 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
270 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
272 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
279 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
280 id tyfields type_id exp
289 sprintf "%s : %s" id_1 id_2
291 | ID COLON ID COMMA tyfields
295 let tyfield = sprintf "%s : %s" id_1 id_2 in
297 sprintf "%s, %s" tyfield tyfields
308 sprintf "%s, %s" $1 $3
332 sprintf "lvalue[%s, part[%s]]" id part
338 | lvalue_subscript {$1}
339 | lvalue_field_access {$1}
346 sprintf "subscript[%s]" exp
354 sprintf "field_access[%s]" field