Notes For Compiler Design
Notes For Compiler Design
LAB FILE
Scanned by CamScanner
Scanned by CamScanner
Experiment: 1
#include<iostream>
#include<coni0.h>
int main()
cout<<'.';
cin>> ip2;
cout<< ‘.';
cin>> ip3;
cout<< '.';
cin>> ip4;
{
cout<< "CLASS A'_'<<end1;
cout<<"NID = “<<ip1<<end1;;
cout<<"HID = "<<ip2<<'.'<<ip3<<‘.'<<ip4;
{
cout<< "CLASS B"<<endl;;
cout<<"NID = "<<ip1<<‘.'<<ip2<<endl;;
cout<<“HID = "<<ip3<<‘.‘<<ip4;
cout<<"I-IID = "<<ip4; /”
} /
}
if( ip1>=240 && ip1<256> .
Scanned by CamScanner
1
cout<< "CLASS E"<<endl;;
cout<<"Rcscrvcd Address";
getcho;
return 0;
}
OUTPUT
00880:: 0.74, Cpu speed: max109% cycles, Frameskip 0, Program:
6 :\TUBB(]CB\B IN>TC
Enter an IP address: 129
Scanned by CamScanner
Experiment: 2
#includc<iastrcmnh>
flincludc<graphicsh>
#includc<conio.lP
#includc<dos.h>
void pt 10
{ int i;
linc(600,20£00,400);
linc(0.20.0,400);
f0r(i=0:i<580;i++)
{ 1inc(0,20,0‘400);
linc(600,20,600‘400);
circ1c(20+i,100+(i/l 0),20);
dclay(2);
cleardeviceo;
void p120
1ine(0,20,0,400);
1ine(600,20,600,400);
int kjj;
f01'(i=580j=580;i>0;i--J++)
{ 1ine(0,20,0,400);
line(600,20,600,400);
circle(20+i,100+(j/10),20);
delay(2);
cleardeviceO;
void main()
{ int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
cout<<"Case 1: Succesfull transmission";
delay(1000);
PUO;
PQO;
delayUOOO); /
int i;
1ine(0,20,0,400);
line(600,20,600,400);
for(i=0;i<480;i++)
{ 1ine(0,20,06,:1)80260)
line 600,20, , S
circl(e(20+i,100+(i/10)’20);
delay(2)§
cleardewceO;
Scanned by CamScanner
dclay(]000);
mm;
max
mmx
linc(0,20,0,400);
1inc(600,20,600,400);
int 1‘11;
for(i=580.j=580;i>100;i—-J-H)
{ line(0,20‘0,400);
linc(600,20,600,400);
circle(20+i,100+G/10),20);
dclay(2);
cleardeviceO;
delay(100);
p11();
max
getchO;
OUTPUT
Scanned by CamScanner
delay(400);
//linc(62+j,50,97+j,75); "
k=j; \/
while(k>=0)
{ floodfillU37+k,129,Wl-IITE);
Scanned by CamScanner
k-=25;
}
delay(400);
}
cleardeviceO;
cout<<"resend packet";
getcho;
for(j=0;j<100;j+=25)
{ cleardeviceO;
f0r(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100,] 25,125+i,] 50);
}
I 1:0;
1ine(37+j,50,1 37+j, 125);
delay(400);
//1ine(62+j,50,97+j ,75);
// if(j>=75)
//
k=j;
if(j >25)
k=25;
whi1e(k>:0)
{ floodfill(137+k,129,WHITE);
-=25;
Scanned by CamScanner I
I
!
delayHOO):
}
f0rLi=50;i<120:j+=25)
{ Cleardcviceo:
tbfii=35;i<150:i+=25)
{ rectangle(i.25.25+i,50);
rectangle(i+100,125,125+i,150);
}
l=0;
line(37+j,50,137+j,125);
delay(400);
//line(62+j,50,97+j,75);
// if0>=75)
L=j;
//if(i>25)
// k=25;
while(k>=0)
{ floodfill(137+k,129,WHITE);
k—=25;
} /
delay(400);
}
getchO;
Scanned by CamScanner
OUTPUT:
Scanned by lCamScanher
Scanned by CamScanner
k-=25;
}
delay(400);
}
cleardeviceO;
cout<<"resend packet";
gelchO;
f0r(j=0;j<lOO;j+=25)
{ cleardeviceO;
for(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100, 125,125+i, 150);
}
Iine(37+j,50,137+j,125);
delay(400);
//1ine(62+j,50,97+j,75);
// if(j>=75)
//
k=j;
1f(j==25)
k=0;
while(k>=0)
{ if(k==25)
{ -=25;
}
Scanned by CamScanner
//
//
floodfill(1 37+k,129,WHITE);
-=25;
}
delay(400);
j=25;
cleardeviceo;
for(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100,125,125+i,150);
}
line(37+j,50,130+j,125);
delay(400);
//1ine(62+j,50,97+j,75);
ifG>=75)
k=j+50;
//if(j>25)
// k=25;
while(k>=0)
{ floodfill(130+k,129,WHITE);
k-=25;
}
delay(400);
j=125;
Scanned by CamScanner
cleanlm in“);
Q lm‘tnnglqifiilfi t LSD):
rwmnglcu1-100.IJSJZS'Q‘LISO);
1‘
linc(3?a‘j.50.1204il‘25):
dclayHOO):
Jiuqfilfii,50.97fi.75);
ifijz=75)
k=j-S:
3.3311125)
21' F25;
while(k>=0)
{ floodfi1l(120+k,129,WHlTE);
k225; }
delay(400);
getch0;}
Scanned by CamScanner
OUTPUT:
Scanned by CamScanher
Experiment: 5
#include <iostream>
#includc <1nap>
#include <queue>
#include <vector>
#include <cmath>
#include <cstri11g>
#include <cli1nits> \
#include <set>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define f first
#define 5 second
bool Visited[MAX+5];
11 dist[MAX+5];
void initializeO
{
for(ll i=0;i<MAX;i++)
{
visited[i]=false;
dist[i]=inf;
}
}
void Dijkstrafll x)
{
priority_queue<PII,Vect0r<PII>,greater<PII> > q;
PH P;
dist[x]=0;
q-push(mp(0,X));
While(!q.empty())
{
P=q.t0p0;
q-p0p0;
Scanned by CamScanner
11 k:P.s;
if(visited[k]=true)
continue;
for(ll i=0;i<list[k].size();i—|—!—)
{
if(!visited[list[k][i].s]&&dist[list[k][i].s]>dist[k]+list[k][i].l‘)
{
dist[list[k][i].s]=dist[k]+1ist[k][i].f;
q.push(make _pair(dist[1ist[k][i].s],1ist[k][i].s));
}
}
visited[k]=true;
}
}
int main() {
boost;
11 i,x,y,w,n,m,j;
cin>>n>>m;
for(i=1 ;i<:m;i++)
{
cin>>x>>y>>w; ,
1ist[X]~pb(mp(W,y));
1ist[y]-pb(mp(W:X)); /
}
for(i=1;i<=n;i++) ,
{ cout<<"TABLE FOR "<<i<<end1;
iriitializeO;
Dijkstrafi); u
11 mini=le9;
f0r0=1;j<=n;i++)
cout<<i<<" "<<j<<" "<<dist[j]<<endl;
cout<<endl;
}
return 0;
}
Wawark is represented as a graph)
6 8 (No. of vertices , No. of edges)
1 2 9 (Starting vertex , Ending vertex , Weight)
1 4 10
2 4 10
2 3 2
3 4 4
Scanned by CamScanner
s;‘
‘r V
:C‘__
559
OYTPUT:
-- " ,-x'«
5122:12373\-‘;LE::\?:ex.}ixnsunnxyagnfi
“Y" 1"!
.ADLEFUAL
tn
‘1
4-.“
:W
~‘_
Ht
:3?
5-‘
_ ,_ fi~
LABL::OK-
‘78
_~;
{-11
1"
_‘_.0
7:7:
:51}
W
b:
1"
WI
’11
O
W
U)
3111
322
330
:14
3511
3611
men: FOR 4 H
41 10
426
4
414
440
+57
4615
:2.
TABLEFORS
5117
5213
5312
547
550
563
TABLEFORG
6122
6213
Scanned by CamScanner
(11“
6415
(‘58
(‘00
Scanned by CamScanner
Experiment: 6
AIM: To implement Bellman Ford Algorithm.
#includc <stdio.h>
#includc <stdlib.h>
#include <string.h>
#include <limits.h>
#include<iostrcam>
using namcspacc std;
struct Edge
{
int src, dest, weight;
};
struct Graph
{ im V, E;
struct Edgc* edge;
};
struct Graph“ createGraphUnt V, int E)
{
strucl Graph* graph =
(struct Graph*) malloc( sizeof(struct Graph) );
graph—>V = V;
graph—>E = E;
graph->edge =
(struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
return graph;
}
void printArrUnt dist[], int n)
{
printf("Vertcx Distance from Source\n"); /
for (int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
Scanned by CamScanner
int u = graph->cdge[j].src;
int v = graph->cdge[j].dest;
}
}
for (int i = 0; i < E; i++)
{
int u = graph->cdge[i].src;
inl v = graph->cdge[i].dcst;
int weight = graph—>cdge[i].weight;
if(dist[u] I= INT_MAX && dist[u] + weight < dist[v])
printf("Graph contains negative weight cycle");
}
printArr(dist, V);
return;
}
int main()
{
int V ;
int E ;
cin>>V>>E;
for(int i=0;i<E;i-+-‘r)
{cin>>graph—>edge[i].src;
cin>>graph—>edge[i] . dest;
cin>>graph->edge[i].weight;}
BellmanFord(graph, 0);
return 0;
H.»
Scanned by CamScanner
#include<iostream>
using namespace std;
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}IT[10];
int main()
{
int dmat[20] [20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
{
scanf("%d",&dmat[i][i]);
~dmat[i] [i]=0;
rt[i]‘dist[j]=dmat[i][j];
rt[i]-fr0m[i]=j;
Scanned by CamScanner
do
count=0;
f0r(i=0;i<n;i++)
f0r0=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].distfi]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}whi1e(count!=0);
f0r(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
f0r0:0;j<n;j++)
{
printf("\t\nn0de %d Via %d Distance%d",j+1,rt[§].from[i]+1,tt[i]-dist[i]);
}
printf("\n\n");
Scanned by CamScanner
, vla 1 Distance;
vla 2 Distances
State value for router ‘ : via 4 Distance:
via 4 Distance:
u
x
.
l
u
n
n
.
.
.
.
.
n
‘
n
n
x
u
l
.
a
u
u
.
n
v
a
n
u
n
n
u
u
»
.
Scanned by CamScanner
printSolution(dist);
Scanned by CamScanner
t
1
if(disflflljj == INF)
p1‘intf("°/o7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
-,-I
int main()
{
int graph[V][V] = { {0, 1, 4,1NF,INF},
{INF, 0, 3, 2,2},
{[NF,INF,O, INF,INF},
{INF,1, 5, O,INF},
{INF,INF,INF,3,0}
};
floydWarshall(graph);
return 0;}
: ll);
Scanned by CamScanner
OUTPUT:
3&1 E:Knctv:c:kIahkshtglesourceshorieshtath.exe
the shortest path to all the vertices from the source is:
xhen the source vertex is: e e 1 4
Scanned by CamScanner