Implementation of Lexical Analyser Using C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1.

Implementation of Lexical Analyser using C


#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
main()
{
FILE *f1;
char c,str[100];
int num=0,i=0,lineno=1;
f1=fopen("input.c","r");
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
num=c-48;
c=getc(f1);
while(isdigit(c))
{
num=num*10+(c-48);
c=getc(f1);
}
printf("%d is a number \n",num);
ungetc(c,f1);
}
else if(isalpha(c))
{
str[i++]=c;
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
str[i++]=c;
c=getc(f1);
}
str[i++]='\0';
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcm
p("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("static",str)==0||strcm
p("switch",str)==0||strcmp("case",str)==0)
printf("%s is a keyword \n",str);
else
printf("%s is a identifier \n",str);
ungetc(c,f1);
i=0;
}
else if(c==' '||c=='\t')
printf("\n");
else if(c=='\n')
lineno++;
else
printf("%c is a special symbol \n",c);
}

printf("Total no. of lines are: %d \n",lineno);


fclose(f1);

}
Output

[cs2884@LabServer compiler]$ cc lex.c


[cs2884@LabServer compiler]$ ./a.out
Enter the file name of program:
lex.c
# is a special symbol
include is identifier
< is a special symbol
ctype is identifier
. is a special symbol
h is identifier
> is a special symbol
# is a special symbol
include is identifier
< is a special symbol
stdio is identifier
. is a special symbol
h is identifier
> is a special symbol
# is a special symbol
include is identifier
2.Implementation of Lexical Analyser using LEX tool

%{
#include<stdio.h>
#include<string.h>
char key[100][100],head[100][100],dig[100][100],op[100][100],id[100][100];
int i=0,j=0,k=0,l=0,a=0,b=0,c=0,d=0,m=0,n=0;
%}
KW "int"|"while"|"if"|"else"|"for"|"char"|"float"|"case"|"switch"
HF "#include<".*">"
OP "+"|"-"|"*"|"/"|"="
DIG [0-9]*|[0-9]*"."[0-9]+
ID [a-zA-Z][a-zA-Z0-9]*
%%
{KW} {strcpy(key[i],yytext);i++;}
{HF} {strcpy(head[j],yytext);j++;}
{DIG} {strcpy(dig[k],yytext);k++;}
{OP} {strcpy(op[m],yytext);m++;}
{ID} {strcpy(id[n],yytext);n++;}
. {}
%%
main()
{
yyin=fopen("input.c","r+");
yylex();
printf("\nThe keywords are");
for(a=0;a<i;a++)
{
printf("\n%s",key[a]);
}
printf("\nThe headerfiles are ");
for (b=0;b<j;b++)
{
printf("\n%s",head[b]);
}

printf("\nThe digits are");


for(c=0;c<k;c++)
{
printf("\n%s",dig[c]);
}
printf("\noperators ...");
for (d=0;d<m;d++)
{
printf("\n%s",op[d]);
}
printf("\nidentifiers....");
for(d=0;d<n;d++)
{
printf("\n%s",id[d]);
}
}

int yywrap()
{
printf("Errors..\n");
return 1;
}

Output
[cs2884@LabServer compiler]$ lex lex.l
[cs2884@LabServer compiler]$ cc lex.yy.c
[cs2884@LabServer compiler]$ ./a.out

The keywords are


int
float
The headerfiles are
#include<stdio.h>
The digits are
0
0.00
operators...
=
=
identifiers...
void
3. Implementation of Calculator using LEX
%{
#include<stdio.h>
#include<stdlib.h>
int op=0;
float a,b,n,i;
void digit();
%}
digit [0-9]+|[0-9]*"."[0-9]+
add "+"
sub "-"
mul "*"
div "/"
pow "^"
end "\n"
%%
{digit} {digit();}
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}
{pow} {op=5;}
{end} {printf("Result is %f",a);printf("\nEnter new equation\n");}
. {exit(0);}
%%
int main()
{
printf("Enter the equation\n");
yylex();
}
int vywrap()
{
return(1);
}
void digit()
{
if(op==0)
{
a=atof(yytext);
}
else
{
b=atof(yytext);
switch(op)
{
case 1:
a=a+b;
break;
case 2:
a=a-b;
break;
case 3 :
a=a*b;
break;
case 4:
a=a/b;
break;
case 5:
i=1;
n=a;
while(i<b)
{
a=a*n;
i++;
}
break;
default:
printf("invalid operation\n");
}
op=0;
}
}

Output

[cs2884@LabServer compiler]$ lex cal.l


[cs2884@LabServer compiler]$ cc lex.yy.c
[cs2884@LabServer compiler]$ ./a.out
Enter the equation
4+7
Result is 11.0
1—1
Result is 0.0
2/2
Result is 1.0
4. Implementation of Desktop calculator using YACC
LEX file

%{
#include "y.tab.h"
extern int yylval;
%}
DIGIT [0-9]+
OP [+|-|*|/]
%%
{DIGIT} {yylval=atoi(yytext); return NUM;}
{OP} {return *yytext;}
[\n] {return NL;}
%%
int yywrap()
{
return 1;
}

YACC file

%{
#include<stdio.h>
#include<stdlib.h>
int yylval;
%}
%token NUM NL
%left '+' '-'
%left '/''*'
%right UMINUS
%%
S:E NL{printf("result is %d",$$);}
E:E'+'E{$$=$1+$3;}
|E'-'E{$$=$1-$3;}
|E'/'E{$$=$1/$3;}
|E'*'E{$$=$1*$3;}
|'-'E %prec UMINUS{$$=-$2;}
|NUM {$$=$1;}
%%
void main()
{
yyparse();
}
int yyerror()
{
printf("input error");
}
Output
[cs2@LabServer compiler]$ lex calyac.l
[cs2@LabServer compiler]$ yacc -d calyac.y
[cs2@LabServer compiler]$ cc lex.yy.c y.tab.c -ll
[cs2@LabServer compiler]$ ./a.out
2+2
result is 4
2-2
result is 0
2/2
result is 1
2*2
result is 4
-2
result is 2
5. C Program to findε Closure of all states of NFA
#include<stdio.h>
#include<stdlib.h>
struct node
{
int st;
struct node *link;
};
void findclosure(int,int);
void insert_trantbl(int,char,int);
int findalpha(char);
void print_e_closure(int);
static int set[20],nostate,noalpha,s,notransition,c,r,buffer[20];
char alphabet[20];
static int e_closure[20][20]={0};
struct node *transition[20][20]={NULL};
void main()
{
int i,j,k,m,t,n;
struct node *temp;
printf("Enter the number of alphabets?\n");
scanf("%d",&noalpha);
getchar();
printf("NOTE:-[ use leter e as epsilon]\n");
printf("NOTE:- [e must be last character,if it is present]\n");
printf("\nEnter alphabets?\n");
for(i=0;i<noalpha;i++)
{
alphabet[i]=getchar();
getchar();
}
printf("\nEnter the number of states?\n");
scanf("%d",&nostate);
printf("\nEnter no of transition?\n");
scanf("%d",&notransition);
printf("NOTE:- [Transition is in the form--> qno albhabet qno]\n",notransition);
printf("NOTE:- [States number must be greater than zero]\n");
printf("\nEnter transition?\n");
for(i=0;i<notransition;i++)
{
scanf("%d %c%d",&r,&c,&s);
insert_trantbl(r,c,s);
}
printf("\n");
printf("e-closure of states...........\n");
printf("------------\n");
for(i=1;i<=nostate;i++)
{
c=0;
for(j=0;j<20;j++)
{
buffer[j]=0;
e_closure[i][j]=0;
}
findclosure(i,i);
printf("\ne-closure(q%d): ",i);
print_e_closure(i);
}
}
void findclosure(int x,int sta)
{
struct node *temp;
int i;
if (buffer[x])
return;

e_closure[sta][c++]=x;
buffer[x]=1;
if (alphabet[noalpha-1]=='e' && transition[x][noalpha-1]!=NULL)
{
temp=transition[x][noalpha-1];
while(temp!=NULL)
{
findclosure(temp->st,sta);
temp=temp->link;
}
}}
void insert_trantbl(int r,char c,int s)
{
int j;
struct node *temp;
j=findalpha(c);
if(j==999)
{
printf("error\n");
exit(0);
}
temp=(struct node *)malloc(sizeof(struct node));
temp->st=s;
temp->link=transition[r][j];
transition[r][j]=temp;
}
int findalpha(char c)
{
int i;
for(i=0;i<noalpha;i++)
if (alphabet[i]==c)
return i;
return(999);
}
void print_e_closure(int i)
{
int j;
printf("{");
for (j=0;e_closure[i][j]!=0;j++)
printf("q%d,",e_closure[i][j]);
printf("}");
}

Output
[cs2884@LabServer compiler]$ cc eclosure.c
[cs2884@LabServer compiler]$ ./a.out
Enter the number of alphabets?
3
NOTE:- [ use letter e as epsilon]
NOTE:- [e must be last character ,if it is present]

Enter alphabets?
0
1
e

Enter the number of states?


3

Enter no of transition?
5
NOTE:- [Transition is in the form–> qno alphabet qno]
NOTE:- [States number must be greater than zero]

Enter transition?
101
1e2
212
2e3
303

e-closure of states……
—————————–
e-closure(q1): {q1,q2,q3,}
e-closure(q2): {q2,q3,}
e-closure(q3): {q3,}[cs2884@LabServer compiler]$

You might also like