C Program Compiler Design

C program to implement simple code generator

C program to test whether a given identifier is valid or not

ALGORITHM:
1. Start
2. Get address code sequence.
3. Determine current location of 3 using address (for 1st operand).
4. If current location not already exist generate move (B,O).
5. Update address of A(for 2nd operand).
6. If current value of B and () is null,exist.
7. If they generate operator () A,3 ADPR.
8. Store the move instruction in memory
9. Stop

Algorithm Explanation

  1. Start: The program begins execution.

  2. Get Address Code Sequence: The user inputs a series of three-address code statements, typically in the format x = y op z where op can be an arithmetic operator (+, -, *, or /).

  3. Determine Current Location of Operand: The program checks the current location of the first operand in the statement.

  4. Check Existing Location: If the current location of the first operand (in register) doesn’t already exist, generate a move instruction to load it into a register.

  5. Update Address of Second Operand: The program updates the address of the second operand.

  6. Check for Null Value: If the value of the register is null, the program exits the processing for that operand.

  7. Generate Operator Instruction: If valid, generate the corresponding operation (e.g., add, subtract) for the second operand.

  8. Store Move Instruction in Memory: The program keeps track of the move instructions generated during the execution.

  9. Stop: The program execution ends.


C Program Explanation

1. Structure Definition

typedef struct {
char var[10]; // Variable name
int alive; // Flag to check if the register is in use
} regist;

This structure represents a register with a variable name and a flag indicating whether it’s currently in use.

2. Global Variables

regist preg[10]; // Array of 10 registers
  • An array of regist structures is defined to keep track of the available registers.

3. Function Definitions

  • substring: Extracts a substring from the given expression.
  • getregister: Allocates a register for a variable and returns its index if available.
  • getvar: Extracts the variable part of an expression.

4. Main Function

void main() {
char basic[10][10], var[10][10], fstr[10], op;
int i, j, k, reg, vc = 0, flag = 0;

clrscr(); // Clear screen
printf("nEnter the Three Address Code:n");
// Input loop for three-address code
for(i = 0;; i++) {
gets(basic[i]);
if(strcmp(basic[i], "exit") == 0)
break; // Exit condition
}

printf("nThe Equivalent Assembly Code is:n");

// Process each input line
for(j = 0; j < i; j++) {
getvar(basic[j], var[vc++]); // Get first variable
strcpy(fstr, var[vc - 1]); // Store the first variable
substring(basic[j], strlen(var[vc - 1]) + 1, strlen(basic[j]));
getvar(basic[j], var[vc++]); // Get second variable
reg = getregister(var[vc - 1]); // Get available register

// Move instruction generation
if (preg[reg].alive == 0) {
printf("nMov R%d,%s", reg, var[vc - 1]);
preg[reg].alive = 1; // Mark register as occupied
}

op = basic[j][strlen(var[vc - 1])]; // Get operator
substring(basic[j], strlen(var[vc - 1]) + 1, strlen(basic[j]));
getvar(basic[j], var[vc++]); // Get third variable

// Arithmetic operation generation
switch(op) {
case '+': printf("nAdd"); break;
case '-': printf("nSub"); break;
case '*': printf("nMul"); break;
case '/': printf("nDiv"); break;
}

// Check for variable in registers
flag = 1;
for(k = 0; k <= reg; k++) {
if(strcmp(preg[k].var, var[vc - 1]) == 0) {
printf(" R%d", k, reg); // Use register
preg[k].alive = 0; // Mark as free
flag = 0;
break;
}
}

// If not found in registers
if(flag) {
printf(" %s, R%d", var[vc - 1], reg);
printf("nMov %s, R%d", fstr, reg); // Final move instruction
}

strcpy(preg[reg].var, var[vc - 3]); // Store variable in register
}

getch(); // Wait for user input
}


//C program to implement Simple Code Generator.
#include
#include
#include
#include
#include
typedef struct
{ char var[10];
int alive;
}
regist;
regist preg[10];
void substring(char exp[],int st,int end)
{ int i,j=0;
char dup[10]="";
for(i=st;i<end;i++)< span="">
dup[j++]=exp[i];
dup[j]='0';
strcpy(exp,dup);
} int getregister(char var[])
{ int i;
for(i=0;i<10;i++)
{
if(preg[i].alive==0)
{
strcpy(preg[i].var,var);
break;
}}
return(i);
}
void getvar(char exp[],char v[])
{ int i,j=0;
char var[10]="";
for(i=0;exp[i]!='';i++)
if(isalpha(exp[i]))
var[j++]=exp[i];
else
break;
strcpy(v,var);
}
void main()
{ char basic[10][10],var[10][10],fstr[10],op;
int i,j,k,reg,vc,flag=0;
clrscr();
printf("nEnter the Three Address Code:n");
for(i=0;;i++)
{
gets(basic[i]);
if(strcmp(basic[i],"exit")==0)
break;
}
printf("nThe Equivalent Assembly Code is:n");
for(j=0;j<i;j++)< span="">
{
getvar(basic[j],var[vc++]);
strcpy(fstr,var[vc-1]);
substring(basic[j],strlen(var[vc-1])+1,strlen(basic[j]));
getvar(basic[j],var[vc++]);
reg=getregister(var[vc-1]);
if(preg[reg].alive==0)
{
printf("nMov R%d,%s",reg,var[vc-1]);
preg[reg].alive=1;
}
op=basic[j][strlen(var[vc-1])];
substring(basic[j],strlen(var[vc-1])+1,strlen(basic[j]));
getvar(basic[j],var[vc++]);
switch(op)
{ case '+': printf("nAdd"); break;
case '-': printf("nSub"); break;
case '*': printf("nMul"); break;
case '/': printf("nDiv"); break;
}
flag=1;
for(k=0;k<=reg;k++)
{ if(strcmp(preg[k].var,var[vc-1])==0)
{
printf("R%d, R%d",k,reg);
preg[k].alive=0;
flag=0;
break;
}} if(flag)
{
printf(" %s,R%d",var[vc-1],reg);
printf("nMov %s,R%d",fstr,reg);
}strcpy(preg[reg].var,var[vc-3]);
getch();
}}</i;j++)<></end;i++)<>


OUT PUT

x = a + b
y = x - c
exit


Mov R0, a
Mov R1, b
Add R1, R0
Mov x, R0
Mov R2, x
Mov R3, c
Sub R3, R2
Mov y, R2

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C program to test whether a given identifier is valid or not
C Program

C Program to Test Whether a Given Identifier is Valid or Not

Rules for Valid Identifiers in C: The identifier must begin with a letter or an underscore (_). It cannot start
C Program to Recognize Strings Under 'a*', 'a*b+', 'abb'
Compiler Design

C Program to Recognize Strings Under ‘a’, ‘ab+’, ‘abb’

This C program is designed to recognize and classify strings according to three specific rules or patterns: a*: A string