Sunday, December 22, 2019

C Program for Lexical analyzer


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>

void main(){

int i,ic=0,m,cc=0,oc=0,j;
char b[30],operators[30],identifiers[30],constants[30];

printf("enter the string : ");
scanf("%[^\n]s",&b);
for(i=0;i<strlen(b);i++){

    if(isspace(b[i])){
        continue;
    }
    else if(isalpha(b[i])){
        identifiers[ic] =b[i];
         ic++;
    }
    else if(isdigit(b[i])){
        m=(b[i]-'0');
        i=i+1;
        while(isdigit(b[i])){
            m=m*10 + (b[i]-'0');
            i++;
        }
        i=i-1;
        constants[cc]=m;
        cc++;
    }
     else{
        if(b[i]=='*'){
            operators[oc]='*';
            oc++;
        }
        else if(b[i]=='-'){
            operators[oc]='-';
            oc++;
        }
        else if(b[i]=='+'){
            operators[oc]='+';
            oc++;
        }
         else if(b[i]=='='){
            operators[oc]='=';
            oc++;
        }

  }

}
// printing

    printf(" identifiers : ");
     for(j=0;j<ic;j++){
        printf("%c ",identifiers[j]);
     }
    printf("\n constants : ");
     for(j=0;j<cc;j++){
        printf("%d ",constants[j]);
     }
    printf("\n operators : ");
      for(j=0;j<oc;j++){
        printf("%c ",operators[j]);
      }


}

Wednesday, November 27, 2019

C Program for counting number of lines , number of spaces, number of digits in a given file


#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void main(){
FILE *fp = fopen("bb.txt","r");
char c;
int cd=0,cs=0,cl=0;
while((c=getc(fp))!=EOF){

    if(isdigit(c)){
        cd=cd+1;
    }
    else if(isspace(c) && c!='\n'){
        cs=cs+1;
    }
    else if(c=='\n'){
        cl=cl+1;
    }
}
printf("number of digits %d \n",cd);
printf("number of spaces %d \n",cs);
printf("number of lines %d \n",cl);
fclose(fp);
getch();

}