You are on page 1of 54

Program 1

/*program t
odesi
gnt
okenseparat
orf
ort
hegi
venexpressi
on*
/

#include<st dio.h>
#include<coni o. h>
#include<st ring. h>
#include<ct ype. h>
voidmai n()
{
char
exp[50] ="\0",
con[ 50]="\0",
kwd[ 50]="\
0",i
d[50]="
\0",
sym[
50]
="\
0",
opr[
50]
="\
0";
charkey[ 6][10]={ "i
f",
"for",
"do",
"while",
"i
nt",
"f
loat"}
;
charch;
charpt r[10] [1
0] ={ "
\0"}
;
i
nti=0,j=0,k=- 1,n=- 1,
p=- 1
,s=-1;
puts("Ent ert heexpressi onforlexicalanalysis")
;
gets(exp) ;
puts("\
nThet okensare" );
do
{
ch=exp[ i];
i
f(i
salpha( ch) )
{
k=-1;
ptr[++n] [
++k] =ch;
i
++;
ch=exp[ i];
i
f(i
salpha( ch) ||
isdi git(
ch) )
{
while(isalpha( ch) |
|i
sdigit(ch))
{
ptr[n][
++k] =ch;
i
++;
ch=exp[ i];
}
while(j<6)
{
i
f(strcmp( key[ j],ptr[n])==0)
{
ptr[n][++k] =''
;
strcat (
kwd, ptr[n])
;
break;
}
i
f (
j==5)
{
ptr[n][++k] =''
;
strcat (
id,ptr[n]);
}
j++;
}
}
else
{
ptr[n][++k] =''
;
strcat (
id,ptr[n]);
}
i
- -
;
ch=exp[ i
];
j=0;
}
elsei f
(isdigit(
ch) )
{
k=- 1
;
ptr[++n] [
++k] =ch;
i
++;
ch=exp[ i
];
i
f (
isdigit(
ch) )
{
whi le(i
sdigit(ch))
{
ptr[n][++k] =ch;
i
++;
ch=exp[ i
];
}
}
i
- -
;
ch=exp[ i
];
ptr[n][++k] =''
;
strcat (
con, ptr[n]);
}
elsei f
(ch==' +'
||
ch=='-
'
||
ch==' *'
|
|ch=='/'|
|ch=='%'
||
ch=='
>'|
|ch=='
<'
||
ch=='='
||
ch=='
!
')
{
opr[++p] =ch;
i
++;
ch=exp[ i];
i
f(ch=='+'|
|ch=='-'|
|ch=='
*'
||
ch=='
/'
||
ch=='%'|
|ch=='
>'
||
ch=='
<'|
|ch=='
='
||
ch=='
!
')
{
opr[++p] =ch;
}
else
{
i
--;
ch=exp[ i];
opr[++p] =''
;
}
}
else
{
sym[ ++s] =ch;
sym[ ++s] =''
;
}
i
++;
}while(exp[ i
]!
='\0'
);
puts("\nKeyword: "
);
puts(kwd) ;
puts("\nI
dent if
ier:");
puts(id);
puts("\nConst ant:");
puts(con) ;
puts("\nOperat or:");
puts(opr) ;
puts("\nSymbol :
");
puts(sym) ;
getch();
}
Program 2:

/*program t
oimpl
ementasymbolt
abl
e*/

#include<st dio.
h>
#include<coni o.h>
#include<mal l
oc. h>
#include<st ri
ng. h>
#include<st dli
b.h>
//#defineNULL0
intsize=0;
voidInsert ()
;
voidDi splay();
voidDel ete();
intSearch( charl ab[ ])
;
voidModi f
y();
structSymbTab
{
charl abel[10],
symbol [
10];
intaddr;
structSymbTab* next;}
;
structSymbTab* fi
rst,
*last;
voidmai n( )
{
intop,y;
charl a[10];
//clrscr( )
;
do
{
printf
("\n\t
SYMBOLTABLEI MPLEMENTATI
ON\
n")
;

printf(
"\n\t
1.
INSERT\n\
t2.
DISPLAY\n\
t3.
DELETE\
n\t
4.SEARCH\
n\t
5.MODI
F
Y\n\t6.
END\ n");
printf
("\n\
tEnteryouroption:"
);
scanf("%d",&op);
swi t ch( op)
{
case1 :
Insert ();
break;
case2:
Di spl ay();
break;
case3:
Del ete();
break;
case4:
pri nt f("
\n\tEntert helabeltobesearched:"
);
scanf ("%s" ,
la);
y=Search( l
a) ;
pri nt f("
\n\tSearchResul t:"
);
if(y==1 )
pri nt f
("\n\tThel abeli
spresenti nthesymboltabl
e\n"
);
else
pri nt f
("\n\tThel abeli
snotpresenti nthesymboltabl
e\n"
);
break;
case5:
Modi f
y( )
;
break;
case6:
exi t (
0) ;
}
}whi le(op<6) ;
get ch( )
;
}
voidI nsert ()
{
i
ntn;
charl [10];
print f("\n\tEnt erthelabel:")
;
scanf ("%s" ,
l);
n=Search( l
);
i
f(n==1 )
pri nt f("
\n\tThel abelexistsalreadyinthesymboltabl
e\n\
tDupl
icat
e
can' tbei nsert ed");
else
{
structSymbTab* p;
p=mal l
oc( sizeof (structSymbTab) )
;
strcpy( p- >label ,
l
);
pri ntf("
\n\ tEnt ert hesymbol:" );
scanf ("
%s" ,
p->symbol );
pri ntf("
\n\ tEnt ert headdress:" );
scanf ("
%d" ,
&p- >addr) ;
p->next =NULL;
if(
si ze==0)
{
first =p;
last =p;
}
else
{
last ->next =p;
last =p;
}
size++;
}
pri nt f(
"\n\tLabeli nsert ed\n");
}
voidDi spl ay( )
{
i
nti ;
structSymbTab* p;
p=f irst;
print f("\
n\ tLABEL\ t\tSYMBOL\ t\tADDRESS\ n"
);
for( i
=0; i
<si ze;i++)
{
pri ntf("
\t %s\ t
\t%s\ t\
t %d\n",
p->label,
p->symbol,
p->addr)
;
p=p- >next ;
}
}
i
ntSearch( charl ab[])
{
inti ,
f l
ag=0;
st ructSymbTab* p;
p=f irst ;
for( i
=0; i
<si ze;i++)
{
if(
st rcmp( p->label ,
l
ab) ==0)
flag=1 ;
p=p- >next ;
}
ret urnf lag;
}
voidModi fy()
{
charl [10],nl[10];
i
ntadd, choi ce,i,
s;
structSymbTab* p;
p=first;
print f
("\n\tWhatdoyouwantt omodi f
y?\
n");
print f
("\n\t1.Onlyt helabel\n\t2.
Onlytheaddress\
n\t
3.Bot
hthelabeland
address\ n" );
print f
("\tEnt eryourchoi ce:" );
scanf ("
%d" ,&choi ce);
swi tch( choi ce)
{
case1 :
pri ntf("\n\tEnt ertheol dlabel:")
;
scanf ("%s" ,
l);
s=Search( l)
;
if(s==0)
pri ntf("\
n\ tLabelnotf ound\ n");
el se
{
pri ntf("\
n\ tEnt erthenew l abel:");
scanf ("%s" ,
nl);
for( i
=0; i<size;i++)
{
if(strcmp( p->label,
l)
==0)
strcpy( p->label,
nl);
p=p- >next ;
}
pri ntf("\
n\ tAfterModi fication:\
n")
;
Di splay( );
}
break;
case2:
pri ntf("\n\tEnt erthelabelwheret headdressistobemodif
ied:");
scanf ("%s" ,
l);
s=Search( l)
;
if(
s==0)
print f("
\n\tLabelnotf ound\n")
;
else
{
print f("
\n\tEnt erthenew address:")
;
scanf ("
%d" ,
&add) ;
for(i=0;i<size;i++)
{
if(
st rcmp( p->label,
l
)==0)
p->addr=add;
p=p- >next ;
}
print f("
\n\tAfterModi fi
cation:
\n"
);
Displ ay();
}
break;
case3:
print f("
\n\tEnt ertheoldlabel:"
);
scanf (
"%s" ,
l);
s=Search( l)
;
if(
s==0)
print f("
\n\tLabelnotf ound\n")
;
else
{
print f("
\n\tEnt erthenew label:")
;
scanf ("
%s" ,
nl);
print f("
\n\tEnt erthenew address:")
;
scanf ("
%d" ,
&add) ;
for(i=0;i<size;i++)
{
if(
st rcmp( p->label,
l
)==0)
{
strcpy( p->label,
nl);
p->addr=add;
}
p=p- >next ;
}
print f("
\n\tAfterModi fi
cation:
\n"
);
Displ ay();
}
break;
}
}
voidDel ete()
{
i
nta;
charl [10];
structSymbTab* p,*
q;
p=f i
rst ;
printf("\n\tEnterthel abeltobedel et
ed:"
);
scanf ("%s" ,
l);
a=Search( l
);
i
f(a==0)
printf (
"\n\tLabelnotf ound\n");
else
{
i
f(strcmp( f
irst-
>label,l
)==0)
fi
rst =first->next;
elsei f(strcmp( l
ast ->l
abel,l
)==0)
{
q=p- >next ;
whi le(strcmp( q- >l
abel,l
)!
=0)
{
p=p- >next ;
q=q- >next ;
}
p->next =NULL;
last=p;
}
else
{
q=p- >next ;
whi le(strcmp( q- >l
abel,l
)!
=0)
{
p=p- >next ;
q=q- >next ;
}
p->next =q->next ;
}
size- -;
printf (
"\n\tAfterDelet i
on:\
n");
Displ ay();
}
}
Program 3:

/*program t
odevel
op al
exi
calanal
yzert
orecogni
zeaf
ew pat
terns
*/

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

#i
ncl
ude<ct
ype.
h>

#i
ncl
ude<st
ring.
h>

voi
dmai
n()

FI
LE*
fi,
*f
o,*
fop,
*f
k;

i
ntf
lag=0,
i
=1;

charc,
t,
a[1
5],
ch[
15]
,f
il
e[20]
;

//cl
rscr(
);

pri
ntf
("\
nEnt
ert
heFi
leName:
")
;

scanf
("%s"
,&f
il
e);
f
i=f
open(f
il
e,"
r")
;
f
o=fopen(
"i
nter.
c",
"w"
);

f
op=f
open(
"oper.
c",
"r"
);

f
k=f
open(
"key.
c",
"r"
);

c=get
c(f
i)
;

whi
le(
!f
eof
(fi
))

i
f(
isal
pha(
c)|
|i
sdi
git
(c)
||
(c=='
['
||
c=='
]'
||
c=='
.
'==1
))

f
put
c(c,
fo)
;

el
se

i
f(
c=='
\n'
)

f
pri
ntf
(fo,
"\
t$\
t")
;

el
sef
pri
ntf
(fo,
"\
t%c\
t",
c);

c=get
c(f
i)
;

f
close(
fi
);

f
close(
fo)
;

f
i=f
open(
"i
nter.
c",
"r"
);

pri
ntf
("\
nLexi
calAnal
ysi
s")
;
f
scanf
(fi
,
"%s"
,a)
;

pri
ntf
("\
nLi
ne:%d\
n",
i
++)
;

whi
le(
!f
eof
(fi
))

i
f(
strcmp(
a,"
$")
==0)

pri
ntf
("\
nLi
ne:%d\
n",
i
++)
;

f
scanf
(fi
,
"%s"
,a)
;

f
scanf
(fop,
"%s"
,ch)
;

whi
le(
!f
eof
(fop)
)

i
f(
strcmp(
ch,
a)==0)

f
scanf
(fop,
"%s"
,ch)
;

pri
ntf
("\
t\t
%s\
t:\
t%s\
n",
a,ch)
;

fl
ag=1;
}fscanf
(fop,
"%s"
,ch)
;

rewi
nd(
fop)
;

f
scanf
(fk,
"%s"
,ch)
;
whi
le(
!f
eof
(fk)
)

i
f(
strcmp(
ch,
a)==0)

f
scanf
(fk,
"%k"
,ch)
;

pri
ntf
("\
t\t
%s\
t:\
tKeyword\
n",
a);

f
lag=1
;

f
scanf
(fk,
"%s"
,ch)
;

rewi
nd(
fk)
;

i
f(
fl
ag==0)

i
f(
isdi
git
(a[
0])
)

print
f("
\t\
t%s\
t:\
tConst
ant
\n"
,a)
;
else

pri
ntf
("\
t\t
%s\
t:\
tIdent
ifi
er\
n",
a);

fl
ag=0;fscanf
(fi
,
"%s"
,a)
;}
getch(
);

I
nput
.c
#incl
ude" stdi
o.h"
#incl
ude" coni
o.h"
voidmai n()
{
i
nta=10,b,c;
a=b*c;
getch()
;
}

key.
c

i
nt
void
mai n
char
i
f
for
whi l
e
else
printf
scanf
FILE
I
nclude
stdio.h
conio.h
i
ost ream.
h

oper.
c

(openpara
)closepara
{openbrace
}closebrace
<lesser
>great er
"doubl equote'si
ngl
equot
e
:colon
;semi colon
#preprocessor
=equal
==asign
%percent age
^bitwise
&reference
*star
+add
-sub
\backslash
/slash

Program 4:

/*program t
olexi
calanal
yzerusi
ngl
ext
ool*
/

#include<st dio.h>
#include<ct ype. h>
#include<coni o.h>
#include<st ring.h>
charvars[ 1
00] [1
00] ;
intvcnt ;
chari nput[1000] ,
c;
chart oken[50] ,
tlen;
intstate=0,pos=0, i
=0,i
d;
char* getAddress( charst r[
])
{
for(i
=0;i<vcnt ;
i++)
if(
strcmp( str,vars[ i]
)==0)
returnvars[ i
];
strcpy( vars[vcnt ],
str);
returnvars[ vcnt ++];
}
inti srelop( charc)
{
if(
c==' +'||c=='-'
||c=='
*'
||
c=='
/'
||
c=='%'
||
c=='
^'
)
ret urn1 ;
else
ret urn0;
}
voi dmai n(void)
{
//cl rscr( );
pri ntf("Ent ert heInputStri
ng:")
;
get s( i
nput );
do
{
c=i nput [pos] ;
put char( c);
swi tch(st ate)
{
case0:
if(
isspace( c))
pri ntf("\
b" );
if(
isal pha( c))
{
token[ 0]=c;
tlen=1 ;
st ate=1 ;
}
if(
isdi git(c))
st ate=2;
if(
isrel op( c))
st ate=3;
if(
c==' ;'
)
pri ntf("\
t <3,3>\n");
if(
c==' =')
pri ntf("\
t <4,4>\n");
break;
case1 :
if(
!isalnum( c))
{
token[ tlen]='\o';
pri ntf("\
b\ t<1,
%p>\ n",
getAddress(t
oken)
);
st ate=0;
pos- -;
}
else
token[ tlen++] =c;
break;
case2:
i
f(!i
sdi git(c))
{
print f(
"\b\t<2,%p>\n"
,&input[
pos])
;
state=0;
pos- -;
}
break;
case3:
i
d=i nput [pos- 1]
;
i
f(c==' ='
)
print f(
"\t<%d,%d>\ n"
,i
d*10,
id*
10)
;
else{
print f(
"\b\t<%d,%d>\n",
id,i
d);
pos- -;
}stat e=0;
break;
}
pos++;
}
whi le(c!=0);
getch( );

}
Program 5:

/*program t
orecogni
zeaval
idari
thmet
icexpressi
onusi
ngYACC*
/

p5.
l

%{
#incl
ude<st
dio.
h>
#incl
ude"y.
tab.
h"
externi
ntyylval
;
%}

%%
[
0-9]+{
yylval
=atoi
(yyt
ext
);
returnNUMBER;
}
[
\t];
[
\n]return0;
.ret
urnyyt
ext[
0];
%%
i
ntyywrap()
{
return1
;
}

p5.
y

%{
#i
nclude<stdi
o.h>
i
ntflag=0;
%}
%tokenNUMBER

%lef
t'+''-'
%lef
t' *'
'/''
%'
%lef
t'('
')
'
%%
Arit
hmet i
cExpressi
on:E{
printf
("\
nResul
t=%d\
n",
$$)
;
return0;
}

E:E'+'
E{ $$=$1 +$3;}
|E'-'
E{ $$=$1- $3;}
|E'*'
E{ $$=$1* $3;}
|E'/'
E{ $$=$1 /$3;}
|E'%'E{ $$=$1 %$3;}
|'
('
E')'{$$=$2; }
|NUMBER{ $$=$1 ;}
;
%%
intmai n()
{
printf(
"\nEnt eranyAri thmeticExpressionwhi chcanhave
operat ionsaddi t
ion,subt
racti
on,mult
ipl
icati
on,di
vision,
modulusand
roundbracket s:\
n");
yyparse( );
if
(fl
ag==0)
printf(
"\nEnt eredarithmeticexpressionisvali
d\ n\
n")
;
return1 ;
}
voi
dyyerror()
{
printf
("\
nent
eredari
thmet
icexpressi
oni
sinval
id\
n\n"
);
flag=1
;
}

Program 6:

/*program t
orecogni
zeaval
idvari
abl
enameusi
ngYACC*
/

p6.
l

%{
#include" y.tab.
h"
%}
%%
[0-9]
+{ returnDI GI
T;}
[a-zA-Z]+{ returnLETTER;
}
[\t]{
;}
\n{ret urn0;}
.{returnyyt ext[0]
;}
%%
i
ntyywrap(
)
{
ret
urn1
;
}

p6.
y

%{
#include<st dio.h>
%}
%tokenDI GITLETTER
%%
stmt :
A
;
A:LETTERB
;
B:LETTERB
|DIGITB
|LETTER
|DIGIT
;
%%
intmai n()
{
pri
nt f(
"enterst ri
ng\
n")
;
yyparse( );
pri
nt f(
"valid");
return1 ;
}
voidyyerror( )
{
printf
("invali
d" );
exit(
0);
}
Program 7:

/*program t
oimpl
ementcal
cul
atorusi
ngLEXandYACC*
/

p7.
l

%{
#include<stdi
o.h>
#include<math.h>
#include"y.
tab.h"
%}
%%
[0-9]+{
yyl val
.dval
=atoi(
yyt
ext
);
returnNUMBER;
}
[\t
]{;}
[\
n]{return0;}
.{
returnyyt ext [
0];
}
%%
voidyyerror( char*st
r)
{
printf
("nInvalidCharact
er.
.
."
);
}
i
ntmai n()
{
printf
("EnterExpression=>");
yyparse( );
return(0);
}
i
ntyywrap( )
{
return1 ;
}

p7.
y

%{
#include<st dio.h>
i
ntyyl ex(voi d);
%}
%union
{
fl
oatdval ;
}
%token<dval >NUMBER
%left'+''
-'
%left'*'
'/'
%nonassocUMI NUS
%type<dval >exp
%%
state:exp{ printf(
"Answer=%f
n",
$1)
;}
;
exp:NUMBER
|exp' +'exp{ $$=$1+$3;
}
|exp'-'
exp{ $$=$1-$3;
}
|exp'*'
exp{ $$=$1*$3;
}
|exp'/'exp{$$=$1/$3;}
|'
('
exp')
' {
$$=$2;}
|'
-'exp%precUMI NUS{ $$=-
$2;
}
;
%%

Program 8:

/*program t
oimpl
ementt
ypechecki
ngf
orgi
venexpressi
on*
/

#include<st dio.h>
#include<st dlib.
h>
#include<st ring.h>
charst r[
50] ,opst r[75];
i
ntf [
2][9]
={ 2,3,4,4,0,6,
6,0,
1,
1,
3,3,
5,5,
0,5,
0};
i
ntcol ,
col1
,col 2;
charc;
swt (
)
{
swi tch( c)
{
case' +':
col =0; break;
case' -'
:col =1;break;
case' *'
:col =2;break;
case' /':
col =3; break;
case' ^':
col =4; break;
case' ('
:col=5; break;
case' )'
:col=6; break;
case' d':col=7; break;
case' $':col=8; break;
defaul t:pri ntf("\
nTERMI NALMISSMATCH\n"
);
exit(1);
}
//ret urn0;
}
intmai n( )
{
inti=0,j=0, col1,cn,k=0;
intt1=0, foundg=0;
chart emp[ 20];
printf("\nent erari thmeti
cexpressi
on:
")
;
scanf ("%s" ,&st r);
whi l
e( st r[i]!
='\0')
i++;
printf("%d" ,
i);
str[i]='$';
printf("");
str[++i ]='\
0' ;
printf("%s\ n",
st r);
c:
i=0;
opst r[0] ='$';
j=1;
c='$';
swt ();
col1=col ;
c=st r[i];
swt ();
col2=col ;
if(
f[1
][col 1]>f [
2] [
col2])
{
opst r[j]
='>';
j++;
}
elsei f
(f[1]
[col1]<f[2][
col
2])
{
opst r[j]
='<';
j++;
}
else
{
opst r[j]
='=';
j++;
}
whi le(
st r[i
]!='
$')
{
c=st r[i
];
swt ();
col1=col;
c=st r[++i];
swt ();
col2=col ;
opst r[j]
=st r[--
i];
j++;
i
f (
f[0][
col 1
]>f [
1][col2]
)
{
opst r[j]
='>';
j++;
}
elsei f
(f[0][
col 1
]<f[1][
col
2])
{
opst r[j]
='<';
j++;
}
else
{
opst r[j]
='=';
j++;
}
i
++;
}
opst r[j]
='$';
opst r[++j ]='\0';
print f
("\nprecedencei nput
:%s\
n",
opst
r);
i=0;
j=0;
whi le(opst r[i]!='
\0')
{
foundg=0;
whi le(foundg! =1)
{
if(
opst r[ i]
==' \0')gotoredone;
if(
opst r[ i]
==' >')foundg=1;
t1=i;
i++;
}
if(
foundg==1 )
for(i=t1;i>0;i--)
if(
opst r[ i]
==' <')break;
if(
i==0) {print f("\nERROR\ n"
);
exi
t(1
);
}
cn=i ;
j=0;
i=t1+1;
whi le(opst r[i]!='
\0')
{
temp[ j
] =opst r[ i
];
j++;i++;
}
temp[ j
] ='\
0' ;
opst r[cn] ='E';
opst r[++cn] ='\0';
strcat (opst r,temp) ;
print f
("\n%s" ,
opst r);
i=1;
}
redone: k=0;
whi le(opst r[k] !
='\0'
)
{
if(
opst r[ k]==' <')
if(
i==0) {print f("\nERROR\ n"
);
exi
t(1
);
}
cn=i ;
j=0;
i=t1+1;
whi le(opst r[i]!='
\0')
{
temp[ j]=opst r[i]
;
j++;i
++;
}
temp[ j]='\0';
opst r[cn] ='E';
opst r[++cn] ='\0';
strcat (opst r,temp) ;
print f
("\n%s" ,
opst r);
i=1;
}
redone: k=0;
whi le(opst r[k]!='\0'
)
{
if(
opst r[ k]=='<' )
{
print f
("\nError" )
;
exit(1);
}
}
if(
(opst r[ 0]='$')&&( opstr[2]
=='$'
))
got
os;
i=1;
whi le(opst r[i
]!='\0')
{
c=opst r[i];
if(
c==' +'|
|c==' *'
||c=='/'|
|c=='
$')
{
temp[ j]=c; j
++; }
i++;
}
temp[ j]='\0';
strcpy( str,temp) ;
got oc;
s:
print f
("\nSuccess" );
return0;
}
Program 9:

/*program t
oconvertt
heBNFrul
esi
ntoYACC*
/

p9.
l

%{
#i nclude"y.tab. h"
#i nclude<st dio. h>
#i nclude<st ring. h>
intLi neNo=1 ;
%}
ident ifi
er[a- zA- Z][_a-zA- Z0-9]*
number[ 0-9] +|([0-9]*\.
[0-9]+)
%%
mai n\(\
)returnMAI N;
ifret urnIF;
elseret urnELSE;
whi l
eret urnWHI LE;
int|
char|
floatret urnTYPE;
{ident if
ier}{st rcpy( yylval.
var,yytext)
;
ret urnVAR; }
{number}{ strcpy( yyl val
.var,yytext)
;
ret urnNUM; }
\<|
\>|
\>=|
\<=|
=={ strcpy(yyl val .var,yytext);
ret urnRELOP; }
[\t ];
\nLi neNo++;
.ret urnyyt ext [0];
%%
intyywrap( )
{
ret urn1 ;
}

p9.
y

%{
#i
ncl
ude<stri
ng.h>
#i
ncl
ude<stdi
o.h>
st
ructquad
{
charop[ 5];
chararg1 [
10];
chararg2[ 10];
charresul t[1
0] ;
}QUAD[ 30] ;
structst ack
{
intitems[ 100] ;
inttop;
}stk;
intIndex=0, tIndex=0,StNo,I
nd,
tI
nd;
externi ntLi neNo;
%}
%uni on
{
charvar[ 1
0] ;
}
%token<var>NUM VARRELOP
%tokenMAI NI FELSEWHI LETYPE
%type<var>EXPRASSI GNMENTCONDI
TIONI
FSTELSESTWHI
LELOOP
%left' -'
'+'
%left' *'
'/'
%%
PROGRAM:MAI NBLOCK
;
BLOCK:' {'
CODE' }
'
;
CODE:BLOCK
|STATEMENTCODE
|STATEMENT
;
STATEMENT:DESCT' ;
'
|ASSI GNMENT' ;
'
|CONDST
|WHI LEST
;
DESCT:TYPEVARLI ST
;
VARLI ST:VAR' ,'
VARLI ST
|VAR
;
ASSI GNMENT:VAR' ='EXPR{
strcpy(QUAD[ I
ndex] .
op,"=");
strcpy(QUAD[ I
ndex] .
arg1,$3) ;
strcpy(QUAD[ I
ndex] .
arg2," ");
strcpy(QUAD[ I
ndex] .
resul t,$1);
strcpy($$, QUAD[ Index++] .resul t);
}
;
EXPR:EXPR' +'EXPR{ AddQuadrupl e("+",
$1,
$3,$$)
;}
|EXPR' -'
EXPR{ AddQuadrupl e("-",
$1,$3,$$);
}
|EXPR' *'
EXPR{AddQuadrupl e("
* "
,$1,
$3,$$);}
|EXPR' /'EXPR{AddQuadrupl e("/",
$1,$3,$$);
}
|'-'
EXPR{AddQuadrupl e("UMI N",$2,"
",$$);}
|'(
'EXPR' )'{
strcpy( $$,$2);}
|VAR
|NUM
;
CONDST:I FST{
Ind=pop( );
sprintf(
QUAD[ I
nd].result,
"%d" ,
Index) ;
Ind=pop( );
sprintf(
QUAD[ I
nd].result,
"%d" ,
Index) ;
}
|IFSTELSEST
;
IFST:IF'('CONDI TION' )
'{
strcpy(QUAD[ I
ndex] .
op,"==" );
strcpy(QUAD[ I
ndex] .
arg1,$3) ;
strcpy(QUAD[ I
ndex] .
arg2," FALSE" );
strcpy(QUAD[ I
ndex] .
resul t,"-
1");
push( I
ndex) ;
Index++;
}
BLOCK{
strcpy(QUAD[ I
ndex] .
op,"GOTO" );
strcpy(QUAD[ I
ndex] .
arg1,"");
strcpy(QUAD[ I
ndex] .
arg2," ");
strcpy(QUAD[ I
ndex] .
resul t,"-
1");
push( I
ndex) ;
Index++;
};
ELSEST:ELSE{
t I
nd=pop( );
Ind=pop( )
;
push(tInd);
sprintf(QUAD[ Ind].
result
,"
%d" ,
Index);
}
BLOCK{
Ind=pop( )
;
sprintf(QUAD[ Ind].
result
,"
%d" ,
Index);
};
CONDI TION:VARRELOPVAR{ AddQuadrupl
e($2,
$1,
$3,
$$)
;
StNo=Index-1 ;
}
|VAR
|NUM
;
WHI LEST:WHI LELOOP{
Ind=pop( )
;
sprintf(QUAD[ Ind].
result
,"
%d" ,
StNo);
Ind=pop( )
;
sprintf(QUAD[ Ind].
result
,"
%d" ,
Index);
}
;
WHI LELOOP:WHI LE'('CONDITION')'{
strcpy( QUAD[ Index].
op,"
==");
strcpy( QUAD[ Index].
arg1,
$3);
strcpy( QUAD[ Index].
arg2,"
FALSE" );
strcpy( QUAD[ Index].
result,
"-
1");
Index++;
}
BLOCK{
strcpy( QUAD[ Index].
op,"
GOTO" );
strcpy( QUAD[ Index].
arg1,
""
);
strcpy( QUAD[ Index].
arg2,"
");
strcpy( QUAD[ Index].
result,
"-
1");
push(Index) ;
Index++;
}
;
%%
externFI LE* yyin;
intmai n(i
ntargc, char*argv[])
{
FILE* f
p;
inti;
if(
argc>1 )
{
fp=fopen( argv[ 1],
"r")
;
if(
!f
p)
{
print f(
"\nFilenotf ound" )
;
exit(0);
}
yyin=f p;
}
yyparse( );
print f(
"\n\n\t\t..
..
..
..
.
..
..
.
.""
\n\t\t
PosOperat orArg1Arg2Resul
t""\
n\t\
t.
..
..
..
.
");
for(i=0;i<I
ndex; i++)
{
print f(
"\n\t\
t%d\ t%s\ t
%s\ t %s\t%s" ,i
,QUAD[ i]
.op,QUAD[ i
].
arg1
,QUAD[i
].
arg2,
QUAD[i]
.result
);
}
print f(
"\n\t\
t...
.
..
.");
print f(
"\n\n");
return0;
}
voidpush( i
ntdat a)
{
stk.top++;
if(
st k.t
op==1 00)
{
print f(
"\nst ackoverf low\ n")
;
exit(0);
}
stk.items[ st k.top]=dat a;
}
intpop( )
{
intdat a;
if(
st k.t
op==-1 )
{
print f(
"\nSt ackunderf low\n");
exit(0);
}
dat a=st k.
items[ stk.top- -
];
returndat a;
}
voidAddQuadrupl e(
charop[ 5] ,
chararg1[
10],
chararg2[
10]
,char
result[
10])
{
strcpy(QUAD[ I
ndex].
op,op);
strcpy(QUAD[ I
ndex].
arg1,
arg1) ;
strcpy(QUAD[ I
ndex].
arg2,arg2) ;
sprintf
(QUAD[ I
ndex].
result,
"t%d" ,
tI
ndex++);
strcpy(result
,QUAD[Index++].result)
;
}
yyerror()
{
printf
("\
nErroronl i
neno: %d",
Li neNo);
}
Program 1
0:

/*program t
oimpl
ementdat
afl
ow andcont
rolf
low.
*/

#include<coni o.h>
#include<st dio.
h>
#include<st dli
b.h>
#def inenul l0
structst ack
{
i
ntno;
structst ack* next ;
}
*start=nul l;
typedefst ructst ackst;
voidpush( );
i
ntpop( );
voiddi spl ay( );
voidmai n()
{
charch;
i
ntchoi ce, item;
do
{
printf("
\n1 :push" );
printf("
\n2: pop" );
printf("
\n3: display" )
;
printf("
\nent eryourchoi ce");
scanf ("
%d" ,
&choi ce);
swi t
ch( choi ce)
{
case1 :push( );
break;
case2: item=pop( );
printf("
t hedel eteel ementin%d" ,
it
em) ;
break;
case3: di spl ay();
break;
default:pri nt f
("\
nwrongchoi ce")
;
};
printf("
\ndoyouwantt ocontinue(y/n"
);
ffl
ush( st din) ;
scanf ("%c" ,&ch) ;
}
whi le(ch==' y' |
|ch==' y'
);
}
voidpush( )
{
st* node;
node=( st * )
mal loc(sizeof(st
));
print f
( "\nent ert henumbert obeinsert
");
scanf ("%d" ,&node- >no);
node- >next =st art;
start =node;
}
intpop( )
{
st* temp;
temp=st art ;
intdat a;
if(
st art ==nul l)
{
print f
( "st acki sal readyempt y")
;
return0;
}
else
{
dat a=t emp- >no;
start =st art ->next ;
free( temp) ;
}
return( dat a) ;
}
voiddi spl ay( )
{
st* temp;
temp=st art ;
whi le(temp- >next !=null)
{
print f
( "\nno=%d" ,t
emp- >no);
temp=t emp- >next ;
}
print f
( "\nno=%d" ,t
emp- >no);
}

Program 1
1:

/*Program t
oImpl
ementSt
ackst
orageal
locat
ionst
rat
egy*
/

#i
nclude<stdi
o.h>
#i
nclude<conio.
h>
#i
nclude<stdl
ib.
h>
structstack
{
intinf
o;
structstack*li
nk;
};
structstack*start
=NULL;

voidmai n()
{
i
ntch;
whi l
e(ch!=6)
{
printf(
"\n1forPush\ n");
printf(
"2f orPop\n" )
;
printf(
"3f orPeep\ n")
;
printf(
"4f orChange\ n")
;
printf(
"5f orDisplay\n")
;
printf(
"6f orexit\
n");
printf(
"Ent erurchoi ce:"
);
scanf("%d" ,
&ch);
swi t
ch(ch)
{
case1 :push( )
;
break;
case2:pop( );
break;
case3:peep( );
break;
case4:change( );
break;
case5:di spl
ay();
break;
case6:break;
}
}
}

push( )
{
structst ack* new,*
temp;
inti=0;
for(temp=st art;t
emp! =NULL;temp=temp->l
ink)
{
i++;
}
if(
i==5)
{
printf("StackOverf l
ow" );
get ch();
}
else
{
new=( structst ack*)malloc(
sizeof
(st
ructst
ack));
printf("Enterval ueforstack:");
scanf ("%d",&new- >i
nfo);
new- >li
nk=st art;
start =new;
}
}

pop( )
{
structst ack*t
emp, *
temp2;
i
nti =0;
for(temp=start;
t emp!=NULL;
temp=t
emp-
>li
nk)
{
i++;
}
i
f(i==0)
{
printf(
"Underfl
ow" );
}
else
{
temp2=st art-
>link;
start=t
emp2;
print
f("
\n*
**Theval
uehasbeenpoped*
**\
n")
;
}
}

display()
{
structst ack* t
emp;
printf(
"\n***
*StackVal ues*
***
\n")
;
for(t
emp=st art;temp!=NULL;temp=t
emp-
>li
nk)
{
pri
ntf("%d\n",
temp- >i
nfo)
;
}
printf
("\n**
*****
*\n"
);
}

peep( )
{
structst ack* temp;
i
ntpos, i=0,j=0;
pri ntf(
"Pleaseent ertheposit
ionnumber:"
);
scanf (
"%d" ,
&pos) ;
for( temp=st art;
temp! =NULL;
temp=temp->li
nk)
{
i++;
}
i
f(pos>i )
{
printf("\
nOverLi mit
\n");
}
el se
{
temp=st art ;
for(j=0;j
<pos- 1;j
++)
{
temp=t emp- >li
nk;
}
pri ntf(
"\nTheval ueofpositi
on%dis:%d",
pos,t
emp-
>inf
o);
}
}

change(
)
{
structst ack* t
emp, *
new;
i
ntpos, i=0,j=0;
pri ntf(
"Pleaseent ert heposi ti
onnumber:");
scanf (
"%d" ,&pos);
for( temp=st art;temp! =NULL; t
emp=t emp->l
ink)
{
i++;
}
i
f(pos>i )
{
printf("\
nOverLi mit\n" )
;
}
el se
{
new=( structst ack* )mal l
oc(sizeof
(st
ructstack)
);
printf("Ent erval uef orst ack:");
scanf ("%d" ,
&new- >inf o)
;
temp=st art;
for(j=0;j<pos- 1;
j++)
{
temp=t emp- >link;
}
temp- >inf o=new- >inf o;
}
}
Program 1
2:

/*Program t
oImpl
ementHeapst
orageal
locat
ionst
rat
egy*
/

#i
nclude"
stdi
o.h"
#i
nclude"
coni
o.h"
#i
nclude"
stdl
ib.
h"
#def
ineTRUE1
#def
ineFALSE0

typedefst ructHeap
{
i
ntdat a;
structHeap* next;
}node;
node* creat e();
voidmai n()
{
i
ntchoi ce,val ;
charans;
node* head;
voiddispl ay( node* );
node* search( node* ,
i
nt);
node* i
nsert (node* )
;
voiddel e(node* *
);
head=NULL;
do
{
printf
("\
nProgram t operf orm variousoperat
ionsonheapusi
ng
dynami cmemorymanagement ")
;
printf
("\
n1 .
Creat e");
printf
("\
n2. Di splay");
printf
("\
n3. Insertanel ementi nalist")
;
printf
("\
n4. Del eteanel ementf rom list")
;
printf
("\
n5. Qui t");
printf
("\
nEnt erYourChoi ce(1
-5)"
);
scanf("%d" ,
&choi ce);
switch(choice)
{
case1:head=creat e()
;
break;
case2:display(head);
break;
case3:head=i nsert(
head) ;
break;
case4:dele(&head) ;
break;
case5:exit(
0);
defaul
t:pri
nt f
("choose");
pri
ntf("
Invali
dChoi ce,Tryagai
n")
;
}
}whil
e(choice!=5);
}

node* creat e()


{
node* temp, *new,*head;
i
ntval ,f
lag;
charans;
ans='y';
node* get_node( );
temp=NULL;
fl
ag=TRUE;
do
{
printf(
"\nEnt ertheElement")
;
scanf (
"%d" ,
&val);
new =get _node( )
;
i
f(new==NULL)
printf(
"\nMemoryi snotall
ocated"
);
new- >dat a=val;
i
f( f
lag==TRUE)
{
head=new;
temp=head;
fl
ag=FALSE;
}
else
{
temp- >next=new;
temp=new;
}
pri
nt f(
"\nDoyouwantt oentermoreel
ement
s?(
y/n)
");
ans=get ch()
;
}while(ans=='y')
;
pri
nt f(
"\nThelistiscreat
ed"
);
returnhead;
}

node*get_node(
)
{
node*temp;
temp=(node*)
malloc(
sizeof
(node)
);
temp->next=NULL;
returntemp;
}

voiddisplay(node*head)
{
node* temp;
temp=head;
if
(temp==NULL)
{
printf(
"\nThelisti
sempt y\
n")
;
return;
}
whi l
e(temp!=NULL)
{
printf(
"%d->"
,temp- >dat
a);
temp=t emp- >next;
}
printf(
"NULL");
}

node*search(
node*
head,
i
ntkey)
{
node*t
emp;
i
ntfound;
temp=head;
i
f(temp==NULL)
{
printf(
"Theli
nkedli
stisempty\n")
;
returnNULL;
}
found=FALSE;
whi l
e(temp!=NULL&&f ound==FALSE)
{
if
(temp- >dat
a!=key)
temp=t emp->next;
else
found=TRUE;
}
if
(found==TRUE)
{
printf(
"\nTheElementsispresentint
heli
st\
n")
;
returnt emp;
}
else
printf(
"\nTheElementisnotpresenti
ntheli
st\
n")
;
returnNULL;
}

node* insert( node* head)


{
intchoice;
node* insert_head( node*);
voidinsert _af ter(node*);
voidinsert _last (
node* );
printf(
"\n1.
I
nsertanodeasaheadnode" )
;
printf(
"\n2.
Insertanodeasal astnode");
printf(
"\n3.
Insertanodeasatt heintermedi
ateposit
ioni
nthel
ist
");
printf(
"\nEnt eryourchoi ceforinserti
onofnode"
);
scanf("%d",&choi ce) ;
swi t
ch( choice)
{
case1 :
head=i nsert _head(head)
;
break;
case2: insert _last(head);
break;
case3: insert _after(head);
break;
}
returnhead;
}

node* insert_head(node*head)
{
node* New, *temp;
New =get _node();
printf("\
nEnt ertheelementwhi chyouwantt
oinsert
");
scanf (
"%d",&New- >data)
;
if
(head==NULL)
head=New;
else
{
temp=head;
New- >next=t emp;
head=New;
}
returnhead;
}

voidinsert_last(
node* head)
{
node* New,*temp;
New =get _node( );
printf("
\nEnt ertheelementwhi
chyouwantt
oinsert
");
scanf (
"%d"
,&New- >data)
;
i
f(head==NULL)
{
head=New;
}
else
{
temp=head;
whi l
e(temp->next !=NULL)
temp=t emp- >next ;
temp- >next=New;
New- >next=NULL;
}
}

voi
dinsert
_af
ter(
node*
head)
{
i
ntkey;
node* New, *temp;
New =get _node( )
;
printf(
"Entert heelementafterwhi
chyouwantt oinsert
");
scanf("%d",&key);
temp=head;
do
{
if
(temp- >dat a==key)
{
printf(
"Enterel ementwhi chyouwantt
oinsert
");
scanf("%d",&New- >data)
;
New- >next =temp- >next
;
temp- >next =New;
return;
}
else
temp=t emp- >next;
}while(temp! =NULL);
}

node* get_prev(node*head,
i
ntval
)
{
node* temp,*prev;
intf l
ag;
temp=head;
if(t
emp==NULL)
ret urnNULL;
flag=FALSE;
prev=NULL;
whi le(temp!=NULL&&! fl
ag)
{
if(t
emp- >data!=val
)
{
prev=t emp;
temp=t emp- >next;
}
else
flag=TRUE;
}

if
(fl
ag)
returnprev;
else
returnNULL;
}

voiddel e(node* *head)


{
intkey;
node* New, *temp, *prev;
temp=* head;
if(temp==NULL)
{
printf("
\nThel i
sti sempt y\n")
;
return;
}
printf("
\nENTERt heEl ementyouwantt
odel
ete:
")
;
scanf (
"%d" ,
&key) ;
temp=search( *head,key);
if(
temp! =NULL)
{
prev=get _prev( *head,key);
if(
prev! =NULL)
{
prev- >next=t emp- >next ;
free(temp) ;
}
else
{
*head=t emp- >next ;
free(temp) ;
}
printf("
\nTheEl ementi sdeleted\
n")
;
}
}
Program 1
3:

/*
program t
oconst
ructadi
rect
edacycl
icgraph(
DAG)*
/

#include<st dio.h>
#include<st ring.h>
#include<ct ype. h>>
intmai n()
{
structda
{
i
ntpt r,l
ef t,
right ;
charl abel ;
}dag[25] ;
i
ntpt r,l
,j,
change, n=0,i
,stat
e=1,
x,y,
k;
charst ore, *input 1
,i
nput[25],
var;
for(i
=0;i<25; i++)
{
dag[ i
].
pt r=(int)NULL;
dag[ i
].
left=(int)NULL;
dag[ i
].
right =(int)NULL;
dag[ i
].
label =NULL;
}
printf("
\n\nENTERTHEEXPRESSI ON\
n\n"
);
scanf (
"%s" ,
input 1);
for(i
=0;i<25; i++)
i
nput [i]
=( i
nt )NULL;
l
=st rl
en( input 1);
a:
for(i
=0;input 1[
i]!
=')'
;
i++);
for(j=i;i
nput 1[ j
]!='(
';
j--
);
for(x=j +1;x<i ;x++)
i
f(isalpha( i
nput 1[
x]))
i
nput [
n++] =i nput 1
[x];
else
i
f(input 1[x] !
=' 0')
store=i nput 1[x];
i
nput [
n++] =st ore;
for(x=j ;x<=i ;x++)
i
nput 1
[x] ='0';
i
f(input 1[0] !
=' 0')gotoa;
for(i=0;i<n; i++)
{
dag[ i]
.label =i nput [i
];
dag[ i]
.pt r=i;
i
f(!i
sal pha( input [
i])
&&! i
sdigit
(input[
i])
)
{
dag[ i]
.right =i -1;
ptr=i ;
var=i nput [i
- 1];
i
f(isalpha( var) )
ptr=pt r- 2;
else
{
ptr=i -
1;
b:
i
f(!i
sal pha( var) &&! i
sdigit(
var))
{
ptr=dag[ ptr] .
lef t
;
var=i nput [pt r];
got ob;
}
else
ptr=pt r- 1;
}
dag[ i]
.left=pt r;
}
}
print f
("\nSYNTAXTREEFORGI VENEXPRESSI ON\n\
n")
;
print f
("\n\ nPTR\ t
\tLEFTPTR\ t
\tRI
GHTPTR\ t
\tLABEL\n\
n")
;
for(i=0;i<n; i++) /*draw t hesynt axtreefort
hefoll
owing
out putwi thpoi ntervalue* /
printf("\
n%d\ t%d\t%d\ t%c\ n",dag[ i].
ptr,dag[ i]
.l
eft,
dag[ i
].
right
,dag[ i
].
label
);
for(i
=0; i
<n; i
++)
{
for(j=0;j<n; j
++)
{
if
((dag[ i
].label==dag[ j
].l
abel &&dag[ i
].
left==dag[ j]
.l
eft)&&dag[i].
right==da
g[j].
right )
{
for(k=0;k<n; k++)
{
if
(dag[ k].left==dag[ j].
ptr)
dag[ k].
left =dag[i].
pt r;
if
(dag[ k].right==dag[ j
].ptr)dag[ k].right=dag[ i].
ptr;
}
dag[ j]
.ptr=dag[ i
].
pt r;
}
}
}
printf("\
nDAGFORGI VENEXPRESSI ON\ n\ n")
;
printf("\
n\ nPTR\ tLEFTPTR\ tRI GHTPTR\ tLABEL\ n\n"
);
for(i
=0; i
<n; i
++)/*draw DAGf ort hef ollowi ngout putwi thpointerval ue*
/
printf("\
n
%d\ t\t%d\ t \
t%d\t\t%c\ n",
dag[ i].
pt r,dag[ i
].
left,
dag[ i
].
right,
dag[ i
].
label);
return0;
}
Program 1
4:

/*
program t
oimpl
ementt
hebackendoft
hecompi
ler*
/

#include<st di o.h>
#include<st di o.h>
#include<st ring. h>
voidmai n()
{
chari code[ 10][30],str[
20],opr[10]
;
i
nti =0;
printf("
\nEnt ert hesetofi ntermedi atecode(
termi
nat
edbyexi
t):
\n"
);
do
{
scanf ("
%s" ,
icode[ i
]);
}whi le(strcmp( icode[i++],
"exit
")!
=0);
printf("
\nt argetcodegenerat i
on");
printf("
\n***********
*****
****
****"
);
i
=0;
do
{
strcpy( str,i
code[ i
]);
swi tch(str[3])
{
case' +'
:
strcpy( opr,
"ADD" )
;
break;
case' -'
:
strcpy( opr,
"SUB" )
;
break;
case' *'
:
strcpy( opr,
"MUL" )
;
break;
case' /'
:
strcpy( opr,
"DI
V");
break;
}
printf("
\n\t
Mov%c, R%d",
str[2],
i)
;
printf("
\n\t
%s%c, R%d",
opr,str[4],
i)
;
printf("
\n\t
MovR%d, %c",
i
,str[0])
;
}while(strcmp(icode[++i
],"
exit")
!=0);
}

Program 1
5:

/*program t
oimpl
ementsi
mpl
ecodeopt
imi
zat
iont
echni
que.
*/

#include<st di
o.h>
voidmai n(
)
{
i
ntn, f
act ,
i;
fact=1;
printf(
"Entert henumber:\
n")
;
scanf (
"%d",&n);
for(i
=n;i>=1;
i--
)
fact=fact*i;
printf(
"Thef actori
alval
ueis:
%d"
,f
act
);
getch();
}
Program 1
6:

/*program toi
mpl
ementsi
mpl
ecodeopt
imi
zat
iont
echni
queusi
ng
do-whil
e.*/

#include<stdio.
h>
voidmai n()
{
i
ntn, f
;
f=1;
printf(
"Enterthenumber:\
n")
;
scanf (
"%d",
&n);
do
{
f=f*
n;
n--;
}while(n>0);
printf(
"Thefactori
alval
ueis:
%d"
,f
);
getch();
}

You might also like