CD Program 1
CD Program 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Typedef struct {
Char data_type[20];
Char variable[20];
Char value[20];
} SymbolEntry;
Strcpy(entry->value, “null”);
}}
Int main() {
outputFile = fopen(“output.txt”,
“w”); if (outputFile == NULL) {
perror(“Error opening output file”);
fclose(inputFile);
return 1;
Char line[100];
SymbolEntry entry;
Fprintf(outputFile, “| %-15s | %-15s | %-15s |\n”, “Data Type”, “Variable”,
“Value”); Fprintf(outputFile, “| | | |\n”);
While (fgets(line, sizeof(line), inputFile) != NULL)
{ processStatement(line, &entry);
Fprintf(outputFile, “| %-15s | %-15s | %-15s |\n”,
Entry.data_type, entry.variable,
entry.value);
}
Fclose(inputFile);
Fclose(outputFile);
Return 0;
PROGRAM:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
Int isKeyword(char *word) {
}
Int isNumericConstant(char *token)
{ Int I;
Int isOperator(char c) {
Char operators[][3] = {“==”, “+”, “-“, “*”, “/”, “=”, “<”, “>”, “&”, “|”, “!”};
Int I;
For (I = 0; I < sizeof(operators) / sizeof(operators[0]); ++i)
{ If (strncmp(operators[i], &c, 1) == 0)
Return 1; // Operator found
}
Return 0; // Not an operator
}
Int isPunctuator(char c) {
Char punctuators[] = {‘,’, ‘;’, ‘(‘, ‘)’, ‘{‘,
‘}’}; Int I;
For (I = 0; I < sizeof(punctuators) / sizeof(punctuators[0]); ++i)
{ If (punctuators[i] == c)
Return 1; // Punctuator found
}
Return 0; // Not a punctuator
Int main() {
Buffer[i++] =
c;
While ((c = fgetc(inputFile)) && (isalnum© || c == ‘_’))
{ Buffer[i++] = c;
}
Buffer[i] = ‘\0’;
If (isKeyword(buffer)) {
} else if (isNumericConstant(buffer)) {
Fprintf(outputFile, “<constant, %s>\n”,
buffer);
} else
{
Fprintf(outputFile, “<identifier, %s>\n”, buffer);
Fseek(inputFile, -1, SEEK_CUR); // Move the file pointer back one position
Token[i++] = c;
Token[i] = ‘\0’;
Fprintf(outputFile, “<constant, %s>\n”, token);
} else if (isspace©) {
} else if (isOperator©) {
Fprintf(outputFile, “<operator, %c>\n”, c);
If (c == ‘=’ && (c = fgetc(inputFile)) == ‘=’)
{ Fprintf(outputFile, “<operator, 🡺\
n”);
} else {
Fseek(inputFile, -1, SEEK_CUR); // Move the file pointer back one position
} else if (isPunctuator©) {
c);
} else {
Fclose(inputFile);
Fclose(outputFile);
Printf(“Token analysis complete. Results written to output.txt.\n”);
Return 0;
}
PROGRAM:
%{
#indude <stdio.h>
#include <sring.h
> Int c = 0 ;
%}
%%
( [A – za – z0 – 9) +{i++;}
%{
#indude <stdio.h>
#include <sring.h
> Int c = 0 ;
%}
%%
( [A – za – z0 – 9) +{i++;}
Int yywarp()
Return 1;
PROGRAM:
LEX PART:
%{
#include<stdio.h>
#include “y.tab.h”
Extern int yylval;
%}
%%
[0-9]+ {
Yylval=atoi(yytext);
Return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
Int yywrap()
{ Return 1;
}
YACC PART:
%{
#include<stdio.h>
Int flag=0;
%}
%token NUMBER
};
E:E’+’E {$$=$1+$3;}
|E’-‘E {$$=$1-$3;}
|E’*’E {$$=$1*$3;}
|E’/’E {$$=$1/$3;}
|E’%’E {$$=$1%$3;}
|’(‘E’)’ {$$=$2;}
| NUMBER {$$=$1;}
%%
Void main()
{
Printf(“\nEnter Any Arithmetic Expression which can have operations Addition,
Subtraction, Multiplication, Divison, Modulus and Round brackets:\n”);
Yyparse();
If(flag==0)
Printf(“\nEntered arithmetic expression is Valid\n\n”);
Void yyerror()
#include <stdio.h>
#include <string.h>
Struct op
{
Char l[20];
Char r[20];
} op[10];
Int main()
Int I, j, n, lineno =
1; Char *match;
Printf(“Enter the number of values:
“); Scanf(“%d”, &n);
For (I = 0; I < n; i++)
Printf(“\tleft\t”);
Scanf(“%s”, op[i].l);
Printf(“\tright:\t”);
Scanf(“%s”, op[i].r);
}
Printf(“Intermediate Code\n”);
For (I = 0; I < n; i++)
{
Match = strstr(op[j].r,
op[i].l); If (match){
Printf(“\n %s is live at %s \n “, op[i].l, op[j].r);
}
}
Return 0;
PROGRAM
#include<stdio.h>
#include<conio.h> #include<stdlib.h> #define size 5
struct stack
int s[size];
int stfull()
return 1;
else
return 0;
{ st.top++;
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop()
{
int item;
item st. s[st.top];
void display()
{
int i;
if (stempty()) printf("\
int main()
char ans;
st.top = -1;
printf("\n\timplementation Of Stack");
do { printf(inMain Menu");
switch (choice)
case 1:
scanf("%d", &item);
if (stfull()) printf("\
push(item);
break;
case 2
if (stempty())
printf("\nEmpty stack!Underflow!!");
else
{ item = pop();
break;
case 3: display();
break;
case 4:
goto halt;
ans = getche();
return 0;
)
PROGRAM :
#include<stdio.h>
main()
{
struct da
int ptr,left,right;
char label;
}dag[25];
int ptr,l,j,change,n=0,i=0,state=1,x,y,k;char
store,*input1,input[25],var; clrscr();
for(i=0;i<25;i++)
dag[i].ptr=NULL;
dag[i].left=NULL;
dag[i].right=NULL;
dag[i].label=NULL;
}
for(j=i;input1[j]!='(';j--);
for(x=j+1;x<i;x++)
if(isalpha(input1[x]))
input[n++]=input1[x]; else
if(input1[x]!='0')
store=input1[x]; input[n+
+]=store;
for(x=j;x<=i;x++)
input1[x]='0';
if(input1[0]!='0')goto a;
for(i=0;i<n;i++)
{
dag[i].label=input[i];
dag[i].ptr=i;
if(!isalpha(input[i])&&!isdigit(input[i]))
dag[i].right=i-1;
ptr=i; var=input[i-
1];
if(isalpha(var))
ptr=ptr-2;
else
ptr=i-1;
b:
if(!isalpha(var)&&!isdigit(var))
goto b;
else
ptr=ptr-1;
}
dag[i].left=ptr;
}
printf("\n\n PTR \t\t LEFT PTR \t\t RIGHT PTR \t\t LABEL
\n\n");
%d\t%d\t%d\t%c\n",dag[i].ptr,dag[i].left,dag[i].right,dag[i].la bel);
getch();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if((dag[i].label==dag[j].label&&dag[i].left==dag[j].left)&&dag[i].right== dag[.right)
{
for(k=0;k<n;k++)
{
dag[j].ptr=dag[i].ptr;
\t\t%d\t\t%d\t\t%c\n",dag[i].ptr,dag[i].left,dag[i].right,dag[i
].label);
getch();
}
PROGRAM :
#include <stdio.h>
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main() {
char icode[10][30], str[20], opr[10];
int i = 0;
clrscr();
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;
printf("\n\tMov %c,R%d", str[2], 1); printf(\n\t%s%c,R%d", opr, str[4],
i); printf("\n\tMov R%d,%c", i, str[0]);
} while (strcmp(icode[++i], "exit") != 0);
getch();
}
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct op
char l;
char r[20];
op[10],pr[10];
void main()
int a,I,,k,j,n,z=0,m,q;
char*p,*1;
char temp,t;
char*temp;
clrscr();
scanf(“%d”,&n); for(i=0;i<n;i+
+)
Printf(“left”);
Op[i].l=grtch();
Printf(“\t right:”);
Scanf(“%s”,,op[i],r);
}
Printf(“intermediate code”);
For(i=0;i<n;i++)
Printf(“%c:’op[i].e);
Printf(“%s\n”;i++)
{temp=op[j].l;
For(j=0;j<n;j++)
P=strch(op[j].r,temp);
If(p)
Pr[z].t=op[i].l;
Strcpy(pr[z].r,op[i],r);
Z++
pr[z].z=op[n-1].l;
Strcpy(pr[z].r;op[n-1].r);
Z++;
For(k=0;k<z;k++)
Printf(“%c\t=”,pr[k].l);
Printf(“%s\n”,pr[k],r);
}
For(m=0;m<z;m++)
Temp=pr[r].r;
For(j=m+1;j<z;j++)
P=strstr(temp,pr[j].r);
If(p)
T=pr[j].l;
Pr[j].l=pr[m].l;
For(i=0;i<z;i++)
L=strchr(pr[i].r,t);
if(l)
A=l-pr[i].r;
Printf(“pos:%d”,a);
Pr[i].r[a]=pr[m].l;
}}
For(i=0;i<z;i++)
Printf(“%c\t”.pr[i].l);
Printf(“%s\n”,pr[i].r);
}
For(i=0;i<z;i++)
For(j=i+1;j<z;j++)
Q=strcmp(pr[i].r,pr[j].r);
If((pr[i].l==pr[j].l)&&!q)
Pr[i].l=’\0’;
Printf(“optimized code”);
For(i=0;i<z;i++)
If(pr[i].l!=’\0’)
Printf(“%c=”,pr[i].l);
Printf(“%s\n”,pr[i].r);
getch();
}
PROGRAM
# include<stdio.h>
# include<conio.h>
#include<alloc.h>
#include<string.h>
struct Listnode
char data[50];
int leader,block,u_goto,c_goto;
char label[10],target[10];
}*temp,*cur,*first=NULL,*last=NULL,*cur1;
FILE *fpr;
Listnode)); strcpy(temp->data,code);
strcpy(temp->label,'\0');
strcpy(temp->target,'\0');
temp->leader=0;
temp->block=0;
temp->u_goto=0;
temp->c_goto=0;
temp->next=NULL;
if(first==NULL)
first=temp;
last=temp;
else
last->next=temp;
last=temp;
void main()
char codeline[50];
char c,dup[50],target[10];
char *substring,*token;
int i=0,block,block1;
int j=0;
fpr= fopen("CDP7.txt","r");
clrscr();
while((c=getc(fpr))!=EOF)
if(c!='\n')
{
codeline[i]=c;
i++;
else
codeline[i]='\0';
createnode(codeline);
i=0;
0'; createnode(codeline);
fclose(fpr);
cur=first;
cur->leader=1;
while(cur!=NULL)
substring=strstr((cur->data),"if");
if(substring==NULL)
if((strstr((cur->data),"goto"))!=NULL)
cur->u_goto=1;
(cur->next)->leader=1;
}
else
cur->c_goto=1;
(cur->next)->leader=1;
substring=strstr((cur->data),":");
if(substring!=NULL)
cur->leader=1;
substring=strstr((cur->data),"call");
if(substring!=NULL)
cur->leader=1;
if(strstr(cur->data,"return")!=NULL)
cur->leader=1;
(cur->next)->leader=1;
cur=cur->next;
cur=first;
while(cur!=NULL)
if((cur->u_goto==1)||(cur->c_goto==1))
substring=strstr(cur->data,":");
if(substring!=NULL)
token=strstr(substring,"L" );
if(token!=NULL)
strcpy(cur->target,token);
else
substring=strstr(cur->data,"L");
if(substring!=NULL)
strcpy(cur->target,substring);
if(strstr(cur->data,":")!=NULL)
strcpy(dup,cur->data);
token=strtok(dup,":");
// printf("\ntoken:%s",token);
if(token!=NULL)
strcpy(cur->label,token);
}
cur=cur->next;
cur=first;
while(cur!= NULL)
cur=cur->next;
if((cur->leader)==1)
{ j+
+;
cur->block=j;
else
cur->block=j;
printf("\n\n......Basic Blocks......\n");
cur=first;
j=0;
printf("\nBlock %d:",j);
while(cur!=NULL)
if ((cur->block)==j)
{
printf("%s",cur->data);
printf("\n\t");
cur=cur->next;
else
{ j+
+;
printf("\nBlock %d:",j);
n\n");
cur=first;
i=0;
while(cur!=NULL)
if((cur->block)!=(cur->next)->block)
block=cur->block;
if(cur->u_goto==1)
strcpy(target,cur->target);
cur1=first; while(cur1!
=NULL)
{
if(strcmp(cur1->label,target)==0)
block1=cur1->block;
printf("\t\tBlock%d--------->Block%d\n",block,block1);
cur1=cur1->next;
else if(cur->c_goto==1)
strcpy(target,cur->target);
cur1=first; while(cur1!
=NULL)
if(strcmp(cur1->label,target)==0)
block1=cur1->block;
printf("\t\tBlock%d---TRUE--->Block%d---FALSE- ->Block%d\n",block,block1,(block+1));
cur1=cur1->next;
else if(strstr(cur->data,"return")==NULL)
printf("\t\tBlock%d--------->Block%d\n",block,(block+1));
}
else
printf("\t\tBlock%d--------->NULL\n",block);
cur=cur->next;
cur=last;
block= cur->block;
printf("\t\tBlock%d-------->NULL",block);
getch();