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

Info iconThis 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. 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); }; } {} { 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();
Background image of page 1

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

View Full DocumentRight 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; A (sloppy) program that uses Flex might look like:
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

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

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