Lexical Analyser

23. Program to implement Lexical Analyser?

Program


#include<conio.h>
#include<stdio.h>
void findtokens();
int i,n,x=0,y=0,q=0,w=0,p,flag,j,k=0,a=0,b,o;
char c,str[20],delm[20]={',',';','{','}','(',')'},opr[20]="<>'+-*/=&",string[20],oprf[20],keys[20][20],ids[20][20],spl[20],stringf[20],keyw[20][20]={"int","void","float","char","if","else","printf","scanf"},cons[20],constr[20];
void display();
void main()
{
            FILE *f1;
            clrscr();
            printf(“The Input:\n”);
            /*------Input the program to be evaluated into the file pgm.txt------*/
            f1=fopen("z:\\pgm.txt","r");
            while((c=getc(f1))!=EOF)
            {            printf("%c" ,c);
            }
            fclose(f1);
            findtokens();
            display();
            getch();

}
void display()
{
                        /*------Displaying all the arrays-----*/
                        printf(" \nOperators : %s ",oprf);
                        printf("\nSpecial Characters : %s " ,spl);
                        printf("\nKeywords :");
                        for(i=0;i<x;i++)
                                    printf(" %s",keys[i]);
                        printf("\nIdentifiers :");
                        for(i=0;i<y;i++)
                                    printf(" %s",ids[i]);
                        printf("\nStrings :");
                        for(i=0;i<w;i++)
                                    printf("%c",stringf[i]);
                        printf("\nConstants : %s",constr );
}
/*----Finding and separating tokens into different arrays----*/
void findtokens()
{
            int flag2,z;
            FILE *f1;
            i=0;
            f1=fopen("z:\\pgm.txt","r");
            while((c=getc(f1))!=EOF)
            {
                        flag=0;
                        flag2=0;
                        /*-----Storing operators in to the array oprf[]----*/
                        for(j=0;opr[j]!='\0';j++)
                                    if(c==opr[j])
                                    {
                                                flag=1;
                                                for(z=0;z<k;z++)
                                                            if(c==oprf[z])
                                                                         flag2=1;
                                                if(flag2==0)
                                                {
                                                            oprf[k]=c;
                                                            k++;
                                                }
                                    }
                        /*----Storing special characters into the array spl[]----*/
                        for(j=0;delm[j]!='\0';j++)
                                                if(c==delm[j])
                                                {

                                                            flag=1;
                                                            for(z=0;z<q;z++)
                                                                        if(c==spl[z])
                                                                                    flag2=1;
                                                            if(flag2==0)
                                                            {
                                                                        spl[q]=c;
                                                                        q++;
                                                            }
                                                }
                   /*-----Storing strings into the array stringf[]-----*/
                   if(c=='"')
                   {
                                    c=getc(f1);
                                    while(c!='"')
                                    {
                                                stringf[w]=c;
                                                            w++;

                                                c=getc(f1);

                                    }
                                    stringf[w]=' ';
                                    w++;
                                    flag=1;
                   }
/*----Storing constants into the array constr[]----*/
                   if((flag==0)&&(c!=' ')&&(c!='\n')&&(c!='\t' ))
                         {
                                    if(isdigit(c)!=0)
                                    {
                                                cons[b]=c;
                                                b++;
                                    }
                                    else
                                                b=0;

                                    string[i]=c;
                                    i++;
                        }
                        else
                        {
                             if((b!=0)&&(i==b))
                             {
                                    for(o=0;o<b;o++)
                                                constr[a++]=cons[o];
                                    constr[a++]=' ';
                                    b=0;
                             }
                             else
                             if(i!=0)
                                         {
                                                flag=0;
                                                 string[i]='\0';
                                                if(strcmp(string,"#include")==0)
                                                {
                                                            while(c!='\n')
                                                                        c=getc(f1);
                                                            k--;
                                                }
                                    /*----Storing keywords into the array keys[]----*/
                                                else
                                                {
                                                 for(p=0;p<=7;p++)
                                                            if(strcmp(string,keyw[p])==0)
                                                                        flag=1;


                                          if(flag==1)
                                                {
                                                            flag2=0;
                                                                         for(z=0;z<x;z++)
                                                                                    if(strcmp(keys[z],string)==0)
                                                                                                flag2=1;
                                                                         if(flag2==0)
                                                                {                  strcpy(keys[x],string);
                                                            x++;
                                                            }
                                                }
                                    /*----Storing identifiers into the array ids[]----*/
                                                else
                                                {
                                                            if((string[0]=='_')||(isalpha(string[0])!=0))
                                                            {       flag=1;
                                                                        for(i=0;string[i]!='\0';i++)
                                                                                   if((isalnum(string[i])==0)&&(string[i]!='_'))
                                                                                                flag=0;
                                                            }
                                                            if(flag==1)
                                                             {       flag2=0;
                                                                         for(z=0;z<y;z++)
                                                                                    if(strcmp(ids[z],string)==0)
                                                                                                flag2=1;
                                                                         if(flag2==0)
                                                                         {
                                                                                     strcpy(ids[y],string);
                                                                                     y++;
                                                                         }
                                                             }
                                                }
                                                }
                                                i=0;
                                                b=0;

                                         }
                                        }
                                    }


                        oprf[k]='\0';
                                    spl[q]='\0';
                                    constr[a]='\0';

}

Output

The Input:

#include<stdio.h>
void main()
{
int a,b;
a=b+5;
printf(“sum=”,%a);
}

Operators :
+
=

Special characters :
(
)
{
,
;
}

Keywords :
void
int
printf

Identifiers :
main
a
b

Strings :
sum=

Constants :
5

No comments:

Post a Comment