Wednesday, 3 June 2015

Saturday, 2 May 2015

STRUCTURE PROGRAMMING AND COMPILER CODE

STRUCTURE PROGRAMMING AND COMPILER CODE

1.FIRST AND FOLLOW

2.ELIMINATION OF LEFT RECURSION

3.SHIFT REDUCE PARSER

4.CODE OPTIMISATION

5.2 PASS ASSEMBLER

          input.txt

6.LEXICAL ANALYSER

         lexinput.txt

7.2 PASS MACRO

         input.txt

input.txt

MACRO

INCR &ARG1,&ARG2,&ARG3

A 1,&ARG1

A 2,&ARG2

A 3,&ARG3

MEND

INCR DATA1,DATA2,DATA3

INCR DATA2,DATA3,DATA1

DATA1 DC F'5'

DATA2 DC F'10'

DATA3 DC F'15'

END

2 PASS MACRO

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct mdt_table{

int index;

char card[30];

}MDT;

typedef struct mnt_table{

int index;

char name[15];

int mdtindex;

}MNT;

typedef struct ala_table{

char index[3];

char arg[15];

}ALA;

typedef struct ala_array{

ALA item[5];

}ALA_ITEM;

MNT mnt[5];

MDT mdt[40];

ALA ala[5];

int mdtc,mntc,mdtp,alac,alap;

char word3[10],ch3[2],word[10],line[20],ch,ch1[2],ch2[2],line1[20],line2[20],k;

FILE *input,*inter,*output;

fpos_t pos;

char* num[5]={"#1","#2","#3","#4","#5"};

void get_word(){

int i=0;

//char ch;

ch=getc(input);

while(ch!=EOF){

if((ch!=' ')&&(ch!=',')&&(ch!='\n'))

word[i++]=ch;

if((ch==' ')||(ch==',')||(ch=='\n')){

word[i]='\0';

break;

}

ch=getc(input);

}

}

void display_mnt(){

int i=0;

printf("\nMNT\n");

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

printf("%d\t%s\t%d",mnt[i].index,mnt[i].name,mnt[i].mdtindex);

printf("\n");

}

}

void display_mdt(){

int i=0;

printf("\nMDT\n");

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

printf("%d\t%s",mdt[i].index,mdt[i].card);

printf("\n");

}

}

void display_ala(){

int i=0;

printf("\nALA\n");

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

printf("%s\t%s",ala[i].index,ala[i].arg);

printf("\n");

}

}

void display_ala_item(ALA_ITEM obj){

int i;

printf("\nALA ITEM\n");

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

printf("%s\t%s",obj.item[i].index,obj.item[i].arg);

printf("\n");

}

}

void search_ALA(){

int i;

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

if(strcmp(word,ala[i].arg)==0){

strcpy(word,ala[i].index);

}

}

}

void PASS1(){

int i;

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

inter=fopen("inter.txt","w+");

mdtc=mntc=alac=0;

while(!feof(input)){

fgetpos(input,&pos);

get_word();

/*printf("%s\n",word);

fprintf(inter,"%s\n",word);*/

if(strcmp(word,"MACRO")!=0){

fsetpos(input,&pos);

fgets(line,20,input);

fprintf(inter,"%s",line);

}

if(strcmp(word,"MACRO")==0){

fgetpos(input,&pos);

get_word();

//mnt

//printf("%s",word);

strcpy(mnt[mntc].name,word);

mnt[mntc].index=mntc;

mnt[mntc].mdtindex=mdtc;

mntc++;

display_mnt();

//mdt

fsetpos(input,&pos);

i=0;

//enter arg line into mdt

while(!feof(input)){

ch=getc(input);

if(ch=='\n'){

line[i]='\0';

break;

}

line[i++]=ch;

}

//enter into mdt

//printf("%s",line);

mdt[mdtc].index=mdtc;

strcpy(mdt[mdtc].card,line);

mdtc++;

display_mdt();

//setup ala

fsetpos(input,&pos);

get_word();

do{

get_word();

strcpy(ala[alac].arg,word);

strcpy(ala[alac].index,num[alac]);

alac++;

}while(ch!='\n');//this is why ch is global

display_ala();

fgetpos(input,&pos);

get_word();

ch1[0]=ch;

ch1[1]='\0';

//printf("%s%s\n",word,ch1);

while(strcmp(word,"MEND")!=0){

//clear the array

memset(line1,0,strlen(line1));

do{

search_ALA();

strcat(line1,word);

strcat(line1,ch1);

//printf("\n%s",line1);

get_word();

ch1[0]=ch;

}while(ch1[0]!='\n');

search_ALA();

strcat(line1,word);

//strcat(line1,ch1);

mdt[mdtc].index=mdtc;

strcpy(mdt[mdtc].card,line1);

mdtc++;

get_word();

ch1[0]=ch;

}

//to enter mend to mdt

mdt[mdtc].index=mdtc;

strcpy(mdt[mdtc].card,word);

mdtc++;

display_mdt();

}

}

fclose(input);

fclose(inter);

}

int search_MNT(){

int i;

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

if(strcmp(word,mnt[i].name)==0){

mdtp=mnt[i].mdtindex;

return mnt[i].index;

}

}

return -1;

}

void get_word_line(){

int i=0;

ch3[1]='\0';

ch3[0]=line1[k];

memset(word3,0,strlen(word));

while(ch3[0]!='\0'){

if((ch3[0]!=' ')&&(ch3[0]!=',')&&(ch3[0]!='\0'))

word3[i++]=ch3[0];

if((ch3[0]==' ')||(ch3[0]==',')||(ch3[0]=='\0')){

word3[i]='\0';

break;

}

k++;

ch3[0]=line1[k];

}

}

void search_ALA_ITEM(ALA_ITEM obj){

int i;

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

if(strcmp(word3,obj.item[i].index)==0){

memset(word3,0,strlen(word3));

strcpy(word3,obj.item[i].arg);

}

}

}

void PASS2(){

int f,i;

input=fopen("inter.txt","r");

output=fopen("output.txt","w+");

memset(word,0,strlen(word));

memset(line,0,strlen(line));

while(!feof(input)){

get_word();

ch2[0]=ch;

ch2[1]='\0';

//printf("%s%s",word,ch2);

//search in mnt

f=search_MNT();

//if not in mnt

if(f==-1){

fprintf(output,"%s%s",word,ch2);

if(strcmp(word,"END")==0)break;

}

else{

//setup ala

alap=0;

//for every call, new ALA_ITEM object

ALA_ITEM obj;

do{

get_word();

strcpy(obj.item[alap].index,num[alap]);

strcpy(obj.item[alap].arg,word);

alap++;

}while(ch!='\n');

//display the current ALA_ITEM object

display_ala_item(obj);

mdtp++;

while(strcmp(mdt[mdtp].card,"MEND")!=0){

memset(line1,0,strlen(line1));

strcpy(line1,mdt[mdtp].card);

//fprintf(output,"%s\n",line1);

k=0;

memset(line2,0,strlen(line2));

do{

get_word_line();

//printf("%s%s\n",word3,ch3);

k++;

//replace # with arg from corresponding ala

search_ALA_ITEM(obj);

//append to line

strcat(line2,word3);

strcat(line2,ch3);

}while(ch3[0]!='\0');

fprintf(output,"%s\n",line2);

mdtp++;

}

}

}

}

void main(){

PASS1();

PASS2();

}

lexinput

#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter");
while(a<b)
a=b+c;
c=a;
}

LEXICAL ANALYSER

 #include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()
{
    char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
    int j;
    char fname[50];
    FILE *f1;
printf("enter file path (drive:\\fold\\filename)\n");
scanf("%s",fname);
f1 = fopen(fname,"r");
    if(f1==NULL)
    {
     printf("file not found");
     exit(0);
    }
    while((ch=fgetc(f1))!=EOF)
    {
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch);
op++;
str[i]='\0';
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')




{
while(ch!='>')
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("%c is a header file\n",ch);
i=-1;
break;
}
if(ch=='"')
{
do
{
ch=fgetc(f1);
printf("%c",ch);
}while(ch!='"');
printf("\b is an argument\n");
i=-1;
break;
}
str[i]='\0';
keyw(str);
}
}
if(i!=-1)
{
str[i]=ch;
i++;
}
else
i=0;
    }
printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword\n",p);
kw++;
flag=1;



break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number\n",p);
num++;
}
else
{
if(p[0]!='\0')
{
printf("%s is an identifier\n",p);
id++;
}
}
}
i=-1;
}

INPUT.TXT

JOHN START 0
USING *,15
L 1,FIVE
A 1,FOUR
ST 1,TEMP
FOUR DC F'4'
FIVE DC F'5'
TEMP DS 2F
END

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More