CSC 109 Test 2 SPRING

CSC 109 Test 2 SPRING - CSc 109 Test 2 Wednesday 25 April...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSc 109 Test 2 Wednesday 25 April 2001 SUGGESTED ANSWERS 1. (See also question 2, which uses the answer to this question) Write a class Toke which reads from an input stream and analyzes the stream one character at a time. The class should have the public method sym() which returns an int and public method next() which gets the next character in the input stream. The method sym() only returns one of four possible values: if the character read is '{' then sym() returns Toke::LPAR, if the character read is '}' then sym() returns Toke::RPAR, if the character read is '\n' then sym() returns Toke::EOLN; otherwise sym() returns Toke::JUNK. In practice, the above constants should have the values LPAR==0, RPAR==1, EOLN==2, JUNK==3. The class Toke might be used as follows: Toke t(cin); cout<<t.sym()<<endl; t.next(); cout<<t.sym()<<endl; .... class Toke{ public: static const int LPAR=0, RPAR=1, EOLN=2, JUNK=3; Toke(istream &in); int sym(); void next(); private: istream *fin; int val; char ch; }; Toke::Toke(istream &in){ fin=&in; ch='z'; //junk to prime the pump next(); } int Toke::sym(){ return val; } void Toke::next(){ if(fin->good()) fin->get(ch); if(!fin->good()) ch='\n'; switch(ch){ case '{': val = LPAR; break; case '}': val = RPAR; break; case '\n': val = EOLN; break; default: val=JUNK; } } 2. Assume we have the grammar given by the following syntax diagrams.-...
View Full Document

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

Page1 / 4

CSC 109 Test 2 SPRING - CSc 109 Test 2 Wednesday 25 April...

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

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