u/Radiant-Aspect-2345

How to remove left recursion from Prefix translation Scheme. urgent please

so i actually did Translation scheme c++ parser by removing left recursion now i want that how can i remove left recursion from this prefix Translation scheme so i can make Parser from it Ts for prefix could please remove left recursion from it

exp -> {cout<<"+"} exp + term

| {cout<<"-"} exp - term

| term

term -> digit

digit -> {cout<<"0"} 0 | {cout<<"1"} 1 | ... | {cout<<"9"} 9

i did with postfix ts and made parser in c++ below you can see, also if you could help how can i build it

Code for Postfix TS using removed TS. COde must be structure like that like postfix

#include <iostream>

#include <cstdlib>

using namespace std;

 

char input[50];

int i = 0;

char lookahead;

 

void exp();

void term();

void factor();

void paren();

 

void rest1();

void rest2();

void rest3();

 

void digit();

 

void match(char);

void eror();

 

int main() {

cout << "Enter String of Token: ";

cin >> input;

 

lookahead = input[i];

 

cout << "Postfix: ";

 

exp();

 

if (lookahead == '\0') {

cout << "\nValid Expression";

}

else {

eror();

}

 

return 0;

}

 

void exp() {

if ((lookahead >= '0' && lookahead <= '9') || lookahead == '(') {

term();

rest1();

}

else {

eror();

}

}

 

void rest1() {

if (lookahead == '+') {

match('+');

term();

cout << '+';   // postfix

rest1();

}

else if (lookahead == '-') {

match('-');

term();

cout << '-';   // postfix

rest1();

}

}

 

void term() {

if ((lookahead >= '0' && lookahead <= '9') || lookahead == '(') {

factor();

rest2();

}

else {

eror();

}

}

 

void rest2() {

if (lookahead == '*') {

match('*');

factor();

cout << '*';   // postfix

rest2();

}

else if (lookahead == '/') {

match('/');

factor();

cout << '/';   // postfix

rest2();

}

}

 

void factor() {

if ((lookahead >= '0' && lookahead <= '9') || lookahead == '(') {

paren();

rest3();

}

else {

eror();

}

}

 

void rest3() {

if (lookahead == '^') {

match('^');

paren();

cout << '^';   // postfix

rest3();

}

}

 

void paren() {

if (lookahead == '(') {

match('(');

exp();

match(')');

}

else if (lookahead >= '0' && lookahead <= '9') {

digit();

}

else {

eror();

}

}

 

void digit() {

if (lookahead >= '0' && lookahead <= '9') {

cout << lookahead;   // operand in postfix

match(lookahead);

}

else {

eror();

}

}

 

void match(char t) {

if (lookahead == t) {

lookahead = input[++i];

}

else {

eror();

}

}

 

void eror() {

cout << "Syntax Error";

exit(0);

}

reddit.com
u/Radiant-Aspect-2345 — 1 day ago

How to solve this Prefix Translation Scheme for parser (urgent please)

so i actually did Translation scheme c++ parser by removing left recursion now i want that how can i remove left recursion from this prefix Translation scheme so i can make Parser from it Ts for prefix could please remove left recursion from it

exp -> {cout<<"+"} exp + term

| {cout<<"-"} exp - term

| term

term -> digit

digit -> {cout<<"0"} 0 | {cout<<"1"} 1 | ... | {cout<<"9"} 9

i did with postfix ts and made parser in c++ below you can see, also if you could help how can i build it

Code for Postfix TS using removed TS. COde must be structure like that like postfix

#include <iostream>

#include <cstdlib>

using namespace std;

 

char input[50];

int i = 0;

char lookahead;

 

void exp();

void term();

void factor();

void paren();

 

void rest1();

void rest2();

void rest3();

 

void digit();

 

void match(char);

void eror();

 

int main() {

cout << "Enter String of Token: ";

cin >> input;

 

lookahead = input[i];

 

cout << "Postfix: ";

 

exp();

 

if (lookahead == '\0') {

cout << "\nValid Expression";

}

else {

eror();

}

 

return 0;

}

 

void exp() {

if ((lookahead >= '0' && lookahead <= '9') || lookahead == '(') {

term();

rest1();

}

else {

eror();

}

}

 

void rest1() {

if (lookahead == '+') {

match('+');

term();

cout << '+';   // postfix

rest1();

}

else if (lookahead == '-') {

match('-');

term();

cout << '-';   // postfix

rest1();

}

}

 

void term() {

if ((lookahead >= '0' && lookahead <= '9') || lookahead == '(') {

factor();

rest2();

}

else {

eror();

}

}

 

void rest2() {

if (lookahead == '*') {

match('*');

factor();

cout << '*';   // postfix

rest2();

}

else if (lookahead == '/') {

match('/');

factor();

cout << '/';   // postfix

rest2();

}

}

 

void factor() {

if ((lookahead >= '0' && lookahead <= '9') || lookahead == '(') {

paren();

rest3();

}

else {

eror();

}

}

 

void rest3() {

if (lookahead == '^') {

match('^');

paren();

cout << '^';   // postfix

rest3();

}

}

 

void paren() {

if (lookahead == '(') {

match('(');

exp();

match(')');

}

else if (lookahead >= '0' && lookahead <= '9') {

digit();

}

else {

eror();

}

}

 

void digit() {

if (lookahead >= '0' && lookahead <= '9') {

cout << lookahead;   // operand in postfix

match(lookahead);

}

else {

eror();

}

}

 

void match(char t) {

if (lookahead == t) {

lookahead = input[++i];

}

else {

eror();

}

}

 

void eror() {

cout << "Syntax Error";

exit(0);

}

reddit.com
u/Radiant-Aspect-2345 — 1 day ago