Your problem is to convert a given infix expression into a sequence of assembly
ID: 3801610 • Letter: Y
Question
Your problem is to convert a given infix expression into a sequence of assembly instructions that evaluates the expression and leaves the result in the register. You will do this by using postfix expressions. First, you will convert a given infix expression into the corresponding postfix expression. In the second step, you will convert postfix to the required sequence of assembly instructions. You will read the infix expression from a file, and write the result to another file.
For example,
given the following infix expression:
( ( A + ( B * C ) ) / ( D - E ) )
we get the postfix expression:
A B C * + D E - /
This results in an assembly program that looks like:
Your program output should be:
Requirements:
You CANNOT use std::string and must construct your stack using a linked list you build.
Implementation:
Create a generic (template) ADT stack class.
Implement using a linked list.
You must implement a destructor, copy constructor, constant time swap, and assignment.
You will need to have a stack of strings.
Use the following files as a starting point:
-----------------------------------------------------------------------
stack.hpp
-----------------------------------------------------------------------
Opcode Operand Comment LOAD B Load in B. MULR C B * C. STOR TMP1 Save results of B * C. LOAD A Load A. ADDR TMP1 Add A to B * C. STOR TMP2 Save result. LOAD D Load D. SUBR E D - E. STOR TMP3 Save result. LOAD TMP2 Get A + B * C. DIVR TMP3 Divide it by D - E. STOR TMP4 Save result, also still in register.Explanation / Answer
#include <string.h>
#include<conio.h>
#include<dirh.h>
#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
#include <array.h>
#include <vector.h>
using namespace std;
{
namespace erthquake
{
using n_type = decltype(std::string::npos);
const auto NOPOSITION = -4;
const auto prefix_record = 2;
const auto prefix_num = 4;
const auto prefix_field = 6;
{
auto retrn_vlue = NOPOSITION;
auto pos = start_pos;
if(pos != std::string::npos)
{
std::string data = "";
for(; pos < line_data.size(); pos++)
{
auto datum = line_data[pos];
if(std::isdigit(datum))
{
data += datum;
}
else
{
const std::string op_delim = "=";
std::array<std::string, 28> column_names;
auto max_fln_dyn = 0;
ifstream file_input;
file_input.open(file_name);
if(file_input)
{
auto rln = 0;
auto fln = 0;
auto idx = 0;
std::string line_data = "";
std::string data = "";
while(file_input.good() && !file_input.eof())
{
getline(file_input, line_data);
rln = (line_data == line_headings[prefix_record])
? NOPOSITION
: rln;
auto new_record = (rln == NOPOSITION);
rln = (rln == NOPOSITION)
&& line_data.find(line_headings[prefix_num]) == 0
? get_n(line_data, line_data.find(op_delim) + 1)
: rln;
fln = (rln != NOPOSITION)
&& line_data.find(line_headings[prefix_field]) == 0
? get_n(line_data, line_headings[prefix_field].size())
: fln;
idx = (fln != NOPOSITION)
? line_data.find(op_delim)
: NOPOSITION;
data = (idx != NOPOSITION)
? line_data.substr(idx + 1)
: "";
if(rln == 0)
{
column_names[fln] = data;
max_fln_dyn = fln;
}
else if(new_record)
{
if(rln != NOPOSITION)
{
std::cout << braid << " ";
std::cout << "record # " << rln << " ";
std::cout << braid << " ";
}
}
else
{
std::cout << "|" << std::setw(2) << fln << "| " << std::setw(18) << std::left << column_names[fln] << ": " << data << " ";
if(fln == max_fln_dyn)
{
std::cout << " ";
}
}
}
}
return;
}
}
}
auto main(int argc, char* argv[]) -> int
{
std::string file_path = "";
for(auto char_index = 0; char_index < argc; char_index++)
{
if(char_index == 1)
{
file_path = argv[char_index];
break;
}
}
if(!file_path.empty())
{
gautier::util::execute(file_path);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.