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

Info iconThis 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{
Background image of page 1

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

View Full DocumentRight 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);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/09/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 8

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online