Sunday, July 24, 2011

Shift Reduce algorithm source code in c for Compiler

#include
#include
#include
#include
#include

using namespace std;


struct stru1
{
    char non_ter[1],pro[25];
}cfg[25];

int n,st=-1,j,i,t=-1,m;
int v,c,p=1;
char str[20],stack[20],ch,tmp[10];

void match(int k);
void matchl(int k);


int main()
{

    printf("Enter the number of productions:\n");
    scanf("%d",&n);
    printf("\n");
    printf("Enter the productions on LEFT and RIGHT sides:\n");

    for(i=0;i\n");
        scanf("%s",cfg[i].pro);
        printf("\n");
    }
    printf("Enter the input string:\n");
    scanf("%s",str);
    printf("\n");

    i=0;
    do
    {
        ch=str[i];
        stack[++st]=ch;
        tmp[0]=ch;
        match(1);
        i++;
    }while(str[i]!='\0');

    c=st;
    v=st;
    puts(stack);
    printf("\n");

    while(st)
    {
        --st;
        v=st;
        t=-1;
        p=0;
        while(v<=c)
        {
            tmp[++t]=stack[v++];
            p++;
        }
        matchl(p);
    }

    cfg[0].non_ter[1]='\0';
    if(strcmp(stack,cfg[0].non_ter)==0)
    printf("String is present in Grammar G\n");
    else
    printf("String is not present in Grammar G\n");
}

void match(int k)
{
    for(j=0;j<=y);

                tmp[t]='\0';
                puts(stack);
                printf("\n");
                break;
            }
        }
    }
}

/*
3
E
a

E
E+E

E
E*E

a*a+a


*/

No comments:

Post a Comment

How to Generate and use the ssh key on Gerrit, github.io, gitlab, and bitbucket.

 Details can be found here -