/* * * C version * */ // E => T + E | T - E | T // T => F * T | F / T | F // F => I | D | ( E ) // I => a | ... | z // D => 0 | ... | 9 #include #include bool E(void); bool T(void); bool F(void); bool I(void); bool D(void); char *s; int main(int argc, char *argv[]) { char *t; s = t = argc == 2 ? argv[1] : ""; if (E() && *s == '\0') { printf("The string \"%s\" is in the language.\n", t); } else { printf("The string \"%s\" is not in the language.\n", t); } return 0; } bool E(void) { if (T()) { if (*s == '+' || *s == '-') { ++s; if (E()) { return true; } return false; } return true; } return false; } bool T(void) { if (F()) { if (*s == '*' || *s == '/') { ++s; if (T()) { return true; } return false; } return true; } return false; } bool F(void) { if (I()) { return true; } else if (D()) { return true; } else if (*s == '(') { ++s; if (E()) { if (*s == ')') { ++s; return true; } } } return false; } bool I(void) { if ('a' <= *s="" &&="" *s=""><= 'z')="" {="" ++s;="" return="" true;="" }="" return="" false;="" }="" bool="" d(void)="" {="" if="" ('0'=""><= *s="" &&="" *s=""><= '9')="" {="" ++s;="" return="" true;="" }="" return="" false;="" }="" *="" *="" *="" c++="" version="" *="" */="" e=""> T + E | T - E | T // T => F * T | F / T | F // F => I | D | ( E ) // I => a | ... | z // D => 0 | ... | 9 #include #include using std::cout; using std::endl; using std::string; bool E(void); bool T(void); bool F(void); bool I(void); bool D(void); string s; int main(int argc, char *argv[]) { string t; s = t = argc == 2 ? argv[1] : ""; if (E() && s == "") { cout < "the="" string="" \""="">< t="">< "\"="" is="" in="" the="" language."="">< endl;="" }="" else="" {="" cout="">< "the="" string="" \""="">< t="">< "\"="" is="" not="" in="" the="" language."="">< endl;="" }="" return="" 0;="" }="" bool="" e(void)="" {="" if="" (t())="" {="" if="" (s[0]="=" '+'="" ||="" s[0]="=" '-')="" {="" s="s.substr(1);" if="" (e())="" {="" return="" true;="" }="" return="" false;="" }="" return="" true;="" }="" return="" false;="" }="" bool="" t(void)="" {="" if="" (f())="" {="" if="" (s[0]="=" '*'="" ||="" s[0]="=" '/')="" {="" s="s.substr(1);" if="" (t())="" {="" return="" true;="" }="" return="" false;="" }="" return="" true;="" }="" return="" false;="" }="" bool="" f(void)="" {="" if="" (i())="" {="" return="" true;="" }="" else="" if="" (d())="" {="" return="" true;="" }="" else="" if="" (s[0]="=" '(')="" {="" s="s.substr(1);" if="" (e())="" {="" if="" (s[0]="=" ')')="" {="" s="s.substr(1);" return="" true;="" }="" }="" }="" return="" false;="" }="" bool="" i(void)="" {="" if="" ('a'=""><= s[0]="" &&="" s[0]=""><= 'z')="" {="" s="s.substr(1);" return="" true;="" }="" return="" false;="" }="" bool="" d(void)="" {="" if="" ('0'=""><= s[0]="" &&="" s[0]=""><= '9')="" {="" s="s.substr(1);" return="" true;="" }="" return="" false;="" }="" *="" *="" *="" java="" version="" *="" */="" e=""> T + E | T - E | T // T => F * T | F / T | F // F => I | D | ( E ) // I => a | ... | z // D => 0 | ... | 9 public class Main { public static void main(String[] args) { s = args.length == 1 ? args[0] : ""; if (E() && i == s.length()) { System.out.println("The string \"" + s + "\" is in the language."); } else { System.out.println("The string \"" + s + "\" is not in the language."); } } private static boolean E() { if (T()) { if (i < s.length()="" &&="" (s.charat(i)="=" '+'="" ||="" s.charat(i)="=" '-'))="" {="" ++i;="" if="" (e())="" {="" return="" true;="" }="" return="" false;="" }="" return="" true;="" }="" return="" false;="" }="" private="" static="" boolean="" t()="" {="" if="" (f())="" {="" if="" (i="">< s.length() && (s.charat(i) == '*' || s.charat(i) == '/')) { ++i; if (t()) { return true; } return false; } s.length()="" &&="" (s.charat(i)="=" '*'="" ||="" s.charat(i)="=" '/'))="" {="" ++i;="" if="" (t())="" {="" return="" true;="" }="" return="" false;="">
Answered Same DayJun 07, 2022

Answer To:

Aditi answered on Jun 08 2022
87 Votes
ASSIGNMENT
1. C++ Solution:
a. Create one main program file and the code for the same is:
#include
#include
#include
using namespace std;
//declare local variables
int i, j;
string input;
//function prototype
int A_G();
in
t E_G();
int P_G(int k);
int L_G();
int I_G();
int UI_G();
int UL_G();
//main method
int main()
{
//declare input file stream
//with file name
ifstream file("input.txt");
//use while loop to read the file
while (!file.eof())
{
//set local variables to 0
i = 0;
j = 0;
//read the file input
file >> input;
//If the return value of E is not equal to -1
//then print message input is in the language
if (A_G() != -1 && i == input.length())
{
cout << "The string " << input << " is in the language\n";
}
//Otherwise, it will be shown not in the language
else
{
cout << "The string " << input << " is not in the language\n";
}
}
//close file stream
file.close();
return 0;
}
int A_G()
{
if (I_G() != -1)
{
i = j;
if (input[i++] == '=')
{
if (E_G() != -1)
return 0;
}
else
{
i = 0;
if (E_G() != -1)
return 0;
}
}
else
{
i = 0;
if (E_G() != -1)
return 0;
}
return -1;
}
int E_G()
{
if (P_G(-1) != -1)
{
if (i == input.length())
return 0;
else
{
//Implement the production O -> + | - | * | / | **
if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')
{
i++;
if (P_G(1) != -1)
return 0;
}
}
}
return -1;
}
int P_G(int k)
{
//cout<<"\nP_G()";
if (I_G() != -1)
{
if (k == -1)
{
i = j;
return 0;
}
else
{
if (j == input.length())
{
i = j;
return 0;
}
else
return E_G();
}
}
else if (L_G() != -1)
{
if (k == -1)
{
i = j;
return 0;
}
else
{
if (j == input.length())
{
i = j;
return 0;
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here