Download this file : lexical
lexical.c
#include"stdio.h"
#include"conio.h"
void main()
{
FILE *fi,*fo,*fop,*fk;
int flag=0,i=1;
char c,t,a[15],ch[15],file[20];
clrscr();
printf("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("\t\tLEXICAL ANALYSIS \n");
fscanf(fi,"%s",a);
printf("\nline : %d\n",i++);
while(!feof(fi))
{
if((strcmp(a,"$")==0))
{
printf("\nline : %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,"%s",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
( openpara
) closepara
{ openbrace
} closebrace
< lesser
> greater
" doublequote
' singlequote
: colon
; semicolon
# preprocessor
= equal
== assign
% 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:
Enter the file name:input.c
# : preprocessor
include : keyword
< : lesser
stdio.h : keyword
> : greater
line : 2
# : preprocessor
include : keyword
< : lesser
conio.h : keyword
> : greater
line : 3
void : keyword
main : keyword
( : openpara
) : 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
( : openpara
) : closepara
; : semicolon
line : 8
} : closebrace