Implementation of Lexical Analyser Using C
Implementation of Lexical Analyser Using C
Implementation of Lexical Analyser Using C
}
Output
%{
#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]);
}
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
Output
%{
#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",¬ransition);
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 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]$