0% found this document useful (0 votes)
13 views5 pages

Ex 6 CD

The document presents a program for designing a LALR Bottom-up Parser in C++. It includes the structure definitions for actions and transitions, the parsing logic, and the implementation of shift and reduce operations. The program reads an input string and processes it according to the defined grammar rules, displaying the parsing stack and actions taken during the process.

Uploaded by

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

Ex 6 CD

The document presents a program for designing a LALR Bottom-up Parser in C++. It includes the structure definitions for actions and transitions, the parsing logic, and the implementation of shift and reduce operations. The program reads an input string and processes it according to the defined grammar rules, displaying the parsing stack and actions taken during the process.

Uploaded by

Krishna Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CS3CO44 COMPILER DESIGN(P)

PRACTICAL-6
AIM:
Write a program to design a LALR Bottom-up Parser.

CODE:
#include <iostream>
#include <stack>
#include <string>
#include <map>

using namespace std;

struct Action {
string row[6];
};

struct Goto {
string r[3];
};

const Action A[12] = {


{"s5", "", "", "s4", "", ""},
{"", "s6", "", "", "", "acc"},
{"", "r2", "s7", "", "r2", "r2"},
{"", "r4", "r4", "", "r4", "r4"},
{"s5", "", "", "s4", "", ""},
{"", "r6", "r6", "", "r6", "r6"},
{"s5", "", "", "s4", "", ""},
{"s5", "", "", "s4", "", ""},
{"", "s6", "", "", "s11", ""},
{"", "r1", "s7", "", "r1", "r1"},
{"", "r3", "r3", "", "r3", "r3"},
{"", "r5", "r5", "", "r5", "r5"}

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

};

const Goto G[12] = {


{"1", "2", "3"},
{"", "", ""},
{"", "", ""},
{"", "", ""},
{"8", "2", "3"},
{"", "", ""},
{"9", "", "3"},
{"", "10", ""},
{"", "", ""},
{"", "", ""},
{"", "", ""},
{"", "", ""}
};

string terminals = "i+*()$";


string nonTerminals = "ETF";
string states = "0123456789abcd";

struct Production {
char left;
string right;
};

const Production rules[6] = {


{'E', "E+T"},
{'E', "T"},
{'T', "T*F"},
{'T', "F"},
{'F', "(E)"},
{'F', "i"}
};

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

stack<string> parseStack;

void printStack(stack<string> s) {
stack<string> temp;
while (!s.empty()) {
temp.push(s.top());
s.pop();
}
while (!temp.empty()) {
cout << temp.top() << " ";
temp.pop();
}
}

void shift(string state, string symbol) {


parseStack.push(symbol);
parseStack.push(state);
}

void reduce(int ruleIndex) {


string rightSide = rules[ruleIndex].right;
char leftSide = rules[ruleIndex].left;
int popCount = rightSide.size() * 2;
while (popCount--) parseStack.pop();
string topState = parseStack.top();
int stateIndex = states.find(topState);
int nonTermIndex = nonTerminals.find(leftSide);
parseStack.push(string(1, leftSide));
parseStack.push(G[stateIndex].r[nonTermIndex]);
}

void parse(string input) {


input += "$";

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

parseStack.push("0");
int i = 0;
while (true) {
string topState = parseStack.top();
char currentSymbol = input[i];
int stateIndex = states.find(topState);
int termIndex = terminals.find(currentSymbol);
string action = A[stateIndex].row[termIndex];
cout << "Stack: "; printStack(parseStack); cout << "\tInput: " << input.substr(i) << "\tAction: " <<
action << endl;
if (action[0] == 's') {
shift(string(1, action[1]), string(1, currentSymbol));
i++;
} else if (action[0] == 'r') {
reduce(action[1] - '1');
} else if (action == "acc") {
cout << " Input accepted successfully!" << endl;
return;
} else {
cout << " Error in parsing!" << endl;
return;
}
}
}

int main() {
string input;
cout << "Enter the input: ";
cin >> input;
parse(input);
return 0;
}

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

Output:

EN23CS3T1005 ADITYA SHUKLA

You might also like