Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created December 9, 2016 03:13
Show Gist options
  • Save rupalbarman/4f7bd43581045fdf2a06a2612901d85b to your computer and use it in GitHub Desktop.
Save rupalbarman/4f7bd43581045fdf2a06a2612901d85b to your computer and use it in GitHub Desktop.
Intermediate code generator ( Three address code) simple
//SIMPLE INTERMEDIATE CODE GENERATOR (3 ADDRESS CODE)
//Author: Rupal Barman
//Modified: 5/11/16
// Does not consider the operator precedence, ie. generates the 3 address code from left to right directly.
// INPUT example: t = q * p + g ; (don't forget to end the statement with a ';' or any character, even a space would work)
// OUTPUT example: v=r*j; w=v+h
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char input[20];
char op, op1, op2;
char temp[5]={'v','w','x','y','z'};
int temp_pointer=0;
void scan()
{
for(int i=0;i<strlen(input); i++)
{
if((input[i]=='+')||(input[i]=='-')||(input[i]=='*')||(input[i]=='/'))
{
op=input[i];
op1=input[i-1];
op2=input[i+1];
cout<<"\n"<<temp[temp_pointer]<<"="<<op1<<op<<op2<<"\n";
input[i+1]=temp[temp_pointer++];
}
}
}
int main()
{
cout<<"\nEnter expression:";
cin>>input;
input[strlen(input)-1]='\0';
scan();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment