.TH G_PRINT 2 "8 April 1991" "Demeter" "Demeter Software" .SH NAME g_print \- sends the generic print message to an object. .SH SYNOPSIS void Universal:: .I g_print ( ostream& strm = cout ) .SH DESCRIPTION .I g_print prints out the object to which the message is sent in textual form, according to the printing commands in the file .I cd-print. The receiving object must be a tree object. That is, it cannot have any cycles or shared subobjects. .I g_print is a generic method implemented at class Universal and inherited by any user class. .SH 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(); .br iCompound = (Compound*)iCompound->g_parse("demeter-input"); iCompound->g_print(); If the .I strm argument is provided, then the output is sent to the ostream in the given .I strm. For example: Compound* iCompound = new Compound(); .br iCompound = (Compound*)iCompound->g_parse("demeter-input"); .br ofstream outFile( "demeter-output" ); .br iCompound->g_print( outFile ); .br 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 .I g_print. .SH EXAMPLE The file .I cd-print, created by Demeter from the class dictionary 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 commands to modify the layout of the output text. There are four printing commands that can be used: .TP 10 *l Add new line. .TP *s Add space. .TP + Indent. .I g_print starts a new indentation level. Indentation is by three spaces. .TP - Unindent. .I g_print ends the current indentation level. .LP As an example of the effect of the printing commands, with the following contents in .I cd-print: .KS Example = Prefix_List. .br Prefix : Numerical | .br Compound. .br Numerical = Real. .br Compound = "(" .br Op .br Prefix_List .br ")" .br Real. .br Op : MulSym | .br AddSym | .br SubSym. .br MulSym = "*". .br AddSym = "+". .br SubSym = "-". .br Prefix_List ~ Prefix { Prefix }. .br *terminal_sets* Number, Ident, String, Real. .KE 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 .I cd-print 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 .I cd-print is now .KS Example = Prefix_List. .br Prefix : Numerical | .br Compound. .br Numerical = Real. .br Compound = "(" .br Op *l + .br Prefix_List .br - *l ")" "value =" .br Real. .br Op : MulSym | .br AddSym | .br SubSym. .br MulSym = "*". .br AddSym = "+". .br SubSym = "-". .br Prefix_List ~ Prefix { *l Prefix }. .br *terminal_sets* Number, Ident, String, Real. .KE and the output is .KS ( + .br ( * .br 3.000000 .br 4.000000 .br ) value = 12.000000 .br 5.700000 .br ) value = 17.700000 .KE 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. .SH FILES cd-print .SH REFERENCES .I User Manual for The C++ Demeter System .br Walter L. Hursch .br Northeastern University, 1993 .I The Annotated C++ Reference Manual .br Margaret A. Ellis and Bjarne Stroustrup .br Addison-Wesley, 1990 .I C++ Primer .br Stanley B. Lippman .br Addison-Wesley, 1989