Syntactic Analysis of C declaration statement

28. Program to check the syntax of a C-Declaration statement?

Program


#include<stdio.h>
#include<conio.h>
char str[50],s[20],idf[50][50];
int i=0,k,flagid,id,o=0;
void extract();
void chkkeywrd();
int keyword(char []);
void isid();
void isarray();
void ispointer();
void mult();
void idmis();
void asgnmnt();

void main()
{
            int p;
            clrscr();
            printf("Enter String :");
            gets(str);

            for(i=0;str[i]!='\0';i++)
            {
                        flagid=0;
                        extract();

                        if(keyword(s))   //keyword check
                        {    
                                    i++;
                                    for(;str[i]!='\0';i++)
                                    {
                                                ispointer();
                                                extract();
                                                chkkeywrd();
                                                isid();
                                                isarray();
                                                asgnmnt();
                                                idmis();
                                                if(str[i]==';')
                                                            break;
                                    }
                        }
                        else       //if there is no keyword
                        {
                            if(k!=0)
                            {      printf("Error :Type Missing ");
                                    getch();
                                    exit(0);
                            }
                        }

            }
            if(str[i-1]!=';')
                        printf("Error:Declaration Terminated incorrectly");
            else
                        printf("No Declaration Error found");

            getch();
}

/*---if there is nothing till a comma or a semicolon Eg: int ;---*/
void idmis()
{
            if(str[i-1]==']')
            flagid=1;
            if((flagid==0)&&((str[i]==',')||(str[i]==';')))
            {
                        printf("Error:Identifier Missing");
                        getch();
                        exit(0);
            }
            else
                        flagid=0;


}

/*----Checking the syntax of assignment----*/
void asgnmnt()
{
            int flaga=0;
            if(str[i]=='=')
            {
                        i++;
/*----get the content after ‘=’----*/
                        while((delm(str[i]))&&(str[i]!='\0'))
                        {
                                    if(!isdigit(str[i])) //check the content is digit or not
                                                flaga=1;
                                                i++;
                        }
/*----if the character after ‘=’ is not digit or if the character before ‘=’ is also ‘=’ then it is an invalid assignment---*/
                        if((flaga==1)||(str[i-1]=='='))
                        {
                                    printf("Error:Invalid Assignment");
                                    getch();
                                    exit(0);
                        }
            }
}

/*---checking the syntax of pointer---*/
void ispointer()
{
            int flagp=0;
            if(str[i]=='*')
            {       i++;
/*--Extracting and checking the content after ‘*’ is keyword or not--*/
                        extract();

                        if(identifier(s))
                        {
                                    flagid=1;
                                    mult();  //checking multiple declaration
/*----Checking for pointer array----*/
                        if((str[i]=='['))
                        {       i++;
/*---get the content in between the square brackets---*/
                                    while((str[i]!='\0')&&(str[i]!=']'))
                                    {
                                    if(!isdigit(str[i])) //check the content is digit or not
                                                flagp=1;
                                    i++;
                                    }
/*---Checking whether the closing square bracket is present or not and the array size is specified or not---*/
                                    if((str[i]!=']')||(str[i-1]=='['))
                                     {
                                    flagp=1;
                                     }
                                     i++;
                        }
                        }
                        else
                                    flagp=1;

                         if(flagp==1)
                         {
                                    printf("Error :pointer declaration error");
                                                getch();
                                    exit(0);
                         }
            }


}

/*----Checking whether the string is an array-----*/
void isarray()
{
            int flagarr=0;
            if((str[i]=='['))
            {     i++;
                        /*---get the content in between the square brackets---*/
                        while((str[i]!='\0')&&(str[i]!=']'))
                        {
                                    if(!isdigit(str[i]))//check the content is digit or not
                                                flagarr=1;
                                    i++;
                        }
/*---Checking whether the string is empty (k=0) or not, the array size is digit or not and the array size is specified or not---*/
               if((k==0)||(flagarr==1)||(str[i]!=']')||(str[i-1]=='['))
               {
                        printf("Error : Array Declaration error");
                                    getch();
                        exit(0);
               }
/*----checking syntax of multidimensional array----*/
                        if(str[i+1]=='[')
                        { i++;  
isarray();
                        }

            }


}

/*-----Checking for multiple declaration of identifier-----*/
void mult()
{
            int j,flagid=0;
            for(j=0;j<o;j++)
                        if(strcmp(s,idf[j])==0)
                                    flagid=1;
            if((flagid==1)&&(o!=0))
            {
                        printf("Error : Multiple declaration for %s",s);
                        getch();
                        exit(0);
            }
            else
            {
                        strcpy(idf[o],s);
                        o++;
            }
}

/*----Checking for identifier and checking whether there is multiple occurrence of it----*/
void isid()
{
if(k!=0)
{
            if(identifier(s))
            {
                  mult();        //Checking for multiple declaration
                  flagid=1;
            }
            else
            {

                        printf("Error :Not an identifier");
                        getch();
                        exit(0);
            }
            }
}

/*----Checking whether the string is an identifier----*/
int identifier(char sr[])
{
            int f=0;
            if(!keyword(sr)) //make sure that the string is not a keyword
            {
/*An identifier must start either with an underscore(‘_’) or an alphabet*/
                        if((sr[0]=='_')||(isalpha(sr[0])))
                        {
                                    f=1;
                                    for(id=1;sr[id]!='\0';id++)
/*An identifier should contain only underscore(‘_’) or an alphanumeral character after the first character*/
                                                if((!isalnum(sr[id]))&&(sr[id]!='_'))
                                                            f=0;

                        }

            }

            return(f);
}

/*----Checking whether more than one keyword is specified in the C declaration statement Eg: int float a;----*/
void chkkeywrd()
{
            if(keyword(s))
            {
                        printf("Error :Too many types in declaration");
                                    getch();
                        exit(0);
            }

}

/*----extracting keywords, identifiers, pointers, arrays, etc
from the C declaration statement and storing it in an array s[]----*/

void extract()
{
            k=0;
            /*----extracting contents up to delimiters or ‘\0’----*/
            while((str[i]!='\0')&&(delm(str[i])))        
            {
                s[k]=str[i];
                k++;i++;
            }
            s[k]='\0';
}

/*----Checking for delimiters----*/
int delm(char c)
{
            int j;
            char de[20]={'=',',',';','[',' '};
            for(j=0;de[j]!='\0';j++)
            {
                        if(de[j]==c)
                                    return(0);
            }
            return(1);
}

/*----Checking whether the string is a keyword----*/
int keyword(char sr[])
{
            int w;
            char keys[20][20]={"int","char","float","double"};
            for(w=0;w<4;w++)
                        if(strcmp(keys[w],sr)==0)
                                    return(1);
            return(0);
}

Output

Enter String :int abc,a[10][20][30];
No Declaration Error found

Enter String :int ;
Error:Identifier Missing

Enter String :int *p;
No Declaration Error found                                                      

Enter String :int *;
Error :pointer declaration error

Enter String :int a[];
Error : Array Declaration error                                                
                                                                                
Enter String :int a,a[10];
Error : Multiple declaration for a                                             

No comments:

Post a Comment