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

Predictive Parsing Table Construction

The document presents a program for constructing a Predictive Parsing Table for LL(1) grammar using C++. It includes functions to compute the First and Follow sets, construct the parsing table, and print it. The program takes user input for grammar productions and outputs the resulting parsing table.

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)
23 views5 pages

Predictive Parsing Table Construction

The document presents a program for constructing a Predictive Parsing Table for LL(1) grammar using C++. It includes functions to compute the First and Follow sets, construct the parsing table, and print it. The program takes user input for grammar productions and outputs the resulting parsing table.

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-5
AIM:
Write a program to implement the construction of a Predictive Parsing Table for LL(1) grammar.

CODE:
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

map<char, vector<string>> productions;

set<char> computeFirst(char nonTerminal, map<char, set<char>>& firstSet) {


if ([Link](nonTerminal) != [Link]()) {
return firstSet[nonTerminal];
}

set<char> first;
for (string production : productions[nonTerminal]) {
char firstSymbol = production[0];
if (isupper(firstSymbol)) {
set<char> tempFirst = computeFirst(firstSymbol, firstSet);
[Link]([Link](), [Link]());
} else { // Terminal or epsilon
[Link](firstSymbol);
}
}
firstSet[nonTerminal] = first;
return first;
}

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

void computeFollow(char nonTerminal, map<char, set<char>>& followSet, map<char, set<char>>&


firstSet) {
for (auto& prod : productions) {
char lhs = [Link];
for (string rhs : [Link]) {
size_t pos = [Link](nonTerminal);
if (pos != string::npos) {
if (pos + 1 < [Link]()) {
char nextSymbol = rhs[pos + 1];
if (isupper(nextSymbol)) {
followSet[nonTerminal].insert(firstSet[nextSymbol].begin(), firstSet[nextSymbol].end());
if (firstSet[nextSymbol].find('e') != firstSet[nextSymbol].end()) { // If epsilon is in First of
nextSymbol
followSet[nonTerminal].insert(followSet[lhs].begin(), followSet[lhs].end());
}
} else {
followSet[nonTerminal].insert(nextSymbol);
}
} else { // If the non-terminal is the last symbol in the production
followSet[nonTerminal].insert(followSet[lhs].begin(), followSet[lhs].end());
}
}
}
}
}

void constructParsingTable(map<char, map<char, string>>& parsingTable, map<char, set<char>>& firstSet,


map<char, set<char>>& followSet) {
for (auto& prod : productions) {
char lhs = [Link];
for (string rhs : [Link]) {
set<char> firstOfRhs;
char firstSymbol = rhs[0];
if (isupper(firstSymbol)) {

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

[Link](firstSet[firstSymbol].begin(), firstSet[firstSymbol].end());
} else { // Terminal or epsilon
[Link](firstSymbol);
}

for (char terminal : firstOfRhs) {


if (terminal != 'e') { // Exclude epsilon
parsingTable[lhs][terminal] = rhs;
}
}

if ([Link]('e') != [Link]()) { // If epsilon is in First of RHS


for (char terminal : followSet[lhs]) {
parsingTable[lhs][terminal] = rhs;
}
}
}
}
}

void printParsingTable(map<char, map<char, string>>& parsingTable) {


cout << "\nPredictive Parsing Table:\n";
cout << "-------------------------\n";
for (auto& row : parsingTable) {
char nonTerminal = [Link];
cout << nonTerminal << ":\n";
for (auto& entry : [Link]) {
char terminal = [Link];
string production = [Link];
cout << " On " << terminal << " -> " << nonTerminal << " -> " << production << "\n";
}
cout << "\n";
}
}

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

int main() {
int numProductions;
cout << "Enter the number of productions: ";
cin >> numProductions;

cout << "Enter the productions (use 'e' for epsilon):\n";


for (int i = 0; i < numProductions; i++) {
char lhs;
string rhs;
cout << "Production " << i + 1 << " (format: A->alpha): ";
cin >> lhs >> rhs;
productions[lhs].push_back(rhs);
}

map<char, set<char>> firstSet;


for (auto& prod : productions) {
computeFirst([Link], firstSet);
}

map<char, set<char>> followSet;


followSet[[Link]()->first].insert('$'); // Add $ to the start symbol's Follow set
for (auto& prod : productions) {
computeFollow([Link], followSet, firstSet);
}

map<char, map<char, string>> parsingTable;


constructParsingTable(parsingTable, firstSet, followSet);

printParsingTable(parsingTable);

return 0;
}

EN23CS3T1005 ADITYA SHUKLA


CS3CO44 COMPILER DESIGN(P)

Output:

EN23CS3T1005 ADITYA SHUKLA

You might also like