PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Typedef struct {
Char data_type[20];
Char variable[20];
Char value[20];
} SymbolEntry;
Void processStatement(char *statement, SymbolEntry *entry) {
Char *token = strtok(statement, “ =\n”); // Split the statement by space and
‘=’ Strcpy(entry->data_type, token);
Token = strtok(NULL, “ =\
n”); Strcpy(entry->variable,
token); Token = strtok(NULL,
“ =\n”); If (token != NULL) {
Strcpy(entry->value, token);
} else {
Strcpy(entry->value, “null”);
}}
Int main() {
FILE *inputFile, *outputFile;
inputFile = fopen(“input.txt”,
“r”);
if (inputFile == NULL) {
perror(“Error opening input
file”); return 1;
}
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) {
Char keywords[][10] = {“if”, “else”, “while”, “for”, “int”, “float”, “char”,
“return”}; Int I;
For (I = 0; I < sizeof(keywords) / sizeof(keywords[0]); ++i)
{ If (strcmp(keywords[i], word) == 0)
Return 1; // Keyword found
}
Return 0; // Not a keyword
}
Int isNumericConstant(char *token)
{ Int I;
For (I = 0; I < strlen(token); ++i) {
If (!isdigit(token[i]) && token[i] !=
‘.’) Return 0; // Not a numeric constant
}
Return 1; // Numeric constant
}
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() {
FILE *inputFile, *outputFile;
Char c, buffer[50], token[50];
inputFile = fopen(“input.c”,
“r”);
outputFile = fopen(“output.txt”, “w”);
if (inputFile == NULL || outputFile == NULL)
{ printf(“Error opening files.\n”);
return 1;
While ((c = fgetc(inputFile)) != EOF)
{ If (isalnum© || c == ‘_’) {
Int I = 0
Buffer[i++] =
c;
While ((c = fgetc(inputFile)) && (isalnum© || c == ‘_’))
{ Buffer[i++] = c;
}
Buffer[i] = ‘\0’;
If (isKeyword(buffer)) {
Fprintf(outputFile, “<keyword, %s>\n”, 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
} else if (c == ‘”’ || c == ‘\’’) {
Int I = 0;
Token[i++] = c;
While ((c = fgetc(inputFile)) && c != ‘”’ && c != ‘\’’) {
Token[i++] = c;
}
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©) {
Fprintf(outputFile, “<punctuator, %c>\n”,
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++;}
“\n” { printf (“%d\n”, i);
I = 0; }
%%
Int yywrap (void)
{} Int main(){
Yy lex();
Return 0;
}
PROGRAM:
%{
#indude <stdio.h>
#include <sring.h
> Int c = 0 ;
%}
%%
( [A – za – z0 – 9) +{i++;}
“\n” { printf (“%d\n”, i);
I = 0; }
%%
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
%left ‘+’ ‘-‘
%left ‘*’ ‘/’ ‘%’
%left ‘(‘ ‘)’
%%
ArithmeticExpression: E{
Printf(“\nResult=%d\n”,$$);
Return 0;
};
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()
Printf(“\nEntered arithmetic expression is Invalid\n\n”);
Flag=1;
PROGRAM:
#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++)
{
Printf(“Line No=%d\n”, lineno);
Printf(“\t\t\t%s=”, op[i].l);
Printf(“%s\n”, op[i].r);
Lineno++;
}
Printf(“***Data Flow Analysis for the Above Code ***\n”);
For (I = 0; I < n; i++)
{
For (j = 0; j < n; j++)
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 top, )st
int stfull()
if (st. top >= size-1)
return 1;
else
return 0;
void push(int item)
{ st.top++;
st.s[st. top] = item, }
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop()
{
int item;
item st. s[st.top];
st.top; return (item);
}
void display()
{
int i;
if (stempty()) printf("\
nStack is Empty!"); else
for (ist. top; i >= 0; i--) printf("\n%d", st. s);
int main()
int item, choice;
char ans;
st.top = -1;
printf("\n\timplementation Of Stack");
do { printf(inMain Menu");
printf(\n1.Push \n2.Pop \n3.Display \n4.exit");
printf("\nEnter Your Choice"); scanf("%d", &choice);
switch (choice)
case 1:
printf("\nEnter The item to be pushed");
scanf("%d", &item);
if (stfull()) printf("\
nStack is Full!"); else
push(item);
break;
case 2
if (stempty())
printf("\nEmpty stack!Underflow!!");
else
{ item = pop();
printf("\n The popped element is %d", item);
break;
case 3: display();
break;
case 4:
goto halt;
printf("\nDo You want To Continue?");
ans = getche();
) while (ans ==Y' || ans == 'y');
halt:
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;
}
printf("\n\nENTER THE EXPRESSION\n\n");
scanf("%s",input1);
/*EX:((a*b-c))+((b-c)*d)) like this give with paranthesis.limitis 25
char ucan change that*/
for(i=0;i<25;i++)
input[i]=NULL;
l=strlen(input1); a:
for(i=0;input1[i]!=')';i++);
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 SYNTAX TREE FOR GIVEN EXPRESSION\n\n");
printf("\n\n PTR \t\t LEFT PTR \t\t RIGHT PTR \t\t LABEL
\n\n");
for(i=0;i<n;i++)/* draw the syntax tree for the followingoutput
with pointer value*/
printf("\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;
printf("\n DAG FOR GIVEN EXPRESSION\n\n");
printf("\n\n PTR \t LEFT PTR \t RIGHT PTR \t LABEL \n\n");
for(i=0;i<n;i++)/*draw DAG for the following output with pointer value*/
printf("\n %d
\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();
printf("\n Enter the set of intermediate code (terminated by exit):\n");
do
scanf("%s", icode[i]);
while (strcmp(icode[i++], "exit") !=
0); printf(in target code generation");
printf("\n*********");
1=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;
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();
printf(“enter the no of values”);
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++;
Printf(“\nafter deadcode elimination”);
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;
}}
Printf(“eliminate common expression”);
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;
struct Listnode *next;
char label[10],target[10];
}*temp,*cur,*first=NULL,*last=NULL,*cur1;
FILE *fpr;
void createnode(char code[50])
temp=(struct Listnode *)malloc(sizeof(struct
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;
//create last node codeline[i]='\
0'; createnode(codeline);
fclose(fpr);
// find out leaders,conditional stmts
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;
//to find labels and targets
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;
//to identify blocks
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);
//to output the control flow from each
block printf ("\t\t.......Control Flow. \
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();