WAP to check the syntax of for loops


#include<stdio.h>
#include<conio.h>
void main()
{
  char str[25],ch,words[25][25];
  int i,k,j,w=0,s=0,cntl=0,cntr=0,p=0,parR=0,f=0,parL=0;
  FILE *fp;
  clrscr();
  fp=fopen("tnest.c","r");
  printf("\n File: tnest.c\n\n");
  if(fp!=NULL)
  {
    do
    {
      j=0;
      ch='d';
      strcpy(str,"");
      while(ch!=' ')
      {
      ch=getc(fp);
      putchar(ch);
      if(ch==EOF) break;
      if(ch=='{')
      cntl++;
      else if(ch=='}')
      cntr++;
      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");
      j=0;
    }
    while(ch!=EOF);
  }

  for(i=0;i<w;i++)
  {//for

    if(!strcmp(words[i],"for"))
    {//1
      s=0;p=0;parL=0;
      if(!strcmp(words[++i],"("))
      {//2
      f++; parR=0;parL++;

      for(j=i;j<w;j++)
      {//3
        if(!strcmp(words[j],";"))
        s++;
        if(!strcmp(words[j],")"))
        {
        parR++;
        //p=1;
        break;
        }
        if(!strcmp(words[j],"newline"))
        {
        break;
        }
      }//3

      if(!strcmp(words[j],")"))
      {
      if((!strcmp(words[++j],";"))||(!strcmp(words[++j],"newline")))
      p=1;
      }
      }//2
    }//1
    if(p!=1||parR!=1||s!=2||parL!=1) break;
  }


  if((parL!=1)||(parR!=1))
  {
  printf("\n\n* Error in syntax of parantheses ");
  printf("of for loop no:%d ",f);
  getch();
  exit(0);
  }
  else if(cntl!=cntr)
    {
    printf("\n\n* Error in syntax of curly braces ");
    getch();
    exit(0);
    }

  else if(s!=2)
      {
      printf("\n\n* Error in syntax of semicolon ");
      printf("of for loop no:%d ",f);
      getch();
      exit(0);
      }

  else if(p==1)
      {
      printf("\n\n* Syntax of for is correct");
      getch();
      exit(0);
      }




  fclose(fp);
  getch();
}

Post a Comment

0 Comments