TERMINAL(2) Demeter Software TERMINAL(2) NAME Terminal - Abstract class, root of the hierarchy of terminal classes. DESCRIPTION Class Terminal has five subclasses: DemIdent, DemNumber, DemReal, DemString and DemText. Terminal class objects sup- port the following methods. int get_line_number() const; This method returns the number of the line in the input file to _g__p_a_r_s_e where a terminal was found. SUBCLASSES DemNumber Objects of class DemNumber contain integer numbers. The printed representation of a DemNumber object is a string of digits representing the integer stored in the object. Class DemNumber supports the standard arithmetic operators by providing an int coercion operator and the corresponding assignment operators. A list of the arithmetic operators implemented by class DemNumber follows. operator int() const DemNumber& operator=( const int n ) DemNumber* operator+=( const int i ) DemNumber* operator+=( const DemNumber* n ) DemNumber* operator-=( const int i ) DemNumber* operator-=( const DemNumber* n ) DemNumber* operator*=( const int i ) DemNumber* operator*=( const DemNumber* n ) DemNumber* operator/=( const int i ) DemNumber* operator/=( const DemNumber* n ) DemNumber* operator%=( const int i ) DemNumber* operator%=( const DemNumber* n ) With these operators it is not only possible to operate Dem- Number objects but also operate them in combination with integer numbers as the following example illustrates. DemNumber* iDemNumber1 = new DemNumber( 3 ); DemNumber* iDemNumber2 = new DemNumber( 7 ); Demeter Last change: 30 March 1992 1 TERMINAL(2) Demeter Software TERMINAL(2) DemNumber* iDemNumber3 = new DemNumber( 9 ); iDemNumber3 += *iDemNumber1 + 8 + *iDemNumber2; After the last assignment, iDemNumber3 should contain the integer number 27. Notice how the objects iDemNumber1 and iDemNumber2 are dereferenced when used as operands in the last assignment. This invokes the int() operator that coerces them into integer numbers. From then on, the integer + operator is used until the += operator is invoked, which is the one attached to DemNumber. DemReal Objects of class DemReal contain floating point numbers. The printed representation of a DemReal object is the integer part, followed by a decimal point, followed by the decimal part of the number stored in the object. Parsing a legal DemReal object requires the presence of the decimal point. Class DemReal supports the standard arithmetic operators by providing double coercion operator and the corresponding assignment operators. A list of the arithmetic operators implemented by class DemReal follows. operator double() const DemReal& operator=( const double d ) DemReal* operator+=( const double d ) DemReal* operator+=( const DemReal* r ) DemReal* operator-=( const double d ) DemReal* operator-=( const DemReal* r ) DemReal* operator*=( const double d ) DemReal* operator*=( const DemReal* r ) DemReal* operator/=( const double d ) DemReal* operator/=( const DemReal* r ) With these operators it is not only possible to operate Dem- Real objects but also operate them in combination with dou- ble numbers as the following example illustrates. DemReal* iDemReal1 = new DemReal( 3.0 ); DemReal* iDemReal2 = new DemReal( 7.0 ); DemReal* iDemReal3 = new DemReal( 9.0 ); iDemReal3 += *iDemReal1 + 8.0 + *iDemReal2; After the last assignment, iDemReal3 should contain the dou- ble number 27.0. Notice how the objects iDemReal1 and iDem- Real2 are dereferenced when used as operands in the last assignment. This invokes the double() operator that coerces Demeter Last change: 30 March 1992 2 TERMINAL(2) Demeter Software TERMINAL(2) them into double numbers. From then on, the double + opera- tor is used until the += operator is invoked, which is the one attached to DemReal. DemIdent Objects of class DemIdent contain strings of alphanumeric characters which start with an alphabetic character. Class DemIdent supports the comparison operators == and != by providing a char* coercion operator as described by the following interfaces. operator char*() const int operator==( const char* str ) int operator==( const DemIdent& other ) int operator!=( const char* str ) int operator!=( const DemIdent& other ) With these operators it is possible to operate DemIdent objects with other DemIdent objects as well as with charac- ter strings as the following example illustrates. DemIdent* iDemIdent1 = new DemIdent( "Id1" ); DemIdent* iDemIdent2 = new DemIdent( "Id2" ); char* str = "Id1"; if( *iDemIdent1 == str && *iDemIdent1 != *DemIdent2 && strcmp( str,*DemIdent2 ) ) cout << "Success"; After this program segment is executed, the string "Success" should be printed. Notice how the objects iDemIdent1 and iDemIdent2 are dereferenced when used as operands in the comparison. This ensures that the appropriate operators are invoked, and that the char*() operator is invoked to coerce iDemIdent2 into a character string. DemString Objects of class DemString contain strings of any character, except for quotation marks (") and new line characters. The printed representation of a DemString object is "", where is the contents of the DemString object. Parsing a legal DemString object requires the presence of the delimiting quotation marks, although the marks are not stored in the object. Class DemString supports the comparison operators == and != by providing a char* coercion operator as described by the Demeter Last change: 30 March 1992 3 TERMINAL(2) Demeter Software TERMINAL(2) following interfaces. operator char*() const int operator==( const char* str ) int operator==( const DemString& other ) int operator!=( const char* str ) int operator!=( const DemString& other ) With these operators it is possible to operate DemString objects with other DemString objects as well as with charac- ter strings as the following example illustrates. DemString* iDemString1 = new DemString( "Str1" ); DemString* iDemString2 = new DemString( "Str2" ); char* str = "Str1"; if( *iDemString1 == str && *iDemString1 != *DemString2 && strcmp( str,*DemString2 ) ) cout << "Success"; After this program segment is executed, the string "Success" should be printed. Notice how the objects iDemString1 and iDemString2 are dereferenced when used as operands in the comparison. This ensures that the appropriate operators are invoked, and that the char*() operator is invoked to coerce iDemString2 into a character string. DemText Objects of class DemText contain strings of up to 10k char- acters which can include any character (blanks, tabs, new lines, etc.) The printed representation of a DemText object is (@ @), where is the contents of the DemText object. Parsing a legal DemText object requires the presence of the delimiters (@ and @), although the delim- iters are not stored in the object. Also, cannot contain the character "@" since it will confuse the scanner. For efficiency, the length of is also stored in the object, and can be accessed with the get_len() method. Class DemText supports the comparison operators == and != by providing a char* coercion operator as described by the fol- lowing interfaces. operator char*() const int operator==( const char* str ) int operator==( const DemText& other ) int operator!=( const char* str ) Demeter Last change: 30 March 1992 4 TERMINAL(2) Demeter Software TERMINAL(2) int operator!=( const DemText& other ) With these operators it is possible to operate DemText objects with other DemText objects as well as with character strings as the following example illustrates. DemText* iDemText1 = new DemText( "Text1" ); DemText* iDemText2 = new DemText( "Text2" ); char* str = "Text1"; if( *iDemText1 == str && *iDemText1 != *DemText2 && strcmp( str,*DemText2 ) ) cout << "Success"; After this program segment is executed, the string "Success" should be printed. Notice how the objects iDemText1 and iDemText2 are dereferenced when used as operands in the com- parison. This ensures that the appropriate operators are invoked, and that the char*() operator is invoked to coerce iDemText2 into a character string. EXAMPLE The following class dictionary illustrates the use of the terminal classes DemIdent and DemText. CodeFragment = CFKind DemIdent DemText. CFKind : Primary | Before | After. Primary = "*primary*". Before = "*before*". After = "*after*". *terminal_sets* DemIdent, DemNumber, DemReal, DemString, DemText. The following object is a legal CodeFragment object contain- ing an DemIdent and a DemText nested objects. *primary* Adjacency (@ if( this->g_equal( adj ) ) result = this; @) Notice that the class dictionary in this example is actually a legal object of yet another class dictionary which uses the terminal classes DemIdent and DemString. The subobjects "*primary*", "*before*", and "*after*" are objects of class DemString, nested in this class dictionary object. SEE ALSO g_parse(2) Demeter Last change: 30 March 1992 5