Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views

System Programming

The document contains details of 4 programming assignments related to system programming subject: 1. Write a program to remove comments and newlines from code. 2. Write a program to simulate a lexical analyzer. 3. Write a program to simulate a lexical analyzer for validating operators. 4. Write a lex program to convert lowercase letters to uppercase and vice versa.

Uploaded by

ronakidea
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

System Programming

The document contains details of 4 programming assignments related to system programming subject: 1. Write a program to remove comments and newlines from code. 2. Write a program to simulate a lexical analyzer. 3. Write a program to simulate a lexical analyzer for validating operators. 4. Write a lex program to convert lowercase letters to uppercase and vice versa.

Uploaded by

ronakidea
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Faculty of Engineering& Technology

Subject Name : System Programming


Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL:-1
Aim:-Write a program to remove comments and new lines from the code in
Programming language.
Input Code:-
#include<stdio.h>
#include<conio.h>
FILE *fp1 ,*fp2;
void check_comment(char); //declaration
void singleline_comment();
void multiline_comment();
int main()
{ char c;
fp1=fopen("program.txt","r");
fp2=fopen("program_removecomment.txt","w");
while((c=fgetc(fp1))!=EOF)
{
check_comment(c); //function call
}
fclose(fp1);
fclose(fp2);
getch();
}
void check_comment(char c) //definition
{
char d;
if(c=='/')
{
{

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

if((d=fgetc(fp1))=='/')
{
singleline_comment();
}
else if(d=='*')
{
multiline_comment();
}
else
{
printf("%c%c",c,d);
fputc(c,fp2);
fputc(d,fp2);
}
}
}
else
{
printf("%c",c);
fputc(c,fp2);
}
}
void singleline_comment()
{
char d,e;
while((d=fgetc(fp1))!=EOF)
{
if(d=='\n')

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

return;
}
}
void multiline_comment()
{
char d,e;
while((d=fgetc(fp1))!=EOF)
{
if(d=='*')
{
e=fgetc(fp1);

if(e=='/')
return;
}}}

Code for program.txt:-


#include <stdio.h>
#include <conio.h>
int main()
{//program
/* wertyjnbgfvd
sdfghnbv
wsesrdtghbgv*/
printf("Practical-4");
return 0;}

Output:-

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

After removing comments from the program:

P R PS Total

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL-2
Aim:-Write a program to simulate lexical analyzer.
Input Code:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{int l,i;
char str[100],word,ky[10];
printf("enter string:");
gets(str);

l=strlen(str);
for(i=0;i<l;i++)
{
word=str[i];
if(word>=97 && word<=122 && word!=105 && word!=102 && word!=110
&& word!=100)
{
printf("id#0%d %c\n",i+1,word);
}
else if(word>=65 && word<=90 && word!=73 && word!=70 && word!=78
&& word!=68)
{
printf("id#0%d %c\n",i+1,word);
}
else if(word==';' || word=='+' || word=='-' || word=='*' || word=='/' || word=='='
|| word==',')
{

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

printf("op#0%d %c\n",i+1,word);
}
else if(word=='i' || word=='f' || word=='n' || word=='d')
{
printf("ky#0%d %c\n",i+1,word);
}
else
{
printf("space\n");
}}}

Output:-

P R PS Total

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL-3
Aim:-Write a C program to simulate lexical analyzer for validating
operators.
Input Code:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{ char s[10];
printf("Enter string:");
gets(s);
switch(s[0])
{
case '>' : if(s[1]=='=')
printf("\n Greater than or equal to.");
else
printf("Greater than.");
break;
case '<' : if(s[1]=='=')
printf("\n Less than or equal to.");
else
printf("Less than.");
break;
case '!' : if(s[1]=='=')
printf("\n Not equal to.");
else
printf("Bit not.");
Enrolment No: 180303108018
Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

break;
case '&' : if(s[1]=='&')
printf("\nLogical AND.");
else
printf("Bitwise AND.");
break;
case '|' : if(s[1]=='|')
printf("\nLogical OR.");
else
printf("Bitwise OR.");
break;
case '=' : if(s[1]=='=')
printf("\nEqual to.");
else
printf("Assignment operator.");
break;
case '+' : if(s[1]=='+')
printf("\nIncrement operator.");
else
printf("Addition.");
break;
case '-' : if(s[1]=='-')
printf("\nDecrement operator.");
else
printf("Subtraction.");
break;
case '*' :
printf("\n* ----> Multiplication ----> Arithmetic operator.");

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

break;
case '/' :
printf("\n/ ----> Division ----> Arithmetic operator.");
break;
case '%' :
printf("\n % ----> Modulo ----> Arithmetic operator.");
break;
default : printf("It is not a operator.");
return 0;
}}

Output:-

P R PS Total

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL-4
Aim:-Write a lex program to Lex program to convert lowercase to
uppercase.
Input Code:-
%{
#include<stdio.h>
%}
%%
[a-z] {printf("%c",yytext[0]-32);}
[A-Z] {printf("%c",yytext[0]+32);}
%%
int yywrap()
{return 1;}
main()
{printf("Enter the string :");
yylex();}

Output:-

P R PS Total

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL-5
Aim:-Write a lex program to count the number of characters, words and
lines in the given input.
Input Code:-
%{
#include<stdio.h>
int lines=1, words=1,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%
\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
%%
main(void)
{yyin= fopen("sp.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" The File possesses");
printf("\n%d lines", lines);
printf("\n%d words",words);
printf("\n%d small letters", s_letters);
printf("\n%d capital letters",c_letters);
printf("\n%d digits", num);

printf("\nIn total %d characters.\n",total);}


int yywrap()

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

{return(1);}
Output:-

P R PS Total

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL-6
Aim:-Implement Recursive Descendent Parsing for the given grammar.
E→T+E/T
T→F*T/F
F → ( E ) /i.
Input Code:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void E();
void T();
void F();
void Eds();
void Tds();
char input[100];
int i,error;
int main()
{
i=0;
error=0;
printf("Enter the string:");
gets(input);
E();
if(strlen(input)==i&&error==0)
printf("Accepted String\n");
else
Enrolment No: 180303108018
Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

printf("Rejecct the string\n");


}
void E() //define the E
{
T();
Eds();
}
void Eds()
{
if(input[i]=='+')
{
i++;
T();
Eds();
}
}
void T()
{
F();
Tds();
}
void Tds()
{
if(input[i]=='*')
{
i++;
F();
Tds();

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

}
}
void F()
{
if (isalnum(input[i]))
i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
else error=1;
}
else error=1;
}

Output:-

P R PS Total

PRACTICAL-7
Aim:-Write a program to left factor the given grammar.
Enrolment No: 180303108018
Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

Input Code:-
#include<stdio.h>
#include<string.h>
int main()
{
char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,l=0,pos;
printf("Enter Production : A->");
gets(gram);
for(i=0;gram[i]!='|';i++,j++)
part1[j]=gram[i];
part1[j]='\0';
for(j=++i,i=0;gram[j]!='\0';j++,i++)
part2[i]=gram[j];
part2[i]='\0';
for(i=0;i<strlen(part1)||i<strlen(part2);i++)
{
if(part1[i]==part2[i])
{
modifiedGram[k]=part1[i]; k++;
pos=i+1;
}

}
for(i=pos,j=0;part1[i]!='\0';i++,j++)
{
newGram[j]=part1[i];
}

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

newGram[j++]='|';
for(i=pos;part2[i]!='\0';i++,j++)
{
newGram[j]=part2[i];
}
modifiedGram[k]='X';
modifiedGram[++k]='\0';
newGram[j]='\0';
printf("\n A->%s",modifiedGram);
printf("\n X->%s\n",newGram);
}

Output:-

P R PS Total

PRACTICAL-8
Aim:-Write a program to remove the Left Recursion from a given

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

grammar.
Input Code:-
#include<stdio.h>

int main()
{
char production[10][10],non_terminal,a,b;
int i,num,index=3;

printf("Enter Number of Grammar: ");


scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\nEnter Production of grammar %d: ",i);
scanf("%s",&production[i][0]);

non_terminal=production[i][0];
printf("The non terminal is: %c\n",non_terminal);

if (non_terminal==production[i][index])
{
printf("It is a left recursive\n\n");
if(non_terminal==production[i][index])
{
a=production[i][index+1];
b=production[i][index+2];

printf("Removed left recursion:\n");


printf("%c->%c%c\'",non_terminal,b,non_terminal);
printf("\n%c\'->%c%c%c\'|Null\n",non_terminal,a,b,non_terminal);

}
else
{
printf("It is not left recursive.\n");
}
}
}
}

Output:-

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

P R PS Total

PRACTICAL:-9

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

AIM:-Write a SAL program in text file and generate SYMTAB and


LITTAB .
Input Code:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
structsym
{
char lab[10];
intval;
};
struct li
{
charoprn[10];
intaddr;
};
main ()
{
FILE *f1;
char la[10],op[10],opr[10],a[1000],c;
inti,j,n,k=0,lc=0,m=0,p=0;
structsym s[10];
struct li l[10];
f1=fopen("assembler.txt","r");
c=fgetc(f1);
i=0;
printf("\nSOURCE PROGRAM \n");
printf("%c",c);
while(c!=EOF)
Enrolment No: 180303108018
Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

{
a[i]=c;
c=fgetc(f1);
i++;
printf("%c",c); }
i=0;
printf("\nINTERMEDIATE FILE \n");
while(strcmp(op,"end")!=0)
{if(a[i]=='\t')
{strcpy(la," ");
i++; }
else
{
j=0;
while(a[i]!='\t')
{la[j]=a[i];
i++;
j++; }
la[j]='\0';
i++; }
if(a[i]=='\t')
{strcpy(op," ");
i++; }
else
{j=0;
while (a[i]!='\t')
{op[j]=a[i];
i++;

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

j++; }
op[j]='\0';
i++; }
if(a[i]=='\n')
{strcpy(opr," ");
i++; }
else
{j=0;
while(a[i] !='\n')
{opr[j]=a [i];
i++;
j++; }
opr[j]='\0';
i++; }
j=0;
if(strcmp (la," ") !=0)
{strcpy(s[m].lab,la);
if(strcmp(op, "start") ==0)
{lc=atoi(opr);
s[m].val=lc,
m++;
continue; }
else if(strcmp (op, "equ") ==0)
{printf("\n%d\t",lc);
s[m].val=atoi(opr);
m++; }
else if(strcmp (op, "resw") ==0)
{printf("\n%d\t",lc);

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

s[m].val=lc;
lc=lc+atoi(opr)*3;
m++; }
else if(strcmp (op, "resb") ==0)
{printf("\n%d\t",lc);
s[m].val=lc;
lc=lc+atoi(opr);
m++; }
else
{printf("\n%d\t",lc);
strcpy(s[m].lab,la);
s[m].val=lc;
lc=lc+3;
m++; }
}else
{printf("\n%d\t",lc);
lc=lc+3; }
if(opr[0] =='=')
{strcpy(l[k].oprn,opr);
k++; }
printf("%s\t%s\n",op,opr); }
if(strcmp(op,"end")==0)
for(n=p;n<k;n++)
{l[n].addr=lc-3;
printf("\n%d\t%s\n",l[n].addr,l[n].oprn);
lc=lc+3;
p++; }
printf("\n symbol table \n");

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

printf("\nname\taddress");
for(i=0;i<m;i++)
printf("\n%s\t%d\n",s[i].lab,s[i].val);
printf("\n Literal table \n");
printf("\nname\taddress");
for(i=0;i<k;i++)
printf("\n%s\t%d\n",l[i].oprn,l[i].addr);
getch();
}

Output:-

P R PS Total

PRACTICAL-10
Aim: - Write a program which demonstrates the use macro features of C
language.
Enrolment No: 180303108018
Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

Input Code:-
//1
#include<stdio.h>
#include<conio.h>
#define INCREMENT(x) ++x
int main()
{
char *ptr="k";
int x=10;
printf("%s",INCREMENT(ptr));
printf("%d",INCREMENT(x));
return 0;
}

//2
#include <stdio.h>
#define MULTIPLY(a,b) a*b
int main()
{
printf("%d", MULTIPLY(2+3,3+5));
return 0;
}

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

//3
#include <stdio.h>
#define merge(a,b) a##b
int main()
{
printf("%d", merge(12,34));
return 0;
}

//4
#include <stdio.h>
#define get(a) #a
int main()
{
printf("%s", get(KEVAL-VIRANI));
return 0;
}

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

//5
#include <stdio.h>
#define PRINT(i, limit) while (i< limit) \
{\
printf("KEVAL"); \
i++; \
}
int main()
{
inti = 0;
PRINT(i, 3);
return 0;
}

//6
#include <stdio.h>
#define square(x) x*x
int main()
{
// Expanded as 36/6*6
int x = 36/square(6);
printf("%d", x);
return 0;
}

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

//7
int main()
{
#if VERBOSE >= 2
printf("Trace Message");
#endif
}

P R PS Total

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

PRACTICAL-11
Aim:- write a program to show quadruple table for given string.
Input Code:-
#include<stdio.h>
#include<string.h>
int main()
{
char line[20];
int s[20];
int t=1;
int i=0;
printf("Enter Your string:");
gets(line);
for(i=0;i<20;i++)s[i]=0;
printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);}

Enrolment No: 180303108018


Faculty of Engineering& Technology
Subject Name : System Programming
Subject Code : 203108352
B.Tech IT Year 2020-21 Semester 6th
Annexure No :

OUTPUT:-

P R PS Total

Enrolment No: 180303108018

You might also like