Program 1:-C++ Program For Finite Automata Based Tokens Recognizer I.E Key Word
Program 1:-C++ Program For Finite Automata Based Tokens Recognizer I.E Key Word
//the n.o. in case is state and the alphabet are the possible input symbol
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int load[7];
int s=0,k;
for(k=0;k<len;k++)
switch(s)
case 0:{
if(job[k]=='f') s=1;
if(job[k]=='w') s=3;
if(job[k]=='d') s=7;
if(job[k]=='g') s=8;
if(job[k]=='v') s=11;
if(job[k]=='i') s=14;
if(job[k]=='c') s=16;
if(job[k]=='e') s=19;
break;
}
case 14:{
if(job[k]=='n') s=15;
break;
void smartdisplay()
char dis[9][6]={"For","While","Do","Goto","Void","Int","Char","Else","If"};
for(int h=0;h<9;h++)
void main()
clrscr();
FILE *fp;
char n[9],buf[100],c;
int bl=0,t;
for(t=0;t<9;t++)
load[t]=0;
gets(n);
fp=fopen(n,"r");
while(1)
{
c=fgetc(fp);
if(c==EOF) break;
if(c=='\n')
work(buf,bl);
bl=0;
buf[bl]='\0';
else {
buf[bl]=c;
bl++;
buf[bl]='\0';
smartdisplay();
fclose(fp);
getch();
}
Program 2:- C++ Program for implement the LEX tool to recognize the
tokens
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
int y;
char table[100],ttype[100][4];
void display()
for(int i=0;i<y;i++)
cout<<table[i]<<" "<<ttype[i]<<endl;
int search(char k)
int i,j=0;
for(i=0;i<y;i++)
if(table[y]==k) j=1;
return(j);
int i,j,s=0,b;
for(i=0;i<len;i++)
switch(s)
case 0:{
if(d[i]=='i') s=1;
if(d[i]=='c') s=3;
case 2: { if(d[i]=='t'){
i++;
while(d[i]!=';')
j=d[i];
if(j>96&&j<123){
b=search(d[i]);
if(b==0) {
table[y]=d[i];
strcpy(ttype[y],"int");
y++;
i++;
}
s=0;
break;
i++;
while(d[i]!=';')
j=d[i];
if(j>96&&j<123){
b=search(d[i]);
if(b==0) {
table[y]=d[i];
strcpy(ttype[y],"char");
y++;
i++;
s=0;
break;
}
void main()
clrscr();
FILE *fp;
char n[9],buf[100],c;
int bl=0;
y=0;
gets(n);
fp=fopen(n,"r");
while(1)
c=fgetc(fp);
if(c==EOF) break;
if(c=='\n')
lexwork(buf,bl);
bl=0;
buf[bl]='\0';
else {
buf[bl]=c;
bl++;
buf[bl]='\0';
display();
fclose(fp);
getch();
}
Program 3:- C++ Program for the grammar is to recognize the given
expression
E->TE`
T->FT`
T`->*FT`/%
F->(E)/id
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
int l,rd,v;
char st[15];
void e();
void f();
void t();
void td();
void ed();
void f() // the F part of grammer seems to be easist but it is actually trickiest
int j;
if(st[rd]=='('){
rd++;
e();
else rd++;
else {
j=st[rd];
if(j>47&&j<58) rd++;
void td()
if(st[rd]=='*'){
rd++;
f();
td();
void t()
f();
void ed()
if(st[rd]=='+') {
rd++;
t();
ed();
//there was suppose to be null guidance here but due to 'if' it skips the process anyways
void e()
t();
ed();
void recdes()
rd=0;
v=0;
e();
void main()
clrscr();
l=strlen(st);
recdes();
if(rd!=l) v=1;// Very Imp. Figure out urself else whole concept is useless!! not in book
if(v==0) cout<<"Valid"<<endl;
else cout<<"Invalid"<<endl;
getch();