Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<declaration> ::= <type> <var> ’;’ | <type> <var> ’=’ <number> ’;’ // <type> ::=

ID: 3705220 • Letter: #

Question

<declaration> ::= <type> <var> ’;’ | <type> <var> ’=’ <number> ’;’
// <type> ::= int | float // <var> ::= A | B | C | D | E // <number> ::= <integer> | <float> // <integer> ::= <integer> <digit> | <digit> // <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 // <float> ::= <integer> ‘.’ <integer> // // 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 <type> <variable> [= number] ; // int A; // no error found // // Please enter a declaration in format <type> <variable> [= number] ; // int a; // illegal variable name // // Please enter a declaration in format <type> <variable> [= number] ; // short B; // unrecognizable type // // Please enter a declaration in format <type> <variable> [= number] ; // float C = 0.5; // no error found // // Please enter a declaration in format <type> <variable> [= number] ; // int A = 10, // unexpected token // // Other sample input: // float B; // float C=0.2; // short D; // float F; // // The last two sample inputs should generate errors because "short" and "F" are // not acceptable tokens.
#include <iostream> #include <string>
using namespace std;
string GetToken(); void error(int);
int main() { string token;
cout << "Please enter a declaration in format " << "<type> <variable> [= 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; } <declaration> ::= <type> <var> ’;’ | <type> <var> ’=’ <number> ’;’
// <type> ::= int | float // <var> ::= A | B | C | D | E // <number> ::= <integer> | <float> // <integer> ::= <integer> <digit> | <digit> // <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 // <float> ::= <integer> ‘.’ <integer> // // 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 <type> <variable> [= number] ; // int A; // no error found // // Please enter a declaration in format <type> <variable> [= number] ; // int a; // illegal variable name // // Please enter a declaration in format <type> <variable> [= number] ; // short B; // unrecognizable type // // Please enter a declaration in format <type> <variable> [= number] ; // float C = 0.5; // no error found // // Please enter a declaration in format <type> <variable> [= number] ; // int A = 10, // unexpected token // // Other sample input: // float B; // float C=0.2; // short D; // float F; // // The last two sample inputs should generate errors because "short" and "F" are // not acceptable tokens.
#include <iostream> #include <string>
using namespace std;
string GetToken(); void error(int);
int main() { string token;
cout << "Please enter a declaration in format " << "<type> <variable> [= 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; } <declaration> ::= <type> <var> ’;’ | <type> <var> ’=’ <number> ’;’
// <type> ::= int | float // <var> ::= A | B | C | D | E // <number> ::= <integer> | <float> // <integer> ::= <integer> <digit> | <digit> // <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 // <float> ::= <integer> ‘.’ <integer> // // 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 <type> <variable> [= number] ; // int A; // no error found // // Please enter a declaration in format <type> <variable> [= number] ; // int a; // illegal variable name // // Please enter a declaration in format <type> <variable> [= number] ; // short B; // unrecognizable type // // Please enter a declaration in format <type> <variable> [= number] ; // float C = 0.5; // no error found // // Please enter a declaration in format <type> <variable> [= number] ; // int A = 10, // unexpected token // // Other sample input: // float B; // float C=0.2; // short D; // float F; // // The last two sample inputs should generate errors because "short" and "F" are // not acceptable tokens.
#include <iostream> #include <string>
using namespace std;
string GetToken(); void error(int);
int main() { string token;
cout << "Please enter a declaration in format " << "<type> <variable> [= 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; }

Explanation / Answer

According to the problem, We have to write a synatx parser which has the give following rules (Grammar)

<declaration>    ::=    <type> <var> ’;’ | <type> <var> ’=’ <number> ’;’

// <type>        ::=    int | float

// <var>        ::=    A | B | C | D | E

// <number>    ::=    <integer> | <float>

// <integer>    ::=    <integer> <digit> | <digit>

// <digit>        ::=    0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

// <float>        ::=    <integer> ‘.’ <integer>

A reasonable approach to do this would be to use regular expression. We will have to create a seperate regular expression for base cases like int, chars(a,b,c,d,e) and then build up on this to higher order expressions.

//global declarations of regex
regex integer("\d+");
regex floating_pt("(\d+).(\d+)");

bool is_int(string s) {
return regex_match(s, integer);
}

bool is_float(string s) {
return regex_match(s, floating_pt);
}

int is_validType(string s) { //returns 0 for int 1 for float
//check whether give string is int|float
regex r("int");
regex f("flaot");
if(regex_match(s, r)) return 0; //int
else if(regex_match(s, f)) return 1;
  
  
//if none of the above,--> invalid data type
return -1;
  
}

int is_validName(string s) {
regex name("[A-E]+"); //will match any string combination of A to E.
return regex_match(s, name);
}

To use the above function, we need to include header files like <regex>.

After declaring these above function it becomes fairly simple, all you have to is start parsinig the string and send appropriate substring to check for required grammar conditions.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote