0% found this document useful (0 votes)
8 views3 pages

22BRS1006 CD Lab3

Uploaded by

Naga Rithesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

22BRS1006 CD Lab3

Uploaded by

Naga Rithesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Compiler Design

Experiment 3

Submitted By : Naga Rithesh (22BRS1006)


Code :
#include <iostream>
#include <string>

using namespace std;

string input;
int index = 0;

bool F();
bool T();
bool E();

bool match(char expected) {


if (input[index] == expected) {
index++;
return true;
}
return false;
}

bool F() {
if (match('(')) {
if (E() && match(')')) {
return true;
}
return false;
} else if (input[index] == 'a') {
index++;
return true;
}
return false;
}
bool T() {
if (F()) {
while (match('*')) {
if (!F()) return false;
}
return true;
}
return false;
}

bool E() {
if (T()) {
while (match('+')) {
if (!T()) return false;
}
return true;
}
return false;
}

int main() {
cout << "Enter the arithmetic expression: ";
cin >> input;

if (E() && index == input.length()) {


cout << "Accepted" << endl;
} else {
cout << "Rejected" << endl;
}

return 0;
}

Output:

You might also like