SP Lab File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

1.

Introduction to System Programming

System programming is the activity of programming system software to support operation & use of the
computer itself rather than any particular application. The primary distinguishing characteristic of
systems programming when compared to application programming is that application programming
aims to produce software which provides services to the user (e.g. word processor), whereas systems
programming aims to produce software which provides services to the computer hardware (e.g. disk
defragmenter). It requires a greater degree of hardware awareness.

System software consists of a variety of programs that support the operation of a computer. These are
developed to make computer better adapted to the need of the user.

Components of system programming include the:

Hardware it provides the basic computing resources like CPU, I/O, and memory devices etc.
Operating system controls and coordinates the use of hardware and software among the various
applications running on the platform.
Application program it defines the way any system resource is used to solve computing
problems of the users. Here the main focus is on application not on system.
System program these are low level programs that comprise of mnemonics or machine level
coding to define the basic functions that build up the OS and that help run all other higher level
applications.

2. Write a program to find total number of tokens in an expression.


#include<stdio.h>
#include<string.h>
#include<math.h>
#define oper(X)(X=='+'||X=='*'||X=='/'||X=='-'||X=='^')
void main()
{
int i,j,l,alpha = 0,opr = 0, con=0;
char s[20],a,b;
printf("\n Enter the expression");
gets(s);
l= strlen(s);
for(i=0;i<l;i++)
{
if (oper(s[i]))
{
opr++;
}
else if (s[i]>= 97 && s[i]<=122)
{
alpha++;
}
else
{

if(s[i]>=48 && s[i]<=57)


{
j=i+1
;
while(s[j]>=48 && s[j]<=57)

{
j++;
}
con++;
i=j;
}
}
}
printf("\n operator=%d \n alphabet = %d \n constant = %d", opr,alpha,con);
}

OUTPUT:

3. Write a program to convert infix to prefix notation.

#include<stdio.h>
#include<string.h>
#define operand(x)(x>='a'&&x<='z'||x>='A'&&x<='Z'||x>='0'&&x<='9')
char in[30],pre[30],stack[30];
int top=-1;
int i=0, m, n, len;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
return(stack[top--]);
}
int check(char x)
{
int y;
switch(x)
{
case '+': y=1;
break;
case '-': y=1;
break;
case '/': y=2;
break;
case '*': y=2;
break;
case '^': y=3;

break;
case '(': y=6;
break;
case ')': y=5;
break;
default : y=-1;
}
return y;
}

int def(char x)
{
int y;
switch(x)
{

case ')': y=0;


break;
case '+': y=1;
break;
case '-': y=1;
break;
case '/': y=2;
break;
case '*': y=2;
break;
case '^': y=3;
break;
case '(': y=6;
break;

default: y=-1;
}
return y;
}

void intopre()
{
int j,k=0;
char x,y;
stack[++top]='\0';
for(j=strlen(in)-1;j>=0;j--)
{
x=in[j];
if(operand(x))
pre[k++]=x;
else if(x=='(')
{
while((y=pop())!=')')
pre[k++]=y;
}
else
{
while(def(stack[top])>check(x))
pre[k++]=pop();
push(x);
}
}
while(top>=0)
pre[k++]=pop();
}

void reverse(char pre[],int len)


{
char arr[len];
for(m=len, n=0;m<=0, n<=len;m--, n++)
{
arr[n]=pre[m];
}
for (n=0;n<=len; n++)
{
printf("%c", arr[n]);
}
}

int main()
{

printf("\nEnter an infix expression : ");


gets(in);
intopre();
len=strlen(pre);
reverse(pre, len);
printf("\n\tThe resulting prefix expression is : %s\n\t\t",arr);

OUTPUT:

4. Write a program to convert infix into postfix notation.

#include<stdio.h>
#include<string.h>
#define size 10
char stack[size];
int tos=0,ele;
void push();
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);

int main()
{
int i=0,j=0,k=0,length;
char temp;
printf("\nEnter an infix expression:");
scanf("%s",infix);
printf("\nThe infix expresson is %s",infix);
length=strlen(infix);
for(i=0;i<length;i++)
{
if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')'
&& infix[i]!='(' )
{
output[j++]=infix[i];
printf("\nThe element added to Q is:%c",infix[i]);
}

else
{
if(tos==0)
{
push(infix[i]);
printf("\nThe pushed element is:%c",infix[i]);
}
else
{
if(infix[i]!=')' && infix[i]!='(')
{
if( prec(infix[i]) <= prec(stack[tos-1]) )
{
temp=pop();
printf("\n the poped element is :%c",temp);
output[j++]=temp;
push(infix[i]);
printf("\n The pushed element is :%c",infix[i]);
show();
}
else
{
push(infix[i]);
printf("\nThe pushed element is:%c",infix[i]);
show();
}
}
else
{
if(infix[i]=='(')

{
push(infix[i]);
printf("\nThe pushed-- element is:%c",infix[i]);
}
if(infix[i]==')')
{
temp=pop();
while(temp!='(')
{output[j++]=temp;
printf("\nThe element added to Q is:%c",temp);
printf("\n the poped element is :%c",temp);
temp=pop();}
}
}

printf("\nthe infix expression is: %s",output);

}
while(tos!=0)
{
output[j++]=pop();
}
printf("the infix expression is: %s\n",output);

}
void push(int ele)

{
stack[tos]=ele;
tos++;
}
char pop()
{
tos--;
return(stack[tos]);
}
void show()
{
int x=tos;
printf("--The Stack elements are.....");
while(x!=0)
printf("%c, ",stack[--x]);
}
int prec(char symbol)
{

if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;

OUTPUT:

5. Write a program to accept a language of type ab*.

#include<stdio.h>
#include<string.h>
char s[10];
int i,size;
void main()
{
printf("enter the string for testing grammar: ");
scanf("%s",&s);
size=strlen(s);
if(s[size-1]=='b'&&s[size-2]=='a')
{
s[size-1]='*';
s[size-2]='*';
for(i=0;i<(size-2);++i)
{
if(s[i]=='*')
{
printf("string is accepted by grammar.");
exit(0);
}
else if(s[i]=='a')
s[i]='*';
else
break;
}
if(i!=(size-2))
printf("string is not accepted by grammar.");
else

printf("string is accepted by grammar.");


}
else
printf("string is not accepted by grammar.");
}

OUTPUT:

6. Write a program to convert NDFA into DFA.

#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';
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;
for(int k=0;k<count;k++)
{
if(!strcmp(d[k].state,d[i].out0))
{
f1=1;
break;
}
}
if(f1==0)
{
for(int h=0;h<ncnt;h++)
{
if(!strcmp(nst[h],d[i].out0))
{
f1=1;
break;

}
}
}
if(f1==0)
{
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;
}
}
if(f2==0)
{
for(int h=0;h<ncnt;h++)
{
if(!strcmp(nst[h],d[i].out1))
{
f2=1;
break;
}
}
}

if(f2==0)
{
strcpy(nst[ncnt],d[i].out1);
ncnt++;
}
}

if(ncnt==0)
goto ahead;

for(i=0;i<ncnt;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))
{

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

OUTPUT:

7. Write a program to convert Regular Expression to NDFA.

#include <iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct table
{
char state;
char out0;
char out1;
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[c-1].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();
}

8. Write a program to make symbol Table for an expression.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char in[50],dig[50],id[50];
int i=0,j=0,k,l=0;
printf("Enter the Expression:\t");
gets(in);
printf("\nKeyword\tIdentifier\tConstants\tOperators\tSpecialCharacters\n");
while(in[i]!='\0')
{
if(isalpha(in[i]))
{
j=0;
while((isalpha(in[i]))||(isdigit(in[i])))
{
id[j]=in[i];
i++;
j++;
}
id[j]='\0';
if(strcmp(id,"char")==0||strcmp(id,"int")==0||strcmp(id,"float")==0||strcmp(id,"if"
)==0||strcmp(id,"then")==0||strcmp(id,"while")==0||strcmp(id,"do")==0||
strcmp(id,"for")==0||strcmp(id,"switch")==0||strcmp(id,"case")==0)
{
printf("\n");
for(l=0;l<j;l++)
printf("%c",id[l]);
}
else
{
printf("\t");
for(l=0;l<j;l++)
printf("%c",id[l]);
}
}
else if(isdigit(in[i]))
{
k=0;
while(isdigit(in[i]))
{
dig[k]=in[i];
i++;
k++;

}
printf("\n\t\t\t");
for(l=0;l<k;l++)
printf("%c",dig[l]);
}
else if(in[i]=='+'||in[i]=='-'||in[i]=='*'||in[i]=='/'||in[i]=='<'||in[i]=='>'||in[i]=='=')
{
printf("\t\t\t\t\t%c",in[i]);
i++;
}
else if(in[i]==';'||in[i]==':'||in[i]=='.'||in[i]=='('||in[i]==')'||in[i]=='{'||in[i]=='}')
{
printf("\t\t\t\t\t\t\t%c",in[i]);
i++;
}
else
i++;
printf("\n");

}
}
OUTPUT:

9. Write a program to evaluate top-down parsing technique.

#include<stdio.h>
void main()
{
int a[30];

int min=10000,temp=0,i,j,lev,n,noofc,z;
printf("please enter how many number:");
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=0;
printf("enter value of root:");
scanf("%d",&a[0]);
for(i=1;i<=n/2;i++)
{
printf("please enter no of child of parent with value: %d",a[i-1]);
scanf("%d",&noofc);

for(j=1;j<=noofc;j++)
{z=(i)*2+j-2;

printf("please enter value of child:");


scanf("%d",&a[z]);
}
}
for(i=n-1;i>=n/2;i--)
{

temp=0;
for(j=i+1;j>=1;j=j/2)
temp=temp+a[j-1];
if(temp<min)
min=temp;
printf("temp min is: %d",temp);
printf("\n");
}
printf("final min value is: %d",min);
}

OUTPUT:

You might also like