43 %token <string> STRING
51 /* from lowest precedence */
58 %nonassoc EQ NEQ GT LT GE LE
62 /* to highest precedence */
64 %type <string> program
73 sprintf "program[%s]" $1
86 | MINUS exp %prec UMINUS
88 sprintf "negation[%s]" $2
90 | ID LBRACK exp RBRACK OF exp
93 let number_of_elements = $3 in
94 let initial_value = $6 in
96 "array[type[%s], size[%s], val[%s]]"
97 type_id number_of_elements initial_value
99 | ID LBRACE rec_field_assignments RBRACE
102 let rec_field_assignments = $3 in
104 "record[type[%s], rec_field_assignments[%s]]"
105 type_id rec_field_assignments
113 sprintf "assign[%s := %s]" $1 $3
117 sprintf "string[%S]" $1
122 sprintf "fun_call[%s, []]" id
124 | ID LPAREN fun_args RPAREN
128 sprintf "fun_call[%s, %s]" id fun_args
132 sprintf "op[%s %s %s]" $1 $2 $3
134 | IF exp THEN exp ELSE exp
139 sprintf "if_then_else[%s, then[%s], else[%s]]" e1 e2 e3
143 sprintf "if_then[%s, then[%s]]" $2 $4
147 sprintf "while[%s, do[%s]]" $2 $4
149 | FOR ID ASSIGN exp TO exp DO exp
155 sprintf "for[%s := %s, to[%s], do[%s]]" id e1 e2 e3
163 sprintf "exps[%s]" $2
165 | LET decs IN exps END
169 sprintf "let[decs[%s], in[exps[%s]]]" decs exps
173 (* Perhaps "void"? *)
178 rec_field_assignments:
183 sprintf "%S = %s" id exp
185 | ID EQ exp COMMA rec_field_assignments
189 let rec_field_assignments = $5 in
190 sprintf "%S = %s, %s" id exp rec_field_assignments
204 sprintf "%s; %s" exp exps
215 sprintf "%s %s" $1 $2
223 let type_id_new = $2 in
224 let type_id_orig = $4 in
225 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
227 | TYPE ID EQ LBRACE RBRACE
230 sprintf "tydec_empty_record[%s]" type_id
232 | TYPE ID EQ LBRACE tyfields RBRACE
236 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
238 | TYPE ID EQ ARRAY OF ID
241 let element_type_id = $6 in
242 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
250 sprintf "vardec[%s, exp[%s]]" id exp
252 | VAR ID COLON ID ASSIGN exp
257 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
261 | FUNCTION ID LPAREN RPAREN EQ exp
265 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
267 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
272 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
274 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
281 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
282 id tyfields type_id exp
291 sprintf "%s : %s" id_1 id_2
293 | ID COLON ID COMMA tyfields
297 let tyfield = sprintf "%s : %s" id_1 id_2 in
299 sprintf "%s, %s" tyfield tyfields
310 sprintf "%s, %s" $1 $3
334 sprintf "lvalue[%s, part[%s]]" id part
340 | lvalue_subscript {$1}
341 | lvalue_field_access {$1}
348 sprintf "subscript[%s]" exp
356 sprintf "field_access[%s]" field