LEXICAL ANALYZER USING C
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()


    {
     char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
     int j;
     char fname[50];
     FILE *f1;
     //clrscr();
    printf("enter file path (drive:\\fold\\filename)\n");
    scanf("%s",fname);
    f1 = fopen(fname,"r");
    //f1 = fopen("Input","r");
     if(f1==NULL)


         {
          printf("file not found");
          exit(0);
         }
         while((ch=fgetc(f1))!=EOF)


             {
            for(j=0;j<=14;j++)


                {
                if(ch==oper[j])


                    {
                    printf("%c is an operator\n",ch);
                    op++;
                    str[i]='\0';
                    keyw(str);
                }
            }
            for(j=0;j<=14;j++)


                {
                if(i==-1)
                break;
                if(ch==seps[j])


                    {
                    if(ch=='#')


                        {
                        while(ch!='>')


                            {
                            printf("%c",ch);
                            ch=fgetc(f1);
                        }
                        printf("%c is a header file\n",ch);
                        i=-1;
                        break;
                    }
                    if(ch=='"')


                        {
                        do


                            {
                            ch=fgetc(f1);
                            printf("%c",ch);
                        }while(ch!='"');
                        printf("\b is an argument\n");
                        i=-1;
                        break;
                    }
                    str[i]='\0';
                    keyw(str);
                }
            }
            if(i!=-1)


                {
                str[i]=ch;
                i++;
            }
            else
            i=0;
             }
            printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
            //getch();
        }
        void keyw(char *p)


            {
            int k,flag=0;
            for(k=0;k<=31;k++)


                {
                if(strcmp(keys[k],p)==0)


                    {
                    printf("%s is a keyword\n",p);
                    kw++;
                    flag=1;
                    break;
                }
            }
            if(flag==0)


                {
                if(isdigit(p[0]))


                    {
                    printf("%s is a number\n",p);
                    num++;
                }
                else


                    {
                    //if(p[0]!=13&&p[0]!=10)
                    if(p[0]!='\0')


                        {
                        printf("%s is an identifier\n",p);
                        id++;
                    }
                }
            }
            i=-1;
        }
output


8 comments:

  1. Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks Lessinvest.com

    ReplyDelete
  2. I’m encouraged while using surpassing along with preachy list that you just adorn such minor timing. LED Strip Lights manufacturer

    ReplyDelete
  3. We amazed using the evaluation a person designed to get this to specific submit amazing. Fantastic exercise! custom joker lego

    ReplyDelete
  4. Them believes magnificent to read simple things these enlightening plus exceptional reports against your web pages. bursa trip from istanbul

    ReplyDelete
  5. a extremely superb webpage. a realy educational in addition to a an extremely superior position. i’m a sucker for the. pool cover

    ReplyDelete
  6. B体育(B SPORTS)是全球最大的在线综合性娱乐平台,爱体育遥遥领先于其他同行,b体育每天为您提供近千场精彩体育赛事。 更有真人、棋牌、彩票、电竞、百家乐、电子、全球各地赛事、动画直播、视频直播等服务。即刻访问B体育官方网站下载体育APP体验精彩赛事投注。机会多多, 惊喜多多。B体育官网地址: https://vipbsports.com

    #B体育 #B体育官网 #B体育APP

    ReplyDelete
  7. B体育(B SPORTS)是全球最大的在线综合性娱乐平台,爱体育遥遥领先于其他同行,b体育每天为您提供近千场精彩体育赛事。 更有真人、棋牌、彩票、电竞、百家乐、电子、全球各地赛事、动画直播、视频直播等服务。即刻访问B体育官方网站下载体育APP体验精彩赛事投注。机会多多, 惊喜多多。B体育官网地址: https://vipbsports.com

    #B体育 #B体育官网 #B体育APP

    ReplyDelete
  8. it is good as well as meanful. it is awesome weblog. Connecting is extremely helpful point. you've truly assisted many individuals that go to weblog and supply all of them usefull info. code promo 1xbet ci

    ReplyDelete