Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ystem Rogramming: Laboratory Manual and Work Book

Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

2150708 System Programming Lab Manual 2019

LABORATORY MANUAL AND WORK BOOK 2019

System Programming
SUBJECT CODE: 2150708
B.E. 5th Semester

Guided By:
PROF BHARAT VAINSH
Department of IT,GEC Bhavnagar

Submitted By:
Enrollment No.
Name

Government Engineering College


BHAVNAGAR

Department of Information Technology


Gujarat Technological University
2019

1 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

INDEX
Sr_No Practical Page Date Sign
No
Unit-1,2
1 Write a Program for token specification and recognition. For 3 24/06/19
example, Separation of keywords from identifiers, identify a set of
operators etc.
2 Program to implement partial lexical Analyzer for 'c' language. 6 26/06/19
Unit-3,4
3 Write a C program to implement a symbol table with functions to 12 1/7/19
create, insert , modify , search and display.
4 Write a C program to implement pass one of a two pass assembler. 17 3/7/19
5 Write a C program to implement pass two of a two pass assembler. 22 8/7/19
6 Write a C program to implement a macro processor. 25 15/7/19
Unit-5,6
7 Write a C program to implement an absolute loader. 28 22/7/19
8 Write a C program to implement a relocating loader. 31 24/7/19
9 Write a C program to implement pass one of a direct - linking 34 29/7/19
loader.
10 Write a C program to implement pass two of a direct - linking 35 31/7/19
loader.
11 Write a C program to implement a simple text editor with features 39 5/8/19
like insertion / deletion of a character, word, sentence.
Unit-7,812/8/19
12 Program to implement partial lexical Analyzer for 'c' language 44 12/8/19
13 Write a program to left factor the given grammar. 48 19/8/19
14 Write a program to remove the Left Recursion from a given 52 21/8/19
grammar.
15 Implement Recursive Descendent Parsing for the given Grammar. 54 26/8/19
E -> T + E / T
T -> F * T / F
F -> ( E ) / i
16 Write a Program to implement Predictive Parser table for the given 58 9/9/19
grammar.
17 Write a SAL program in text file and generate SYMTAB and LITTAB 62 16/9/19
18 Write a program which generates Quadruple Table for the given 64 18/9/19
postfix String.
19 To write a program for implementing a Lexical analyzer using LEX 65 25/9/19
tool in Linux platform.
20 Write a YACC program with error recovery. 68 7/10/19

2 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 1
AIM: Write a Program for token specification and recognition. For example, Separation of
keywords from identifiers, identify a set of operators etc.

Source Code:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
char *keyword[10]={"if","else","eof"};
void main()
{
char s[100],n[100];
int i,k,j;
clrscr();
printf("Enter your string:");
gets(s);
for(i=0;i<strlen(s);i++)
{
k=0;
if(s[i]==' ')
{
continue;
}
if(isalpha(s[i])||s[i]=='_')
{
while(isalnum(s[i])||s[i]=='_')
{
n[k]=s[i];
k++;
i++;
}
n[k]='\0';
i=i-1;
for(j=0;strcmp(keyword[j],"eof")!=0;j++)
{
if(strcmp(n,keyword[j])==0)
{
printf("\n%s is keyword",n);
goto not;
}
}

3 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

printf("\n%s is an identifier",n);
}
else if(isdigit(s[i]))
{
while(isdigit(s[i]))
{
n[k]=s[i];
i++;
k++;
}
n[k]='\0';
printf("\n %s is a constant",n);
i=i-1;
}
else if(s[i]=='='||s[i]=='+'||s[i]=='-'||s[i]=='/'||s[i]=='%'||s[i]=='*')
{
printf("\n%c is an operator",s[i]);
}
else if(s[i]=='('||s[i]==')'||s[i]=='{'||s[i]=='}'||s[i]=='['||s[i]==']')
{
printf("%c is an bracket",s[i]);
}
else
{
printf("%c is special symbol",s[i]);
}
not:
}
getch();
}

4 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

5 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 2

AIM: Program to implement partial lexical Analyzer for 'c' language.

Source Code:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
char c, exp[1000],s1[20],s2[20];
int ope();
int i,j,k,l,error;
int braces=0;
int uoperator_=0,boperator_=0,identifier=0,constant_=0;
FILE *f1;
clrscr();
printf("enter the string");
printf("\nAt end enter @\n");
for(i=0;(c=getchar())!='@';i++)
exp[i]=c;
exp[i]='@';
i=0;
abc:
while(exp[i]!='@')
{
j=0;
while(exp[i]==' '||exp[i]=='\n'||exp[i]=='\t'||exp[i]==';')
i++;
if(ope(exp[i]))
{
k=0;
while(k<2&&exp[i]!=';'&&exp[i]!='
'&&exp[i]!='@'&&exp[i]!='\n'&&exp[i]!='\t'&&!((exp[i]>=97&&exp[i]<=122)||(exp[i]>=65&&exp
[i]<=90)||(exp[i]>=48&&exp[i]<=57)))
{
k++;
s1[j]=exp[i];
i++;
j++;
}

s1[j]='\0';
f1=fopen(("C:\TurboC++\Disk\TurboC3\BIN\BVV\opt.txt","r");
6 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

while(!feof(f1))
{
fscanf(f1,"%s",&s2);
if (strcmp(s1,s2)==0)
{
printf("\n%s is operator",s1);
if(strcmp(s1,"++")==0||strcmp(s1,"--")==0)
uoperator_++;
else
boperator_++;

if(strcmp(s1,"(")==0||strcmp(s1,"{")==0||strcmp(s1,"[")==0)
braces++;

if(strcmp(s1,")")==0||strcmp(s1,"}")==0||strcmp(s1,"]")==0)
braces--;
fclose(f1);
goto abc;
}
}
fclose(f1);
}
while(exp[i]!='
'&&exp[i]!='@'&&exp[i]!=';'&&exp[i]!='"'&&exp[i]!='\n'&&exp[i]!='\t'&&!ope(exp[i]))
{
s1[j]=exp[i];
i++;
j++;
}
s1[j]='\0';
//search for the keyword
f1=fopen("C:\TurboC++\Disk\TurboC3\BIN\BVV\key.txt","r");
while(!feof(f1))
{
fscanf(f1,"%s",&s2);
if (strcmp(s1,s2)==0)
{
printf("\n%s is keyword",s1);
fclose(f1);
goto abc;
}
}

fclose(f1);
//search for operator

7 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

/* f1=fopen("C:\TurboC++\Disk\TurboC3\BIN\BVV\opt.txt","r");
while(!feof(f1))
{
fscanf(f1,"%s",&s2);

if (strcmp(s1,s2)==0)
{
printf("\n%s is operator",s1);
if(strcmp(s1,"++")==0||strcmp(s1,"--")==0)
uoperator_++;
else
boperator_++;
fclose(f1);
goto abc;
}
}
fclose(f1);*/
j=0,k=0,error=0;
l=strlen(s1);
while(j<l)
{
if((s1[j]>=48&&s1[j]<=57)||s1[j]==46)
{
if(s1[j]==46)
error++;
k++;
}
if(error>1)
{
printf("\n%s is constant which should not contain more then 1
decimal point",s1);
goto abc;
}
if(k==l)
{
printf("\n%s is constant",s1);
constant_++;
goto abc;
}
j++;
}
j=0;

while(j<l)
{

8 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

if(s1[0]>=48&&s1[0]<=57||s1[0]==95)
{
printf("\n%s is identifier which should not start whit
%c",s1,s1[0]);
goto abc;
}

if(!((s1[j]>=48&&s1[j]<=57)||(s1[j]>=97&&s1[j]<=122)||(s1[j]>=65&&s1[j]<=90)||s1[j]==
95))
{
printf("\n%s is identifier which should not contain %c",s1,s1[j]);
goto abc;
}
if(j==l-1)
{
printf("\n%s is identifier",s1);
identifier++;
goto abc;
}
j++;
}
}
//expresion syntex//
if((identifier&&boperator_!=0)&&((identifier+constant_)==boperator_))
printf("\nerror\n expression syntex");
if(braces!=0)
printf("\nerror\n brace is expected");
getch();
}
int ope(char c1)
{
FILE *f1;
char c2;
f1=fopen("C:\TurboC++\Disk\TurboC3\BIN\BVV\opt.txt","r");
while(!feof(f1))
{
c2=fgetc(f1);
if (c1==c2)
{
fclose(f1);
return(1);
}
}
fclose(f1);
return(0);}

9 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Key.txt Opt.txt <=


" >=
asm auto break case cdecl char { ==
class const continue _cs default } !=
; ^
delete
[] |
do double _ds else enum _es &&
()
extern _export far _fastcall float for ||
.
friend goto huge if inline int -> ?:
interrupt _loadds long near new ++ -- =
operator & *=
pascal private protected public register * /=
return + %=
_saveregs _seg short signed sizeof _ss - +=
static struct switch template this ~ -=
! <<=
typedef
sizeof >>=
union unsigned virtual void volatile &=
/
while ^=,|=,
%
<< ,,
>> #,##,=
<
>

Ope.txt

"[]()+-/*><=#%!,|&^{}

10 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

11 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 3

AIM: Write a C program to implement a symbol table with functions to create, insert , modify ,
search and display.

Source Code:

Algorithm:

1. open the file ( assembly language program.)


2. Separate the mnemonic instructions into label , opcode and operand
3. Check whether the label is not empty
4. if true check whether opcode is START and store the label in symbol table and assign the
operand as program counter value.
5. If opcode is RESB then store the label and program counter in label & value field of
symbol table correspondingly. Add the operand with program counter.
6. If opcode is RESW then store the label and program counter in label & value field of
symbol table correspondingly. Multiply the operand by 3 and Add the operand with program
counter.
7. If opcode is WORD then store the label and program counter in label & value field of
symbol table correspondingly. Increment the program counter by 3.
8. If opcode is BYTE then store the label and program counter in label & value field of
symbol table correspondingly. Find the length of the constant and add it to program counter.
9. If opcede is EQU then store the label and program counter in label & value field of
symbol table correspondingly.
10. if steps 4 to 9 fails then store the label and program counter in label & value field of
symbol table correspondingly. . Increment the program counter by 3.
11. If the label is empty , Increment the program counter by 3.
12. Steps 2 to 10 are executed until EOF is encountered.
13. Display the content of symbol table.

PROGRAM :
#include<stdio.h>
#include<conio.h>
struct sym
{
char lab[10];
int val;
};
void main ()
{
FILE *f1;
char la[10],op[10],opr[10],a[1000],c,key[10];
int i,j,lc=0,m=0,flag,ch=0;
struct sym s[10];
clrscr();
f1=fopen("a1.txt","r");
12 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

c=fgetc(f1);
i=0;
printf ("\n SOURCE PROGRAM \n");
while(c!=EOF)
{
a[i]=c;
c=fgetc(f1);
i++;
}
while(ch<4)
{
printf("1-symbol table creation\n");
printf("2-serch\n");
printf("3-display\n");
printf(">3-Exit\n");
printf("enter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
i=0;
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
{

13 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

j=0;
while(a[i]!='\t')
{
op[j]=a[i];
i++;
j++;
}
op[j]='\0';
i++;
}
if(a[i]=='\t')
{
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++;
printf("%s\t%s\t%s\n",la,op,opr);
continue;
}
else if(strcmp(op,"equ")==0)
{
s[m].val=atoi(opr);
m++;
}
else if(strcmp(op,"resw")==0)
{
s[m].val=lc;

14 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

lc=lc+atoi(opr) *3;
m++;
}
else if(strcmp(op,"resb")==0)
{
s[m].val=lc;
lc=lc+atoi(opr);
m++;
}
else
{
s[m].val=lc;
lc=lc+3;
m++;
}
}
else
lc=lc+3;
printf("%s\t%s\t%s\n",la,op,opr);
}
break;
case 2:
printf("enter the lable to be searched\n");
scanf("%s",&key);
flag=0;
for(i=0;i<m;i++)
{
if(strcmp(key,s[i].lab)==0)
{
printf("%s\t%d\n",s[i].lab,s[i].val);
flag=1;
break;
}
else
continue;
}
if(flag==0)
printf("lable not found\n");
break;
case 3:
printf("\n symbol table \n");
for(i=0;i<m;i++)
printf("\n%s\t%d\n",s[i].lab,s[i].val);
break;
}
}
}

15 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

16 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 4

AIM: Write a C program to implement pass one of a two pass assembler.

Source Code:

Algorithm:

1. open the file ( assembly language program.)


2. Separate the mnemonic instructions into label , opcode and operand.
3. Generate the symbol table
4. to generate Literal table check whether operand[0]is equal to ‘=’ then copy the operand
to literal table.
5. If opcode ‘END’ is encountered then check whether literal are assigned address.
6. Check whether the literal address is zero
7. if true then store the pc to the value of the literal table for the first literal .
8. Increment pc by 3.
9. steps 7 & 8 are repeated until all the literals are assigned addresses.
10. Print the pc , label , opcode & operand and store the intermediate code in file for later
use by Pass 2.
11. Steps 2 to 10 are executed until EOF is encountered.

PROGRAM :

#include<stdio.h>
#include<conio.h>
struct sym
{
char lab[10];
int val;
};
struct li
{
char oprn[10];
int addr;
};
main ()
{
FILE *f1;
char la[10],op[10],opr[10],a[1000],c;
int i,j,n,k=0,lc=0,m=0,p=0;
struct sym s[10];
struct li l[10];
clrscr();
f1=fopen("pass1inp.txt","r");
17 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

c=fgetc(f1);
i=0;
printf ("\n SOURCE PROGRAM \n");
printf("%c",c);
while (c !=EOF)
{
a[i]=c;
c=fgetc(f1);
i++;
printf("%c",c);
}
i=0;
printf("\n INTERMEDIATE 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++;

j++;
}

18 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

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);
s[m] .val=lc;
lc=lc+atoi(opr) *3;
m++;
}
else if (strcmp (op, "resb") ==0)
{
printf("\n%d\t",lc);

19 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

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");
for(i=0;i<m;i++)
printf("\n%s\t%d\n",s[i].lab,s[i].val);
printf("\n Literal table \n");
for(i=0;i<k;i++)
printf("\n%s\t%d\n",l[i].oprn,l[i].addr);
getch();
}

20 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

21 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 5

AIM: Write a C program to implement pass two of a two pass assembler.

Source Code:

Algorithm:

1. Open the symbol table, Literal table and intermediate file.


2. Check whether opcode is not equal to end. If false goto step 7.
3. search optable for opcode
4. when condition is true display the corresponding machine value from optable
5. Check whether the operand is literal , if true get the literals address from the literal
table .Find the displacement address and display it.
6. otherwise check the operand for symbol in symbol table
7. If the instruction is format 4 store the symbol value as operand address. Otherwise Find
the displacement address and display it.
8. If opcode is BYTE or WORD then convert constant to object code.
9. goto step 2.
10. stop the process.

PROGRAM :
/* Pass 2 Assembler */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct
{
char sym[10];
int val;
}s[10];
struct
{
char opt[10];
int val;
}o[10];
struct
{
char lit[10];
int addr;
}l[10];
struct
{
22 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

char op[10],opr[10];
int lc;
}inter[10];
main()
{
FILE *f1,*f2,*f3,*f4;
int i=0,j=0,k=0,m=0,n=0,r;
char c,a[1000];
clrscr();
f1=fopen("symbb.txt","r");
f2=fopen("op.txt","r");
f3=fopen("li.txt","r");
f4=fopen("inter.txt","r");
printf("symbol table\n\n");
while(!feof(f1))
{
fscanf(f1,"%s%d",s[j].sym,&s[j].val);
printf("%s\t%d\n",s[j].sym,s[j].val);
j++;
}
printf("optable\n\n");
while(!feof(f2))
{
fscanf(f2,"%s%d",o[k].opt,&o[k].val);
printf("%s\t%d\n",o[k].opt,o[k].val);
k++;
}
printf("Literal table\n\n");
while(!feof(f3))
{
fscanf(f3,"%s%d",l[m].lit,&l[m].addr);
printf("%s\t%d\n",l[m].lit,l[m].addr);
m++;
}
printf("Intermediate file\n");
while(!feof(f4))
{
fscanf(f4,"%d%s%s",&inter[n].lc,inter[n].op,inter[n].opr);
printf("%d\t%s\t%s\n",inter[n].lc,inter[n].op,inter[n].opr);
}
rewind(f4);
printf("\nmachine instruction \n\n");
while(!feof(f4))
{
fscanf(f4,"%d%s%s",&inter[n].lc,inter[n].op,inter[n].opr);
if((strcmp(inter[n].op,"equ")==0)||(strcmp(inter[n].op,"word")==0))
continue;

23 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

if((strcmp(inter[n].op,"resw")==0)||(strcmp(inter[n].op,"resb")==0))
continue;
printf("%d\t",inter[n].lc);
for(i=0;i<k;i++)
{
if(strcmp(inter[n].op,o[i].opt)==0)
printf("%d\t",o[i].val);
}
for(i=0;i<m;i++)
{
if(inter[n].op[0]!='*')
if(strcmp(inter[n].opr,l[i].lit)==0)
printf("%d\n",l[i].addr-inter[n].lc-3);
}

for(i=0;i<j;i++)
{
if(strcmp(inter[n].opr,s[i].sym)==0)
{
if(inter[n].op[0]=='+')
printf("%d\n",s[i].val);
else
printf("%d\n",s[i].val-inter[n].lc-3);
}
}
if(inter[n].op[0]=='*')
{
printf("%s\n",inter[n].opr);
}
}
getch();
return 0;
}

24 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 6
AIM: Write a C program to implement a macro processor.

Source Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct mdt
{
char lab[10];
char opc[10];
char oper[10];
}d[10];

void main()
{
char label[10],opcode[10],operand[10],newlabel[10],newoperand[10];
char macroname[10];
int i,lines;
FILE *f1,*f2,*f3;
clrscr();
f1 = fopen("MACIN.txt","r");
f2 = fopen("MACOUT.txt","w");
f3 = fopen("MDT.txt","w");
fscanf(f1,"%s %s %s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"MACRO")==0)
{
strcpy(macroname,label);
fscanf(f1,"%s%s%s",label,opcode,operand);
lines = 0;
while(strcmp(opcode,"MEND")!=0)
{
fprintf(f3,"%s\t%s\t%s\n",label,opcode,operand);
strcpy(d[lines].lab,label);
strcpy(d[lines].opc,opcode);
strcpy(d[lines].oper,operand);
fscanf(f1,"%s %s %s",label,opcode,operand);
lines++;
}
}

25 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

else if(strcmp(opcode,macroname)==0)
{
printf("lines=%d\n",lines);
for(i=0;i<lines;i++)
{
fprintf(f2,"%s\t%s\t%s\n",d[i].lab,d[i].opc,d[i].oper);
printf("DLAB=%s\nDOPC=%\nDOPER=%s\n",d[i].lab,d[i].opc,d[i].oper);
}
}
else
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(f1,"%s%s%s",label,opcode,operand);
}
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
fclose(f1);
fclose(f2);
fclose(f3);
printf("FINISHED");
getch();
}

MACIN.TXT MACOUT.TXT

CALC START 1000 CALC START 1000


SUM MACRO ** ** LDA LENGTH
** LDA #5 ** COMP ZERO
** ADD #10 ** JEQ LOOP
** sTA 2000 ** LDA #5
** MEND ** ** ADD #10
** LDA LENGTH ** sTA 2000
** COMP ZERO LENGTH WORD S
** JEQ LOOP ZERO WORD S
** SUM ** ** LDA #5
LENGTH WORD S ** ADD #10
ZERO WORD S ** sTA 2000
LOOP SUM ** ** END **
** END **

MDT.TXT

** LDA #5
** ADD #10
** sTA 2000

26 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

27 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 7

AIM: Write a C program to implement an absolute loader.

Source Code:

Algorithm :

1. Read the input line from the object program.


2. Check whether it is Header record.
3. If step 2 is satisfied , get the stating address from the header record and assign It to a
variable.
4. When the condition is false , check for text record.
5. Read a character from the text record and subtract it by 48.
6. Read the next character from the text record and concatenate these two characters and
store the content in the location specified.
7. Increment the address and repeat the steps 4 to 7 until END record is encountered.
8. Jump to the address specified in END record.

PROGRAM :
#include <stdio.h>
#include <conio.h>

void main()
{
FILE *txt;
char c,programe[10],startadd[5],length[5];
char *addr;
int i,a,b;
clrscr();
txt=fopen("absinp.txt","r");
c=getc(txt);
if(c=='H')
{
fscanf(txt,"%s %d %s",programe, &addr ,length);
printf("\n\n STARTING ADDRESS =%u",addr);
}
while(c!='T')
c=getc(txt);
if(c=='T')
{
for(i=0;i<9;i++)
c=getc(txt);

c=getc(txt);
while(c!='\n')
{
28 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

a=((int)c-48);
a*=10;
c=getc(txt);
b=((int)c-48);
*addr = a+b;
printf("\n\nADDRESS = %u VALUE STORED =%d",addr,*addr);
addr++;
c=getc(txt);
}
}
getch();
}

INPUT:

H COPY 5000 1E
T 5000 1E 141033482039001036281030301015482061
E 5000

29 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

30 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 8

AIM: Write a C program to implement a relocating loader.

Source Code:

Algorithm :

1. Open the object program file and read the Header record.
2. get a free memory location from the operating system.
3. Read the next record from the file.
4. Check whether it is text record.
5. if the condition is true read a character from the text record and subtract it by 48.
6. Read the next character from the text record and concatenate these two characters and
store the content in the location specified.
7. Increment the address by 1.
8. Repeat steps 5 to 8 until ‘\n’ is encountered.
9. If step 4 is false then check for Modification record.
10. If true add the starting address of the program with the address specified in the
modification record to be altered
11. Repeat steps 3 to 10 until EOF is encountered.

PROGRAM:
//Program for relocatable loader
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
long int stoh(char *a);
char *rec, *name, *addr,*x, *length, *objcode, *rebit;
long int addr1, start, length1, rebit1, x1;
int mask,rebit2,i,j,y;
long int c,d;
FILE *fp;
clrscr();
fp= fopen("obj1.txt","r");
fscanf(fp, "%s %s %s %s\n",rec, name, addr, length);
printf("Give the starting address of the program =");
scanf("%lx",&start);
if(strcmp(rec,"H")==0)
{
printf("Program Name =%s\n",name);
addr1 = stoh(addr);

31 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

printf("The Starting Address =%lx\n", start+addr1);


length1 = stoh(length);
printf("The length of the program =%lx\n",length1);
}
printf("\noutput\n");
printf("\tAddr\tcode\n");
while(!feof(fp))
{
fscanf(fp,"%s ",rec);
if(strcmp(rec,"E")==0)
{
fscanf(fp,"%s",addr);
addr1 = stoh(addr);
printf("\nAddress of the first executable
Instruction = %lx",start+addr1);
break;
}
else if(strcmp(rec,"T")==0)
{
fscanf(fp,"%s %s %s %s\n", addr, length, rebit,
objcode);
addr1 = stoh(addr);
length1 = stoh(length);
rebit1=stoh(rebit);
rebit2=(int)rebit1;
mask = 0x0800;
for(i=0;i<2*length1;i+=6)
{
for(j=0;j<6;j++)
x[j]=objcode[i+j];

d=0x0;
for(j=0;j<6;j++)
{
c=x[j]-0x30;
if(c>0x9)
c-=0x7;
d=d*0x10+c;
}
y=rebit2&mask;
if(y == mask)
d=start+d;
mask=mask>>1;
printf("\t%lx\t%06lx\n",start+addr1,d);
addr1+=3;
}
}

32 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

}
fclose(fp);
getch();
}
long int stoh(char *a)
{
int i, l,c;
long int d=0;
l=strlen(a);
for(i=0;i<l;i++)
{
c=a[i]-0x30;
if(c>0x9)
c-=0x7;
d = d*0x10+c;
}
return d;
}

INPUT FILE(obj1.txt)

H ADDN 000000 0016


T 000000 0F E00 50000914000C02000F000005000008
E 000000

33 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 9

AIM: Write a C program to implement pass one of a direct - linking loader.

Source Code:
Algorithm:

1. Get the Program starting address from operating system.


2. set the first control section address(CSADDR) to Program starting address.
3. Read the next input record
4. Store the length of the program in a variable CSLTH.
5. Enter the control section name into ESTAB with the value CSADDR
6. Read the next input record
7. If the record type is D
8. then enter the symbol into ESTAB with value(CSADDR + indicated address)
9. Repeat step 8 until all the symbols in the record are processed.
10. Repeat steps 6 to 9 until E record type is encountered.
11. Add CSLTH to CSADDR to get the starting address for next control section.
12. Repeat steps 3 to 9 until end of file is encountered.
13. Display the External symbol table.

34 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 10

AIM: Write a C program to implement pass two of a direct - linking loader.

Source Code:

Algorithm :
1. Set CSADDR & EXECADDR to PROGADDR.
2. Read the Header record.
3. Set CSLTH to control section length.
4. Read the next input record.
5. If the scanned character is ‘T’ then
6. read a character from the text record and subtract it by 48.
Read the next character from the text record and concatenate these two characters and store
the content in the location specified.

7. Increment the address by 1.


8. Repeat step 2 until \n is reached.
9. If the scanned character is ‘M’ then
10. Add the modifying symbol value at location.
11. Repeat steps 4 to 10 until E record type is encountered.
12. If an address is specified in the End record then set EXECADDR to CSADDR+ specified
address
13. add CSLTH to CSADDR
14. Repeat steps 2 to 13 until EOF is encountered.

PROGRAM :
#include<stdio.h>
#include<conio.h>
struct ex
{
char name[20];
char symbol[20];
int address;
int length;
}extab[30];
void main()
{
FILE *in,*p;
Int e=0,a,b,I,count=0,t=0,j=0;
Char c,*addr,symbol[10],name[10],*st;
Clrscr();
In=fopen(“solo.txt”,”r”);
P=fopen(“ob.txt”,”r”);
C=fgetc(p);
While(!feof(in))
{
35 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

fscanf(in,”%s%s%d%d”,extab[e].name,extab[e].symbol,&extab[e].address,&extab[e].length);
e++;
}
st=extab[0].address;
while(c!EOF)
{
if(c==’H’)
{
fscanf(p,%s”,name);
for(j=0;j<e;j++)
{
if(strcmp(name,extab[j].name)==0)
{
t=addr=extab[j].address;
break;
}
}
}
if(c==’M’)
{
c=getc(p);
fscanf(p,”%d%s”,&a,name);
adder=t+a;
for(j=0;j<e;j++)
{
if(strcmp(name,extab[j].symbo)==0)
{
i=extab[j].address;
break;
}
}
*addr+=i/100;
addr++;
*addr+=i%100;
}
if(c==’T’)
{
for(i=0;i<=6;i++)
c=getc(p);
c=getc(p);
while(c!=’\n’)
{
a=c-48;
a*=10;
c=getc(p);
b=c-48;
*addr=a+b;

36 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

addr++;
count++;
c=getc(p);
}
}
c=getc(p);
}
for(j=0;j<count;j++)
{
printf(“Address=%u value =%d\n”,st,*st);
st++;
}
getch();
}

Input :

H proga 0 9
D lista 2 enda 3
T 01 09 000009010012180005
M 1 lista
E proga
H progb 0 18
D listb 12 endb 13
T 10 09 000009010012180005
M 7 listb
E progb

37 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

38 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 11

AIM: Write a C program to implement a simple text editor with features like insertion / deletion
of a character, word, sentence.

Source Code:

Algorithm :
Create a new file.
1. Get the file name from the user
2. open the file in write mode.
3. using the getchar function read the character from the user store it in the above file.

Editing a file
1. open the file in write mode.
2. get the index of the character to be edited
3. Read the character to be replaced.
4. Now the corresponding array index is changed with the character.

Deleting a character
1. open the file in write mode.
2. get the index of the character to be deleted
3. transfer the content of index +1 to index
4. Repeat step 3 until end of array is encountered.

Inserting a character
1. open the file in write mode.
2. get the character & the index value of the character to be inserted
3. Insertion is done by shifting process.

PROGRAM:
void flash();
void end () ;
#include<stdio.h)
#include<conio.h>
#pragma startup flash
#pragma exit end
void main()
{
FILE *file;
char fname[13J;
int select,i=0,index,j,k;
char character,imformation[150],input[3];
clrscr();
printf("1 )CREATE A NEW FILE\n2 )OPEN A FILE \n “);
printf ("Enter the option you want : ") ;
scanf ("%d" ,&select);
39 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

switch(select)
case 1:
printf(“Enter the file name: “);
scan(“%d", &select);
switch(select)
{
case 1:
printf(“enter the file name:”);
scanf(“%s”, fname);
file=fopen(fname,”w”);
printf(“Enter the content to be stored :\nl”); textcolor(BLUE+BLINK);
cprintf("\nPRESS CTRL+Z TO EXIT \n");
while((character=getchar( ))!=EOF)
putc(character,file);
fclose(file);
break;
case 2:
print("Enter the file to be open: “);
scanf("%s",fname);
do
{
printf (" \n l-_o- SHOW THE CONTENTS\n \n2 >EDIT AN CHARACTER \n \n3-- DELETE AN
CHARACTER\n\n4 -- INSERT AN CHARACTER\n\n5-- EXIT”);
printf("\n\n\nENTER YOUR CODE :");
scanf(“ %d “ ,$Se1ect ) ;
Switch(select )
{
case 1: // view the contents
file=fopen(fname, “r");
i =o;
while((character=getc(file))!=EOF)
{
information[i]=character;;
printf("%c",information[i];
i++;
}
fclose(file);
getch();
break;
case 2://modifing
file=fopen (fname, "w''');
printf("ENTER THE INDEX VALUE OF THE CHARACTER TO BE CHANGED :");
scanf ("%d ", &index) ;
printf(“ENTER THE CHARACTER TO BE CHANGED: “);
scanf(“%s",input);
for(j=0;j<i;j++)
{

40 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

if(j==index)
{
information[index]=input[0]; break;
}
}
for(j=0;j<i;j++)
putc(information[j],file);
fclose(file); break;
case 3://deleting
file=fopen(fname," w") ;
printf("Enter the index value of the character to be deleted :");
scan f ( “ %d" , & index) ;
for(k=index;k<i;k++)
information[k]=imformation[k+1];
i--;
for(j=0;j<i;j++)
putc(information[j],file);
fclose(file); break;
case 4:
f i1e=fopen (fname, "w''') ;
printf ("Enter the inde>: value of the character to be changed:");
scanf ("%d ", &dndex) ;
printf("ENTER THE CHARACTER TO BE CHANGED: ");
scanf ("%S", input);
for(k=i;k>=index;k--)
information[k]=information[k+1];
information[index]=input[0];
for(j=0;j<=i;j++)
putc(information[j],file);
fclose(file)
break ;
} / / Switch
}
while (se1ect! = 5);
break;
default:
textcolor(RED+BLINK);
cprintf(“REDQUEEN :");
printf("Error in option Terminating the program");

}
getch ( ) ;
}
void flash()
{
int i,j;
c1rscr ( ) ;

41 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

textcolor(RED);
goto(27,10);
cprjntf ("UMBERLLA RESEARCH CENTER “ );
gotoxy(27, 13);
cprintf(“RED QUEEN SOFTWARE INC");
gotoxy ( 27, 16);
cprintf("TEXT EDITOR --- TRIAL VERSION 1.0”);
gotoxy(27, 19);
textcolor(GREEN+BLINK);
cprintf("ALL RIGHTS ARE RESERVED");
textcolor(WHITE);
getch();
}
void end ()
{
int i,j;
c1rscr ( ) ;
textcolor(RED) ;
gotoxy (27, 10);
cprintf(“UMBERLLA RESEARCtt CENTER");
gotoxy(27, 13);
cpr'intf ("RED QUEEN SOFTWARE INC");
gotoxy (27, 16);
cprintf(“THANKS FOR USING THE EDITOR “);
gotoxy (27, 19);
textcolor(GREEN+BLINK);
cprintf("ALL RIGHTS ARE RESERVED");
getch();
}

42 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

43 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 12

AIM: To write a program for implementing a Lexical analyzer using LEX tool in Linux platform.

ALGORITHM:

Step1: Lex program contains three sections: definitions, rules, and user subroutines. Each
section must be separated from the others by a line containing only the delimiter, %%. The
format is as follows: definitions %% rules %% user_subroutines

Step2: In definition section, the variables make up the left column, and their definitions make
up the right column. Any C statements should be enclosed in %{..}%. Identifier is defined such
that the first letter of an identifier is alphabet and remaining letters are alphanumeric.

Step3: In rules section, the left column contains the pattern to be recognized in an input file to
yylex(). The right column contains the C program fragment executed when that pattern is
recognized. The various patterns are keywords, operators, new line character, number, string,
identifier, beginning and end of block, comment statements, preprocessor directive statements
etc.

Step4: Each pattern may have a corresponding action, that is, a fragment of C source code to
execute when the pattern is matched.

Step5: When yylex() matches a string in the input stream, it copies the matched text to an
external character array, yytext, before it executes any actions in the rules section.

Step6: In user subroutine section, main routine calls yylex(). yywrap() is used to get more input.

Step7: The lex command uses the rules and actions contained in file to generate a program,
lex.yy.c, which can be compiled with the cc command. That program can then receive input,
break the input into the logical pieces defined by the rules in file, and run program fragments
contained in the actions in file.

Source Code:

//Implementation of Lexical Analyzer using Lex tool


%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf("\n%s is a preprocessor directive",yytext);}
int |
float |
char |
double |
44 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
goto {printf("\n\t%s is a keyword",yytext);}
"/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);}
{identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}
\{ {if(!COMMENT)printf("\n BLOCK BEGINS");}
\} {if(!COMMENT)printf("BLOCK ENDS ");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);}
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
int main(int argc, char **argv)
{
FILE *file;
file=fopen("var.c","r");
if(!file)
{
printf("could not open the file");
exit(0);
}
yyin=file;
yylex();
printf("\n");
return(0);
}
int yywrap()
{
return(1);
}

45 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

INPUT:
//var.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
}
int main(int argc, char **argv)
{
FILE *file;
file=fopen("var.c","r");
if(!file)
{
printf("could not open the file");
exit(0);
}
yyin=file;
yylex();
printf("\n");
return(0);
}
int yywrap()
{
return(1);
}

46 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

47 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 13

AIM: Write a program to left factor the given grammar.

Source Code:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

//Structure Declaration

struct production
{
char lf;
char rt[10];
int prod_rear;
int fl;
};
struct production prodn[20],prodn_new[20]; //Creation of object

//Variables Declaration
int b=-1,d,f,q,n,m=0,c=0;
char terminal[20],nonterm[20],alpha[10],extra[10];
char epsilon='^';

//Beginning of Main Program


void main()
{
clrscr();

//Input of Special characters


cout<<"\nEnter the number of Special characters(except non-terminals): ";
cin>>q;
cout<<"Enter the special characters for your production: ";
for(int cnt=0;cnt<q;cnt++)
{
cin>>alpha[cnt];
}

//Input of Productions
cout<<"\nEnter the number of productions: ";
cin>>n;
for(cnt=0;cnt<=n-1;cnt++)
{
cout<<"Enter the "<< cnt+1<<" production: ";
48 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

cin>>prodn[cnt].lf;
cout<<"->";
cin>>prodn[cnt].rt;
prodn[cnt].prod_rear=strlen(prodn[cnt].rt);
prodn[cnt].fl=0;
}

//Condition for left factoring


for(int cnt1=0;cnt1<n;cnt1++)
{
for(int cnt2=cnt1+1;cnt2<n;cnt2++)
{
if(prodn[cnt1].lf==prodn[cnt2].lf)
{
cnt=0;
int p=-1;
while((prodn[cnt1].rt[cnt]!='\0')&&(prodn[cnt2].rt[cnt]!='\0'))
{
if(prodn[cnt1].rt[cnt]==prodn[cnt2].rt[cnt])
{
extra[++p]=prodn[cnt1].rt[cnt];
prodn[cnt1].fl=1;
prodn[cnt2].fl=1;
}
else
{
if(p==-1)
break;
else
{
int h=0,u=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
prodn_new[++b].lf=alpha[c];
for(g=cnt;g<=prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[u++]=prodn[cnt1].rt[g];
m=1;
break;
}
}
cnt++;
}
if((prodn[cnt1].rt[cnt]==0)&&(m==0))

49 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

{
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
}
if((prodn[cnt2].rt[cnt]==0)&&(m==0))
{
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt1].rt[g];
}
c++;
m=0;
}
}
}

//Display of Output
cout<<"\n\n********************************";
cout<<"\n AFTER LEFT FACTORING ";
cout<<"\n********************************";
cout<<endl;
for(int cnt3=0;cnt3<=b;cnt3++)
{
cout<<"Production "<<cnt3+1<<" is: ";
cout<<prodn_new[cnt3].lf;
cout<<"->";
cout<<prodn_new[cnt3].rt;
cout<<endl<<endl;
}

for(int cnt4=0;cnt4<n;cnt4++)
{
if(prodn[cnt4].fl==0)
{

50 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

cout<<"Production "<<cnt3++<<" is: ";


cout<<prodn[cnt4].lf;
cout<<"->";
cout<<prodn[cnt4].rt;
cout<<endl<<endl;
}
}
getche();
} //end of main program

51 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 14

AIM: Write a program to remove the Left Recursion from a given grammar.

Source Code:

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char s[30],temp[20],LHS[30],fp[30],sp[30],a[30];
int i,start,j,k=-1,flag1=0,flag2=0;
clrscr();
printf("enter production :");
gets(s);
start=2;
LHS[0]=s[0];
LHS[1]='\0';
strcpy(fp,LHS);
strcat(fp,"=");
strcpy(sp,LHS);
strcat(sp,"'=");
r:
j=0;
for(i=start;s[i]!='|' && s[i]!='\0';i++)
{
temp[j]=s[i];
j++;
}
temp[j]='\0';
start=i+1;
if(temp[0]==LHS[0])
{
if(flag1==1)
{
strcat(sp,"|");
}
strcat(sp,a+1);
strcat(sp,LHS);
strcat(sp,"'");
flag1=1;
}
else
{
if(flag2==1)
{
52 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

strcat(fp,"|");
}
strcat(fp,a);
strcat(fp,LHS);
strcat(fp,"'");
flag2=1;
}
if(start<strlen(s))
{
goto r;
}
strcat(sp,"|null");
printf("\n%s",fp);
printf("\n%s",sp);
getch();
}

53 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 15

AIM: Implement Recursive Descendent Parsing for the given Grammar.


E -> T + E / T
T -> F * T / F
F -> ( E ) / i

Source Code:

#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[]) //Pushes The Set Of Characters on to the Stack
{
for(i=0;k[i]!='\0';i++)
{
if(top<9)
a[++top]=k[i];
}
}
char TOS() //Returns TOP of the Stack
{
return a[top];
}
void pop() //Pops 1 element from the Stack
{
if(top>=0)
a[top--]='\0';
}
void display() //Displays Elements Of Stack
{
for(i=0;i<=top;i++)
printf("%c",a[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
int l;
printf("\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
54 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

}
char* stack(){
return a;
}
int main()
{
char ip[20],r[20],st,an;
int ir,ic,j=0,k;
char t[5][6][10]={"$","$","TH","$","TH","$",
"+TH","$","e","e","$","e",
"$","$","FU","$","FU","$",
"e","*FU","e","e","$","e",
"$","$","(E)","$","i","$"};
clrscr();
printf("\nEnter any String(Append with $)");
gets(ip);
printf("Stack\tInput\tOutput\n\n");
push("$E");
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
if(TOS()==an)
{
pop();
display();
display1(ip,j+1);
printf("\tPOP\n");
j++;
}
an=ip[j];
st=TOS();
if(st=='E')ir=0;
else if(st=='H')ir=1;
else if(st=='T')ir=2;
else if(st=='U')ir=3;
else if(st=='F')ir=4;
else {
error();
break;
}
if(an=='+')ic=0;
else if(an=='*')ic=1;
else if(an=='(')ic=2;
else if(an==')')ic=3;
else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
else if(an=='$')ic=5;

55 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

strcpy(r,strrev(t[ir][ic]));
strrev(t[ir][ic]);
pop();
push(r);
if(TOS()=='e')
{
pop();
display();
display1(ip,j);
printf("\t%c->%c\n",st,238);
}
else{
display();
display1(ip,j);
printf("\t%c->%s\n",st,t[ir][ic]);
}
if(TOS()=='$'&&an=='$')
break;
if(TOS()=='$'){
error();
break;
}
}
k=strcmp(stack(),"$");
if(k==0 && i==strlen(ip))
printf("\n Given String is accepted");
else
printf("\n Given String is not accepted");
return 0;
}

56 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

57 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 16

AIM: Implement Predictive Parser for the given grammar.


E -> T + E / T
T -> F * T / F
F -> ( E ) / i

Source Code:

#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[]) //Pushes The Set Of Characters on to the Stack
{
for(i=0;k[i]!='\0';i++)
{
if(top<9)
a[++top]=k[i];
}
}
char TOS() //Returns TOP of the Stack
{
return a[top];
}
void pop() //Pops 1 element from the Stack
{
if(top>=0)
a[top--]='\0';
}
void display() //Displays Elements Of Stack
{
for(i=0;i<=top;i++)
printf("%c",a[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
int l;
printf("\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
}
char* stack(){
58 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

return a;
}
int main()
{
char ip[20],r[20],st,an;
int ir,ic,j=0,k;
char t[5][6][10]={"$","$","TH","$","TH","$",
"+TH","$","e","e","$","e",
"$","$","FU","$","FU","$",
"e","*FU","e","e","$","e",
"$","$","(E)","$","i","$"};
clrscr();
printf("\nEnter any String(Append with $)");
gets(ip);
printf("Stack\tInput\tOutput\n\n");
push("$E");
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
if(TOS()==an)
{
pop();
display();
display1(ip,j+1);
printf("\tPOP\n");
j++;
}
an=ip[j];
st=TOS();
if(st=='E')ir=0;
else if(st=='H')ir=1;
else if(st=='T')ir=2;
else if(st=='U')ir=3;
else if(st=='F')ir=4;
else {
error();
break;
}
if(an=='+')ic=0;
else if(an=='*')ic=1;
else if(an=='(')ic=2;
else if(an==')')ic=3;
else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
else if(an=='$')ic=5;
strcpy(r,strrev(t[ir][ic]));
strrev(t[ir][ic]);

59 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

pop();
push(r);
if(TOS()=='e')
{
pop();
display();
display1(ip,j);
printf("\t%c->%c\n",st,238);
}
else{
display();
display1(ip,j);
printf("\t%c->%s\n",st,t[ir][ic]);
}
if(TOS()=='$'&&an=='$')
break;
if(TOS()=='$'){
error();
break;
}
}
k=strcmp(stack(),"$");
if(k==0 && i==strlen(ip))
printf("\n Given String is accepted");
else
printf("\n Given String is not accepted");
return 0;
}

60 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

61 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 17

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

Source Code:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *f;
char ch;
int line=0,word=0;
clrscr();
f=fopen("student","w+");
printf("Enter text press ctrol+z to quit\n");
do
{
ch=getchar();
putc(ch,f);
}
while(ch!=EOF);

printf("Total byts of files =%d ",ftell(f));


rewind(f);
while((ch=getc(f))!=EOF)
{
if(ch=='\n')
line++;
if(isspace(ch)||ch=='\t'||ch=='\n')
word++;
putchar(ch);
}
fclose(f);
printf("\n no of line=%d\n",line);
printf("no of word=%d\n",word);
getch();
}

62 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

63 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 18

AIM: Write a SAL program in text file and generate SYMTAB and LITTAB Use macro features of C
language

Source Code:

#include<stdio.h>
#include<conio.h>
#define square(x) x*x
void main()
{
int n,s;
printf("Enter a number : ");
scanf("%d",&n);
s=square(n);
printf("Square of given number = %d",s);
getch();
}

64 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 19
AIM: Write a program which generates Quadruple Table for the given postfix String

Source Code:

#include<stdio.h>
#include<string.h>

main()
{

char line[20];
int s[20];
int t=1;

int i=0;
printf("Enter 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]);
(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++)
{

65 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

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

66 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

67 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Practical 20
AIM: Write a YACC program with error recovery.

Source Code:

// desk calculator with error recovery


%{
#include<ctype.h>
#include<stdio.h>
#define yystype double /*double type for stack */
%}
% token NUMBER
% left '+' , '-'
% left '*' , '/'
% right UMINUS
%%
lines : lines expr '\n' { printf("%g \n",$2); }
| lines \n
| /* empty */
| error '\n' { yyerror("Re-enter last line");
yyerror;
}

expr : expr '+' expr { $$ = $1 +$3; }


| expr '-' expr { $$ =$1 - $3; }
| expr '*' expr { $$ =$1 * $3; }
| expr '/' expr { $$ =$1 / $3; }
| '(' expr ')' { $$ =$2; }
| '-' expr % prec UMINUS {$$ = -$2: }
| NUMBWER {;}
%%
yylex()
{
int c;
c=getch();
if(isdigit())
{
yylval = c-'0';
return DIGIT;
}
return c;
}
main()
{
yyparse();
}

68 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

yyerror (char *s)


{
printf("%s",s);
}

output: 2 3 +

is "Re-enter last line"

69 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH
2150708 System Programming Lab Manual 2019

Conclusion

70 BE 5th IT GECBVN
Guided By: PROF BHARAT VAINSH

You might also like