Sie sind auf Seite 1von 21

1)

Yacc unambiguous calculator


%{
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#define YYSTYPE double
%}
%%
input :
| input line
;
line : '\n'
| expr '\n' {printf("Result is %g",$1);}
;
expr : expr '+' term {$$=$1+$3;}
| expr '-' term {$$=$1-$3;}
| term
{$$=$1;}
;
term : term '*' factor {$$=$1*$3;}
| term '/' factor {$$=$1/$3;}
| factor
{$$=$1;}
;
factor : NUM {$$=$1;}
;
NUM : digit {$$=$1;}
| NUM digit {$$=$1*10+$2;}
;
digit : '0' {$$=0;}
| '1' {$$=1;}
| '2' {$$=2;}
| '3' {$$=3;}
| '4' {$$=4;}
| '5' {$$=5;}
| '6' {$$=6;}
| '7' {$$=7;}
| '8' {$$=8;}
| '9' {$$=9;}
;
%%
yylex()
{
return getchar();
}
main()
{

yyparse();yyerror(char *s)
{
printf("%s",s);}
2)bison calculator:

//

#include <stdio.h>
/* Called by yyparse on error. */
/* Infix notation calculator. */
%{
#define YYSTYPE double
#include <math.h>
#include <stdio.h>
int yylex (void);
void yyerror (char const *);
%}
/* Bison declarations. */
%token NUM
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%right '^' /* exponentiation */
%% /* The grammar follows. */
input: /* empty */
| input line
;
line:

'\n'
| exp '\n' { printf ("\t%.10g\n", $1); }

;
exp:

NUM
{ $$ = $1;
}
| exp '+' exp
{ $$ = $1 + $3; }
| exp '-' exp
{ $$ = $1 - $3; }
| exp '*' exp
{ $$ = $1 * $3; }
| exp '/' exp
{ $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2;
}
| exp '^' exp
{ $$ = pow ($1, $3); }
// | exp
| '(' exp ')'
{ $$ = $2;
}

;
%%
int main (void)
{

return yyparse ();


}
//

#include <stdio.h>
/* Called by yyparse on error. */ void yyerror (char const *s)
{
fprintf (stderr, "%s\n", s);
}
#include <ctype.h>
int
yylex (void)
{
int c;
/* Skip white space. */
while ((c = getchar ()) == ' ' || c == '\t')
;
/* Process numbers. */
if (c == '.' || isdigit (c))
{
ungetc (c, stdin);
scanf ("%lf", &yylval);
return NUM;
}
/* Return end-of-input. */
if (c == EOF)
return 0;
/* Return a single char. */
return c;
}

3)Token separation:
#include<iostream>
#include<cctype>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string str,t;
cout<<"Enter your string";
getline(cin,str,'@');
istringstream s(str);
cout<<"\n Tokenizing The String you have entered";
int a,b,c,d,f;
a=b=c=d=f=0;
string e[6];
cout<<" ";
while(getline(s,t,' '))
{
cout<<t<<"\n";
if((t=="+")||(t=="-")||(t=="*")||(t=="/")||(t=="=")||(t=="=="))
{
a++;
e[1]+=" ";
e[1]+=t;
}
else
if((t==")")||(t==",")||(t=="(")||(t=="{")||(t=="}")||(t=="'")||(t==";")||(t==":")||(t=="#")||(t=="<")||(t==
">"))
{
b++;
e[2]+=" ";
e[2]+=t;
}
else if(t=="0"||t=="1"||t=="2"||t=="3"||t=="4"||t=="5"||t=="6"||t=="7"||t=="8"||t=="9")
{
c++;
e[3]+=" ";
e[3]+=t;
}

else
if(t=="int"||t=="float"||t=="char"||t=="string"||t=="main"||t=="void"||t=="double"||t=="while"||t==
"for"||t=="return"||t=="if"||t=="else"||t=="return"||t=="include")
{
d++;
e[4]+=" ";
e[4]+=t;
}else
{
f++;
e[5]+=" ";
e[5]+=t;
}
}
cout<<"\n separating every token into their group\n";
cout<<"\noperators:"<<e[1]<<"\t"<<a;
cout<<"\nliterals:"<<e[2]<<"\t"<<b;
cout<<"\nconsatnt:"<<e[3]<<"\t"<<c;
cout<<"\nkeywords:"<<e[4]<<"\t"<<d;
cout<<"\nidentifiers:"<<e[5]<<"\t"<<f-1;
return 0;
}

4)Macro processing:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string a="" ,tr="" ,val="";
int k,pos=0,j=0,p,l,s,l1,i,t=1,st=0,k1,val1,pv,len,line;
char buf[20];
cout<<"enter\n";
getline(cin,a,'\t');
for(pos=a.find("#define",0);pos!=string::npos;pos=a.find("#define",pos))
{
j++;
pos++;
}
for(k=0;k<j;k++)
{
p=a.find("#define",0);
i=p+8;
pv=a.find(" ",i);
len=pv-i;
tr=a.substr(i,len);
k1=a.find(";",pv+1);
val1=k1-pv+1;
val=a.substr(pv+1,val1);
line=k1-p;
a.erase(p,line);
for(pos=a.find(tr,0);pos!=string::npos;pos=a.find(tr,pos))
{
a.erase(pos,len);
a.insert(pos,val);
pos++;
}
}
cout<<"the macro processed program is \n"<<a;
return 0;
}

5)Symbol table:
#include<iostream>
#include<string>
#include<cctype>
#include<iomanip>
using namespace std;
struct symbol
{
string identifier;
string value;
string type;
string memory;
};
const int MAX_SIZE=100;
string str[MAX_SIZE];
symbol table[MAX_SIZE];
int memstart=10000;
int symbolcount=0,symbolptr=0,datatypeptr=-1;
string datatype[]={"int","char","float","double","long"};
int getcode()
{
int w=-1;
do
{
w++;
cin>>str[w];
}
while(str[w]!="end");
return w;
}
bool isdatatype(string token)
{
for(int j=0;j<5;j++)
{
if(datatype[j]==token)
{
datatypeptr=j;

return true;
}
}
return false;
}
bool isduplicate(string token)
{
for(int i=0;i<symbolcount;i++)
{
if(table[i].identifier==token)
{
symbolptr=i;
return true;
}
}
return false;
}
void showtable()
{
cout<<"Symbol table\n";
cout<<"-------------------------------------\n";
cout<<setiosflags(ios::left)<<setw(20)<<"identifier";
cout<<setiosflags(ios::left)<<setw(15)<<"type";
cout<<setiosflags(ios::left)<<setw(15)<<"value";
cout<<setiosflags(ios::left)<<setw(15)<<"memory\n";
cout<<"--------------------------------------\n";
for(int i=0;i<symbolcount;i++)
{
cout<<setiosflags(ios::left)<<setw(20)<<table[i].identifier+"";
cout<<setiosflags(ios::left)<<setw(15)<<table[i].type+"";
cout<<setiosflags(ios::left)<<setw(15)<<table[i].value+"";
cout<<setiosflags(ios::left)<<setw(15)<<table[i].memory+"";
cout<<endl;
}
cout<<"-----------------------------------\n";
}
string getmemory()
{
string digits="0123456789ABCDEF";
string result;
int temp=memstart;
do
{
result=digits[temp%16]+result;
temp/=16;

}
while(temp);
switch(datatypeptr)
{
case 0:
memstart+=sizeof(int);
break;
case 1:
memstart+=sizeof(char);
break;
case 2:
memstart+=sizeof(double);
break;
case 3:
memstart+=sizeof(long);
break;
case 4:
memstart+=sizeof(float);
break;
}
return result;
}
int main()
{
int tokencount,temp;
bool data,operators,current=false,previous=false;
cout<<"\nEnter the code:\n";
tokencount=getcode();
for(int i=0;i<tokencount;i++)
{
if(!(isdatatype(str[i])))
{
temp=0;
for(int j=0;j<=str[i].length();j++)
{
operators=false;
if((ispunct(str[i][j])!=0&&str[i][j]!='.')||j==str[i].length())
{
if(str[i][j]=='=')
{
previous=current;
current=true;
}
else
{
previous=current;
current=false;
}

operators=true;
}
if((operators||j==str[i].length())&&j-temp>0)
{
string subtoken=str[i].substr(temp,j-temp);
if(previous)
{
table[symbolptr].value=subtoken;
}
else if(!isduplicate(subtoken))
{
table[symbolcount].identifier=subtoken;
table[symbolcount].type=datatype[datatypeptr];
table[symbolcount].memory=getmemory();
symbolptr=symbolcount;
symbolcount++;
}
}
if(operators)
{
temp=j+1;
}
}
}
}
showtable();
return(EXIT_SUCCESS);
}

6)Token separation using lex:


%{
#include<stdio.h>
int ch=9;
int k=0,o=0,i=0,d=0;
%}
letter [a-zA-Z]
digit [0-9]
%%
(int|main)

{
printf("\n Recognized keyword :%s\n",yytext);
k++;

}
[%=+-]

{
printf("\nRecognized operator : %s\n",yytext);
o++;

}
{letter}({letter}|{digit})* {
printf("Recognized Identifier : %s\n",yytext);
i++;
}
[;}{] {
printf("Recognized Delimiter :%s\n",yytext);
d++;
.

}
{}

%%

int main(void)
{
yylex();
printf("\nNo of Keywords :%d\n",k);
printf("No of Operators :%d\n",o);
printf("No of Identifiers :%d\n",i);
printf("No of Delimiter :%d\n",d);
return 0;
}

Data.txt:

main()
{
int a, b, c;
a = 10; b = 20 ;
c = a + b;
}

7)Minimization of dfa:
using namespace std;
#include<iostream>
#include<string>
#include<fstream>
class DFATable
{
public:
string state;
string state_transition[2];
string is_final_state;
};
class process
{
public:
int dt_count;
DFATable dt[20];
process()
{
int i;
cout<<endl<<"Enter no of states : ";
cin>>dt_count;
cout<<endl<<"State trans1 trans2 isfinal?"<<endl;
cout<<""<<endl;
for(i=0;i<dt_count;i++)
cin>>dt[i].state>>dt[i].state_transition[0]>>dt[i].state_transition[1]>>dt[i].is_final_state;
}
void display_DFATable()
{
int i;
string token;
//cout<<endl<<"The i/p DFA table is : "<<endl<<"State \t a \t b \t final accepting state
?"<<endl;
//cout<<""<<endl;
for(i=0;i<dt_count;i++)
{
if(dt[i].state=="")

continue;
cout<<dt[i].state<<"\t"<<dt[i].state_transition[0]<<"\t"<<dt[i].state_transition[1]<<"\t"<<dt[i].is_final_st
ate<<endl;
}
}
void minimize_DFA()
{
int i,j;
for(i=0;i<dt_count-1;i++)
{for(j=i+1;j<dt_count;j++)
{
if(dt[i].state_transition[0]==dt[j].state_transition[0]&&dt[i].state_transition[1]==dt[j].state_transition[1]
&& dt[i].is_final_state==dt[j].is_final_state)
{
replace_state_of_row(i,j);
dt[j].state="";
}
}
}
}
void replace_state_of_row(int i,int j)
{
int k;
for(k=0;k<dt_count;k++)
{
if(dt[k].state_transition[0]==dt[j].state)
dt[k].state_transition[0]=dt[i].state;
if(dt[k].state_transition[1]==dt[j].state)
dt[k].state_transition[1]=dt[i].state;
}
}
};
int main()
{
process p;
cout<<endl<<"The i/p DFA table is : "<<endl<<"State \t a \t b \t final accepting state ?"<<endl;
cout<<""<<endl;
p.display_DFATable();
p.minimize_DFA();
cout<<endl<<"The minimized DFA table is : "<<endl<<"State \t a \t b \t final accepting state
?"<<endl;
cout<<""<<endl;
p.display_DFATable();
return 0;
}

8) Sic assembler:
%{
#include<string.h>
char e[100],f[100],g[100],h[100],j[100],k[100],l[100],b[100],c[100],d[100];
int c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c0=0,c9=0;
int i,add=4096;
%}
L[A-Z]
D[0-9]
%%
{L}+ {
Inst(yytext);
}
%%
Inst(char *s){
if((c1==0)&&(strcmp(s,"STA")==0)){
c1=1;
sprintf(b,"0c%x",add);
printf("%s\t",b);
add=add+3;
hex(b);
}
else if((c2==0)&&(strcmp(s,"LDA")==0)){
c2=1;
sprintf(c,"00%x",add);
printf("%s\t",c);
add=add+3;
hex(c);
}
else if((c3==0)&&(strcmp(s,"JSUB")==0)){
c3=1;
sprintf(d,"48%x",add);
printf("%s\t",d);
add=add+3;
hex(d);
}
else if((c4==0)&&(strcmp(s,"LDS")==0)){
c4=1;
sprintf(e,"6C%x",add);
printf("%s\t",e);
add=add+3;
hex(e);

}else if((c5==0)&&(strcmp(s,"LDX")==0)){
c5=1;
sprintf(f,"04%x",add);
printf("%s\t",f);
add=add+3;
hex(f);
}
else if((c6==0)&&(strcmp(s,"ADD")==0)){
c6=1;
sprintf(g,"18%x",add);
printf("%s\t",g);
add=add+3;
hex(g);
}
else if((c7==0)&&(strcmp(s,"DIV")==0)){
c7=1;
sprintf(h,"24%x",add);
printf("%s\t",h);
add=add+3;
hex(h);
}
else if((c8==0)&&(strcmp(s,"MUL")==0)){
c8=1;
sprintf(j,"20%x",add);
printf("%s\t",j);
add=add+3;
hex(j);
}
else if((c9==0)&&(strcmp(s,"COMP")==0)){
c9=1;
sprintf(k,"28%x",add);
printf("%s\t",k);
add=add+3;
hex(k);
}
else if((c0==0)&&(strcmp(s,"STS")==0)){
c0=1;
sprintf(l,"7C%x",add);
printf("%s\t",l);
add=add+3;
hex(l);
}
}
hex(char *l){
for(i=0;l[i]!=NULL;i++){
switch(l[i]){
case '0': printf("0000");
break;

case '1': printf("0001");


break;
case '2': printf("0010");
break;
case '3': printf("0011");
break;
case '4': printf("0100");
break;
case '5': printf("0101");
break;
case '6': printf("0110");
break;
case '7': printf("0111");
break;
case '8': printf("1000");
break;
case '9': printf("1001");
break;
case 'a':
case 'A': printf("1010");
break;
case 'b':
case 'B': printf("1011");
break;
case 'c':
case 'C': printf("1100");
break;
case 'd':
case 'D': printf("1101");
break;
case 'e':
case 'E': printf("1110");
break;
case 'f':
case 'F': printf("1111");
break;
}
}
}

int main(){
printf("OBJECT CODE:\n\n");
yylex();
}

9)Simulation of dfa:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[10],b[10],e[20][20],f[10];
int c,i,j;
cout<<"enter the string:";
cin>>a;
cout<<"enter the starting state:";
cin>>b;
cout<<"enter the Final state:";
cin>>f;
cout<<"enter the no.of states:";
cin>>c;
char d[50]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(j=0;j<strlen(a);j++)
{
for(i=0;i<c;i++)
{
cout<<"mov("<<d[i]<<","<<a[j]<<"):";
cin>>e[i][j];
}
}
cout<<"the transition table\n\n";
cout<<"\t\t\t\ta\t\t\t\tb\t\t\n\n";
for(j=0;j<c;j++)
{
for(i=0;i<strlen(a);i++)
{
cout<<"\t\t\t\t"<<e[j][i];
//cout<<"\t\t\t";
}cout<<"\n\n";
}
char z[10],u[10];
//u='A';
int y;
i=0;
cout<<"enter the string:";
cin>>z;
cout<<"\n";
int x=0,v=0;
u[0]='A';
for(x=0;x<strlen(z);x++)
{
//u='\0';

//cout<<z[x]<<"\n";
if(z[x]=='a')
{
y=0;
//x++;
}
else
{
y=1;
//x++;
}
cout<<"-->"<<u[i];
//u='\0';
i++;
u[i]=e[v][y];
//cout<<u<<"\n";
if(u[i]=='D')
{v=3;
}
if(u[i]=='A')
{v=0;
}
if(u[i]=='B')
{v=1;
}
if(u[i]=='C')
{v=2;
}
//u='\0';
//cout<<"-->"<<u;
}
//while(z[x]=='$');
cout<<"-->"<<u[i];
if(u[i]=='D')
{
cout<<"\n yes \n";
}
else
cout<<"\n no \n";
}

10)simulation of nfa:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{ int m,n[11][4],i=0,j=0,k=0,p,u=0,w,h,t,r;
int s[10],a[10],b[10],g;char l,f[10];
int count=0;
cout<<"Enter Input:";
cin>>f;
w=strlen(f);
cout<<"Enter NFA:";
for(i=0;i<11;i++)
{ for(j=0;j<4;j++)
{ cin>>n[i][j];
}
}
i=0;j=0;
s[j++]=0;
while(n[i][2]!=11)
{ i=n[i][2];
if(i!=11)
s[j++]=i;
}
i=0;
while(n[i][3]!=11)
{ i=n[i][3];
if(i!=11)
s[j++]=i;
}
k=0;i=0;
while(k!=j)
{ t=j;
for(k=i;k<j;k++)
{ i=s[k];
while(i!=11)
{ i=n[i][2];
for(m=0;m<j;m++)
{ if(s[m]==i)
i=11;
}
if(i!=11)
s[j++]=i;
}
i=s[k];
while(i!=11)
{ i=n[i][3];

for(m=0;m<j;m++)
{ if(s[m]==i)
i=11;
}
if(i!=11)
s[j++]=i;
}
}
if(t<j)
i=t;
}
for(u=0;u<w;u++)
{ if(f[u]=='a')
{ h=0;
}
else
{ h=1;
}
r=0;k=0;
while(k<j)
{ i=s[k++];
if(n[i][h]!=11)
{ a[r++]=n[i][h];}
}
j=0;
for(p=0;p<r;p++)
{
i=a[p];
s[j++]=i;
while(n[i][2]!=11)
{ i=n[i][2];
if(i!=11)
{ s[j++]=i;}
}
i=a[p];
while(n[i][3]!=11)
{ i=n[i][3];
if(i!=11)
{ s[j++]=i;}
}
t=0;i=0;
while(t!=j)
{ t=j;
for(k=i;k<j;k++)
{ i=s[k];
while(i!=11)
{ i=n[i][2];

for(m=0;m<j;m++)
{ if(s[m]==i)
i=11;
}
if(i!=11)
{ s[j++]=i;}
}
i=s[k];
while(i!=11)
{ i=n[i][3];
for(m=0;m<j;m++)
{ if(s[m]==i)
i=11;
}
if(i!=11)
{ s[j++]=i;}
}
}
if(t<j)
i=t;
}
}
}
count=0;
for(i=0;i<j;i++)
{
if(s[i]==10)
count++;
}
if(count>0)
cout<<"YES";
else
cout<<"NO";
return 0;
}

Das könnte Ihnen auch gefallen