G_PRINT(2) Demeter Software G_PRINT(2) NAME g_print - sends the generic print message to an object. SYNOPSIS void Universal:: _g__p_r_i_n_t ( ostream& strm = cout ) DESCRIPTION _g__p_r_i_n_t prints out the object to which the message is sent in textual form, according to the printing commands in the file _c_d-_p_r_i_n_t. The receiving object must be a tree object. That is, it cannot have any cycles or shared subobjects. _g__p_r_i_n_t is a generic method implemented at class Universal and inherited by any user class. RETURN VALUES Once an object has been created or parsed in, it can be sent the g_print message. For example: Compound* iCompound = new Compound(); iCompound = (Compound*)iCompound->g_parse("demeter-input"); iCompound->g_print(); If the _s_t_r_m argument is provided, then the output is sent to the ostream in the given _s_t_r_m. For example: Compound* iCompound = new Compound(); iCompound = (Compound*)iCompound->g_parse("demeter-input"); ofstream outFile( "demeter-output" ); iCompound->g_print( outFile ); outFile.close; Notice that since ofstream is a subclass of ostream in the C++ class library, both output to the screen and to a disk file can be handled smoothly by _g__p_r_i_n_t. EXAMPLE The file _c_d-_p_r_i_n_t, created by Demeter from the class dic- tionary at generation time, contains the grammar the generic printer uses in printing the object to which the message is sent. In this file the user can add or remove printing com- mands to modify the layout of the output text. There are four printing commands that can be used: *l Add new line. *s Add space. + Indent. _g__p_r_i_n_t starts a new indentation level. Demeter Last change: 8 April 1991 1 G_PRINT(2) Demeter Software G_PRINT(2) Indentation is by three spaces. - Unindent. _g__p_r_i_n_t ends the current indentation level. As an example of the effect of the printing commands, with the following contents in _c_d-_p_r_i_n_t: Example = Prefix_List. Prefix : Numerical | Compound. Numerical = Real. Compound = "(" Op Prefix_List ")" Real. Op : MulSym | AddSym | SubSym. MulSym = "*". AddSym = "+". SubSym = "-". Prefix_List ~ Prefix { Prefix }. *terminal_sets* Number, Ident, String, Real. the object with input text: (+ (* 3.0 4.0) 12.0 5.7) 17.7 is printed as ( + ( * 3.000000 4.000000 ) 12.000000 5.700000 ) 17.700000. The following modification of _c_d-_p_r_i_n_t adds indentation and unindentation commands around the args part of the compound production. We also add new lines around the args part and before each repeated Prefix of a Prefix_list. The modified _c_d-_p_r_i_n_t is now Example = Prefix_List. Prefix : Numerical | Compound. Numerical = Real. Compound = "(" Op *l + Prefix_List - *l ")" "value =" Real. Op : MulSym | AddSym | SubSym. Demeter Last change: 8 April 1991 2 G_PRINT(2) Demeter Software G_PRINT(2) MulSym = "*". AddSym = "+". SubSym = "-". Prefix_List ~ Prefix { *l Prefix }. *terminal_sets* Number, Ident, String, Real. and the output is ( + ( * 3.000000 4.000000 ) value = 12.000000 5.700000 ) value = 17.700000 Notice that we have also added the constant string "value =" before the val part in the same production Compound. This addition of concrete syntax does not require recompilation and produces the improved output shown. FILES cd-print REFERENCES _U_s_e_r _M_a_n_u_a_l _f_o_r _T_h_e _C++ _D_e_m_e_t_e_r Walter L. Hursch Northeastern University, 1993 _T_h_e _A_n_n_o_t_a_t_e_d _C++ _R_e_f_e_r_e_n_c_e _M_a_n_u_a_l Margaret A. Ellis and Bjarne Stroustrup Addison-Wesley, 1990 _C++ _P_r_i_m_e_r Stanley B. Lippman Addison-Wesley, 1989 Demeter Last change: 8 April 1991 3