Xref: utzoo comp.lang.c:11688 comp.lang.scheme.c:5 Path: utzoo!utgpu!attcan!uunet!husc6!bloom-beacon!mit-eddie!rutgers!att!mtuxo!mtgzz!drutx!druin!ldg From: ldg@druin.ATT.COM (Doug Gibbons) Newsgroups: comp.lang.c,comp.lang.scheme.c Subject: Re: Need C language Description Message-ID: <288@druin.ATT.COM> Date: 3 Aug 88 15:37:32 GMT References: <12707@agate.BERKELEY.EDU> Organization: AT&T, Denver, CO Lines: 85 Attribute grammars are yet another type of formal language definition, combining context free syntax with context sensitive semantics. An MS thesis I did while at the University of Colorado described the syntax and static semantics of C using an attribute grammar written in Aladin. Since I was finishing just as X3J11 was starting, the grammar does not describe ANSIisms. Below is a grammar excerpt describing a C shift expression. The CFG production appears first, followed by the attribution and context conditions which must hold true. With a little effort, you can get a feel for what is going on here. The "{in|out}" attributes are used to thread various symbol tables through the production. The interesting things computed here are: SHIFT_EXP[1].at_type the expression's type SHIFT_EXP[1].at_value the expression's value SHIFT_EXP[1].at_eval_time when value can be known SHIFT_EXP[1].at_const_exp is it a constant expression? ------------------------------------------------------------------------------- rule r_39 : SHIFT_EXP ::= SHIFT_EXP SHIFT_OP ADD_EXP static % % Do the UAC balancing on the operands. Both must be integral types. % The type is that of the converted left operand. If the right operand % is negative or greater than the size (in bits) of the type of the % left after the UAC, then the result is undefined. % SHIFT_EXP[1].at_const_exp := SHIFT_EXP[2].at_const_exp and ADD_EXP.at_const_exp; SHIFT_EXP[1].at_eval_time := if f_is_defined(SHIFT_EXP[1].at_value) then sc_constant else sc_run_time fi; SHIFT_EXP[1].at_id_defs_out := ADD_EXP.at_id_defs_out; SHIFT_EXP[1].at_lval_class := sc_rval; SHIFT_EXP[1].at_tag_names_out := ADD_EXP.at_tag_names_out; SHIFT_EXP[1].at_tag_props_out := ADD_EXP.at_tag_props_out; SHIFT_EXP[1].at_type := f_balance(SHIFT_EXP[2].at_type, ADD_EXP.at_type); SHIFT_EXP[1].at_value := f_eval_binary ( SHIFT_OP.at_op, SHIFT_EXP[2].at_value, ADD_EXP.at_value); SHIFT_EXP[2].at_id_defs_in := SHIFT_EXP[1].at_id_defs_in; SHIFT_EXP[2].at_tag_names_in := SHIFT_EXP[1].at_tag_names_in; SHIFT_EXP[2].at_tag_props_in := SHIFT_EXP[1].at_tag_props_in; ADD_EXP.at_id_defs_in := SHIFT_EXP[2].at_id_defs_out; ADD_EXP.at_tag_names_in := SHIFT_EXP[2].at_tag_names_out; ADD_EXP.at_tag_props_in := SHIFT_EXP[2].at_tag_props_out; condition f_is_integral(SHIFT_EXP[2].at_type) and f_is_integral(ADD_EXP.at_type) message "error: shift requires integral operands"; condition not f_is_neg_i(SHIFT_EXP[1].at_value) message "warning: result of negative shift is non-portable"; condition if f_is_defined(ADD_EXP.at_value) then f_is_gt_i ( ADD_EXP.at_value, f_mach_val ( f_mach_bit_sizeof(SHIFT_EXP[1].at_type), sc_uint)) else true fi message "warning: magnitude of right operand of shift causes undefined value"; end; ------------------------------------------------------------------------------- -- Doug Gibbons | ldg@druhi.ATT.COM or att!druhi!ldg -- AT&T Bell Laboratories -- Denver CO