Tuesday, 14 October 2014

C Program to implement Recursive Deceptive Parsing

#include
#include
#include
void main()
{
int i,j,k;
char a[20],b[20],c[20],m,n[10],o[10];
clrscr();
printf("Enter a recursive string:");
gets(a);
if(a[0]==a[3]) {
m=a[0];
printf("%c\n",m);
}
else {
printf("Entered string is not recursive");
}
j=0;
for(i=4;a[i]!=NULL;i++) {
if(a[i]!='/') {
b[j++]=a[i];
printf("%c",b[j]);
}
else {
goto a1;
}
}
a1:
b[j]='\0';
j=0;
for(i=i+1;a[i]!=NULL;i++) {
c[j]=a[i];
j++;
}
c[j]='\0';
n[0]=m;
n[1]='-';
n[2]='>';
j=3;
for(i=0;c[i]!=NULL;i++) {
n[j]=c[i];
j++;
}
n[j++]=m;
n[j++]='`';
n[j++]='\0';
printf("\n\nOutput string1 is:");
puts(n);
o[0]=m;
o[1]='-';
o[2]='>';
j=3;
for(i=0;b[i]!=NULL;i++)
{
o[j]=b[i];
j++;
}
puts(b);
o[j++]=m;
o[j++]='`';
o[j++]='/';
o[j++]='$';
o[j++]='\0';
printf("\n\nOutput string is:");
puts(o);
getch();
}

C Program to implement Recursive Descentive Parsing.

#include
#include
#include
#include
#include
char ip_sym[15],ip_ptr=0,op[50],tmp[50];
void e_prime();
void e();
void t_prime();
void t();
void f();
void advance();
int n=0;
void e(){
            strcpy(op,"TE'");
 printf("E=%-25s",op);
 printf("E->TE'\n");
            t();
 e_prime();
}
void e_prime(){
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
            if(op[i]!='e')
 tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='E';n++);
if(ip_sym[ip_ptr]=='+')            {
                         i=n+2;
 do {
                                    op[i+2]=op[i];
 I++;
                        }while(i<=l);
                         op[n++]='+';
                        op[n++]='T';
                        op[n++]='E';
                        op[n++]=39;
                        printf("E=%-25s",op);
                        printf("E'->+TE'\n");
                        advance();
                        t();
e_prime();
            }
else{
                        op[n]='e';
                        for(i=n+1;i<=strlen(op);i++)
                        op[i]=op[i+1];
                        printf("E=%-25s",op);
                        printf("E'->e");
}
}
void t(){
 int i,n=0,l;
            for(i=0;i<=strlen(op);i++)
            if(op[i]!='e')
            tmp[n++]=op[i];
            strcpy(op,tmp);
            l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
i=n+1;
            do{
                        op[i+2]=op[i];
                        i++;
            }while(i < l);
            op[n++]='F';
            op[n++]='T';
 op[n++]=39;
            printf("E=%-25s",op);
            printf("T->FT'\n");
 f();
 t_prime();
}
void t_prime(){
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
             if(op[i]!='e')
            tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
if(ip_sym[ip_ptr]=='*')            {
                         i=n+2;
 do{
                                    op[i+2]=op[i];
                                    i++;
                        }while(i < l);
                        op[n++]='*';
                        op[n++]='F';
                        op[n++]='T';
                        op[n++]=39;
                        printf("E=%-25s",op);
                        printf("T'->*FT'\n");
                        advance();
                        f();
                        t_prime();
            }
            else{
op[n]='e';
            for(i=n+1;i<=strlen(op);i++)
            op[i]=op[i+1];
            printf("E=%-25s",op);
            printf("T'->e\n");
            }
}
void f(){
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
             if(op[i]!='e')
 tmp[n++]=op[i];
            strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='F';n++);
            If((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I')){
                        op[n]='i';
                        printf("E=%-25s",op);
                        printf("F->i\n");
                        advance();
            }
            else{
                        if(ip_sym[ip_ptr]=='(')                         {
                                    advance();
                                    e();
                                    if(ip_sym[ip_ptr]==')'){
                                                advance();
                                                i=n+2;
                                                do{
                                                            op[i+2]=op[i];
                                                            i++;
                                                }while(i<=l);
                                    op[n++]='(';
                                    op[n++]='E';
                                    op[n++]=')';
                                    printf("E=%-25s",op);
                                    printf("F->(E)\n");
                        }
            }
            else{
                        printf("\n\t syntax error");
                        getch();
                        exit(1);
                        }
            }
}
void advance(){
            ip_ptr++;
}
void main(){
            int i;
            clrscr();
            printf("\nGrammar without left recursion");
 printf("\n\t\t E->E+T/T \n\t\t T->T*F/F \n\t\t F->(E)/Id ");
 printf("\n Enter the input expression:");
            gets(ip_sym);
            printf("Expressions");
            printf("\t Sequence of production rules\n");
            e();
            for(i=0;i < strlen(ip_sym);i++){
                        if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&&
                        ip_sym[i]!=')'&&ip_sym[i]!='i'&&ip_sym[i]!='I'){
                                    printf("\nSyntax error");
                                    break;
                        }
                        for(i=0;i<=strlen(op);i++)
                        if(op[i]!='e')
                        tmp[n++]=op[i];
                        strcpy(op,tmp);
                        printf("\nE=%-25s",op);
            }
            getch();
}

C Program To Find Follow Of Given String.

#include
#include

intn,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);

int main()
{
inti,z;
charc,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf(" %c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}         
while(z==1);
}
void follow(char c)
{
            if(a[0][0]==c)f[m++]='$';
for(i=0;i
{
for(j=2;j
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
                                                }
}
}
}
void first(char c){
int k;
if(!(isupper(c)))f[m++]=c;
for(k=0;k
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else
if(islower(a[k][2]))f[m++]=a[k][2];

else first(a[k][2]);
}
}

}

C Program To Find First Of Given Sting

#include
#include
void FIRST(char );
intcount,n=0;
charprodn[10][10], first[10];
main(){
inti,choice;
charc,ch;
printf("How many productions ? :");
scanf("%d",&count);
printf("Enter Number Of Productions  :");
for(i=0;i
scanf("%s%c",prodn[i],&ch);
do{
n=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void FIRST(char c){
int j;
if(!(isupper(c)))first[n++]=c;
for(j=0;j
if(prodn[j][0]==c){
if(prodn[j][2]=='$') first[n++]='$';
else
if(islower(prodn[j][2]))first[n++]=prodn[j][2];
else FIRST(prodn[j][2]);
}
}

}

Sunday, 24 August 2014

C Program to Draw Pascal's triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1


#include
int main()
{
    int rows,coef=1,space,i,j;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=0;i    {
        for(space=1;space<=rows-i;space++)
        printf("  ");
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                coef=1;
            else
               coef=coef*(i-j+1)/j;
            printf("%4d",coef);
        }
        printf("\n");
    }
}

Write a C program to display reverse pyramid.

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *


 #include
void  main()
{
    int rows,i,j,space;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=rows;i>=1;--i)
    {
        for(space=0;space           printf("  ");
        for(j=i;j<=2*i-1;++j)
          printf("* ");
        for(j=0;j            printf("* ");
        printf("\n");
    }
    return 0;
}

Friday, 22 August 2014

C Program to print half pyramid as using number

#include
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5