Vous êtes sur la page 1sur 32

y

S stem r o rammin

LAB FILE

AMITY SCHOOL OF ENGINEERING


&
TECHNOLOGY

SUBMITTED TO:

SUBMITTED BY:

INDEX
S.No.

Date

Assignment

Remarks

Practical-1
AIM: WAP to create a Lexical Analyzer to count number of identifiers,
constants and operators in a given expression.
CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#define ope(x)(x=='+'||x=='-'||x=='*'||x=='/'||x=='%'||x=='='||x=='^'||x==')'||x=='(')
#define alp(x) ((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
#define num(x) ((str[i]>='0'&&str[i]<='9'))
void main()
{
clrscr();
char str[20];
int cons=0,iden=0,oper=0,keyw=0;
cout<<"Enter an expression:";
gets(str);
int l=strlen(str);
for(int i=0;i<=l;i++) {
if(alp(str[i])) {
while(alp(str[i]))
i++;
iden++;
i--;
}
else if(num(str[i]))
{
while(num(str[i]))
i++;
cons++;
i--;
}
else if(ope(str[i]))
oper++; }
cout<<"\nNumber of identifiers : "<<iden;
cout<<"\nNumber of constants : "<<cons;
cout<<"\nNumber of operators : "<<oper;
getch();

OUTPUT:

PRACTICAL 2
AIM: WAP to convert an infix expression into prefix

expression. CODE:
#includ
e<stdio.
h>
#includ
e<string
.h>
#includ
e<conio
.h>
#includ
e<iostre

am.h>
#define operand(x)(x>='a'&&x<='z' || x>='A'&&x<='Z' ||
x>='0'&&x<='9')
char infix[30]
,prefix[30]
,stack[30]; int top=1,i=0;
void push(char x)
{
stack[++top]=x;

}
char pop()
{
return(stack[top--]);
}
i
n
t
p
rt
y

(
c
h
a
r
x
)
{

int y;
switch(x)
{
case '(':y=6;
case '^':

break;
y=5;
break;

break;
case '/': y=4; case '*': y=4;
break;
y=3;
case '+':
break;
case '-':y=3; case ')':y=5;

b
reak;
default
:
y=-1;

}
return y;
}
int stkprty(char x)
{
int y;
switch(x)
{
case '(':y=6; break;
y=5;
case '^': case break;
'/': y=4; case break;
y=4;
'*': case '+': break;
y=3;
case '-':y=3; break;
case ')':y=0;
default:
}
return y;

break;
break;
y=-1;

}
void infixtoprefix() {
int j,l=0; char x,y;
stack[++top]='\0';
for (j=strlen(infix)-1; j>=0; j--) {
x=infix[j];
if (operand(x))
prefix[l++]=x;
else if (x=='(')
{
while ((y=pop()) !=')') prefix[l++]=y;

while (stkprty(stack[top])>=prty(x)) prefix[l++]=pop();


push(x);
while (top>=0)
prefix[l++]=pop();
}
void main()
{
}
clrscr();

cout<<"\nEnter an infix expression : ";


gets(infix);
infixtoprefix();
strrev(prefix);
cout<<"\nThe resulting prefix expression is :
"<<prefix;
getch();
}

OUTPUT:

AIM: WAP to convert an infix expression into


postfix expression. CODE:
#include<conio.h>
#include<ctype.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#define opeh(x)(x=='/'||x=='*')
#define opel(x)(x=='+'||x=='-')
char str[20] ,postfix[20] ,stack[ 10];
int top=-1;
void push(char c)
{
stack[++top]=c;
}
char pop()
{
return stack[top--];
}
void main()
}

{
clrscr();
cout<<"\nEnter the infix expression : ";
gets(str);
int l=strlen(str);
for(int i=l- 1 ;i>=0;i--)
str[i+1 ]=str[i];
str[0]='(';
str[++l]=')';
str[++l]='\0';
int j=-1,k=-1;
for(i=0;i<l;i++)
{
if(str[i]=='(')
push(str[i]);
else if(str[i]==')')
{
while(stack[top] !='(')
postfix[++k]=pop();
top--;
}
else if(opeh(str[i]))
push(str[i]);
else if(opel(str[i]))
{
while(opeh(stack[j]))
pos
tfix[+
+k]=pop();
push(str[i]);

}
else if(isalpha(str[i]))
postfix[++k]=str[i];
}
p
o
s
t
f
i
x
[
+
+
k
]
=
'\

0
';
c
o
u
t
<
<
p
o
s
t
f
i
x
;
g
e
t
c
h
(
)
;
}

OUTPUT:

AIM: WAP to accept a string of the grammar


a*ab. CODE:
#include<iostrea
m.h>
#include<conio.h
>
#include<stdio.h>
#include<string.h
>
}

void main()
{
clrscr();
char
str[41];
int f=0;
int l;
cout<<"\nEnter String of the form \"
a*ab \" : "; gets(str);
l=strlen(str);
if(str[l- 1 ]=='b'&&str[l-2]=='a')
f=1;
for(int i=0;i<l-2;i++)
{
if(str[i] !
='a') {
f=
0;
break;
}
}
if(f==1)
cout<<"\nString ACCEPTED";
else
cout<<"\nString NOT ACCEPTED";
getch();

PRACTICAL 18

OUTPUTS:

AIM: WAP to convert NDFA into DFA. CODE:


#include<iostream.h> #include<conio.h>
#include<stdio.h> #include<string.h>
struct table
{
char state [20]; char out0[20]; char
out1[20];
};
void strsort(char c[]) {
int l=strlen(c); char t;
for(int j=0;j<l- 1 ;j++)
{
for(int i=0;i<l- 1-j ;i++)
{
if(c[i]>c[i+1])
{
t=c[i];
c[i]=c[i+1];
c[i+1]=t;
}
}
}
}
void strred(char c[]) {
int l=strlen(c); char t;
char s[100];
for(int i=0,n=0;i<l;i++)
{
while(c[i]==c[i+1])
i++;
s [n++]=c[i];
}
s[n]='\0';

PRACTICAL 20
strcpy(c,s);

void main() {
clrscr();
struct table n[20];
int count=0;
cout<<"\nEnter number of states in NDFA : \n";
cin>>count;
for(int i=0;i<count;i++)
{
cout<<"Enter state : ";
cin>>n[i] .state;
cout<<"Enter output states for input '0' : ";
cin>>n[i] .out0;
cout<<"Enter output states for input '1' : ";
cin>>n[i] .out1;
}
cout<<"\n\n\t\t\t***********NDFA TABLE***********";
cout<<"\n\t\t\tCurrent State\t\tNext State";
cout<<"\n\t\t\t
\t\tI/P 0\tI/P 1 ";
for(i=0;i<count;i++)
{
cout<<"\n\t\t\t";
cout<<n[i] .state<<"\t\t\t"<<n[i] .out0<<"\t"<<n[i] .out1; }
getch();
char str[ 100]="";
struct table d[20];
char nst[20][20]; int
ncnt=0;
char nst2 [20] [20];
for(i=0;i<count;i++)
{
strcpy(d[i].state,n[i].state);
strcpy(d[i] .out0,n[i] .out0);
strcpy(d[i] .out1,n[i] .out1);
} for(i=0;i<count;i++)
{
strsort(d[i].state);
strsort(d[i] .out0);
strsort(d[i] .out1);
}
int f1,f2;
repeat: for(i=0;i<count;i++) {
f1=0;

void main() {
for(int k=0;k<count;k++)
{
if(! strcmp(d[k] .state,d[i] .out0))
{
f1=1;
break;
}

for(int h=0;h<ncnt;h++)
{
if(! strcmp(nst[h] ,d[i] .out0)) {
f1=1;
break;
}

strcpy(nst[ncnt],d[i] .out0);
ncnt++;
} f2=0;
for( k=0;k<count;k++)
{
if(! strcmp(d[k] .state,d[i] .out1))
{
f2=1;
break;

for(int h=0;h<ncnt;h++)
{
if(! strcmp(nst[h] ,d[i] .out1)) {
f2=1;
break;
}

strcpy(nst[ncnt],d[i] .out1); ncnt++;

if(ncnt==0)
}

void main() {
goto ahead;
for(i=0;i<ncn
t;i++) {
strsort(nst[i]);
}
//new states = ncnt
//add new states to
dfa table int j;
for(i=count,j=0;i<count+ncnt;i+
+,j++) {
strcpy(d[i].state,nst[j]);
//taking union
strcpy(str,"");
for(int k=0;k<strlen(d[i]
.state);k++) {
for(int r=0;r<count;r++)
{
if(d[i].state[k]==d[r].state[0]&&(strlen(d[r].state)
==1)) {
strcat(str,d[r] .out0);
}
}
}
strsort(str);
strred(str);
strcpy(d[i] .out0,str);
////////////////////////////
strcpy(str,"");
//////////////////
for( k=0;k<strlen(d[i]
.state);k++) {
for(int r=0;r<count;r++)
{
if(d[i].state[k]==d[r].state[0]&&(strlen(d[r].state)
==1)) {

void main() {
strcat(str,d[r] .out1);
}}
strsort(str); strred(str);
strcpy(d[i] .out1,str);
} count+=ncnt;
for(i=0;i<=count;i++) {
strsort(d[i].state); strsort(d[i] .out0); strsort(d[i] .out1);
}
ncnt=0;
goto repeat;

}
ahead:

cout<<"\n\n\t\t********************DFA TABLE********************";
cout<<"\n\t\t\tCurrent State\t\tNext State";
cout<<"\n\t\t\t
\t\tI/P 0\tI/P 1 ";
cout<<"\n\t\t*************************************************";
for(i=0;i<count;i++)
{
cout<<"\n\t\t\t";
cout<<d[i] .state<<"\t\t\t"<<d[i] .out0<<"\t"<<d[i] .out1;
} getch();

OUTPUTS:

PRACTICAL 6
AIM: WAP to convert RE into NDFA.
CODE:
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct table
{
char
state;
char
out0;
char out 1;
char oute[5];
};
void main()
{ clrscr()
;
char re[31];
table n[26];
int c=0;
cout<<"Enter the regular expression : ";
gets(re);
int l=strlen(re);
for(int i=0;i<l;i++)
{ if(re[i]=='(')
{
int j=i;
j++;
if(re[j]=='0')
{ j+
+;
if(re[j]=='|')
{ j+
+;
if(re[j]=='1')
{ j+
+;
if(re[j]==')')
{ j+
+;
if(re[j]=='*')
n[c].state='a'+c;

n[c- 1] .oute[0]=n[c] .state; n[c- 1] .oute[ 1]='\0';


c++;
n[c-1].out0='-'; n[c-1].out1='-';
n[c- 1] .oute[0]='a'+c; n[c-1].oute[1]='a'+c+1; n[c- 1]
.oute[2]='a'+c+5; n[c- 1] .oute[3]='\0'; n[c].state='a'+c; n[c]
.out0='a'+c+2; n[c].out1='-'; strcpy(n[c] .oute,"-"); c++;
n[c].state='a'+c; n[c] .out1='a'+c+2;
n[c] .out0='-'; strcpy(n[c] .oute,"-"); c++;
n[c].state='a'+c; n[c] .out0='-'; n[c] .out1='-';
n[c] .oute[0]='a'+c+2;
n[c] .oute[ 1 ]='\0';
c++;
n[c].state='a'+c; n[c] .out0='-'; n[c].out1='-';
n[c] .oute[0]='a'+c+2;
n[c] .oute[ 1 ]='\0';
c++;
n[c].state='a'+c; n[c] .out0='-'; n[c].out1='-';
n[c] .oute[0]='a'+c-5; n[c].oute[1]='a'+c+1;
n[c] .oute[2]='\0';
c++;
n[c].state='a'+c; n[c] .out0='-'; n[c].out1='-'; strcpy(n[c]
.oute,"-"); i=i+5;
}
else
{
n[c].state='a'+c;

n[c- 1] .oute[0]=n[c] .state;


n[c- 1] .oute[ 1]='\0';
c++;
n[c-1].out0='-'; n[c1].out1='-';
n[c- 1] .oute[0]='a'+c;
n[c-1].oute[1]='a'+c+1;
n[c- 1] .oute[2]='\0';
n[c].state='a'+c; n[c]
.out0='a'+c+2;
n[c].out1='-';
strcpy(n[c] .oute,"-"); c+
+;
n[c].state='a'+c;
n[c] .out1='a'+c+2;
n[c] .out0='-';
strcpy(n[c] .oute,"-");
c++;
n[c].state='a'+c;
n[c] .out0='-';
n[c].out1='-';
n[c] .oute[0]='a'+c+2;
n[c] .oute[ 1 ]='\0';
c++;
n[c].state='a'+c;
n[c] .out0='-';
n[c].out1='-';
n[c] .oute[0]='a'+c+2;
n[c] .oute[ 1 ]='\0';
c++;
n[c].state='a'+c;
n[c] .out0='-';
n[c].out1='-';
n[c] .oute[0]='a'+c+1;
n[c] .oute[ 1 ]='\0';
c++;
n[c].state='a'+c; n[c]
.out0='-'; n[c].out1='-';
strcpy(n[c] .oute,"-");
i=i+4;

else if(re[i]=='0'||re[i]==' 1')


{
if(re[i+ 1] !='*'&&re[i+1] !='|')//single input char
{
n[c]. state='a'+c;
char tmp[2];
tmp[0]=n[c].state;
tmp[1 ]='\0';
strcat(n[c-1 ] .oute,tmp);
c++;
n[c].state='a'+c; n[c]
.out0='-' ; n[c] .out1='-'
; strcpy(n[c] .oute,"-");

if(re[i]=='0')
{
n[c- 1] .out0=n[c] .state;
n[c-1].out1='-';
strcpy(n[c-1 ] .oute,"-");
}
else if(re[i]=='1')
{
n[c- 1] .out1=n[c] .state;
n[c-1].out0='-';
strcpy(n[c-1 ] .oute,"-");
} c+
+;
} else
{
}
}
}
n[c].state='a'+c;
n[c] .out0='-';
n[c].out1='-';
strcpy(n[c] .oute,"-");
cout<<"\n\n";
cout<<"State\t Next state at 0\tNext state at 1 \tNext state at e\n\n";
for(i=0;i<=c;i++)
{
cout<<n[i].state<<"\t\t"<<n[i] .out0<<"\t\t\t"<<n[i] .out1<<"\t\t\t"<<n[i] .oute;
cout<<"\n";
} getch()

}
OUTPUT:

Vous aimerez peut-être aussi