DEVELOP A LEXICAL ANALYZER TO RECOGNIZE A FEW PATTERNS IN C( WITH OUTPUT)



AIM:
To develop a lexical analyzer to identify identifiers, constants, comments, operators etc using C program

ALGORITHM:
 Step1: Start the program.
 Step2: Declare all the variables and file pointers.
 Step3: Display the input program.
 Step4: Separate the keyword in the program and display it.
 Step5: Display the header files of the input program
 Step6: Separate the operators of the input program and display it.
 Step7: Print the punctuation marks.
 Step8: Print the constant that are present in input program.
 Step9: Print the identifiers of the input program.


PROGRAM CODE:

//Develop a lexical analyzer to recognize a few patterns in C.

#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
void keyword(char str[10])
{
 if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("printf",str)==0||strcmp("switch",str)==0||strcmp("case",str)==0)
  printf("\n%s is a keyword",str);
 else
  printf("\n%s is an identifier",str);
}
void main()
{
 FILE *f1,*f2,*f3;
 char c,str[10],st1[10];
 int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
 f1=fopen("input","r");
 f2=fopen("identifier","w");
 f3=fopen("specialchar","w");
 while((c=getc(f1))!=EOF)
 {
  if(isdigit(c))
  {
  tokenvalue=c-'0';
  c=getc(f1);
  while(isdigit(c))
  {
   tokenvalue*=10+c-'0';
   c=getc(f1);
  }
  num[i++]=tokenvalue;
  ungetc(c,f1);
  }
  else
  if(isalpha(c))
  {
   putc(c,f2);
   c=getc(f1);
   while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
   {
    putc(c,f2);
    c=getc(f1);
   }
   putc(' ',f2);
   ungetc(c,f1);
  }
  else
  if(c==' '||c=='\t')
  printf(" ");
  else
  if(c=='\n')
  lineno++;
  else
  putc(c,f3);
 }
 fclose(f2);
 fclose(f3);
 fclose(f1);
 printf("\n the no's in the program are:");
 for(j=0;j<i;j++)
  printf("\t%d",num[j]);
 printf("\n");
 f2=fopen("identifier","r");
 k=0;
 printf("the keywords and identifier are:");
 while((c=getc(f2))!=EOF)
 if(c!=' ')
 str[k++]=c;
 else
 {
  str[k]='\0';
  keyword(str);
  k=0;
 }
 fclose(f2);
 f3=fopen("specialchar","r");
 printf("\n Special Characters are");
 while((c=getc(f3))!=EOF)
 printf("\t%c",c);
 printf("\n");
 fclose(f3);
 printf("Total no of lines are:%d",lineno);
}


OUTPUT:


RESULT:

Thus the program for developing a lexical analyzer to recognize a few patterns in C has been executed successfully.


Comments

  1. out put screen not appears for this program

    ReplyDelete
  2. I see that you have this lexer tutorial, and a symbol table one to follow. Do you have any plans to put out a parser tutorial without bison, flex?

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. MGM Springfield - Casino Vacation Rentals
    For your entertainment, 서귀포 출장안마 MGM 영천 출장마사지 Springfield has some of the 정읍 출장안마 best hotels, restaurants, 충청북도 출장안마 and 세종특별자치 출장안마 other amenities that we have on the market, including our Mohegan Sun

    ReplyDelete

Post a Comment

Popular posts from this blog

IMPLEMENTATION OF A LEXICAL ANALYZER USING LEX (WITH OUTPUT)

IMPLEMENTATION OF SYMBOL TABLE USING C (WITH OUTPUT)

IMPLEMENTATION OF CALCULATOR USING LEX & YACC