Tuesday 14 October 2014

C Program to crate lexical analyzer which will do space removal.

#include
#include
#include
#include

int key = 0;
char expr[100];
char cont[][20]={"CONTROLS","for","do","while","NULL",};
char cond[][20]={"CONDITION","if","then","NULL"};
char oprt[][20]={"OPERATOR","+","-","*","/","%","<","<=",">",">=","=","(",")","NULL"};
char branch[][20]={"BRANCHING","goto","jump" ,"NULL"};

void checking(char[],char[][20]);

void main()
{
int i,j,l,k,m,n;
char sbexpr[50],txt[3];
clrscr();
cout<<"Enter the expression:";
gets(expr);
for(i=0;expr[i]!=NULL;i++)
{
key=0;
for(j=i,k=0;expr[j]!=32 && expr[j]!=NULL;i++,j++,k++)
sbexpr[k]=expr[j];
sbexpr[k]=NULL;

if(key==0) checking(sbexpr,cond);

if(key==0) checking(sbexpr,cont);

if(key==0) checking(sbexpr,branch);

if(key==0)
{
for(m=0;sbexpr[m]!=NULL;m++)
{
key=0;
txt[0]= sbexpr[m];
txt[1] = NULL;

if(key==0) checking(txt,oprt);
if((key==0) || ((sbexpr[m]>=97 && sbexpr[m]<=122) || (sbexpr[m]>=65 && sbexpr[m]<=90)))
{
cout<<"\n"<"<<"Identifier\n";
key = 1;
}
}
}
if(key == 0)
{
cout<<"\n"<"<<"Address\n";
key = 1;
}
}

getch();
}


void checking (char expr[],char check[][20])
{
for(int i=1;strcmp(check[i],"NULL")!=0;i++)
{
if(strcmp(expr,check[i])==0)
{
cout<"<key = 1;
}
}
}

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]);
}
}

}