Cs1307 - System Software Lab[1]

85
SYSTEM SOFTWARE LABORATORY LIST OF EXPERIMENTS 1. Design of an Editor: Design of a Line or Screen Editor using C Language. 2. Design of an Assembler. 3. Simulation of Loaders. 4. Interprocess Communication. 5. Token Separation and Symbol Table Manipulation. 6. Construction of Parsing Table. 2

Transcript of Cs1307 - System Software Lab[1]

Page 1: Cs1307 - System Software Lab[1]

SYSTEM SOFTWARE LABORATORY

LIST OF EXPERIMENTS

1. Design of an Editor: Design of a Line or Screen Editor using C Language.

2. Design of an Assembler.

3. Simulation of Loaders.

4. Interprocess Communication.

5. Token Separation and Symbol Table Manipulation.

6. Construction of Parsing Table.

2

Page 2: Cs1307 - System Software Lab[1]

INDEX

EXP.NO.

NAME OF EXPERIMENTPAGE

NO.

1 TEXT EDITOR 03

ASSEMBLER

2a ONE PASS ASSEMBLER 10

2b PASS ONE OF A TWO PASS ASSEMBLER 16

2c PASS TWO OF A TWO PASS ASSEMBLER 20

LOADER

3a RELOCATION LOADER 25

3b ABSOLUTE LOADER 30

3c PASS ONE OF A DIRECT LINKING LOADER 34

3d PASS TWO OF A DIRECT LINKING LOADER 39

INTERPROCESS COMMUNICATION

4a ONE PASS MACROPROCESSOR 45

4b TWO PASS MACROPROCESSOR 49

TOKENS AND SYMBOL TABLE

5a TOKEN SEPERATION 55

5b SYMBOL TABLE MANIPULATION 60

6 SHIFT REDUCE PARSING TABLE CONSTRUCTION 66

3

Page 3: Cs1307 - System Software Lab[1]

EX.NO-1 TEXT EDITOR

AIM:

To write a C program to create a text editor.

ALGORITHM:

STEP 1: Provide blank screen for the user to type the document

STEP 2: Instruct the user to enter the text using the editor

STEP 3: Display the entered character on the screen

STEP 4: Characters displayed on the screen are stored in a character array

STEP 5: Specify if the content has to be saved or not.

STEP 6: If SAVE option is selected

STEP 6a: Get the data file name from the user

STEP 6b: Open the file in write mode

STEP 6c: Move the content of the character array to the file and close the file

STEP 7: If OPEN option is selected

STEP 7a: Open the data file in the read mode

STEP 7b: Read one character at a time from the file and move it to the

character array

STEP 7c: Repeat the above steps until the end of file is reached

STEP 8: Display the characters from the character array on the screen

STEP 9: Finally close the text editor and exit.

4

Page 4: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>FILE *fp1;char buff[2000],filename[12];int cnt;void newfile(){char c,ch;clrscr();printf("\n *************************");printf("\n NEW FILE CREATION");printf("\n *************************");printf("\n TYPE TEXT \n TO TERMINATE PRESS TAB+ENTER KEYS");cnt=0;while((c=getchar())!='\t')buff[cnt++]=c;buff[cnt]='\0';printf("\n *****WARNING*****");printf("\n\t FILE IS NOT SAVED,DO YOU WANT TO SAVE?(Y/N)");ch=getch();if(ch=='Y'||ch=='y'){printf("\n ENTER THE NAME FOR FILE:");loop:scanf("%s",filename);fp1=fopen(filename,"r");if(fp1==NULL){fp1=fopen(filename,"w");fprintf(fp1,"%s",buff);}else{printf("\n FILE ALREADY EXISTS,DO YOU WANT TO OVERWRITE?(y/n)");ch=getch();if(ch=='Y'||ch=='y'){fp1=fopen(filename,"w");fprintf(fp1,"%s",buff);}else

5

Page 5: Cs1307 - System Software Lab[1]

{printf("\n ENTER THE NEW FILE NAME:");goto loop;}}fclose(fp1);}}void openfile(){char c;clrscr();printf("\n ***********************");printf("\n OPEN AND DISPLAY FILE");printf("\n ***********************");printf("\n ENTER THE FILENAME:");loop:scanf("%s",filename);fp1=fopen(filename,"r");if(fp1==NULL){printf("\n FILE DOES NOT EXISTS");printf("\n ENTER THE CORRECT FILENAME:");goto loop;}else{printf("\n********FILE CONTENT******** \n");while(!feof(fp1)){c=getc(fp1);putchar(c);}getch();}fclose(fp1);}void main(){char ch;while(1){clrscr();printf("\n *******************");printf("\n TEXT EDITOR ");printf("\n ********************");

6

Page 6: Cs1307 - System Software Lab[1]

printf("\n\tNEW(N)\t\tOPEN(O)\t\tEXIT(E)");printf("\n\n ENTER THE OPTION YOU NEED TO PERFORM:\n");ch=getch();switch(ch){case'N':case'n':newfile();break;case'O':case'o':openfile();break;case'E':case'e':exit(1);break;}}}

7

Page 7: Cs1307 - System Software Lab[1]

OUTPUT:

******************* TEXT EDITOR ******************** NEW(N) OPEN(O) EXIT(E)

ENTER THE OPTION YOU NEED TO PERFORM: N

************************* NEW FILE CREATION ************************* TYPE TEXT TO TERMINATE PRESS TAB+ENTER KEYSHITHIS IS A SAMPLE FILE.

*****WARNING***** FILE IS NOT SAVED,DO YOU WANT TO SAVE?(Y/N): yENTER THE NAME FOR FILE:samp

******************* TEXT EDITOR ******************** NEW(N) OPEN(O) EXIT(E)

ENTER THE OPTION YOU NEED TO PERFORM: O

*********************** OPEN AND DISPLAY FILE *********************** ENTER THE FILE NAME: samp********FILE CONTENT********HITHIS IS A SAMPLE FILE.  ******************* TEXT EDITOR ******************** NEW(N) OPEN(O) EXIT(E)

8

Page 8: Cs1307 - System Software Lab[1]

ENTER THE OPTION YOU NEED TO PERFORM: E

RESULT:

Thus the program to create a text editor was executed successfully.

9

Page 9: Cs1307 - System Software Lab[1]

EX.NO-2a ONE PASS ASSEMBLER

AIM:

To write a C program to implement a one pass assembler.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Initialize all the variables.

STEP 3: Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode.

STEP 4: Read the source program.

STEP 5: If the op code read in the source program is START, the line is written to the

output file.

STEP 6: The source program is read line by line until the reach of opcode END.

STEP 7: Check whether the op code read is present in the operation code table.

STEP 8: If the op code is present, then the address is incremented by 3 and if the

operand’s address is known then load it.

STEP 9: Else maintain the address in the linked list for which the operand is required.

STEP 10: If the op code read is WORD, the location counter is incremented by 3.

STEP 11: If the op code read is RESW, the operand value is multiplied by 3 and then the

location counter is incremented.

STEP 12: If the op code read is RESB, the location counter value is incremented by

operand value.

STEP 13: If the op code read is BYTE, the location counter is auto incremented.

STEP 14: Write the object code with address to the output file.

STEP 15: Finally terminate the one pass assembler.

10

Page 10: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#define MAX 20

struct input{char opcode[10],mnemonic[10],operand[10],label[10];int loc;};struct input table[MAX];

struct symtab{char sym[10];int f,val,ref;};struct symtab symtbl[MAX];

void main(){int f1,i=1,j=1,flag,locctr,x;char add[10],code[10],mnemcode[10];FILE *fp1,*fp2,*fp3;clrscr();fp1=fopen(“input.dat”,”r”);fp2=fopen(“optab.dat”,”r”);fp3=fopen(“spout.dat”,”w”);fscanf(fp1,”%s%s%s”,table[i].label,table[i].opcode,table[i].operand);if(strcmp(table[i].opcode,”START”)==0){locctr=atoi(table[i].operand);i++;fscanf(fp1,”%s%s%s”,table[1].label,table[i].opcode,table[i].operand);}elselocctr=0;while(strcmp(table[i].opcode,”END”)!=0){if(strcmp(table[i].label,”**”)!=0){for(x=1;x<j;x++){

11

Page 11: Cs1307 - System Software Lab[1]

f1=0;if((strcmp(symtbl[x].sym,table[i].label)==0)&&(symtbl[x].f==1)){symtbl[x].val=locctr;symtbl[x].f=0;table[symtbl[x].ref].loc=locctr;f1=1;break;}}if(f1==0){strcpy(symtbl[j].sym,table[i].label);symtbl[j].val=locctr;symtbl[j].f=0;j++;}}rewind(fp2);fscanf(fp2,”%s%s”,code,mnemcode);while(strcmp(code,”END”)!=0){if(strcmp(table[i].opcode,code)==0){strcpy(table[i].mnemonic,mnemcode);locctr+=3;for(x=1;x<=j;x++){flag=0;if(strcmp(table[i].operand,symtbl[x].sym)==0){flag=1;if(symtbl[x].f==0)table[i].loc=symtbl[x].val;break;}}if(flag!=1){strcpy(symtbl[j].sym,table[i].operand);symtbl[j].f=1;symtbl[j].ref=I;j++;}}fscanf(fp2,”%s%s”,code,mnemcode);

12

Page 12: Cs1307 - System Software Lab[1]

}if(strcmp(table[i].opcode,”WORD”)==0){locctr+=3;strcpy(table[i].mnemonic,’\0’);table[i].loc=atoi(table[i].operand);}else if(strcmp(table[i].opcode,”RESW”)==0){locctr+=(3*(atoi(table[i].operand)));strcpy(table[i].mnemonic,’\0’);table[i].loc=atoi(‘\0’);}else if(strcmp(table[i].opcode,”RESB”)==0){locctr+=(atoi(table[i].operand));strcpy(table[i].mnemonic,’\0’);table[i].loc=atoi(‘\0’);}else if(strcmp(table[i].opcode,”BYTE”)==0){++locctr;if((table[i].operand[0]==’C’)||(table[i].operand[0]==’X’))table[i].loc=(int)table[i].operand[2];elsetable[i].loc=locctr;}i++;fscanf(fp1,”%s%s%s”,table[i].label,table[i].opcode,table[i].operand);}for(x=1;x<=I;x++)fprintf(fp3,”%s\t%s\t%s\t%s\n”,table[x].label,table[x].opcode,table[x].operand,strcat(table[x].mnemonic,itoa(table[x].loc,add,10)));for(x=1;x<=j;x++)printf(“%s\t%d\n”,symtbl[x].sym,symtbl[x].val);getch();}

13

Page 13: Cs1307 - System Software Lab[1]

INPUT FILES

“input.dat”

** START 2000** LDA FIVE** ADD ALPHA** SUB CHARZ** STA C1

ALPHA RESW 1FIVE RESW 1CHARZ WORD 1C1 RESW 1

** END

“optab.dat”

ADD 18ADDR 90SUB 1CSUBR 94MUL 20MULR 98DIV 24DIVR 9CLDA 00LDB 68LDX 04LDCH 50STA 0CSTB 78STX 10STCH 54TIX 2CJ 3CJSUB 48RSUB 4CJEQ 30JLT 38JGT 34START *END *

14

Page 14: Cs1307 - System Software Lab[1]

OUTPUT FILES

“spout.dat”

** START 2000 0LDA FIVE 002015

** ADD ALPHA 182012** SUB CHARZ 1C2018** STA C1 0C2021

ALPHA RESW 1 0FIVE RESW 1 0CHARZ WORD 1 1C1 RESW 1 0

** END 0

RESULT:

Thus the program to implement a one pass assembler was executed successfully.

15

Page 15: Cs1307 - System Software Lab[1]

EX.NO-2b PASS ONE OF A TWO PASS ASSEMBLER

AIM:

To write a C program to implement PASS ONE of a two pass assembler.

ALGORITHM:

STEP 1: Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode.

STEP 2: Read the source program.

STEP 3: If the op code read in the source program is START, the variable location

counter is initialized with the operand value.

STEP 4: Else the location counter is initialized to 0.

STEP 5: The source program is read line by line until the reach of opcode END.

STEP 6: Check whether the op code read is present in the operation code table.

STEP 7: If the op code is present, then the location counter is incremented by 3.

STEP 8: If the op code read is WORD, the location counter is incremented by3.

STEP 9: If the op code read is RESW, the operand value is multiplied by 3 and then

the location counter is incremented.

STEP 10: If the op code read is RESB, the location counter value is incremented by

operand value.

STEP 11: If the op code read is BYTE, the location counter is auto incremented.

STEP 12: The length of the source program is found using the location counter value.

16

Page 16: Cs1307 - System Software Lab[1]

PROGRAM:

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

void main(){char opcode[10],mnemonic[10],operand[10],label[10],code[10];int locctr,start,length;FILE *fp1,*fp2,*fp3,*fp4;clrscr();fp1=fopen(“INPUT.DAT”,”r”);fp2=fopen(“SYMTAB.DAT”,”w”);fp3=fopen(“OUTPUT.DAT”,”w”);fp4=fopen(“OPTAB.DAT”,”r”);fscanf(fp1,”%s%s%s”,label,opcode,operand);if(strcmp(opcode,”START”)==0){start=atoi(operand);locctr=start;fprintf(fp3,”\t%s\t%s\t%s\n”,label,opcode,operand);fscanf(fp1,”%s%s%s”,label,opcode,operand);}elselocctr=0;while(strcmp(opcode,”END”)!=0){fprintf(fp3,”%d\t”,locctr);if(strcmp(label,”**”)!=0)fprintf(fp2,”%s\t%d\n”,label,locctr);rewind(fp4);fscanf(fp4,”%s”,mnemonic);while(strcmp(mnemonic,”END”)!=0){if(strcmp(opcode,mnemonic)==0){locctr+=3;break;}fscanf(fp4,”%s”,mnemonic);}if(strcmp(opcode,”WORD”)==0)locctr+=3;

17

Page 17: Cs1307 - System Software Lab[1]

else if(strcmp(opcode,”RESW”)==0)locctr+=(3*(atoi(operand)));else if(strcmp(opcode,”RESB”)==0)locctr+=(atoi(operand));else if(strcmp(opcode,”BYTE”)==0)++locctr;fprintf(fp3,”%s\t%s\t%s\n”,label,opcode,operand);fscanf(fp1,”%s%s%s”,label,opcode,operand);}fprintf(fp3,”%d\t%s\t%s\t%s\n”,locctr,label,opcode,operand);length=locctr-start;printf(“\n Length of the program is %d”,length);fclose(fp1);fclose(fp2);fclose(fp3);fclose(fp4);getch();}

18

Page 18: Cs1307 - System Software Lab[1]

INPUT FILES:

“INPUT.DAT”

MAIN START 5000BEGIN LDA NUM1** STA NUM2** STCH CHAR1** STCH CHAR2NUM1 WORD 5NUM2 RESW 1CHAR1 BYTE 5CHAR2 RESB 1** END BEGIN

“SYMBOL.DAT”

BEGIN 5000NUM1 5012NUM2 5015CHAR1 5018CHAR2 5019

“OUT.DAT”

MAIN START 5000BEGIN LDA NUM1** STA NUM2** STCH CHAR1** STCH CHAR2NUM1 WORD 5NUM2 RESW 1CHAR1 BYTE 5CHAR2 RESB 1

OUTPUT

Length of the program is 20

RESULT:

Thus the program to implement one pass of a two pass assembler was executed

successfully.

19

Page 19: Cs1307 - System Software Lab[1]

EX.NO-2c PASS TWO OF TWO PASS ASSEMBLER

AIM:

To write a C program to implement PASS TWO of a two pass assembler.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Initialize all the variables.

STEP 3: Open the output file of pass one of two pass assembler.

STEP 4: Read the content of the file.

STEP 5: If the opcode is START assign the program address as specified in it.

STEP 6: Read the next line of the program.

STEP 7: According to the opcode execute the corresponding instruction and refer

SYMBOL TABLE for the address of the operand to write to the file.

STEP 8: If op code is BYTE then write address, label, opcode, operand into a file

STEP 9: Else if op code is WORD then write address, label, op code, operand into a file

STEP 10: Else if op code is “RESB” or “RESW” write address, label, op code,

operand into a file.

STEP 11: If it is not match anything then write address, label, op code, operand into a file

STEP 12: Finally terminate pass two of two pass assembler.

20

Page 20: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>void main(){char symbol[20],opcode[20],mnemonic[20],code[20],operand[20],character,add[20],objectcode[20],label[20];int locctr,flag,flag1,loc;FILE *fp1,*fp2,*fp3,*fp4;clrscr();fp1=fopen(“OUT1.DAT”,”r”);fp2=fopen(“TWOOUT1.DAT”,”w”);fp3=fopen(“OPTAB1.DAT”,”r”);fp4=fopen(“SYMTAB1.DAT”,”r”);fscanf(fp1,”%s%s%s”,label,opcode,operand);if(strcmp(opcode,”START”)==0){fprintf(fp2,”\t%s\t%s\t%s\n”,label,opcode,operand);fscanf(fp1,”%d%s%s%s”,&locctr,label,opcode,operand);}while(strcmp(opcode,”END”)!=0){flag=0;rewind(fp3);fscanf(fp3,”%s%s”,mnemonic,code);while(strcmp(mnemonic,”END”)!=0){if((strcmp(opcode,mnemonic)==0)&&(strcmp(code,”*”)!=0)){flag=1;break;}fscanf(fp3,”%s%s”,mnemonic,code);}if(flag==1){flag1=0;rewind(fp4);while(!feof(fp4)){

21

Page 21: Cs1307 - System Software Lab[1]

fscanf(fp4,”%s%d”,symbol,&loc);if(strcmp(symbol,operand)==0){flag1=1;break;}}if(flag1==1){itoa(loc,add,10);strcpy(objectcode,strcat(code,add));}}else if(strcmp(opcode,”BYTE”)==0||strcmp(opcode,”WORD”)==0){if(operand[0]==’C’||operand[0]==’X’){character=operand[2];itoa(character,add,16);strcpy(objectcode,add);}else{itoa(atoi(operand),add,10);strcpy(objectcode,add);}}elsestrcpy(objectcode,”\0”);fprintf(fp2,”%d\t%s\t%s\t%s\t%s\n”,locctr,label,opcode,operand,objectcode);fscanf(fp1,”%d%s%s%s”,&locctr,label,opcode,operand);}fprintf(fp2,”\t%s\t%s\t%s\n”,label,opcode,operand);printf(“\n Output”);fclose(fp1);fclose(fp2);fclose(fp3);fclose(fp4);getch();}

22

Page 22: Cs1307 - System Software Lab[1]

INPUT FILES:

“INPUT.DAT”

** START 2000** LDA FIVE** ADD ALPHA** SUB CHARZ** STA C1

ALPHA RESW 1FIVE RESW 1CHARZ WORD 1C1 RESW 1

** END

“OPTAB.DAT”

ADD 18ADDR 90SUB 1CSUBR 94MUL 20MULR 98DIV 24DIVR 9CLDA 00LDB 68LDX 04LDCH 50STA 0CSTB 78STX 10STCH 54

“OUT.DAT”

** START 2000 2000 ** LDA FIVE 2003 ** STA ALPHA 2006 ** LDCH CHARZ 2009 ** STCH C12012 ALPHA RESW 12015 FIVE WORD 52018 CHARZ BYTE C’Z’

23

Page 23: Cs1307 - System Software Lab[1]

2019 C1 RESB 12020 ** END **“SYMTAB.DAT”

ALPHA 2012FIVE 2015CHARZ 2018C1 2021

OUTPUT FILE:

“TWOOUT.DAT”

** START 20002000 ** LDA FIVE 0020152003 ** STA ALPHA 0C20122006 ** LDCH CHARZ 5020182009 ** STCH C1 5420212012 ALPHA RESW 12015 FIVE WORD 5 52018 CHARZ BYTE C’Z’ 5a2019 C1 RESB 1

** END **

RESULT:

Thus the program to implement two pass of a two pass assembler was executed

successfully.

24

Page 24: Cs1307 - System Software Lab[1]

EX.NO-3a RELOCATION LOADER

AIM:

To write a C program to implement relocation loader.

ALGORITHM:

STEP 1: Start the program

STEP 2: Include the necessary header file and variable

STEP 3: Open the two file for read and write.

STEP 4: Read the content

STEP 5: Using while loop perform the loop until character is not equal to E

STEP 6: If the character is H, Get the variable add, length, and input

STEP 7: Else if the character is T,

STEP 7a: Get the variable address and bitmask and perform for loop for

starting zero to up to length.

STEP 7b: Get the op code ,address and assign relocation bit to bitmask

STEP 8: If relocation bit is zero Then Assign actual address to address; else Add the

address and star value.

STEP 9: Finally terminate the program.

25

Page 25: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>struct object_code{int locctr;char add[10];}obcode[300];void main(){char input[100][16],output[100][16],binary[20],address[20],stloc[10];int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;FILE *fp1,*fp2;clrscr();fp1=fopen(“rin.dat”,”r”);fp2=fopen(“rout.dat”,”w”);printf(“Enter the location where the program has to be loaded:”);scanf(“%s”,stloc);start=atoi(stloc);location=start;tloc=start;fscanf(fp1,”%s”,input[i]);while(strcmp(input[i],”T”)!=0){strcpy(output[i],input[i]);i++;fscanf(fp1,”%s”,input[i]);strcpy(output[i],input[i]);}itoa(start,output[2],10);while(strcmp(input[i],”E”)!=0){strcpy(output[i],input[i]);if(strcmp(input[i],”T”)==0){for(j=0;j<3;j++){i++;fscanf(fp1,”%s”,input[i]);strcpy(output[i],input[i]);

26

Page 26: Cs1307 - System Software Lab[1]

}bitmask=atoi(output[i]);itoa(bitmask,binary,2);strcpy(output[i],NULL);textloc=atoi(output[i-2]);textloc=textloc+start;itoa(textloc,output[i-2],10);for(n=0;n<(textloc-(tloc+tlen));n++){strcpy(obcode[inc].add,”XX”);obcode[inc++].locctr=location++;}tlen=atoi(output[i-1]);tloc=textloc;k=0;}else{if(binary[k]==’1’){num=0;len=strlen(output[i]);strcpy(address,NULL);for(j=2;j<len;j++){address[num]=output[i][j];output[i][j]=’\0’;num++;}loc=atoi(address);loc=loc+start;itoa(loc,address,10);strcat(output[i],address);}k++;len=strlen(output[i]);num=0;for(n=0;n<len;n++){obcode[inc].add[num++]=output[i][n];if(num>1){obcode[inc++].locctr=location++;num=0;}}

27

Page 27: Cs1307 - System Software Lab[1]

}i++;fscanf(fp1,”%s”,input[i]);}strcpy(output[i],input[i]);i++;fscanf(fp1,”%s”,input[i]);loc=atoi(input[i]);loc=loc+start;strcpy(output[i],itoa(loc,address,10));count=0;i=0;n=0;fprintf(fp2,”%d\t”,obcode[n].locctr);for(n=0;n<inc;n++){fprintf(fp2,”%s”,obcode[n].add);i++;if(i>3){fprintf(fp2,”\t”);i=0;count++;}if(count>3){fprintf(fp2,”\n%d\t”,obcode[n+1].locctr);count=0;}}getch();}

28

Page 28: Cs1307 - System Software Lab[1]

INPUT FILE

“rin.dat”

H COPY 000000 001077T 000000 ID 17202D 69202D 4B101036 032026 290000 332007 4B10105D 3F2FEC 032010T 0000ID 13 0F20160 010003 0F200D 4B10105D 3E2003 454F46E 000000

OUTPUT

Enter the location where the program has to be loaded: 2000

OUTPUT FILE

2000 6922024B 10103603 20262900 003320072016 4B12103F 20020320 10010003 0F200D4B2032 10105D3E 2003454F 46

RESULT:

Thus the program to implement relocation loader was executed successfully.

29

Page 29: Cs1307 - System Software Lab[1]

EX.NO-3b ABSOLUTE LOADER

AIM:

To write a C program to implement absolute loader.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Assign the required variable

STEP 3: Open the files for read and write

STEP 4: Read the content

STEP 5: Using while loop perform the loop until character is not equal to E

STEP 6: Then compare the character is equal to H

STEP 7: If H then read start from input file and get length, and input

STEP 8: Else if the character is T Then Perform the fprintf in fp1 for input file.

STEP 9: Else if it is not H or T Then Perform the fprintf in fp2 for output file for,

STEP 10: Finally terminate the program.

30

Page 30: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct object_code{int locctr;char byte[5];};struct object_code code[200];void main(){FILE *fp1,*fp2;char input[15];int i,len,n=0,count=0,inc=0,textloc,tlen,tloc=0,num=0,loc=0;clrscr();fp1=fopen("lout.dat","r");fp2=fopen("loadout1.dat","w");rewind(fp1);rewind(fp2);fscanf(fp1,"%s",input);if(strcmp(input,"H")==0){for(i=0;i<4;i++){if(i==1)fscanf(fp1,"%x",&loc);elsefscanf(fp1,"%s",input);}}tloc=loc;while(strcmp(input,"E")!=0){if(strcmp(input,"T")==0){fscanf(fp1,"%x",&textloc);for(i=0;i<(textloc-(tloc+tlen));i++){strcpy(code[inc].byte,"xx");code[inc++].locctr=loc++;}

31

Page 31: Cs1307 - System Software Lab[1]

fscanf(fp1,"%x",tlen);tloc=textloc;}else{len=strlen(input);for(i=0;i<len;i++){code[inc].byte[num++]=input[i];if(num>1){code[inc].locctr=loc;loc++;inc++;num=0;}}}fscanf(fp1,"%s",input);}n=0;i=0;count=0;fprintf(fp2,"%x\t",code[i].locctr);for(i=0;i<inc;i++){fprintf(fp2,"%s",code[i].byte);n++;if(n>3){fprintf(fp2,"\t");n=0;count++;}if(count>3){fprintf(fp2,"\n%x\t",code[i+1].locctr);count=0;}}getch();}

32

Page 32: Cs1307 - System Software Lab[1]

INPUT FILE:

“lout.dat”

H COPY 002000 00107AT 002000 1E 142033 4830309 102036 282030

303015 483061 3C2003 00202A 0C203900202D

T 00201E 15 2C2036 483061 182033 4C0000454F46 200003 100000

T 002039 1E 242030 302030 E0305D 30303F08305D 303057 53A039 2C305E 38303F

T 002057 0A 102036 4C0000 F1 201000T 002071 19 342030 E03079 303064 4FA0039

DC3079 2C2036 383064 4C0000 15E 002000

OUTPUT FILE:

“loadout1.dat”

2000 14203348 30309102 03628203 030301542010 830613C2 00300202 A0C20390 0202D2C22020 03648306 11820334 C0000454 F46200002030 31000002 42030302 030E0305 D30303F02040 8305D303 05753A03 92C305E3 8303F1022050 0364C000 0F120100 0342030E 030793032060 0644FA00 39DC3079 2C203638 30644C002070 0015

RESULT:

33

Page 33: Cs1307 - System Software Lab[1]

Thus the program to implement absolute loader was executed successfully.

EX.NO-3c PASS ONE OF A DIRECT LINKING LOADER

AIM:

To write a C program to implement pass one of a direct linking loader.

ALGORITHM:

STEP 1: Enter the location where the program has to be loaded.

STEP 2: Assign the address got from the user as the first control section address.

STEP 3: Read the header record of the control Section.

STEP 4: From the details of the header read store the control section length in variable.

STEP 5: Enter the control Section name with its address into the external symbol table.

STEP 6: For each symbol in the subsequent ‘D’ records the symbol must be entered into

the symbol table along with its address, added along with the corresponding

control section address until an end record is reached.

STEP 7: Assign the starting address of next control section as the address of the current

control section plus the length of the control section.

STEP 8: Repeat the process from step 6 to 8 until there are no more input.

34

Page 34: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct estab{char csect[10];char sym_name[10];int add,len;}table[10];void main(){char in[10];int i,cnt=0,start,len,loc;FILE *fp1,*fp2;clrscr();fp1=fopen(“link1in.dat”,”r”);fp2=fopen(“link1out.dat”,”w”);printf(“\n Enter the location where the program has to be loaded”);scanf(“%x”,&start);fprintf(fp2,”CSECT\Tsymname\Taddress\Tlength\n”);rewind(fp1);while(!feof(fp1)){fscanf(fp1,”%s”,in);if(strcmp(in,”H”)==0){fscanf(fp1,”%s”,in);strcpy(table[cnt].csect,in);strcpy(table[cnt].sym_name,”**”);fscanf(fp1,”%s”,in);table[cnt].add=atoi(in)+start;fscanf(fp1,”%x”,&len);table[cnt++].len=len;fscanf(fp1,”%s”,in);}if(strcmp(in,”D”)==0){fscanf(fp1,”%s%x”,in,&loc);while(strcmp(in,”R”)!=0){

35

Page 35: Cs1307 - System Software Lab[1]

strcpy(table[cnt].csect,”**”);strcpy(table[cnt].sym_name,in);table[cnt].add=loc+start;table[cnt++].len=0;fscanf(fp1,”%s%x”,in,&loc);}while(strcmp(in,”T”)!=0)fscanf(fp1,”%s”,in);}if(strcmp(in,”T”)==0)while(strcmp(in,”E”)!=0)fscanf(fp1,”%s”,in);fscanf(fp1,”%s”,in);start=start+len;}for(i=0;i<cnt;i++)fprintf(fp2,”%s\t%s\t%x\t%x\n”,table[i].csect,table[i].sym_name,table[i].add,table[i].len);fclose(fp1);fclose(fp2);getch();}

36

Page 36: Cs1307 - System Software Lab[1]

INPUT FILE:

“link1in.dat”

H PROGA 000000 000070D LISTA 000040 ENDA 000054R LISTB ENDB LISTC ENDCT 000020 10 0201D 77100004 150014T 000054 16 1000014 15100006 00002F 100014 FFFFCOM 000024 05 +LISTBM 000054 06 +LISTCM 000058 06 +ENDCM 000064 06 +LISTBE 000000H POGB 000000 000088D LISTB 000060 ENDB 000070R LISTA ENDA LISTC ENDC T 000036 11 0100000 772027 05100000T 000070 18 100000 05100006 05100030 100000M 000037 05 +LISTAM 000044 05 +ENDA M 000070 06 +ENDAM 000074 06 +ENDC M 000078 06 +ENDC M 000082 06 +ENDAE OOOOOO

H PROGC 000000 000057 D LISTC 000030 ENDC 000042R LISTA ENDA LISTB NDBT 000018 12 03100000 7100004 05100000T 000042 15 100030 100008 100011 100000M 000019 05 +LISTAM 000023 05 +LISTB M 000027 05 +ENDAM 000048 06 +LISTAM 000051 06 +ENDA M 000054 06 +LISTBE 000000

37

Page 37: Cs1307 - System Software Lab[1]

OUTPUT FILE:

CSECT SYMNAME ADDRESS LENGTHPROGA ** 2000 70** LISTA 2040 0** ENDA 2054 0POGB ** 2070 88** LISTB 20d0 0** ENDB 20e0 0PROGC ** 20f8 57** LISTC 2128 0** ENDC 213a 0

RESULT:

38

Page 38: Cs1307 - System Software Lab[1]

Thus the program to implement pass one of a direct linking loader was executed

successfully.

EX.NO-3d PASS TWO OF A DIRECT LINKING LOADER

AIM:

To write a C program to implement pass two of a direct linking loader.

ALGORITHM:

STEP 1: Assign the control section address in a variable, CSADR.

STEP 2: Read the Header record.

STEP 3: From the information available in the header record read, store the control

section length in a variable.

STEP 4: Do the following process until an ‘end’ record is reached

STEP 5: If the subsequent records read is a text record ‘T’, and if the object code is in

character form convert it into machine representation, and move the object code

from the record to the memory location control sections address plus the

specified address in the text record.

STEP 6: If the sequent records read is modification record ‘M’ then search for the

modifying symbol name in the external symbol table created by pass 1 if it is

found the add, or subtract the corresponding symbol address found with the

value starting at the location (CSADD plus the specified address in the

modification record).

STEP 7: Add the control section length to the current control section address to the

address of the next control section, and repeat the entire process until there is

no more input.

39

Page 39: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>struct objectcode{char byte[15];int locctr;};struct objectcode code[500];void main(){int loc,length,tlen=0,textloc,inc=0,i,j,reloc,maddr,tloc;int len,num=0,k,newval,value,addr,start,x;char input[20],newloc[20],oper[5],symbol[20],modval[20],in[20];FILE *fp1,*fp2,*fp3;clrscr();fp1=fopen(“INP3EX9.DAT”,”r”);fp2=fopen(“INP4EX9.DAT”,”r”);fp3=fopen(“OUTPUT.DAT”,”w”);printf(“\n ENTER THE LOCATION TO BE LOADED:”);scanf(“%d”,&start);fscanf(fp1,”%s”,input);while(!feof(fp1)){if(strcmp(input,”H”)==0){fscanf(fp1,”%s”,input);fscanf(fp1,”%s”,input);loc=atoi(input)+start;tloc=loc;fscanf(fp1,”%d”,&length);fscanf(fp1,”%s”,input);}if(strcmp(input,”D”)==0){do{fscanf(fp1,”%s”,input);

40

Page 40: Cs1307 - System Software Lab[1]

}while(strcmp(input,”T”)!=0);}if(strcmp(input,”T”)==0){fscanf(fp1,”%d”,&textloc);textloc=textloc+start;for(i=0;i<textloc-(tloc+tlen);i++){strcpy(code[inc].byte,”XX”);code[inc++].locctr=loc++;}fscanf(fp1,”%d”,&tlen);tloc=textloc;fscanf(fp1,”%s”,input);}if(strcmp(input,”M”)==0){fscanf(fp1,”%s”,input);maddr=atoi(input)+start;fscanf(fp1,”%d”,&value);fscanf(fp1,”%s”,oper);fscanf(fp1,”%s”,input);rewind(fp2);fscanf(fp2,”%s%d”,symbol,&addr);while(!feof(fp2)){if(strcmp(input,symbol)==0){if(strcmp(oper,”+”)==0)newval=addr+value;elsenewval=addr-value;}fscanf(fp2,”%s%d”,symbol,&addr);}for(x=0;x<inc;x++){if(code[x].locctr==maddr)break;}itoa(newval,modval,10);j=strlen(modval);for(k=0;k<j;k++){code[x].byte[num++]=modval[k];

41

Page 41: Cs1307 - System Software Lab[1]

if(num>1){x++;num=0;}}fscanf(fp1,”%s”,input);}if(strcmp(input,”E”)==0){fscanf(fp1,”%s”,input);start=start+length;}else{len=strlen(input);j=0;for(i=0;i<2;i++)code[inc].byte[num++]=input[i];if(i==2){while(i!=len){in[j]=input[i];i++;j++;}}in[j]=’\0’;reloc=atoi(in)+start;itoa(reloc,newloc,10);if(num>1){code[inc].locctr=loc;loc++;inc++;num=0;}for(k=0;k<j;k++){code[inc].byte[num++]=newloc[k];if(num>1){code[inc].locctr=loc;loc++;inc++;

42

Page 42: Cs1307 - System Software Lab[1]

num=0;}}}fscanf(fp1,”%s”,input);}for(i=0;i<inc;i++)fprintf(fp3,”\n%d\t%s”,code[i].locctr,code[i].byte);fclose(fp1);fclose(fp2);fclose(fp3);getch();}

INPUT FILES:

“INP3EX9.DAT”

H PROGA 000000 000012D LISTA 000006 ENDA 000007R LISTB ENDBT 000000 12 000016 100004 150014 180012M 000004 05 + LISTBE 000000

H PROGB 000000 000012D LISTB 000010 ENDB 000011R LISTA ENDAT 000000 12 030005 770007 050016 000012M 000001 05 - LISTAE 000000

“INP4EX9.DAT”

LISTA 2006ENDA 2007LISTB 2008ENDB 2010

43

Page 43: Cs1307 - System Software Lab[1]

OUTPUT:

ENTER THE LOCATION TO BE LOADED: 2000

OUTPUT FILE:

2000 002001 202002 162003 102004 202005 132006 152007 202008 142009 182010 202011 122012 032013 202014 012015 772016 202017 192018 05

44

Page 44: Cs1307 - System Software Lab[1]

RESULT:

Thus the program to implement pass two of a direct linking loader was executed

successfully.

EX.NO-4a ONE PASS MACRO PROCESSOR

AIM:

To write a C program to implement a one pass macro processor.

ALGORITHM:

STEP 1: Start the macro processor program.

STEP 2: Include the necessary header files and variables.

STEP 3: Open the three files.

STEP 4: Read the variable until the op code is not equal to zero.

STEP 5: Check if the op code is equal to Macro if Macro Then

Copy macro name=label

Get the variable label, op code and operand.

STEP 6: If op code is not equal to MEND perform the operations.

STEP 7: Copy the variable.

STEP 8: Else if op code is equal to macro name, Perform for loop from 0 to length.

STEP 9: Else if it does not match, Write label, op code, operand in to a file.

STEP 10: Finally terminate the program.

45

Page 45: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>void main(){int n=0,I,flag=0;char ilab[20],iopd[20],oper[20],NAMTAB[20][20];FILE *fp1,*fp2,*DEFTAB;clrscr();fp1=fopen(“MACROIN.DAT”,”r”);fp2=fopen(“MACROOUT.DAT”,”w”);rewind(fp1);fscanf(fp1,”%s%s%s”,ilab,iopd,oper);while(!feof(fp1)){if(strcmp(iopd,”MACRO”)==0){strcpy(NAMTAB[n],ilab);DEFTAB=fopen(NAMTAB[n],”w”);fscanf(fp1,”%s%s%s”,ilab,iopd,oper);while(strcmp(iopd,”MEND”)!=0){fprintf(DEFTAB,”%s\t%s\t%s\n”,ilab,iopd,oper);fscanf(fp1,”%s%s%s”,ilab,iopd,oper);}fclose(DEFTAB);n++;}else{flag=0;for(i=0;i<n;i++){if(strcmp(iopd,NAMTAB[i])==0){flag=1;DEFTAB=fopen(NAMTAB[i],”r”);fscanf(DEFTAB,”%s%s%s”,ilab,iopd,oper);while(!feof(DEFTAB))

46

Page 46: Cs1307 - System Software Lab[1]

{fprintf(fp2,”%s\t%s\t%s\n”,ilab,iopd,oper);fscanf(DEFTAB,”%s%s%s”,ilab,iopd,oper);}

break;}}if(flag==0)fprintf(fp2,”%s\t%s\t%s\n”,ilab,iopd,oper);}fscanf(fp1,”%s%s%s”,ilab,iopd,oper);}printf(“\n OUT IN MACROPROSCESSOR”);getch();}

INPUT FILE:

“MACROIN.DAT”

M1 MACRO **** LDA N1** ADD N2** STA N3** MEND **M2 MACRO **** LDA N1** SUB N2** STA N4** MEND **M3 MACRO **** LDA N1** MUL N2** STA N5** MEND **** START 1000** M3 **** M2 **** M1 **** END **

47

Page 47: Cs1307 - System Software Lab[1]

OUTPUT FILE:

“MACROOUT.DAT”

** START 1000** LDA N1** MUL N2** STA N5** LDA N1** SUB N2** STA N4** LDA N1** ADD N2** STA N3

RESULT:

48

Page 48: Cs1307 - System Software Lab[1]

Thus the program to implement one pass macro processor was executed

successfully.

EX.NO-4b TWO PASS MACROPROCESSOR

AIM:

To write a C program to implement a two pass macro processor.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Read input line from input program.

STEP 3: If opcode is ‘MACRO’ goto next step.

STEP 4: Enter macro name into namtab.

STEP 5: Enter macro prototype into deftab.

STEP 6: Read next line.

STEP 7: If op code is not MEND, substitute positional notation for parameters

STEP 8: Store the beginning and end of definition in namtab.

STEP 9: Read next input line.

STEP 10: If the op code is END, Exit the program.

49

Page 49: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<string.h>FILE *f;char var[25][10];char s[25],str[50],v[25],tmp[25];int j,c,i=0,l;int t=0;struct deftab{char sym[25];char xas[25];}dt[50];void variable(void){fscanf(f,”%s”,str);strcpy(dt[t++].xas,str);j=1;i=0;do{c=0;do{if(str[j] !=’&’)v[c++]=str[j];j++;}while(str[j]!=’,’ && str[j]!=’\0’);v[c]=’\0’;strcpy(var[i],v);i++;}while(str[j++]!=’\0’);}int main(){int cnt=0,m,n,mn;char res[25];

50

Page 50: Cs1307 - System Software Lab[1]

FILE *f1,*f2;f=fopen(“msource.dat”,”r”);f1=fopen(“deftab.dat”,”w+”);f2=fopen(“namtab.dat”,”w+”);do{fscanf(f,”%s”,str);while(strcmp(str,”MACRO”)){if(!strcmp(str,”END”))goto b;strcpy(s,str);fscanf(f,”%s”,str);}fprintf(f2,”%s\t%d”,s,t);strcpy(dt[t].sym,s);variable();fscanf(f,”%s”,str);do{strcpy(dt[t].sym,str);fscanf(f,”%s”,str);cnt=0;c=0;for(l=0;l<strlen(str);l++){if(str[l]==’&’){cnt++;m=1;}}if(cnt){for(l=m+1;l<strlen(str);l++){tmp[c++]=str[l];tmp[c]=’\0’;for(n=0;n<i;n++)if(!strcmp(tmp,var[n])){mn=n;goto a;}}a:

51

Page 51: Cs1307 - System Software Lab[1]

c=0;for(n=0;n<m;n++)res[c++]=str[n];res[c++]=’?’;res[c++]=mn+49;for(n=l+1;n<strlen(str);n++)res[c++]=str[n];res[c]=’\0’;strcpy(dt[t++].xas,res);}else{strcpy(dt[t++].xas,str);}fscanf(f,”%s”,str);}while(strcmp(str,”MEND”));fprintf(f2,”\t%d\n”,t);strcpy(dt[t].sym,”MEND”);strcpy(dt[t++].xas,” “);}while(!feof(f));b:for(n=0;n<t;n++)fprintf(f1,”%s\t\t%s\n”,dt[n].sym,dt[n].xas);fclose(f);fclose(f1);fclose(f2);return 0;}

52

Page 52: Cs1307 - System Software Lab[1]

INPUT FILE:

“msource.dat”

COPY START 0RDBUFF MACRO &INDEV,&BUFADR,&RECLTH

CLEAR XCLEAR ACLEAR SLDT #4096TD =X’&INDEV’JEQ *-3RD =X’&INDEV’COMPR A,SJEQ *+11STCH &BUFADR,XTIXR TJLT *-19STX &RECLTHMEND

WRBUFF MACRO &OUTDEV,&BUFADR,&RECLTHCLEAR XLDT &RECLTHLDCH &BUFADR,XTD =X’&OUTDEV’TIXR TJLT *-14MEND

FIRST STL RETADRCLOOP RDBUFF F1,BUFFER,LENGTH

LDA LENGTHCOMP #0JEQ ENDFILWRBUFF 05,BUFFER,LENGTHJ CLOOP

ENDFIL WRBUFF 05,EOF,THREEJ @RETADR

EOF BYTE C’EOF’THREE WORD 3RETADR RESW 1LENGTH RESW 1

53

Page 53: Cs1307 - System Software Lab[1]

BUFFER RESB 4096END FIRST

OUTPUT FILES:

“namtab.dat”

RDBUFF 0 14WRBUFF 15 22

“deftab.dat”

RDBUFF &INDEV,&BUFADR,&RECLTHCLEAR XCLEAR ACLEAR SLDT #4096TD =?_JEQ *-3RD =?_COMPR A,SJEQ *+11STCH &?_TIXR TJLT *-19STX &?_MEND WRBUFF &OUTDEV,&BUFADR,&RECLTHCLEAR XLDT &?_LDCH &?_TD =?_TIXR TJLT *-14MEND

54

Page 54: Cs1307 - System Software Lab[1]

RESULT:

Thus the program to implement two pass macro processor was executed

successfully.

EX.NO-5a TOKEN SEPERATION

AIM:

To write a program for implementation of token separation.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Read a character from the keyboard.

STEP 3: If the character is an alphabet goto steps.

(i) Check whether the character is an operator.

(ii) Otherwise identity as special character.

STEP 4: Goto step 8.

STEP 5: Read character until we encounter a non-alphabetical character &

from a string.

STEP 6: If a string is “DEFINE”or “CONST” identify the variable next to it as a

constant & goto step8 or goto step7.

STEP 7: Check whether the string is a keyboard.

(i) If yes, identify the string is a keyboard.

(ii) Otherwise identity a string as identifier.

STEP 8: It is reached step the program otherwise goto step1.

STEP 9: End the program

55

Page 55: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<ctype.h>void main(){charkwrd[23][12]={"include","define","for","else","if","goto","int","float","register","char","double","auto","void","while","do","switch","case","union","struct","static"};char op[10]={'+','-','*','/','%','<','>','=','!','&'};char symbol[18]={';',',',':','{','}','[',']','#' ,'(',')','"'};char fun[4][10]={"printf","scanf","main","getch"};char np[10],inp[100],optr[15],sym[15],temp[15];int ip=0,fn,fk,h,i,j,ops,sy,l,kw,flag,k,r,cons,c,fnd,ck,hd;char kd[15][10],constant[15],iden[15][10],fnn[15][10];int v,id,f,g,x,m,y;clrscr();printf("enter the expression with $");do{inp[ip]=getchar();ip++;}while((inp[ip-1])!='$');inp[ip]='\0';start:for(i=0;i<strlen(inp);i++){if(toascii(inp[i]==32)){goto next;}for(j=0;j<10;j++){if(inp[i]==op[j]){

56

Page 56: Cs1307 - System Software Lab[1]

printf("\n %c-operator",inp[i]);,kgoto next;}if(inp[i]==symbol[j]){printf("\n %c-symbol",inp[i]);goto next;}}if(inp[i]=='\n')goto next;if(inp[i]=='$'){goto print;}}next:for(l=0;l<i;l++){temp[l]=inp[l];}temp[l]='\0';ck=0;for(c=0;c<l;c++){if(isdigit(temp[c])){printf("\n%s-constant",temp);ck=1;}if(ck==1){goto next2;}}for(fn=0;fn<4;fn++){if((strcmp(temp,fun[fn]))==0){printf("\n %s-function",temp);goto next2;}}for(kw=0;kw<20;kw++)

{

57

Page 57: Cs1307 - System Software Lab[1]

if((strcmp(temp,kwrd[kw]))==0){flag=1;fnd=kw;}}if(flag==1){printf("\n %s-keyword",kwrd[fnd]);flag=0;goto next2;}elseif((strcmp(temp,"stdio.h"))==0||(strcmp(temp,"conio.h"))==0||(strcmp(temp,"string.h"))==0){printf("\n %s-headerfile",temp);goto next2;}else{if(temp[0]<=97||temp[0]>=122)goto next2;if(temp[0]=='"')goto next2;printf("\n%s-identifier",temp);goto next2;}next2:m=0;for(x=i+1;i<strlen(inp);x++,i++){inp[m]=inp[x];m++;}inp[m]='\0';goto start;print:getch();}

58

Page 58: Cs1307 - System Software Lab[1]

OUTPUT:

59

Page 59: Cs1307 - System Software Lab[1]

RESULT:

Thus the program for Implementation of token separation was executed and

the output was verified.

EX.NO-5b SYMBOL TABLE MANIPULATION

AIM:

To write a C program to implement Symbol Table for performing insert,

display, delete, search and modify.

ALGORITHM:

STEP 1: Start the program for performing insert, display, delete, search and modify

option in symbol table. Define the structure of the Symbol Table.

STEP 2: Enter the choice for performing the operations in the symbol Table.

STEP 3: If the entered choice is 1, search the symbol table for the symbol

to be inserted.

STEP 4: If the symbol is already present, it displays “Duplicate Symbol”.

Else, insert the symbol and the corresponding address in the symbol table.

STEP 5: If the entered choice is 2, the symbols present in the symbol table

are displayed.

STEP 6: If the entered choice is 3, the symbol to be deleted is searched in

the symbol table.

STEP 7: If it is not found in the symbol table it displays “Label Not found”.

Else, the symbol is deleted.

STEP 8: If the entered choice is 5, the symbol to be modified is searched in the

symbol table. The label or address or both can be modified.

60

Page 60: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>struct table{char var[20];int value;};struct table tb[20];int I,j,n;void create();void modify();int search(char variable[],int n);void insert();void display();void main(){int ch,result=0;char v[20];clrscr();while(1){printf(“\n OPTIONS:”);printf(“\n 1.CREATION”);printf(“\n 2.INSERTION”);printf(“\n 3.MODIFY”);printf(“\n 4.SEARCH”);printf(“\n 5.DISPLAY”);printf(“\n 6.EXIT”);printf(“\n ENTER UR CHOICE:”);scanf(“%d”,&ch);switch(ch){case 1:create();break;case 2:

61

Page 61: Cs1307 - System Software Lab[1]

insert();break;case 3:modify();break;case 4:printf(“\n ENTER THE VARIABLE TO BE SEARCHED:”);scanf(“%s”,v);result=search(v,n);if(result==0)printf(“\n VARIABLE IS NOT PRESENT”);elseprintf(“\n VARIABLE IS PRESENT IN LOCATION ‘%d’ AND ITS VALUE IS ‘%d’”,result,tb[result].value);break;case 5:display();break;default:exit(0);}}}void create(){printf(“\n ENTER THE NUMBER OF ENTRIES:”);scanf(“%d”,&n);printf(“\n ENTER THE ENTRIES:”);for(i=1;i<=n;i++){scanf(“%s%d”,tb[i].var,&tb[i].value);for(j=1;j<I;j++){if(strcmp(tb[j].var,tb[i].var)==0){printf(“\n VARIABLE ALREADY EXITS \n ENTER ANOTHER VARIABLE:”);scanf(“%s%d”,tb[i].var,&tb[i].value);}}}}void insert(){if(n>=20)printf(“\n TABLE IS FULL SO CANT INSERT:”);else{n++;printf(“\n ENTER THE VARIABLE AND VALUE:”);scanf(“%s%d”,tb[n].var,&tb[n].value);for(i=1;i<n;i++)

62

Page 62: Cs1307 - System Software Lab[1]

{if(strcmp(tb[i].var,tb[n].var)==0){printf(“\n VARIABLE ALREADY EXITS \n ENTER ANOTHER VARIABLE:”);scanf(“%s%d”,tb[n].var,&tb[n].value);}}}}void modify(){char v[20];int r;printf(“\n ENTER THE VARIABLE TO MODIFY:”);scanf(“%s”,v);r=search(v,n);if(r==0)printf(“\n ELEMENT IS NOT PRESENT IN TABLE:”);else{printf(“\n ENTER THE NEW VARIABLE AND VALUE:”);scanf(“%s%d”,tb[i].var,&tb[i].value);for(j=1;j<=n;j++){if(j!=i){if(strcmp(tb[j].var,tb[i].var)==0){printf(“\n VARIABLE ALREADY EXITS \n ENTER ANOTHER VARIABLE:”);scanf(“%s%d”,tb[i].var,&tb[i].value);}}}}}int search(char v[],int n){int flag=0;for(i=1;i<=n;i++){if(strcmp(tb[i].var,v)==0){flag=1;break;}}if(flag==1)return I;elsereturn 0;}void display(){printf(“\n THE ELEMENTS IN THE TABLE ARE:”);

63

Page 63: Cs1307 - System Software Lab[1]

for(i=1;i<=n;i++)printf(“\n %s\t%d\t”,tb[i].var,tb[i].value);getch();}

OUTPUT:

OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:1 ENTER THE NUMBER OF ENTRIES:2 ENTER THE ENTRIES:x 2 y 3

OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:2 ENTER THE VARIABLE AND VALUE: z 4

OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:5 THE ELEMENTS IN THE TABLE ARE: x 2 y 3 z 4

OPTIONS: 1.CREATION

64

Page 64: Cs1307 - System Software Lab[1]

2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:3 ENTER THE VARIABLE TO MODIFY:x ENTER THE NEW VARIABLE AND VALUE:w 5

OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:5 THE ELEMENTS IN THE TABLE ARE: w 5 y 3 z 4

OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:4 ENTER THE VARIABLE TO BE SEARCHED:y VARIABLE IS PRESENT IN LOCATION ‘2’ AND ITS VALUE IS ‘3’

OPTIONS: 1.CREATION 2.INSERTION 3.MODIFY 4.SEARCH 5.DISPLAY 6.EXIT ENTER UR CHOICE:6

65

Page 65: Cs1307 - System Software Lab[1]

RESULT:

Thus the program for symbol table creation was executed and the output was

verified.

EX.NO-6 SHIFT REDUCE PARSING TABLE CONSTRUCTION

AIM:

To write a c program for the implementation of parsing table.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include the needed header file.

STEP 3: Declare the variables.

STEP 4: Check whether the expression is accepted or not.

STEP 5: The input symbol passed into stack one by one.

STEP 6: This statement is changed into E.

STEP 7: Calculate into E->E*E, E->E+E the E->i performed.

STEP 8: The grammar is checked when $ symbol is occurred.

STEP 9: Then the expression grammar is accepted.

STEP 10: Print the result.

STEP 11: Stop the program.

66

Page 66: Cs1307 - System Software Lab[1]

PROGRAM:

#include<stdio.h>#include<conio.h>#include<string.h>#include<types.h>#include<process.h>void sta();char st[100],exp[100];int q,top=0,i,l,n,j;void main(){clrscr();st[++top]='$';i=0;printf("\n enter the input");gets(exp);l=strlen(exp);l--;printf("\n stack\tinput\t action\n");n=top;for(j=0;j<=n;j++)printf("%c",st[j]);printf("\t%s",exp);do{printf("shift");st[++top]=exp[i];l--;exp[i]='\0';

sta();if(st[top]=='i'){printf("reduce by E->i");st[top]='E';sta();}i++;

67

Page 67: Cs1307 - System Software Lab[1]

}while(l>0);do{if(st[top--]=='+'){printf("reduce by E->E+E");st[++top]='\0';sta();}else if(st[top--]=='*'){printf("reduce by E->E*E");

st[++top]='\0';sta();}}while(st[++top]!='$');printf("accept");getch();}void sta(){int k;n=top;printf("\n");for(j=0;j<=n;j++)printf("%c",st[j]);printf("\t");for(k=i;k<=i+7;k++)printf("%c",exp[k]);}

68

Page 68: Cs1307 - System Software Lab[1]

OUTPUT:

69

Page 69: Cs1307 - System Software Lab[1]

RESULT:

Thus the c program for the implementation of parsing table was

executed and the output was verified successfully.

70