Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// CS4303 Programming Language Concepts // Lab exercise - Scanner // // Name ______________________ ID _____________________ // // A simple lexical analyzer for C/C++-style variable

// CS4303 Programming Language Concepts // Lab exercise - Scanner // // Name ______________________ ID _____________________ // // A simple lexical analyzer for C/C++-style variable declarations. // The grammar for the declarations is as follows: // //  ::=   ; |   =  ; //  ::= int | float //  ::= A | B | C | D | E //  ::=  |  //  ::=   |  //  ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 //  ::=  .  // // The task of this exercise is to write a lexical analyzer (or // scanner) for the tokens used in the above grammar. The following // is the regular expression that defines the tokens: // // = | ; | int | float | A | B | C | D | E | [0-9]+ | [0-9]+\.[0-9]+ // // The program will read a declaration from the keyboard, and the // scanner you design should recognize and print out all tokens // included in the input. For example, given the following declaration: // // int A = 123; // // your program should print: // // int // A // = // 123 // ; // // Make sure your program print out the token one per line in the order // they appear in the input. Once an erroneous token is encountered, your // scanner should print out an error message and stopped scanning. For // example, given the following input: // // int A = 0#; // // your program should print: // // int // A // = // 0 // #: Error: Unrecognizable token // // Note that tokens may NOT be separated by spaces. For example, the above // input: // // int A = 123; // // does not have a space to separate 123 and ;. Also, the following inputs // are also legal and generate the same output: // // int A=123; // intA=123; // // However, a whole token cannot be separated by spaces. For example, the // following input will cause 12 and 3 to be regarded as two distinct tokens. // // int A = 12 3; // // The ouput will look like: // int // A // = // 12 // 3 // ; // // Also note that the scanner doesn't check for syntactic errors. Therefore the // above input is legal to this program. // // Important!!! Save your GetToken program. We are going to use it in future // labs. #include  #include  using namespace std; string GetToken(); void error(int); int main() { string token; cout << "Please enter a declaration in format " << "  [= number];" << endl; cout << "The following are the tokens in the input:" << endl; token = GetToken(); while (token != "") { cout << token << endl; token = GetToken(); } cout << "Done!" << endl; return 0; } string GetToken() { string token; char ch; // Write the code here. Read the next token and store it in variable "token". // The token must be read character by character. Use the regular expression // defined above to extract tokens from the input. // // To read a character from keyboard, use: // // cin.get(ch); // // where "ch" is a character variable. return token; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions