c109s06f - CSE 109 Final Examination Friday 5 May 2006...

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

View Full Document Right Arrow Icon
CSE 109 Final Examination Friday 5 May 2006 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<SUGGESTED ANSWERS>>>>>>>>>>>>>>>>>>>>>>>> 1. Questions 1 and 2 deal with different parts of the same problem. Read both questions before starting. Expressions like *&*a.b()->c.gg().e.f are syntactically correct in C++ and satisfy the following syntax diagrams for A, where () denotes ovals and [] denotes boxes. X A +-('&')--[A]--+ --[Y]----+------------------> ------|-('*')--[A]--|---> ^ | +--('->')--+ +--[X]--------+ | +-->| |--[Y]-+ Y +-----------------+ | +--('.')---+ | ----(Ident)----| |--> +-<----------------<-----+ +--('(')--(')')---+ (where, although not relevant to this question, Ident is a sequence of one or more lower case letters). Write a class CParse that gets tokens from a lexical analyzer of type Flex (see question 2) that returns one of the Flex constants AMPERSAND(0), SPLAT(1), IDENT(2), PERIOD(3), ARROW(4), LPAR(5), RPAR(6), EOLN(7), and JUNK(8). The method parse() should determine whether a sequence of tokens from one line of a file satisfies the above syntax. If the line does, then parse() does nothing; if it does not, parse() diagnoses the first error. The following (sloppy) code shows how CParse would be used. int main() {ifstream f("cparse.dat"); CParse p(f); p.parse(); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #ifndef CPARSE_H #define CPARSE_H # class CParse {public: CParse(istream &in=cin); void parse(); private: void A(); void X(); void Y(); int tok; Flex f; static void check(bool b,char*mess); }; } CParse::CParse(istream &in):f(in) {} { void CParse::parse() {tok=f.next(); A(); check(tok==Flex::EOLN,"<--End of line expected\n"); } void CParse::A() {switch(tok) {case Flex::AMPERSAND: case Flex::SPLAT: tok=f.next();
Image of page 1

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

View Full Document Right Arrow Icon
A(); break; default: X(); } } void CParse::X() {Y(); while(tok==Flex::PERIOD || tok==Flex::ARROW) {tok=f.next(); Y(); } } void CParse::Y() {check(tok==Flex::IDENT," Ident, '*', or '&' expected"); tok=f.next(); if(tok==Flex::LPAR) {tok=f.next(); check(tok==Flex::RPAR," ')' expected"); tok=f.next(); } } void CParse::check(bool b, char *mess) {if(!b) {cout<<"<-- ERROR: "<<mess<<endl; exit(1); } } #endif >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Write a class Flex that acts as a lexical analyzer for CParse above. Class Flex can assume that the header for the class Scan we developed for p3 is stored in scan.h. Calls to the Flex method next() should echo to the console each character it gets from Scan. Note that an Ident is a sequence of one or more lowercase letters. Also, recall that scan has the following methods: Scan(istream &in=cin); char next(); void newLine(); void reset(); bool good()const;
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