Please implement using vectors 4.3 Task 3: Calculate FIRST Sets Compute the FIRS
ID: 3587309 • Letter: P
Question
Please implement using vectors
4.3 Task 3: Calculate FIRST Sets Compute the FIRST sets of all non-terminals. Then, for each of the non-terminals of the input grammar, in the order that it appears in the grammar, output one line in the following format: FIRST() { } Where should be replaced by the non-terminal and should be replaced by a comma-separated list of elements of the set ordered in the following manner: lfe belongs to the set, represent it as # If e belongs to the set, it should be listed before any other elements All other elements of the set should be sorted in the order in which they appear in the grammar Example: Given the input grammar decl-> idList colon ID # idList-> ID idList1 # idList! -> # idList1-) COMMA ID idList 1 # the expected output for task 3 is FIRST (decl) - ID j FIRST (idlist) = { ID } FIRST (idList 1) { #, COMMA } 4.4 Task 4: Calculate FOLLOW Sets For each of the non-terminals of the input grammar, in the order that they appear in the non- terminals section of the input, compute the FOLLOW set for that non-terminal and output one line in the following format FOLLOW () { } = Where should be replaced by the non-terminal and should be replaced by the comma-separated list of elements of the set ordered in the following manner: o If EOF belongs in the set, represent it as $ · If EOF belongs in the set, it should be listed before any other elements o All other elements of the set should be sorted in the order in which they appear in the grammarExplanation / Answer
Calculate the first sets
#include <iostream>
using namespace std;
//This array stores the terminals already printed
char printed[50];
int index=0;
//This resets the printed terminals record for the next production
void resetPrinted()
{
index=0;
for(int i=0;i<50;++i) printed[i] = '0';
}
//Checks whether the nonterminal char found has already been printed for this production
bool alreadyPrinted(char c)
{
for(int i=0;i<50;++i)
if(printed[i]==c) return true;
return false;
}
bool checkChar(char c)
{
if(!isupper(c))
{
if(!alreadyPrinted(c))
{
cout<<c<<" ";
printed[index++] = c;
}
return true;
}else
return false;
}
void printFirst(char **input, int production, int nProductions)
{
char c;
int index=0;
while(c=input[production][index]!='=')index++;
index++; //We have found '=', we need to skip it now.
while((c=input[production][index])!='')
{
//Is terminal
if(checkChar(c)){}
//Is not terminal
else
{
for(int i=0;i<nProductions;++i)
{
if(i==production) continue;
if(c==input[i][0]) {printFirst(input, i, nProductions); break;}
}
}
//Skip until next terminal or non-terminal is found
while((c=input[production][++index])!=','){ if(c=='') return;};
c=input[production][++index]; //We have found a comma, we need to look at the next character now
}
}
int main()
{
int nProductions = 0;
char **input;
cout<<"Enter the number of productions: ";
cin>>nProductions;
cin.ignore();
input = new char*[nProductions];
for(int i=0;i<nProductions;++i)
{
input[i] = new char[50];
}
cout<<"Enter the productions: ";
for(int i=0;i<nProductions;++i)
cin>>input[i];
for(int i=0;i<nProductions;++i)
{
resetPrinted();
cout<<"{ ";
printFirst(input, i, nProductions);
cout<<"} ";
}
}