Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// CS4303 Programming Language Concepts // Lab exercise - Parser // // Name ______________________ ID _____________________ // // A simple parser for C/C++-style variable declarations.

// CS4303 Programming Language Concepts // Lab exercise - Parser // // Name ______________________ ID _____________________ // // A simple parser for C/C++-style variable declarations. // THe grammar is as follows: // // ::= ; | = ; // ::= int | float // ::= A | B | C | D | E // ::= | // ::= | // ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 // ::= . // // Input is entered at the keyboard. // If the input is correct, the program should print // "no error found", otherwise, it should print the type // of error and terminate its execution. There are four // possible errors: // // "unrecognizable type" // "illegal variable name" // "unexpected number" // "; expected" // // Error message is printed out by calling function // "error". An error code ranging from 0 to 4 can be // passed as an argument to the function indicating what // message is to be printed. The mapping from the error // code to the message can be found by looking at the // definition of function "error". // // The following are some sample input and the response // from the program: // // Please enter a declaration in format [= number] ; // int A; // no error found // // Please enter a declaration in format [= number] ; // int a; // illegal variable name // // Please enter a declaration in format [= number] ; // short B; // unrecognizable type // // Please enter a declaration in format [= number] ; // float C = 0.5; // no error found // // Please enter a declaration in format [= number] ; // int A = 10, // unexpected token #include #include using namespace std; string token; string GetToken(); void error int main() { cout << "Please enter a declaration in format " << " [= number] ;" << endl; token = GetToken(); // Write the code here error(0); return 0; } string GetToken() { // Use the Gettoken function you have designed in Lab 1. } void error(int code) { switch (code) { case 0: cout << "no errors found" << endl; break; case 1: cout << "unrecognizable type" << endl; break; case 2: cout << "illegal variable name" << endl; break; case 3: cout << "unexpected number" << endl; break; case 4: cout << "; expected" << endl; } return; }

-----------------------------------------

source code from lab1

// programming langauge concepts

//lab exercise = scanner

#include

#include

#include

#include

#include

using namespace std;

string GetToken();

void error(int);

int main() {

// to read the variable by the user

string token;

// description to tell the reader what to put

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;

cin.get();

return 0;

}

string GetToken() {

locale loc;

string token;

string error;

bool goToken = true;

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.

while (goToken == true)

{

cin.get(ch);

while (ch == ' ')

{

cin.get(ch);

}

//token = ch;

if (isdigit(ch))

{

//token = token + ch;

while (isdigit(ch))

{

//cout <<"digit"<< endl;

token = token + ch;

//cout << token << endl;

cin.get(ch);

//deal with decimals

if (ch == '.')

{

token = token + ch;

cin.get(ch);

}

//deals with semicolon

if (ch == ';')

{

token = token + ' ' + ch;

cin.get(ch);

//goToken = false;

//break;

}

if (ch == '#')

{

//error = token;

token = token + ' ' + ch + ':' + " Error: Unrecognizable token";

}

//GetToken();

//goToken= false;

}

//token = token + ch;

//cout << token << endl;

//cout << "token" << endl;

goToken = false;

}

else

{

token = token + ch;

//type

if (token == "i")

{

goToken = true;

}

else if (token == "in")

{

goToken = true;

}

else if (token == "int")

{

goToken = false;

}

else if (token == "f")

{

goToken = true;

}

else if (token == "fl")

{

goToken = true;

}

else if (token == "flo")

{

goToken = true;

}

else if (token == "floa")

{

goToken = true;

}

else if (token == "float")

{

goToken = false;

}

//variable

else if (token == "A" || token == "B" || token == "C" || token == "D" || token == "E")

{

goToken = false;

}

//equal

else if (token == "=")

{

goToken = false;

}

//semicolon

else if (token == ";")

{

goToken = false;

}

//enter

else if (token == " ")

{

token = "";

goToken = false;

}

else

{

//error = token;

token = token + ':' + " Error: Unrecognizable token";

goToken = false;

}

}

}

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

More Books

Students also viewed these Databases questions