assignmentmp345

66
TWO PASS MACROPROCESSOR AIM: To implement two pass macro processor in C language. Theory:- The program which is responsible for processing of macro i.e. group of instructions. Function of macro processor: 1. Recognize macro definition 2. Store the macro definition 3. Recognize the macro call 4. Perform macro expansion The purpose of pass2 is to recognize the macro call and perform The macro expansion. Database required for pass2: 1.The copy of the input macro source deck. 2.The output expanded source deck to be used as input to the assembler. 3.The Macro Definition Table (MDT) created by pass1. 4.The Macro Name Table (MNT) created by pass1. 5.The Macro Definition Table Pointer (MDTP) used to indicate the next line of text to be used during macro expansion.

Upload: s5sunil

Post on 22-Oct-2014

105 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AssignmentMP345

TWO PASS MACROPROCESSOR

AIM: To implement two pass macro processor in C language.

Theory:-

The program which is responsible for processing of macro

i.e. group of instructions.

Function of macro processor:

1. Recognize macro definition

2. Store the macro definition

3. Recognize the macro call

4. Perform macro expansion

The purpose of pass2 is to recognize the macro call and

perform

The macro expansion.

Database required for pass2:

1. The copy of the input macro source deck.

2. The output expanded source deck to be used as input

to the assembler.

3. The Macro Definition Table (MDT) created by pass1.

4. The Macro Name Table (MNT) created by pass1.

5. The Macro Definition Table Pointer (MDTP) used to

indicate the next line of text to be used during macro

expansion.

6.The Argument List Array (ALA) used to substitute

macro call argument for the index markers in the stored

macro definition.

Page 2: AssignmentMP345
Page 3: AssignmentMP345

ALGORITHM:

1. Get the statement from the input file

2. If the statement has the directive “MACRO”, then the

number of macro “n” will be incremented by 1

3. Repeat the steps 1 and 2 until an end of file is encountered

4. Open “n” number of macro files in write mode and rewind

the input file pointer

5. If the directive is “MACRO” then, do the following

5.1 Enter the macro name present in the operand fiel

5.2 Write the line to the expanded output fil

5.3 Enter the lines in the body of each macro in to the

corresponding files already opened in step 4 5.4 Write the

body of each macro to the expanded output files until a

“MEND” is reached 6. Write the remaining lines directly to the

expanded file.

CONCLUSION:

Thus two pass macro processor is implemented in C language.

Page 4: AssignmentMP345

PROGRAM:

/* program to Perform pass2 of macro*/

#include <stdio.h>

char optable[50][20];

char argtab[20][10];

int cnt = 0;

int def_cnt = 0,nam_cnt = 0,arg_cnt = 0;

FILE *exp;

struct definition {

char instruction[20];

char operand[30];

}deftab[30]; struct name {

char MacroName[20];

int beg;

int end;

}namtab[5]; void initialize() {

FILE *optab,*deftable,*namtable;

char mnemonic[20],opcode[20];

optab = fopen("optab.txt","r");

deftable = fopen("deftab.txt","r");

namtable = fopen("namtab.txt","r");

do {

fscanf(optab,"%s %s",mnemonic,opcode);

strcpy(optable[cnt++],mnemonic);

}

while(!feof(optab)); do {

fscanf(deftable,"%s %s",deftab[def_cnt].instruction,deftab[def_cnt].operand);

Page 5: AssignmentMP345

def_cnt++;

}while(!feof(deftable));

do

{

fscanf(namtable,"%s%d%d",namtab[nam_cnt].MacroName,

&namtab[nam_cnt].beg,&namtab[nam_cnt].end);

nam_cnt++; }

while(!feof(namtable));

fclose(deftable);

fclose(optab);

fclose(namtable);

}

int ismacro

(char *str)

{

int i;

for(i=0;i<nam_cnt;i++)

if(!strcmp(namtab[i].MacroName,str))

return 1;

return 0;

}

int iskeyword(char *str)

{ int i;

for(i=0;i<cnt;i++)

if(!strcmp(optable[i],str))

return 1; return 0;

Page 6: AssignmentMP345

}

void expand(char *name,char *args) {

FILE *argtbl;

int beg,end,i,j,index;

char operand[30],tmp[20];

argtbl = fopen("argtab.txt","a+"); for(i=0;i<nam_cnt;i++)

{

if(!strcmp(namtab[i].MacroName,name))

{

beg = namtab[i].beg; end = namtab[i].end; } }arg_cnt = 1; i=0; do {

j=0;

do

{

argtab[arg_cnt][j++] = args[i++];

}while(args[i] != ',' && args[i] != '\0');

argtab[arg_cnt][j] = '\0';

arg_cnt++;

}while(args[i++] != '\0');

fprintf(argtbl,"\n%s :",name);

for(i=0;i<arg_cnt;i++)

{

fprintf(argtbl,"%s",argtab[i]);

}for(i=beg+1;i<=end;i++)

{ fprintf(exp,"\t%s\t",deftab[i].instruction);

strcpy(operand,deftab[i].operand);

for(j=0;j<strlen(operand);j++)

{

Page 7: AssignmentMP345

if(operand[j] == '?') {

operand[j] = '%';

index = operand[j+1]-48;

operand[j+1] = 's';

sprintf(tmp,operand,argtab[index]);

strcpy(operand,tmp);

}

}

fprintf(exp,"%s\n",operand);

}

fclose(argtbl);

getch();

}

void main()

{

FILE *source,*argtbl;

char str[30],str1[30],str2[30];

int i;

source = fopen("prog.txt","r");

argtbl = fopen("argtab.txt","w+");

exp = fopen("exppgm.txt","w+");

fclose(argtbl);

initialize(); do { fscanf(source,"%s %s",str,str1); beg:

if(feof(source)){}

else if(!strcmp(str1,"MACRO"))

{

strcpy(optable[cnt++],str); fscanf(source,"%s",str2); do { fscanf(source,"%s

Page 8: AssignmentMP345

%s",str,str1); }while(strcmp(str,"MEND")); strcpy(str,str1);

fscanf(source,"%s",str1);

goto beg;

}else if(iskeyword(str)) { if(ismacro(str)) {

fprintf(exp,".\t%s\t%s\n",str,str1);

expand(str,str1);

}else fprintf(exp,"\t%s\t%s\n",str,str1); }else {

fscanf(source,"%s",str2);

if(ismacro(str1))

{

fprintf(exp,".%s\t%s\t%s\n",str,str1,str2);

fprintf(exp,"%s",str);

expand(str1,str2);

}else fprintf(exp,"%s\t%s\t%s\n",str,str1,str2); }

}while(!feof(source));

fclose(source);

}

SINGLE PASS MACRO PROCESSOR

AIM: To implement a single pass macro processor in C

Page 9: AssignmentMP345

language.

Theory:- The program which is responsible for processing of

macro i.e. group of instructions.

Function of macro processor:

1. Recognize macro definition

2. Store the macro definition

3. Recognize the macro call

4. Perform macro expansion

The purpose of pass 1 is to recognize macro definition and

store the macro definition.

Database required for pass1:

1. The copy of the input macro source deck.

2. The output expanded source deck copy for use by pass2.

3. The Macro Definition Table (MDT) used to store the body

of macro definitions.

4. The Macro Name Table (MNT) used to store the names of

defined macros.

5. The Macro Definition Table Counter (MDTC) used to

indicate the next available entry in MDT.

6. The Macro Name Table Counter (MNTC) used to indicate

the next available entry in MNT.

7. The Argument List Array (ALA) used to substitute index

markers for arguments before storing a macro definition.

ALGORITHM:

Page 10: AssignmentMP345

1. Get the statement from the input file

2. If the statement has the directive “MACRO”, then the number of macro

“n” will be incremented by 1

3. Repeat the steps 1 and 2 until an end of file is encountered

4. Open “n” number of macro files in write mode and rewind the input file

pointer

5. If the directive is “MACRO” then, do the following

5.1 Enter the macro name present in the operand field

5.2 Write the line to the expanded output file

5.3 Enter the lines in the body of each macro in to the

corresponding files already opened in step 4 5.4 Write the body of each

macro to the expanded output files until a “MEND” is reached 6. Write

the remaining lines directly to the expanded file.

6. EXIT.

Page 11: AssignmentMP345

CONCLUSION:

Thus a single pass macro processor is implemented in C

language.

Page 12: AssignmentMP345

PROGRAM:

/* C Program to implement SINGLE PASS MACRO PROCESSOR */

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void main()

{int n,flag,i;

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");

n=0;

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

Page 13: AssignmentMP345

}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\n",ilab,iopd,oper);

while(!feof(DEFTAB))

{

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

}

fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);

getch();

}

MACROIN.DAT M1

MACRO ** **

LDA N1 **

ADD N2 **

STA N3 **

MEND ** M2

MACRO ** **

Page 14: AssignmentMP345

LDA N1 **

SUB N2 **

STA N4 **

MEND ** M3

MACRO ** **

LDA N1 **

MUL N2 **

STA N5 **

MEND ** **

START 1000 **

M3 ** **

M2 ** **

M1 ** **

END **

Page 15: AssignmentMP345

ABSOLUTE LOADER

AIM: To implement an Absolute loader in C language.

Theory:-

The assembler punches the machine language translation

of the source program on cards. The loader in turn accepts the

machine language text and places it into core at the site

prescribed by assembler.

Absolute loader requires two types of card:

1. Text Cards:- They contain the machine instructions that

the assembler has created along with the assigned core

location.

2. Transfer Card:- It contains the entry point of the program,

which where the loader is to transfer control when all

instruction are loaded.

Card Type Count Address Contents

0

Card Type Count Address Contents

1 0 -

Page 16: AssignmentMP345

Type=0 Type=1

Set CURLOC to

Address Field

READ CARD

Transfer to location CURLOC

Set LNG to COUNT field

Move LNG bytes of contents to CURLOC

Card Type

INITIALIZE

Page 17: AssignmentMP345

ALGORITHM:

1. Read the Header record

2. Verify program name and length

3. Read first Text record from the input file

4. Process the following steps until an End record is reached

5.1 If object code is in character form, convert it to internal hexadecimal

representation.

5.2 Move objects codes to specified locations in memory.

5.3 Write the starting location counter value of a block of object code and the

Corresponding internal representation to the output file

5.4 Read next Text record from the input file

5. Go to the address specified in End record

6. Close all the files and exit.

CONCLUSION:

Thus an Absolute loader is implemented in C language

Page 18: AssignmentMP345

PROGRAM:

/* C Program to implement ABSOLUTE LOADER */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char input[10];

int start,length,address;

FILE *fp1,*fp2;

clrscr();

fp1=fopen("input.dat","r");

fp2=fopen("output.dat","w");

fscanf(fp1,"%s",input);

while(strcmp(input,"E")!=0)

{

if(strcmp(input,"H")==0) {

fscanf(fp1,"%d",&start);

fscanf(fp1,"%d",&length);

fscanf(fp1,"%s",input);

}else if(strcmp(input,"T")==0) {

fscanf(fp1,"%d",&address);

fscanf(fp1,"%s",input);

fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);

fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);

fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);

address+=3;

Page 19: AssignmentMP345

fscanf(fp1,"%s",input);

}else {

fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);

fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);

fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);

address+=3;

fscanf(fp1,"%s",input);

} }fclose(fp1); fclose(fp2);

printf("FINISHED");

getch();

}

INPUT.DAT: H 1000 232

T 1000 142033 483039 102036

T 2000 298300 230000 282030 302015

Page 20: AssignmentMP345

RELOCATING LOADER

AIM: To implement a Relocating loader in C language.

Theory:- To avoid possible reassembling of all subroutines when

a single subroutine is changed and to perform the tasks of

allocation and linking for the programmer, the general class of

relocating loader was introduced. BSS (Binary Symbolic

Subroutine) is an example of relocating loaders.

For each source program the assembler outputs:

The machine translation of the program

Transfer vector that consists of addresses containing

names of the subroutines referenced by the source

program.

Relocation bits are associated with each instruction or

address filed.

Length of the entire program.

Length of the transfer vector portion

After loading the machine translation of the program

and the transfer vector into core, the loader would then load

each subroutine identified in the transfer vector. It would then

place the transfer instruction to the corresponding subroutine

in each entry in the transfer vector.

BSS loader perform all the four functions

1. Allocation:- With the help of program length information

2. Linking:- With the help of transfer vector

3. Relocation:- With the help of relocation bits

4. Loading

Page 21: AssignmentMP345

ALGORITHM:

1. Enter the new starting location to which the object code has to be

relocated

2. Read the content of the input file as strings one at a time

3. Transfer the strings from input file to output file, until ‘T’ is encountered

4. Move the consecutive next three strings from input to output

5. Convert the current string, which is the relocation bit associated with each

text record to binary form.

6. Make the necessary changes in the corresponding words of object code by

adding the new starting address with the address part of the object code for

which the corresponding relocation bit is set and store the updated object

code in the output.

7. Move the object code for which the corresponding relocation bit is not set

from output to input without change

8. Repeat steps from 2 to 7 until end record is encountered

9. If object code is in character form, convert it to internal hexadecimal

representation

10. Move objects codes to specified locations in memory

11. Write the starting location counter value of a block of object code and the

corresponding internal hexadecimal representations to the output file.

CONCLUSION:

Thus a Relocating loader is implemented in C language

PROGRAM: /* C Program to implement RELOCATING LOADER */

Page 22: AssignmentMP345

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void main()

{

char add[6],length[10],input[10],bitmask[12],binary[12],relocbit;

int start,len,inp,i,address,opcode,addr,actualadd;

FILE *fp1,*fp2;

clrscr();

printf("Enter the actual starting address: ");

scanf("%d",&start);

fp1=fopen("input.dat","r");

fp2=fopen("output.dat","w");

fscanf(fp1,"%s",input);

while(strcmp(input,"E")!=0)

{

if(strcmp(input,"H")==0) {

fscanf(fp1,"%s",add);

fscanf(fp1,"%s",length);

fscanf(fp1,"%s",input);

}if(strcmp(input,"T")==0) {

fscanf(fp1,"%d",&address);

fscanf(fp1,"%s",bitmask);

address+=start;

len=strlen(bitmask);

for(i=0;i<len;i++)

Page 23: AssignmentMP345

{fscanf(fp1,"%d",&opcode);

fscanf(fp1,"%d",&addr);

relocbit=bitmask[i];

if(relocbit=='0')

actualadd=addr; else actualadd=addr+start;

fprintf(fp2,"%d\t%d%d\n",address,opcode,actualadd);

address+=3;

}

fscanf(fp1,"%s",input); } }fclose(fp1); fclose(fp2);

printf("FINISHED");

getch();

}INPUT.DAT: H 1000 200

T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765

T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477

E 1000

Enter the actual starting address: 7000

PASS ONE OF DIRECT LINKING LOADER

Page 24: AssignmentMP345

AIM: To implement pass one of direct-linking loader in C

language.

Theory:-

Pass 1

Page 25: AssignmentMP345

PLA<- IPLA

Read Next Card

Write in Copy File

ESD Type

Value<-PLA Value<-PLA+ADDR

SLength<-Length

PLA <-PLA+SLength

Go To Pass 2

Is symbol in GEST?

Enter Symbol & Value in GEST

Print Symbol & Value For LoadMAP

ERROR

Card Type

α

α

α

α

Page 26: AssignmentMP345

ALGORITHM:

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

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

3. Read the header record of the control section

a. From the details of the header read and store the control section

length in a variable

b. Enter the control section name with its address into the external

symbol table

4. 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 until the END record is reached.

5. Assign the starting address of next control section as the address of the

current control section plus the length of the control section

6. Repeat the process from step 3 to 5 until there is no more records

CONCLUSION:

Thus pass one of direct-linking loader is implemented in C language

Page 27: AssignmentMP345

PROGRAM:

/* C Program to implement PASS ONE OF DIRECT LINKING LOADER */

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

struct estab

{char csect[10];

char sym_name[10];

int add,length;

}table[10];

void main()

{char input[10];

int i,count=0,start,length,loc;

FILE *fp1,*fp2;

clrscr();

fp1=fopen("linkin.dat","r");

fp2=fopen("linkout.dat","w");

printf("\nEnter the location where the program has to be located: ");

scanf("%x",&start);

fprintf(fp2,"CSect\tSym_Name\tAddress\t\tLength\n\n");

rewind(fp1);

while(!feof(fp1))

{fscanf(fp1,"%s",input); if(strcmp(input,"H")==0)

{fscanf(fp1,"%s",input); strcpy(table[count].csect,input);

strcpy(table[count].sym_name,"**");

fscanf(fp1,"%s",input);

Page 28: AssignmentMP345

table[count].add=atoi(input)+start;

fscanf(fp1,"%x",&length);

table[count++].length=length;

fscanf(fp1,"%s",input);

}if(strcmp(input,"D")==0)

{fscanf(fp1,"%s%x",input,&loc); while(strcmp(input,"R")!=0)

{strcpy(table[count].csect,"**"); strcpy(table[count].sym_name,input);

table[count].add=loc+start;

table[count++].length=0;

fscanf(fp1,"%s%x",input,&loc);

}while(strcmp(input,"T")!=0)

fscanf(fp1,"%s",input); }if(strcmp(input,"T")==0) while(strcmp(input,"E")!=0)

fscanf(fp1,"%s",input);

fscanf(fp1,"%s",input);

start=start+length;

}for(i=0;i<count;i++)

fprintf(fp2,"%s\t%s\t\t%x\t\t%x\

n",table[i].csect,table[i].sym_name,table[i].add,table[i].length);

fcloseall();

getch(); }

OUTPUT:

Enter the location where the program has to be located: 5075 LINKIN.DAT H

PROGA 000000 000070 D LISTA 000040 ENDA 000054 R LISTB ENDB LISTC

ENDC T 000020 10 032010 77100004 15001 T 000054 16 100014 15100006

00002F 10001

M 000024 05 +LISTB M 000054 06 +LISTC M 000058 06 +ENDC M 000064 06

+ENDC E 000000 H PROGB 000000 000088 D LISTB 000060 ENDB 000070 R

Page 29: AssignmentMP345

000070 18 100000 05100006 0510020 0510030 M 000037 05 +LISTA M

000044 05 +ENDA M 000070 06 +ENDA M 000074 06 +ENDC M 000078

06 +ENDC M 000082 06 +ENDA E 000000 H PROGC 000000 000057 D

LISTC 000030 ENDC 000042 R LISTA ENDA LISTB ENDB T 000018 12

03100000 77100004 05100000 T 000042 15 100030 100008 100011

100000 M 000019 05 +LISTA M 000023 05 +LISTB M 000027 05 +ENDA

M 000048 06 +LISTA M 000051 06 +ENDA M 000054 06 +LISTB E

000000 LINKOUT.DAT CSect Sym_Name Address Length PROGA ** 5075

70 ** LISTA 50b5 0 ** ENDA LISTA ENDA LISTC ENDC T 000036 11

03100000 772027 0510030 T 50c9 0 PROGB ** 50e5 88 ** LISTB 5145 0

** ENDB 5155 0 PROGC ** 516d 57 ** LISTC 519d 0 ** ENDC 51af 0

Page 30: AssignmentMP345

PASS TWO OF DIRECT LINKING LOADER

AIM: To implement pass two of direct-linking loader in C

language.

Theory:-

ALGORITHM:

Page 31: AssignmentMP345

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

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

address

3. Read the header record of the control section

i) From the details of the header read and store the control

section length in a variable

ii) Enter the control section name with its address into the

external symbol table

4. 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 until the END record is reached

5. Assign the starting address of next control section as the address of

the current control section plus the length of the control section

6. Repeat the process from step 3 to 5 until there is no more records

CONCLUSION:

Thus pass two of direct-linking loader is implemented in C

language.

Page 32: AssignmentMP345

PROGRAM:

/* C Program to implement PASS TWO OF DIRECT LINKING LOADER */

#include<stdio.h>

#include<conio.h>

Void main()

{FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7,*fp8;

char rec[20],name[20],len[20],t[20],string[1000],mod[40]

[40],est1[20],est2[20],est4[20],sign[40]

[1], temp;

int ptn1[20][20],ptn2[20][20];

int i,h=0,lent[20],i,st,m1,m2,start[20],add[20],k=0,est3,z1=0,val[40],ptn[40],

offset [40],j,num,progsta rt,count=0;

fp1=fopen("DLL_IN.txt","r");

fp2=fopen("ESTAB.txt","r");

fp3=fopen("ntemp.txt","w");

fp4=fopen("memory.txt","w");

fp6=fopen("six.txt","w");

fscanf(fp1,"%s%s%x%s",rec,name,&st,len);

for(l=0;l<3;l++)

{if(l==1)

fp3=fopen("ntempb.txt","w");

if(l==2)

fp3=fopen("ntempc.txt","w");

fscanf(fp1,"%s",t);

do{

Page 33: AssignmentMP345

if(strcmp(t,"T")==0)

{fscanf(fp1,"%x%x",&start[h],&lent[h]);

if(h!=0) {

for(i=0;i<(start[h]-(start[h-1]+lent[h-1]));i++)

fprintf(fp3,"x");

}h++;

fscanf(fp1,"%s",t);

do{

fprintf(fp3,"%s",t);

fscanf(fp1,"%s",t);

}while((strcmp(t,"T")!=0)&&(strcmp(t,"M")!=0));

}else if(strcmp(t,"M")==0)

{fscanf(fp1,"%x%x%s%s",&ptn[k],&offset[k],sign[k],mod[k]); fscanf(fp2,"%s%s

%x%s",est1,est2,&est3,est4);

progstart=est3;

do{

if(strcmp(mod[k],est2)==0){

val[z1]=est3;

z1++;

break;

}else if(strcmp(mod[k],est1)==0) {

val[z1]=est3;

z1++;

break;

}fscanf(fp2,"%s%s%x%s",est1,est2,&est3,est4);

}while(!feof(fp2));

rewind(fp2);

Page 34: AssignmentMP345

fscanf(fp1,"%s",t);

k++;

}else if(strcmp(t,"E")==0)

{fscanf(fp1,"%s",t); if(l!=2)

fscanf(fp1,"%s%s%x%s",rec,name,&st,len);

break;

}else if(strcmp(t,"R")==0)

{while(strcmp(t,"T")!=0) fscanf(fp1,"%s",t); }else if(strcmp(t,"D")==0)

{while(strcmp(t,"R")!=0) fscanf(fp1,"%s",t); }}while(1); fclose(fp3);

for(i=0;i<k;i++){

if(l==0)

fp3=fopen("ntemp.txt","r+");

if(l==1)

fp3=fopen("ntempb.txt","r+");

if(l==2)

fp3=fopen("ntempc.txt","r+");

fp5=fopen("temp1.txt","w");

fseek(fp3,(ptn[i]*2)+1,0);

for(j=0;j<offset[i];j++)

{fscanf(fp3,"%c",&temp);

fprintf(fp5,"%c",temp); }fprintf(fp5,"\n"); fclose(fp5);

fp5=fopen("temp1.txt","r");

fscanf(fp5,"%x",&num);

if(sign[i][0]=='+')

{num=num+val[i];

fseek(fp3,(ptn[i]*2)+1,0);

Page 35: AssignmentMP345

if(offset[i]==5)

fprintf(fp3,"0%x",num);

else

fprintf(fp3,"00%x",num);

}else

{num=num-val[i]; fseek(fp3,(ptn[i]*2)+1,0);

fprintf(fp3,"00%x",num); }fclose(fp3);

fclose(fp5); }k=0;h=0;z1=0; }fp3=fopen("ntemp.txt","r");

fscanf(fp3,"%s",string);

fclose(fp3);

fprintf(fp6,"%s",string);

fp3=fopen("ntempb.txt","r");

fscanf(fp3,"%s",string);

fclose(fp3);

fprintf(fp6,"%s",string);

fp3=fopen("ntempc.txt","r");

fscanf(fp3,"%s",string);

fclose(fp3);

fprintf(fp6,"%s",string);

fclose(fp6);

fp6=fopen("six.txt","r");

fscanf(fp6,"%s",string);

for(i=0;i<strlen(string);i++)

{if(i==0) {

fprintf(fp4,"%x\t",progstart);

progstart+=16;

}if((i%8==0)&&(i!=0))

Page 36: AssignmentMP345

fprintf(fp4,"\t");

if((i%32==0)&&(i!=0)) {

fprintf(fp4,"\n");

fprintf(fp4,"%x\t",progstart);

progstart+=16;

}fprintf(fp4,"%c",string[i]);

}return 0; }

DLL_IN.TXT H PROGA 000000 00003A

D LISTA000030 ENDA000050.

R LISTB LISTC ENDC

T000000 1D 172027 4B100000 032023 290000 332007 4B100000 3F2FEC

032016 0F2016

T00001D 0D 010003 0F200A 4B100000 3E2000

M000004 05 + LISTB

M000011 05 + LISTC

E000000 H PROGB 000000 00002E

D LISTB000060 ENDB000070 .

R LISTA ENDA

T000000 1D B410 B400 B440 77201F E3201B 332FFA DB2015 A004 332009

57900000 B850

T000020 0E 3B2FE9 13100000 4F0000 F1000000

M000007 05 + LISTA

M000022 05 + ENDA

E000000 H PROGC 000000 00001C

D LISTC000030 ENDC000042 .

R LISTA ENDA

T000000 1C B410 77100000 E32012 332FFA 53900000 DF2008 B850 3B2FEE

Page 37: AssignmentMP345

4F000005

M000006 05 + LISTA

M000013 06 + ENDA

E 000000

ESTAB.TXT PROGA - 003000 000063

- LISTA 003030 -

- ENDA 003050 -

PROGB - 00306300007f

- LISTB 0030c3 -

- ENDB 0030d3 -

PROGC - 0030e2000051

- LISTC 003112 -

- ENDC 003124 –

MEMORY.TXT 3000 1720274B 1030c303 20232900 00332007 3010 4B103112

3F2FEC03 20160F20 16010003 3020 0F200A4B 1000003E 2000B410

B400B440 3030 77205013 201B332F FADB2015 A0043320 3040 09579000

00B850xx x3B2FE91 30305004 3050 F0000F10 00000B41 07710000 0E050423

3060 32FFA539 00000DF2 008B0034 02FEE4F0 3070 00005

Page 38: AssignmentMP345

AIM:- TO CHECK WHETHER STRING BELONGS TO A GRAMMAR OR NOT

ALGORITHMStart.Declare two character arrays str[],token[] and initialize integer variables a=0,b=0,c,d. Input the string from the user.Repeat steps 5 to 12 till str[a] =’\0’.If str[a] =='(' or str[a] =='{' then token[b] =’4’, b++. If str[a] ==')' or str[a] =='}’ then token[b] =’5’, b++.Check if isdigit(str[a]) then repeat steps 8 till isdigit(str[a])a++.a--, token[b] =’6’, b++.If str[a]=='+’ then token[b]='2',b++. If(str[a]=='*') then token[b]=’3’,b++. a++.token[b]='\0';then print the token generated for the string . b=0.Repeat step 22 to 31 till token[b]!='\0' c=0.Repeat step 24 to 30 till (token[b]=='6' and token[b+1]=='2' and token[b+2]=='6') or (token[b]=='6' and token[b+1]=='3'and token[b+2]=='6') or (token[b]=='4' and token[b+1]=='6' and token[b+2]=='5') or (token[c]!='\0').token[c]='6';c++;Repeat step 27 to 28 till token[c]!='\0'. token[c]=token[c+2].c++.token[c-2]=’\0’. print token.b++.Compare token with 6 and store the result in d.If d=0 then print that the string is in the grammar. Else print that the string is not in the grammar. Stop.

Page 39: AssignmentMP345

PROGRAM: TO CHECK WHEATHER A STRING BELONGS TO A GRAMMAR OR NOT.

#include<stdio.h>#include<conio.h>#include<ctype.h>#include<string.h>void main(){

int a=0,b=0,c;char str[20],tok[11];clrscr();printf("Input the expression = ");gets(str);while(str[a]!='\0'){

if((str[a]=='(')||(str[a]=='{')){

tok[b]='4';b++;

}if((str[a]==')')||(str[a]=='}')){

tok[b]='5';b++;

}if(isdigit(str[a])){

while(isdigit(str[a])){

}a--;

a++;

tok[b]='6';b++;

}if(str[a]=='+'){

tok[b]='2';b++;

}if(str[a]=='*'){

tok[b]='3';b++;

Page 40: AssignmentMP345

}a++;

}tok[b]='\0'; puts(tok); b=0;while(tok[b]!='\0'){

if(((tok[b]=='6')&&(tok[b+1]=='2')&&(tok[b+2]=='6'))||((tok[b]=='6')&&(tok[b+1

]=='3')&&(tok[b+2]=='6'))||((tok[b]=='4')&&(tok[b+1]=='6')&&(tok[b+2]=='5'))/*||((tok[b]!=6)&&(tok[b+1]!='\0'))*/)

{tok[b]='6'; c=b+1; while(tok[c]!='\0'){

tok[c]=tok[c+2];

c++;

}

}int d;

}else{

}

tok[c]='\0'; puts(tok); b=0;

b++;puts(tok);

d=strcmp(tok,"6");if(d==0){

}else{

}

printf("It is in the grammar.");

printf("It is not in the grammar.");

getch();}

Page 41: AssignmentMP345

OUTPUT

Input the expression = (23+)46254625462546254625It is not in the grammar.

Input the expression = (2+(3+4)+5)462462652654624626526546246265265462462652654624626526546246526546246526546246526546246526546262654626265462654626546566It is in the grammar.

Page 42: AssignmentMP345

TITLE: Study of Lex- A Lexical Analyzer Generator.

Theory:

Lex is a program generator designed for lexical processing of character input streams. It accepts a high level, problem oriented specification for character string matching and produces a program in a general purpose language which recognizes regular expressions. The user in the source specifications given to lex specifies the regular expressions. The Lex written code recognizes these expressions in an input stream and partitions the input stream into strings matching the expressions. At the boundaries between strings program sections provided by the user are executed. The Lex source file associates the regular expressions and the program fragments . As each expression appears in the input to the program written by lex , the corresponding fragment is excuted.

Lex turns the user’s expressions and actions ( called source in this memo) into the host general purpose language; the generated program is named yylex. The yylex program will recognize expressions in a stream ( called input in this memo) and perform the specified actions for each expression as it is detected.

+--------------+

Source -> | Lex | ->yylex

+-------------+

+------------+

Input -> | yylex | -> Output

+ ----------- +

An Overview of Lex

Figure 1

Page 43: AssignmentMP345

Working of LEX :

Summary of Source Format.

The general form of a Lex source file is:{ definitions}

%%{ rules}

%%{ user subroutines}

The definitions section contains a combination of

1) Definitions, in the form “ name space translation “2) Included code, in the form “ space code”3) Include code , in the form

%{code

%}4) Start conditions, given in the form

%S name1 name2………..5) Character set tables , in the form

%Tnumber space character-string……%T

6) Changes to internal array sizes, in the form%x num

Where num is a decimal integer representing an array size and x selects the parameter as follows.

Letter Parameter P Positions n Statese tree nodesa transitionsk packed character classes o output array size

Page 44: AssignmentMP345

Lines in the rules section have the form “ expression action “ where the action may be continued on succeeding lines by using braces to delimit it.

Regular expression in Lex use the following operators.

x the character “x”“x” an “x” , even if x is an operator\x an “x”, even if x is an operator. [xy] the character x or y[x-z] the characters x, y or z[ ^x] any character but x.

. any character but newline^x an x at the beginning of a line.<y>x an x when lex is in start condition y. x$ an x at the end of a line.x? an optional x.x* 0,1,2,……..instances of x. x+ 1,2,3,…….. instances of x.x | y an x or a y.(x) an xx / y an x but only if followed by y{ xx } the translation of xx from the definitions section. x{ m ,n } m through n occurrences of x.

Page 45: AssignmentMP345

Aim: Study of Device drivers.

Theory:

The purpose of a device driver is to handle requests made by the kernel with regard to a particular type of device. There is a well defined and consistent interface for the kernel to make these requests. By isolating device specific code in device drivers and by having a consistent interface to the kernel, adding a new device is easier.

A device driver is a software module that resides within the Digital Unix kernel and is the software interface to a hardware device or devices. A hardware device is a peripheral, such as a disk controller , tape controller , or network controller device. In general , there is a one device driver for each type of hardware device. Device drivers can be classified as:

1) Block Device Drivers2) Character Device Drivers(including terminal drivers)3) Network Device Drivers4) Pseudo device drivers.

1. Block Device Driver

A block device driver is a driver that performs I/O by using file system block sized buffers from a buffer cache supplied by the kernel. The kernel also provides for the device driver support interfaces that copy data between the buffer cache and the address space of a process.

Block device drivers are particularly well- suited for disk drives, the most common block devices. For block devices , all I/O occurs through the buffer cache.

2. Character Device Driver:

A character device driver does not handle I/O through the buffer cache, so it is not tied to a single approach for handling I/O. You can use a character

Page 46: AssignmentMP345

SARASWATI EDUCATION SOCIETY’S

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY

DR.N.Y.TASGAONKAR EDUCATIONAL COMPUS, CHANDHAI, POST:-NASAPURE, TAL:-KARJAT, DIST:-RAIGAD

device driver for a device such as a line printer that handles one character at a time. However , character drivers are not limited to performing I/O one character at a time. ( despite the name “ character” driver) . For example , tape drivers frequently perform I/O in 10 K chunks. You can also use a character device driver when it is necessary to copy data directly to or froma user process. Because of their flexibility in handling I/O , many drivers are character drivers . Line printers, interactive terminals, and graphics displays are examples of devices that require character device drivers.

A terminal device driver is actually a character device driver that handlesI/O character processing for a variety of terminal devices. Like any character device, a terminal device can accept or supply a stream of data based on a request from a user process. It cannot be mounted as a file system and therefore does not use data caching.

3. Network Device Driver

A network Device driver attaches a network subsystem to a network interface , prepares the network interface for operation, and governs the transmission and reception of network frames over the network interface.

4. Pseudo device Driver

Not all device drivers control physical hardware . Such device drivers are called “pseudo device” drivers. Like block and character device drivers , pseudo device drivers make use of the device driver interfaces. Unlike block and character device drivers, pseudo device drivers do not operate on a bus. One example of a pseudo device driver is the pseudo terminal or pty terminal driver , which simulates a terminal device . The pty terminal driver is a character device driver typically used for remote logins.

CONLUSION: Thus we have studied Device drivers.

DEPT. OF COMPUTER ENGINEERING

Page 47: AssignmentMP345

SARASWATI EDUCATION SOCIETY’S

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY

DR.N.Y.TASGAONKAR EDUCATIONAL COMPUS, CHANDHAI, POST:-NASAPURE, TAL:-KARJAT, DIST:-RAIGAD

AIM:- PROGRAM FOR COMPUTE A FIRST OF NON TERMINALS

DEPT. OF COMPUTER ENGINEERING

Page 48: AssignmentMP345

SARASWATI EDUCATION SOCIETY’S

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY

DR.N.Y.TASGAONKAR EDUCATIONAL COMPUS, CHANDHAI, POST:-NASAPURE, TAL:-KARJAT, DIST:-RAIGAD

AIM:-

PROGRAM FOR COMPUTE A FOLLOW OF NON TERMINALS

Write this practical from a CD_CSE_VII.pdf file

which attached to this mail

DEPT. OF COMPUTER ENGINEERING

Page 49: AssignmentMP345

SARASWATI EDUCATION SOCIETY’S

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY

DR.N.Y.TASGAONKAR EDUCATIONAL COMPUS, CHANDHAI, POST:-NASAPURE, TAL:-KARJAT, DIST:-RAIGAD

AIM:-

STUDY OF LEX AND YACC TOOLS

Write this practical from a CD_CSE_VII.pdf file

which attached to this mail

(MINIMUM 5 PAGES)

DEPT. OF COMPUTER ENGINEERING

Page 50: AssignmentMP345

SARASWATI EDUCATION SOCIETY’S

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY

DR.N.Y.TASGAONKAR EDUCATIONAL COMPUS, CHANDHAI, POST:-NASAPURE, TAL:-KARJAT, DIST:-RAIGAD

AIM:-

CONSTRUCTING NFA FROM REGULAR EXPRESSION

Write this practical from a CD_CSE_VII.pdf file

which attached to this mail

(MINIMUM 5 PAGES)

DEPT. OF COMPUTER ENGINEERING