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_plus[%s + %s]" $1 $3
136 sprintf "op_minus[%s - %s]" $1 $3
140 sprintf "op_times[%s * %s]" $1 $3
144 sprintf "op_divide[%s / %s]" $1 $3
148 sprintf "op_eq[%s = %s]" $1 $3
152 sprintf "op_neq[%s <> %s]" $1 $3
156 sprintf "op_gt[%s > %s]" $1 $3
160 sprintf "op_lt[%s < %s]" $1 $3
164 sprintf "op_ge[%s >= %s]" $1 $3
168 sprintf "op_le[%s <= %s]" $1 $3
172 sprintf "op_and[%s & %s]" $1 $3
176 sprintf "op_or[%s | %s]" $1 $3
178 | IF exp THEN exp ELSE exp
183 sprintf "if_then_else[%s, then[%s], else[%s]]" e1 e2 e3
187 sprintf "if_then[%s, then[%s]]" $2 $4
191 sprintf "while[%s, do[%s]]" $2 $4
193 | FOR ID ASSIGN exp TO exp DO exp
199 sprintf "for[%s := %s, to[%s], do[%s]]" id e1 e2 e3
207 sprintf "exps[%s]" $2
209 | LET decs IN exps END
213 sprintf "let[decs[%s], in[exps[%s]]]" decs exps
217 (* Perhaps "void"? *)
222 rec_field_assignments:
227 sprintf "%S = %s" id exp
229 | ID EQ exp COMMA rec_field_assignments
233 let rec_field_assignments = $5 in
234 sprintf "%S = %s, %s" id exp rec_field_assignments
248 sprintf "%s; %s" exp exps
259 sprintf "%s %s" $1 $2
267 let type_id_new = $2 in
268 let type_id_orig = $4 in
269 sprintf "tydec_alias[from[%s], to[%s]]" type_id_new type_id_orig
271 | TYPE ID EQ LBRACE RBRACE
274 sprintf "tydec_empty_record[%s]" type_id
276 | TYPE ID EQ LBRACE tyfields RBRACE
280 sprintf "tydec_record[%s, fields[%s]]" type_id tyfields
282 | TYPE ID EQ ARRAY OF ID
285 let element_type_id = $6 in
286 sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
294 sprintf "vardec[%s, exp[%s]]" id exp
296 | VAR ID COLON ID ASSIGN exp
301 sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
305 | FUNCTION ID LPAREN RPAREN EQ exp
309 sprintf "fundec[%s, arguments[], exp[%s]]" id exp
311 | FUNCTION ID LPAREN tyfields RPAREN EQ exp
316 sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
318 | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
325 "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
326 id tyfields type_id exp
335 sprintf "%s : %s" id_1 id_2
337 | ID COLON ID COMMA tyfields
341 let tyfield = sprintf "%s : %s" id_1 id_2 in
343 sprintf "%s, %s" tyfield tyfields
354 sprintf "%s, %s" $1 $3
363 sprintf "lvalue[%s, part[%s]]" id part
369 | lvalue_subscript {$1}
370 | lvalue_field_access {$1}
377 sprintf "subscript[%s]" exp
385 sprintf "field_access[%s]" field