c109f07f - CSE 109 Final Exam Wednesday 12 December 2007...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Final Exam Wednesday 12 December 2007 8:00-11:00 AM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<< 1. Read both this question and question 2 before starting this question. Write a class Lex to be used by class Parse in question 2. Class Parse is used to parse a single line of text from the console. The only symbols allowed by class Parse are '<', '>', '[', ']', '{', '}', '0', '1', '.', and '\n'. The method Lex::next() should return an int corresponding to the above symbols, 0 for '<', 1 for '>', ... , 9 for '\n', and 10 for anything else. The program below indicates how class Lex should behave. If the input line is t < > 01. { [ then the output would be t('JUNK') <('<') >('>') 0('0') 1('1') .('.') {('{') [('[') where blanks and tabs are ignored int main(){ char* table[]={"<", ">", "[", "]", "{", "}", "0", "1", ".", "\n","JUNK"}; //the entries in 'table' are arranged according to the value that should //be returned by next(). a.getCh() returns the most recent character read Lex a; int tok; tok=a.next(); while(tok!=Lex::EOLN){ cout<<a.getCh()<<"('"<<table[tok]<<"') "; tok=a.next(); } cout<<endl; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include <iostream> using namespace std; u class Lex{//Ignore blanks and tabs, return one of the constants below public: Lex(); static const int LANGL=0,RANGL=1,LSQB=2,RSQB=3,LCURL=4,RCURL=5, ZERO=6,ONE=7,PERIOD=8,EOLN=9,JUNK=10; int next(); char getCh()const; private: char ch; }; } Lex::Lex(){ch=' ';} L int Lex::next(){ char table[]="<>[]{}01.\n"; if(ch!='\n'){ cin.get(ch); while(ch==' ' || ch=='\t') cin.get(ch); for(int j=0;j<10;j++) if(ch==table[j]) return j; return JUNK; } else return EOLN; } char Lex::getCh()const{
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
return ch; } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Write a class Parse that uses class Lex above to determine whether a line entered at the console obeys the syntax of the diagrams for P given below. Below the digrams is a program that illustrates the use of the class Parse. In the diagrams parentheses indicate a circle and square brackets indicate a rectangle. int main(){ Parse p; p.parse();//diagnose error or write out "Successful parse" return 0; } +---( < )--[ A ]--( > )--+ A | | P ------|---( [ )--[ B ]--( ] )--|------> -->[ A ]-------------> | | ^ | +---( { )--[ C ]--( } )--+ | | B +--[ A ]-+ --------( 0 )---------------------------> | | ^ | +-->( 1 )->+ | | C |<-----( 0 )<--| ------( 0 )--( . )--[ B ]------------> | | | | +<-----( 1 )<--+ +->( [ )--[ P ]--( ] )-->+ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< class Parse{ public: Parse(); void parse(); private: int tok; Lex lex; void p(); void A(); void B(); void C(); void check(bool b,char *mess); void next(); }; } Parse::Parse(){} P void Parse::next(){ tok=lex.next(); cout<<lex.getCh(); } void Parse::parse(){ next(); p(); check(tok==Lex::EOLN,"Junk at end of line"); cout<<"Successful parse\n"; } void Parse::p(){ A(); while(tok==Lex::LANGL || tok==Lex::LSQB || tok==Lex::LCURL)
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern