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

#Include Int Main (Int X, y 5 X Y+4 Printf ("X %D",X) Return 0 )

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)
28 views8 pages

#Include Int Main (Int X, y 5 X Y+4 Printf ("X %D",X) Return 0 )

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

C-program

#include<stdio.h>
int main()
{
int x,y=5;
x=y+4;
printf("x=%d",x);
return 0;
}

Output
C-program
#include <stdio.h>
#include <string.h>
void main()
{
char icode[10][30], str[20], opr[10];
int i=0;
printf("Enter the set of intermediate code (terminated by 'exit'):\n");
do
{
scanf("%s", icode[i]);
}while (strcmp(icode[i++], "exit") != 0);
printf("Target code generated is:\n");
i=0;
do
{
strcpy(str, icode[i]);
switch (str[3])
{
case '+':
strcpy(opr, "ADD");
break;
case '-':
strcpy(opr, "SUB");
break;
case '*':
strcpy(opr, "MUL");
break;
case '/':
strcpy(opr, "DIV");
break;
case '\0':
printf("MOV %c, R%d\n", str[2], i);
printf("MOV R%d, %c\n", i, str[0]);
continue;
default:
continue; // Skip if the operator is unknown
}
printf("MOV %c, R%d\n", str[2], i);
printf("%s %c, R%d\n", opr, str[4], i);
printf("MOV R%d, %c\n", i, str[0]);
}while (strcmp(icode[++i], "exit") != 0);
}
C-program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int i=1,j=0,no=0,tmpch=90;
char str[100],left[15],right[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
struct exp
{
int pos;
char op;
}k[15];
void main()
{
printf("Enter the Expression : ");
scanf("%s",str);
printf("The intermediate code is :\n");
findopr();
explore();
}
void findopr()
{
for(i=0;str[i]!='\0';i++)
if(str[i]==':')
{
k[j].pos=i;
k[j++].op=':';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='/')
{
k[j].pos=i;
k[j++].op='/';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='*')
{
k[j].pos=i;
k[j++].op='*';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='+')
{
k[j].pos=i;
k[j++].op='+';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='-')
{
k[j].pos=i;
k[j++].op='-';
}
}
void explore()
{
i=1;
while(k[i].op!='\0')
{
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos]=tmpch--;
printf("\t%c := %s%c%s\n",str[k[i].pos],left,k[i].op,right);
i++;
}
fright(-1);
if(no==0)
{
fleft(strlen(str));
printf("\t%s := %s\n",right,left);
exit(0);
}
printf("\t%s := %c\n",right,str[k[--i].pos]);
}
void fleft(int x)
{
int w=0,flag=0;
x--;
while(x!= -1 &&str[x]!= '+' &&str[x]!='*'&&str[x]!='='&&str[x]!='\0'&&str[x]!='-
'&&str[x]!='/'&&str[x]!=':')
{
if(str[x]!='$'&& flag==0)
{
left[w++]=str[x];
left[w]='\0';
str[x]='$';
flag=1;
}
x--;
}
}
void fright(int x)
{
int w=0,flag=0;
x++;
while(x!= -1 && str[x]!= '+'&&str[x]!='*'&&str[x]!='\0'&&str[x]!='='&&str[x]!=':'&&str[x]!='-
'&&str[x]!='/')
{
if(str[x]!='$'&& flag==0)
{
right[w++]=str[x];
right[w]='\0';
str[x]='$';
flag=1;
}
x++;
}
}
C-program
#include<stdio.h>

#include<math.h>

#include<string.h>

#include<ctype.h>

#include<stdlib.h>

int n, m = 0, p, i = 0, j = 0;
char a[10][10], f[10];
void follow(char c);
void first(char c);
int main() {
int i, z;
char c, ch;
printf("Enter the no of productions : \n");
scanf("%d", & n);
printf("Enter the productions:\n");
for (i = 0; i < n; i++)
scanf("%s%c", a[i], & ch);
do {
m = 0;
printf("Enter a variable whose fisrt & follow is to be found:");

scanf("%c", & c);


first(c);
printf("First(%c)={", c);
for (i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
strcpy(f, " ");
m = 0;
follow(c);
printf("Follow(%c)={", c);
for (i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
printf("Want to continue or not(1/0) ? ");
scanf("%d%c", & z, & ch);
}
while (z == 1);
return (0);
}
void first(char c) {
int k;
if (!isupper(c))
f[m++] = c;
for (k = 0; k < n; k++) {
if (a[k][0] == c) {
if (a[k][2] == '$')
follow(a[k][0]);
else if (islower(a[k][2]))
f[m++] = a[k][2];
else
first(a[k][2]);

}
}
}
void follow(char c) {
if (a[0][0] == c)
f[m++] = '$';
for (i = 0; i < n; i++) {
for (j = 2; j < strlen(a[i]); j++) {
if (a[i][j] == c) {
if (a[i][j + 1] != '\0')
first(a[i][j + 1]);
if (a[i][j + 1] == '\0' && c != a[i][0])
follow(a[i][0]);

}
}
}
}
Output

You might also like