CD Assessment 4

You might also like

You are on page 1of 9

BCSE307P – Complier Design Lab

Winter Semester 2022-23


Assessment - 4

Name: SURJOSNATH GUHA THAKURTA


Registration No: 21BDS0177
Lab Class: VL2023240101246
Class Slot: L45 + L46
Faculty in Charge: NAVAMANI T M
Date: 12-07-2023

School of Computer Science and Engineering (SCOPE)


Vellore Institute of Technology
Vellore.
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

Table of Contents

Ex.No. Title Date Page No.


Generating 3 Address Code
1 Question 1 11-07-23 3
2 Question 2 11-07-23 7

Signature of the student (Digital)

[Question 1]
Page 2 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

Aim:
Generate Three address code for the following statement
Eg. a = (-c * b) + (-c * d)

Ans:
t₁ = -c
t₂ = b*t₁
t₃ = -c
t₄ = d * t₃
t₅ = t₂ + t₄
a = t₅

Code:
#include <iostream>
#include <string>
#include <stack>

using namespace std;

// Function to check if a character is an operator


bool isOperator(char ch)
{
    return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}

// Function to get the precedence of an operator


int getPrecedence(char op)
{
    if (op == '*' || op == '/')
        return 2;
    else if (op == '+' || op == '-')
        return 1;
    else
        return 0;
}

// Function to generate three-address code


string generateThreeAddressCode(string expression)
{
    string code = "";
    stack<char> operators;
    stack<string> operands;
    int tempCount = 1;

    for (int i = 0; i < expression.length(); i++)


    {
        char ch = expression[i];

Page 3 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

        if (ch == ' ')


            continue;
        else if (isalnum(ch))
        {
            string operand;
            while (i < expression.length() && isalnum(expression[i]))
                operand += expression[i++];
            i--;
            operands.push(operand);
        }
        else if (ch == '(')
        {
            operators.push(ch);
        }
        else if (ch == ')')
        {
            while (!operators.empty() && operators.top() != '(')
            {
                string op2 = operands.top();
                operands.pop();
                string op1 = operands.top();
                operands.pop();
                char op = operators.top();
                operators.pop();

                string temp = "t" + to_string(tempCount++);


                operands.push(temp);

                code += temp + " = " + op1 + " " + op + " " + op2 + "\n";
            }
            operators.pop();
        }
        else if (isOperator(ch))
        {
            while (!operators.empty() && operators.top() != '(' &&
getPrecedence(operators.top()) >= getPrecedence(ch))
            {
                string op2 = operands.top();
                operands.pop();
                string op1 = operands.top();
                operands.pop();
                char op = operators.top();
                operators.pop();

                string temp = "t" + to_string(tempCount++);


                operands.push(temp);

                code += temp + " = " + op1 + " " + op + " " + op2 + "\n";
Page 4 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

            }
            operators.push(ch);
        }
    }

    while (!operators.empty())
    {
        string op2 = operands.top();
        operands.pop();
        string op1 = operands.top();
        operands.pop();
        char op = operators.top();
        operators.pop();

        string temp = "t" + to_string(tempCount++);


        operands.push(temp);

        code += temp + " = " + op1 + " " + op + " " + op2 + "\n";
    }

    string result = operands.top();


    code += "a = " + result;

    return code;
}

int main()
{
    string expression;
    cout << "Enter an assignment statement: ";
    getline(cin, expression);

    string threeAddressCode = generateThreeAddressCode(expression);


    cout << "Three Address Code:\n"
         << threeAddressCode << endl;

    return 0;
}

Output:

Page 5 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

[Question 2]
Page 6 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

Aim:
Generate the three address codes for the following:
for(i = 1; i<=10; i++)
{ a[i]=x * 5; }

Ans:
i=1
L:t1=x*5
t2=&a
t3=sizeof(int)
t4=t3*i
t5=t2+t4
*t5=t1
i=i+1
if i<10 goto L

Code:
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> generateThreeAddressCode(const std::string &code)


{
    std::vector<std::string> threeAddressCode;
    std::string currentCode;

    // Initialize variables
    threeAddressCode.emplace_back("i=1");

    // Loop start
    threeAddressCode.emplace_back("L:");
    currentCode = "t1=x*5";
    threeAddressCode.emplace_back(currentCode);

    currentCode = "t2=&a";
    threeAddressCode.emplace_back(currentCode);

    currentCode = "t3=sizeof(int)";
    threeAddressCode.emplace_back(currentCode);

    currentCode = "t4=t3*i";
    threeAddressCode.emplace_back(currentCode);

    currentCode = "t5=t2+t4";

Page 7 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

    threeAddressCode.emplace_back(currentCode);

    currentCode = "*t5=t1";
    threeAddressCode.emplace_back(currentCode);

    currentCode = "i=i+1";
    threeAddressCode.emplace_back(currentCode);

    // Loop condition
    currentCode = "if i<10 goto L";
    threeAddressCode.emplace_back(currentCode);

    return threeAddressCode;
}

int main()
{
    std::string userInput;
    std::cout << "Enter the code: ";
    std::getline(std::cin, userInput);

    std::vector<std::string> threeAddressCode =
generateThreeAddressCode(userInput);

    std::cout << "\nGenerated Three-Address Code:\n";


    for (const auto &code : threeAddressCode)
    {
        std::cout << code << std::endl;
    }

    return 0;
}

Output:

Page 8 of 9
Name: Surjosnath Guha Thakurta
Registration No.: 21BDS0177

Page 9 of 9

You might also like