0% found this document useful (0 votes)
5 views8 pages

Te Cs Spcc Lab Edited1.1

The document contains multiple C programs that demonstrate different functionalities such as generating quadruples from input, converting C code to machine code, eliminating left recursion in grammars, and extracting macro names from source code. Each program reads input, processes it, and outputs results accordingly. The output examples illustrate how the programs work with specific inputs.

Uploaded by

Mazin Momin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Te Cs Spcc Lab Edited1.1

The document contains multiple C programs that demonstrate different functionalities such as generating quadruples from input, converting C code to machine code, eliminating left recursion in grammars, and extracting macro names from source code. Each program reads input, processes it, and outputs results accordingly. The output examples illustrate how the programs work with specific inputs.

Uploaded by

Mazin Momin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Program:

//Quadruples program
#include<stdio.h>
int main()
{
char inp[20][20]; int len[10]; int op[10]
[10],a1[10][10],a2[10][10],res[10][10]; char temp;
FILE * f1; int
i=0,j=0,a,b,fop=0,fa1=0,fres=0,t;
f1=fopen("input1.txt","r"); while(1) {

temp=fgetc(f1);
if (temp=='\n')
{
len[i]=j;
j=0;
i++;
}
elseif(feof(f1))
break;
else
{
inp[i][j]=temp;
j++;
}
} a=i; b=j;
printf("Input
file:\n"); for
(i=0;i<a;i++) {

for (j=0;j<len[i];j++)
printf("%c",inp[i][j]);
printf("\n");
}
//Start of the table
printf("Result \tArg1 \tOpt \tArg2 \n");
for (i=0;i<a;i++)
{
t=0;
for (j=0;j<len[i];j++)
{
temp=inp[i][j];
if (fres==0)
{
if (temp=='=')
{
fres=1; t=0;printf("\t");
}
else
{
printf("%c",temp);
op[i][t]=temp;
t++;
}
}
else
{
if (fa1==0)
{
if ((temp=='+')||(temp=='-')||(temp=='*')||(temp=='/'))
{
fa1=1; printf("\t");
printf("%c",temp);
printf("\t");
}
else
printf("%c",temp);
if (j==(len[i]-1))
{
printf("\t");
printf("=");
printf("\t");
}
}
else
{
printf("%c",temp);
}
}
}
fres=fa1=fop=0;
printf("\n");
}
fclose(f1);
return 0;
}

Output:
Code:
#include<stdio.h>
int main()
{
char c[100];
char oper,arg1[10],arg2[10],res[10];
int i=0,r=0,a1p=1,rp=1;
printf("Enter C Code ");
scanf("%s",c);
while (c[i]!='\0')
{
if (c[i]==';')
break;
if (rp==1)
{
if (c[i]!='=')
{
res[r]=c[i];
r++;
}
else
{
rp=0;
es[r]='\0';
r=0;
}
}
else if (a1p==1)
{
if (c[i]!='*' && c[i]!='+' & c[i]!='-')
{
arg1[r]=c[i];
r++;
}
else
{
a1p=0; arg1[r]='\0'; r=0;
oper=c[i];
}
}
else
{
arg2[r]=c[i];
r++;
}
i++;
}
arg2[r]='\0';
printf("Machine Code is \n");
printf("MOV AX,%s\n",arg1);
if (oper=='*')
printf("MUL %s\n",arg2);
if (oper=='+')
printf("ADD AX,%s\n",arg2);
if (oper=='-')
printf("SUB AX,%s\n",arg2); printf("MOV %s,AX\n",res);
}

Output:
[User@localhost Desktop]$ gcc codegen.c
[User@localhost Desktop]$ ./a.out
Enter C Code d=t2;Machine Code is MOV AX,t2
MOV d,AX
[User@localhost Desktop]$ ./a.outEnter C Code t1=a*b;
Machine Code isMOV AX,a MUL b
MOV t1,AX
[User@localhost Desktop]$ ./a.out
Enter C Code t2=c+t1;

Machine Code is
MOV AX,c
ADD AX,t1
MOV t2,AX
Program :
#include<stdio.h>
#include<string.h >
#define SIZE 50

int main ()
{
char nt;
char beta,alpha;
int num;
char prod[10][SIZE];
int index=3;
printf("Total number of productions : ");
scanf("%d",&num);
printf("Enter the grammar(s):\n");
for(int i=0;i<num;i++)
{
scanf("%s",prod[i]);
}
for(int i=0;i<num;i++)
{
printf("\nThe grammar : : : %s",prod[i]);
nt=prod[i][0];
if(nt==prod[i][index])
{
alpha=prod[i][index+1];
printf(" It is left recursive.\n");
while(prod[i][index]!=0 && prod[i][index]!='|')
index++;
if(prod[i][index]!=0)
{
beta=prod[i][index+1];
printf("After eliminating left recursion:\n");
printf("%c->%c%c\'",nt,beta,nt);
printf("\n%c\'->%c%c\'|E\n",nt,alpha,nt);
}
else
printf(" It can't be reduced\n");

}
else
printf(" It is not left recursive.\n");index=3;
}
}
Output :
Program :
MakeMNT.c
using namespace std;
#include<iostream>
#include<string.h>
#include<stdio.h>
int main(int argc, char **argv)
{

char buf[100],mnt[10][100]; int


flag = 0,flag1=0, cnt = 0, i=0;
FILE *in, *out; if(argc == 1) {
} else {

cout<<"Error:Enter input source code\n";

in = fopen(argv[1], "r");
out = fopen("MNT", "w");
cout<<"INPUT:";
while(fgetc(in) != EOF)
{
fseek(in, (ftell(in) - 1), SEEK_SET);
fgets(buf, 100, in);
cout<<buf;
if(flag==1 && flag1==0)
{
strncpy(mnt[i++],buf,4);
flag1=1;
}
if(strstr(buf,"MACRO")!='\0')
flag=1;
if(strstr(buf,"MEND")!='\0')
{
flag=0;
flag1=0;
}
}
int j=0;
cout<<"Macro Name\n";
char temp[100];
fputs("Macro Name\n",out);
while(j<i)
{
strcpy(temp,mnt[j]);
strcat(temp," \n");
fputs(temp,out);
cout<<mnt[j++]<<"\n";
}
}
fclose(in);
return 0;
}
Assume INPUT file (prog.txt ) :

A 1,Data
MACRO
INC1
A 1,DATA
A 2,DATA
MEND
DATA DC F'10'
M 2,Data
MACRO
ADD2 &ARG1
A 1,&ARG1
MEND
INCR
ADD

OUTPUT:

Run
> MakeMNT prog.txt

Output :
Macro Name
INC1
ADD2

=========

You might also like