Tuesday, March 13, 2012

Seperation of terminals,non-terminals and productions in the CFG in a compiler Design

Let us consider the productions of grammer G, that may contain any number of productions then the code is,

PROGRAM:

#include<iostream>
#include<string.h>
using namespace std;
class production
{
private:
char left;
char right[10][10];
int noa,i,j;
public:
void setproduction(char str[15])
{
j=0;noa=0;
left=str[0];
for(i=3;str[i]!='\0';i++){
if(str[i]!='/'){
   right[noa][j++]=str[i];
right[noa][j]='\0';
}
else{
   right[noa][j]='\0';noa++;j=0;
}
}
}
void printproduction()
{
cout<<left<<"->";
for(i=0;i<=noa;i++)
{
cout<<right[i];
if(i!=noa){cout<<"/";}
}
}
friend class CFG;
};