Friday, May 28, 2010

WAP in C to generate recursive descend parser


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

int lptr=0;//look ahead pointer
char buff[100];//buffer to store input string
int success=1;//to reflect success or failure

void E();
void EDASH();
void T();
void TDASH();
void V();

void start()
{
 E();
}

int isvalid(char ch)
{
 if(isalpha(ch))
 return 1;
 else if(toascii(ch)>=47&&toascii(ch)<=57)
 return 1;
 else
 return 0;
}

void advance()
{
 lptr++;
}

void E()
{
 T();
 EDASH();
}

void EDASH()
{
 if(buff[lptr]=='+')
 {
  advance();
  T();
  EDASH();
 }
}

void T()
{
 V();
 TDASH();
}

void TDASH()
{
 if(buff[lptr]=='*')
 {
  advance();
  V();
  TDASH();
 }
}

void V()
{
 if(isvalid(buff[lptr]))
 {
  advance();
 }
 else if(buff[lptr]=='(')
 {
  advance();
  E();
  if(buff[lptr]==')')
  {
   advance();
  }
  else
  {
   printf("\n* Right parantheses missing ");
   success=0;
  }
 }
 else
 {
  printf("\n* Symbol(s) unidentified");
  success=0;
  getch();
  exit(0);
 }
}


void main()
{
 int n;
 clrscr();
 printf("\n\t----------------");
 printf("\n\t    GRAMMAR");
 printf("\n\t----------------\n");
 printf("\n\t E ::= TE'");
 printf("\n\t E'::= +TE'|$");
 printf("\n\t T ::= VT'");
 printf("\n\t T'::= *VT'|$");
 printf("\n\t V ::= <id>");
 printf("\n\t----------------\n\n");
 printf("\n Enter an expression: ");
 gets(buff);
 n=strlen(buff);
 start();

 if(success==1&&lptr>=n)
 {
  printf("\n* Correct syntax of expression");
 }
 else
 {
  printf("\n* Incorrect syntax of expression");
 }

 getch();
}

WAP to generate symbol table


#include<stdio.h>
#include<conio.h>
int isnum(int n)
{
      if(n>0&&n<100)
      return 1;
      else
      return 0;
}
void main()
{
      FILE *fp;
      char words[50][25],str[25],ch;
      char name[50][25];
      int size[50],add[50];
      int temp,ad=1000;
      int i,j,w=0,d=0;
      fp=fopen("tsym.c","r");
      clrscr();
      printf("\n File: tsym.c\n\n");
      if(fp!=NULL)
      {
                  do
                  {
                              j=0;
                              ch='d';
                              strcpy(str,"");

                              while(ch!=' ')
                              {
                                          ch=getc(fp);
                                          printf("%c",ch);

                                          if(ch==EOF) break;

                                          str[j++]=ch;


                                          if(ch==' '||ch=='\n'||ch=='='||ch=='[')
                                          {
                                                      j--;
                                                      break;
                                          }

                                          if(ch==';'||ch==','||ch==']')
                                          {
                                                      j--;
                                                      break;
                                          }

                              }

                              str[j]='\0';


                              if(j>0)
                              strcpy(words[w++],str);



                              if(ch==';')
                              strcpy(words[w++],";");

                              j=0;
                  }while(ch!=EOF);
      }



      for(i=0;i<w;i++)
      {
                  if(!strcmp(words[i],"int"))
                  {
                              i++;

                              for(;strcmp(words[i],";");i++)
                              {
                                 temp=atoi(words[i]);
                                 if(isnum(temp))
                                 {
                                          d--;
                                          ad=ad-size[d];
                                          size[d]=temp*2;
                                          ad=ad+size[d];
                                          d++;
                                 }
                                 else
                                 {
                                 strcpy(name[d],words[i]);
                                 size[d]=2;
                                 add[d]=ad;
                                 ad=ad+size[d];
                                 d++;
                                 }
                              }
                  }
                  else if(!strcmp(words[i],"char"))
                  {
                              i++;
                              for(;strcmp(words[i],";");i++)
                              {
                                 temp=atoi(words[i]);
                                 if(isnum(temp))
                                 {
                                          d--;
                                          ad=ad-size[d];
                                          size[d]=temp*1;
                                          ad=ad+size[d];
                                          d++;
                                 }
                                 else
                                 {
                                 strcpy(name[d],words[i]);
                                 size[d]=1;
                                 add[d]=ad;
                                 ad=ad+size[d];
                                 d++;
                                 }
                              }
                  }
                  else if(!strcmp(words[i],"float"))
                  {
                              i++;
                              for(;strcmp(words[i],";");i++)
                              {
                                 temp=atoi(words[i]);
                                 if(isnum(temp))
                                 {
                                          d--;
                                          ad=ad-size[d];
                                          size[d]=temp*4;
                                          ad=ad+size[d];
                                          d++;
                                 }
                                 else
                                 {
                                 strcpy(name[d],words[i]);
                                 size[d]=4;
                                 add[d]=ad;
                                 ad=ad+size[d];
                                 d++;
                                 }
                              }
                  }
                  else if(!strcmp(words[i],"double"))
                  {
                              i++;
                              for(;strcmp(words[i],";");i++)
                              {
                                 temp=atoi(words[i]);
                                 if(isnum(temp))
                                 {
                                          d--;
                                          ad=ad-size[d];
                                          size[d]=temp*8;
                                          ad=ad+size[d];
                                          d++;
                                 }
                                 else
                                 {
                                 strcpy(name[d],words[i]);
                                 size[d]=8;
                                 add[d]=ad;
                                 ad=ad+size[d];
                                 d++;
                                 }
                              }
                  }
      }


      printf("\n\n\tSYMBOL TABLE:\n");
      printf("SYMBOL       SIZE      ADDRESS");
      for(i=0;i<d;i++)
      {
         printf("\n%-10s",name[i]);
         printf("%-10d",size[i]);
         printf("%-10d",add[i]);
      }
      getch();
}

WAP to print comments in a given program


#include<stdio.h>
#include<conio.h>
void main()
{
  char str[25],ch,words[50][25];
  int i,k,j,w=0;
  FILE *fp;
  clrscr();
  fp=fopen("tcom.c","r");
  printf("\n File: tcom.c\n\n");
  if(fp!=NULL)
  {
    do
    {
      j=0;
      ch='d';
      strcpy(str,"");
      while(ch!=' ')
      {
      ch=getc(fp);
      putchar(ch);
      if(ch==EOF) break;
      str[j]=ch;
      if(ch!=' '&&ch!='\n'&&ch!=';'&&ch!='/'&&ch!='*')
      j++;
      if(ch==' '||ch=='\n'||ch==';'||ch=='/'||ch=='*') break;
      }
      str[j]='\0';

      strcpy(words[w++],str);
      if(ch=='/')
      strcpy(words[w++],"/");

      if(ch=='*')
      strcpy(words[w++],"*");

      if(ch==';')
      strcpy(words[w++],";");

      if(ch=='\n')
      strcpy(words[w++],"newline");

      if(ch==EOF)
      strcpy(words[w++],"EOF");

      if(ch==' ')
      strcpy(words[w++]," ");

      j=0;
    }
    while(ch!=EOF);
  }


  printf("\n\n\n* Single line comments are:\n");
  for(i=0;i<w;i++)
  {//1
    if(!strcmp(words[i],"/"))
    {//2
      ++i;
      /*single line*/
      if(!strcmp(words[++i],"/"))
      {//3
         do
         {//4
             i=i+1;
             if(strcmp(words[i],"newline")&&strcmp(words[i],"EOF"))
             printf("%s",words[i]);
             if(!strcmp(words[i],"newline")||(!strcmp(words[i],"EOF")))
             break;
         }while(i<w);//4
         printf("\n");
      }//3
      /*single line*/


    }//2

  }//1



  printf("\n\n\n* Multiple line comments are:\n");
  for(i=0;i<w;i++)
  {//1
    if(!strcmp(words[i],"/"))
    {//2

      ++i;
      /*multiline*/
      if(!strcmp(words[++i],"*"))
      {//5
       do
       {//6
                  if(strcmp(words[++i],"*"))
                  printf("%s",words[i]);
                  else
                  { //7
                              if(strcmp(words[++i],"/"))
                              {//8
                              printf("\n");
                              break;
                              } //8
                              else
                              printf("%s",words[++i]);
                  }//7
       }while(i<w);//6
      }//5
      /*multi line*/

    }//2

  }//1

  fclose(fp);
  getch();
}

Contact Form

Name

Email *

Message *