July 2014

Operator Precedence


Simple program for operator precedence parser in C.
It takes the expression as input and calculates the precedence of the operators.

Subscribe to the blog to get instant updates via E-Mail.

PROGRAM CODE:


 #include<stdio.h>  
 #include<string.h>  
 void main()  
 {  
   int i,j,cnt=1;  
   char operators[] ="*/%+-",input[100];  
   printf("Enter the statement : ");  
   gets(input);  
   for(i=0;i<strlen(operators);i++)  
   {  
     for(j=0;j<strlen(input);j++)  
     {  
       if(input[j]==operators[i])  
       {  
         printf("%d %c=%c%c%c\n",cnt++,input[j-1],input[j-1],input[j],input[j+1]);  
         input[j+1]=input[j-1];  
       }  
     }  
   }  
 }  

OUTPUT:



Subscribe to the blog to get instant updates via E-Mail.

Notify the Suggestions about the program through comments ... :)

Lexical Analyzer


Simple Lexical Analyzer program. It takes the filename in the same directory as the input and analyzes the program for variables operators and special characters. and prints the output for each values separately.

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 void main()  
 {  
      FILE *fp;  
      int i=0;  
      char input[100],filename[20];  
      clrscr();  
      printf("Enter Filename : ");  
      scanf("%s",filename);  
      fp=fopen(filename,"r");  
      while(!feof(fp))  
      {  
           input[i]=fgetc(fp);  
           i++;  
      }  
      input[--i]='\0';  
      for(i=0;i<strlen(input);i++)  
      {  
           if(isalpha(input[i]))  
           {  
                printf("%c is a Character.\n",input[i]);  
           }  
           else if(input[i]=='='||input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/'||input[i]=='%')  
           {  
                printf("%c is an Operator.\n",input[i]);  
           }  
           else if(isspace(input[i])||iscntrl(input[i]))  
           {  
           }  
           else if(isdigit(input[i]))  
           {  
                printf("%c is a number.\n",input[i]);  
           }  
           else  
           {  
                printf("%c is a Special character.\n",input[i]);  
           }  
      }  
      getch();  
 }  
Notify the Suggestions about the program through comments ... :)