Ex. No: 2
Date:
Develop a lexical analyzer to recognize a few
patterns in C. (Ex. identifiers, constants, comments, operators etc.)
AIM:
To Write a C program to develop a lexical analyzer to recognize a few patterns in C.
ALGORITHM:
1. Start the program
2. Include the header files.
3. Allocate memory for the variable by dynamic memory allocation function.
4. Use the file accessing functions to read the file.
5. Get the input file from the user.
6. Separate all the file contents as tokens and match it with the functions.
7. Define all the keywords in a separate file and name it as key.c
8. Define all the operators in a separate file and name it as open.c
9. Give the input program in a file and name it as input.c
10. Finally print the output after recognizing all the tokens.
11. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h> void main()
{
FILE *fi,*fo,*fop,*fk; int flag=0,i=1;
char c,t,a[15],ch[15],file[20]; clrscr();
printf("\n Enter the File Name:"); scanf("%s",&file);
fi=fopen(file,"r"); fo=fopen("inter.c","w");
fop=fopen("Oper.c","r");
fk=fopen("key.c","r"); c=getc(fi); while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1)) fputc(c,fo);
else
{
if(c=='\n') fprintf(fo,"\t$\t");
else fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi); fclose(fo);
fi=fopen("inter.c","r"); printf("\n Lexical Analysis"); fscanf(fi,"%s",a);
printf("\n Line: %d\n",i++); while(!feof(fi))
{
if(strcmp(a,"$")==0)
{
printf("\n Line: %d \n",i++); fscanf(fi,"%s",a);
}
fscanf(fop,"%s",ch);
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch); printf("\t\t%s\t:\t%s\n",a,ch); flag=1;
}
fscanf(fop,"%s",ch);
}
rewind(fop); fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%k",ch); printf("\t\t%s\t:\tKeyword\n",a); flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk); if(flag==0)
{
if(isdigit(a[0])) printf("\t\t%s\t:\tConstant\n",a);
else
printf("\t\t%s\t:\tIdentifier\n",a);
}
flag=0; fscanf(fi,"%s",a);
}
getch();
}
Key.c
int void main char if
for
while else printf scanf FILE
include stdio.h conio.h iostream.h
Oper.c
( open para
) closepara
{ openbrace
} closebrace
< lesser
> greater
" doublequote ' singlequote
: colon
; semicolon
# preprocessor
= equal
== asign
% percentage
^ bitwise
& reference
* star
+ add
- sub
\ backslash
/ slash
INPUT.c
#include "stdio.h"
#include "conio.h" void main()
{
int a=10,b,c; a=b*c; getch();
}
OUTPUT:
Line:1
# : preprocessor include : Identifier " : doublequote stdio.h : Keyword " : doublequote
Line: 2
# : preprocessor include : Identifier " : doublequote conio.h : Keyword " : doublequote
Line: 3
void : Keyword main : Keyword ( : open
) : closepara
Line: 4
{ : openbrace
Line: 5
int : Keyword a : Identifier
= : equal
10 : Constant
, : Identifier b : Identifier
, : Identifier c : Identifier
; : semicolon
Line: 6
a : Identifier
= : equal
b : Identifier
* : star
c : Identifier
; : semicolon
Line: 7
getch : Identifier ( : open
) : closepara
; : semicolon
Line: 8
} : closebrace
RESULT:
Thus the above program for developing the lexical the lexical analyzer and recognizing the few pattern s in C is executed successfully and the output is verified
getting error in this code :/
ReplyDeleteHere is new code for lexical analyzer in C++.
ReplyDelete