Sunteți pe pagina 1din 13

Program 1:- C++ Program for finite automata based Tokens recognizer

i.e key word


//can recognise for, while, do, goto, void, int, char, if, else

//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];

void work(char job[100],int len)

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 1:{if(job[k]=='o') s=2; else s=0; break;}

case 2:{if(job[k]=='r'){ s=0; load[0]++;} else s=0; break;}

case 3:{if(job[k]=='h') s=4; else s=0; break;}

case 4:{if(job[k]=='i') s=5; else s=0; break;}

case 5:{if(job[k]=='l') s=6; else s=0; break;}

case 6:{if(job[k]=='e'){ s=0; load[1]++;} else s=0; break;}

case 7:{if(job[k]=='o'){ s=0; load[2]++;} else s=0; break;}

case 8:{if(job[k]=='o') s=9; else s=0; break;}

case 9:{if(job[k]=='t') s=10; else s=0; break;}

case 10:{if(job[k]=='o'){ s=0; load[3]++;} else s=0; break;}

case 11:{if(job[k]=='o') s=12; else s=0; break;}

case 12:{if(job[k]=='i') s=13; else s=0; break;}

case 13:{if(job[k]=='d'){ s=0; load[4]++;} else s=0; break;}

case 14:{

if(job[k]=='n') s=15;

else {if(job[k]=='f') {s=0; load[8]++;} else s=0;}

break;

case 15:{if(job[k]=='t'){ s=0; load[5]++;} else s=0; break;}

case 16:{if(job[k]=='h') s=17; else s=0; break;}

case 17:{if(job[k]=='a') s=18; else s=0; break;}

case 18:{if(job[k]=='r'){ s=0; load[6]++;} else s=0; break;}

case 19:{if(job[k]=='l') s=20; else s=0; break;}

case 20:{if(job[k]=='s') s=21; else s=0; break;}


case 21:{if(job[k]=='e'){ s=0; load[7]++;} else s=0; break;}

void smartdisplay()

char dis[9][6]={"For","While","Do","Goto","Void","Int","Char","Else","If"};

for(int h=0;h<9;h++)

if(load[h]!=0)cout<<dis[h]<<" was found "<<load[h]<<" times"<<endl;

void main()

clrscr();

FILE *fp;

char n[9],buf[100],c;

int bl=0,t;

for(t=0;t<9;t++)

load[t]=0;

cout<<"Enter file name"<<endl;

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);

void lexwork(char d[100],int len)


{

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 1: { if(d[i]=='n') s=2; else s=0;}

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;

case 3:{ if(d[i]=='h') s=4; else s=0;}

case 4:{ if(d[i]=='a') s=5; else s=0;}

case 5:{ if(d[i]=='r'){

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;

cout<<"Enter file name"<<endl;

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`

E`->+TE`/% %-signify ebsille

T->FT`

T`->*FT`/%

F->(E)/id

eg: 5+(6*9) should be accepted

Double digit n.o. r not taken into consideration

Refer Aho Ullman page 183

#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();

if(st[rd]!=')'){ v=1; rd=l;}

else rd++;

else {

j=st[rd];

if(j>47&&j<58) rd++;

else { v=1; rd=l;}

void td()

if(st[rd]=='*'){

rd++;

f();

td();

void t()

f();

td(); //the d added to name t is to signify T` part of grammer


}

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();

cout<<"Input the string"<<endl;


gets(st);

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();

S-ar putea să vă placă și