CSc 109 Test 1 Wednesday 24 April 2002 =============== SUGGESTED ANSWERS ================== 1. (25 pts) Given the syntax diagrams below (where [] stands for a box and () stands for a circle), write the declarations and definitions for the class Parse such that the program in main() displays "Success" if and only if a line of text entered from the console satisfies the diagram for A. I have started the answer. You can assume that the members I have declared have been defined. Your program should assume that no blanks or tabs are allowed. A B ----> (a) --->[B]----------------> ----------->(c)-->[B]-->(c)----- ^ | | | | | |----(d)----------------->| [B]<--(z)<- | | |----(e)----->[A]-----------> int main() {Parse p; p.par(); cout<<"Success\n"; } class Parse {public: Parse(); private: static void errCheck(bool b,char *mess);//if b display message and exit() char ch; // ..... your private and public declarations are below <<<<<<<<<<<<<< #include #include |----(e)----->[A]----------- class Parse {public: Parse(); private: static void errCheck(bool b,char *mess);//if b display message and exit() char ch; public: void par(); private: void A(); void B(); }; int main() {Parse p; p.par(); cout<<"Success\n"; cin.get(); cin.get(); } void Parse::par() {cin.get(ch); A(); cout<<"\nch == "< a,b(3); Safe d(23.4); cout< 3 23.4 Undefined Value */ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include template class Safe {public: Safe(); Safe(const T&t); template friend ostream & operator<<(ostream &out,const Safe & s); private: bool set; T value; }; template Safe::Safe() {set=false;} template Safe::Safe(const T&t) {set=true; value=t; } template ostream & operator<<(ostream &out,const Safe & s) {if(!s.set) out<<"Undefined Value"; else out< a,b(3); Safe c,d(23.4); cout<(,)--->[boolExp]--->(,)---> (Here the parentheses are replaced by pairs of commas (,) ) Thus, the user could write b = ,false or true, and false which would produce a different result than b = false or true and false. Now we need to write a program to compile Modest programs with such expressions. That turns out to be quite simple. We can create a subclass of FullParser which overloads boolFactor(), where the overloaded boolFactor() checks for a comma. If it finds a comma it deals with it. Otherwise it passes the token back to the FullParser version of boolFactor. Call the subclass UParser. In the main program we need only change the declaration of the parser from FullParser p; to UParser p; Below I list my declaration of FullParser. First, indicate what changes need to be made to the declaration for class FullParser. Second, provide the declaration and definitions (code) for class UParser. class FullParser: public Parser {public: FullParser(istream &in=cin,ostream &out=cout,ostream & fcode=cout); private: void boolExp(); void intExp(); void intTerm(); void boolTerm(); void boolFactor(); void intFactor(); void genLogicCode(int op); }; <<<<<<<<<<<<<<<<<<<<<<<<<< // in class FullParser change "private:" to "protected:" // prefixe void boolFactor(); with "virtual " class UParser:public FullParser {public: UParser(istream & in=cin,ostream & out=cout,ostream &codeout=cout); void boolFactor(); }; UParser::UParser(istream & in,ostream &out,ostream &codeout) :FullParser(in,out,codeout){} void UParser::boolFactor() {if(token==Tokenize::COMMA) {token=tok->next(); boolExp(); errCheck(token!=Tokenize::COMMA,"comma"); token=tok->next(); } else FullParser::boolFactor(); }