Chapter One Straight-Line Program Interpreter from “Modern Compiler Implementation in Java Second Edition” (c) 2002 by Andrew W. Appel, Translation to C++ by James Pate Williams, Jr. on Thursday, April 3, 2025

Wayback in the Spring Semester of 2006, after I was awarded my Doctor of Philosophy Degree in Computer Science, I partially audited a Compiler Design Course. Due to my mental aberrations, I was unable to complete the course. The instructor was on a Sabbatical from the United States Air Force Academy in Colorado Springs, Colorado. The textbook we used, and I still have a copy, was “Modern Compiler Implementation in Java Second Edition” © 2002 by Andrew W. Appel. Below is a translation from Java to C++ that I just completed.

// Chapter One program translated from Java to C++ by
// James Pate Williams, Jr. (c) Wednesday April 3, 2025
// Reference: "Modern Complier Implementation in Java
// Second Edition" (c) 2002 by Andrew W. Appel

#ifndef _SLPInterpreter_H
#include <iostream>
#include <stack>
#include <string>
#include <vector>

class TableEntry {
public:
	std::string symbol, value;
	TableEntry(std::string symbol, std::string value) {
		this->symbol = symbol;
		this->value = value;
	}
};

std::stack<std::string> sStack;
std::vector<TableEntry> symbolTable;

class Exp {
public:
	Exp() { };
	virtual ~Exp() { };
};

std::stack<Exp> eStack;

class ExpList {
public:
	ExpList() { };
	virtual ~ExpList() { };
};

class Stm {
public:
	Stm() { };
	virtual ~Stm() { };
};

class CompoundStm : public Stm {
public:
	Stm stm1, stm2;
	CompoundStm(Stm stm1, Stm stm2) {
		this->stm1 = stm1;
		this->stm2 = stm2;
	};
};

class AssignStm : public Stm {
public:
	std::string id;
	Exp exp;
	AssignStm(std::string id, Exp exp) {
		this->id = id;
		this->exp = exp;
		bool found = false;
		for (int i = 0; !found && i < (int)symbolTable.size(); i++) {
			if (symbolTable[i].symbol == id) {
				found = true;
			}
		}
		if (!found) {
			symbolTable.push_back(TableEntry(id, ""));
		}
	};
	void Print() {
		std::cout << this->id << ' ';
	};
};

class PrintStm : public Stm {
public:
	ExpList exps;
	PrintStm(ExpList exps) {
		this->exps = exps;
	};
};

class IdExp : public Exp {
public:
	std::string id;
	IdExp(std::string id) {
		this->id = id;
		Print();
		TableEntry te(id, "");
	};
	void Print() {
		std::cout << id << ' ';
	};
};

class NumExp : public Exp {
public:
	int num;
	NumExp(int num) {
		this->num = num;
		Print();
		char buffer[128] = { };
		_itoa_s(num, buffer, 127, 10);
		sStack.push(std::string(buffer));
	};
	void Print() {
		std::cout << num << ' ';
	};
};

enum class ArithmeticOp {
	Plus, Minus, Times, Div
};

class OpExp : public Exp {
public:
	Exp left, right;
	ArithmeticOp op;
	OpExp(Exp left, ArithmeticOp op, Exp right) {
		this->left = left;
		this->op = op;
		this->right = right;
		std::string ops = "";
		switch (op) {
		case ArithmeticOp::Plus:
			ops = "+";
			break;
		case ArithmeticOp::Minus:
			ops = "-";
			break;
		case ArithmeticOp::Times:
			ops = "*";
			break;
		case ArithmeticOp::Div:
			ops = "/";
			break;
		};
		std::cout << ops << std::endl;
		eStack.push(left);
		eStack.push(right);
		sStack.push(ops);
	};
};

class EseqExp : public Exp {
public:
	Stm stm; Exp exp;
	EseqExp(Stm stm, Exp exp) {
		this->stm = stm;
		this->exp = exp;
	};
};

class PairExpList : public ExpList {
public:
	Exp head;
	ExpList tail;
	PairExpList(Exp head, ExpList tail) {
		this->head = head;
		this->tail = tail;
	};
};

class LastExpList : public ExpList {
public:
	Exp head;
	LastExpList(Exp head) {
		this->head = head;
	};
};

#endif _SLPInterpreter_H

int main() {
	int a = 0, b = 0;
	Stm prog(CompoundStm(AssignStm("a",
		OpExp(NumExp(5), ArithmeticOp::Plus, NumExp(3))),
		CompoundStm(AssignStm("b",
			EseqExp(PrintStm(PairExpList(IdExp("a"),
				LastExpList(OpExp(IdExp("a"),
					ArithmeticOp::Minus, NumExp(1))))),
				OpExp(NumExp(10), ArithmeticOp::Times, IdExp("a")))),
			PrintStm(LastExpList(IdExp("b"))))));
	bool first = true;
	int result = 0;
	//sStack.push("0");
	while (!sStack.empty()) {
		std::string lts, ops, rts;
		if (first) {
			ops = sStack.top();
			sStack.pop();
			lts = sStack.top();
			sStack.pop();
			rts = sStack.top();
			sStack.pop();
			first = false;
		}
		else {
			lts = sStack.top();
			sStack.pop();
			ops = sStack.top();
			sStack.pop();
			rts = sStack.top();
			sStack.pop();
		}
		int lvi = std::stoi(lts);
		int rvi = std::stoi(rts);
		if (ops == "+") {
			result = lvi + rvi;
		}
		else if (ops == "-") {
			result = lvi - rvi;
		}
		else if (ops == "*") {
			result = lvi * rvi;
		}
		else if (ops == "/") {
			result = lvi / rvi;
		}
		char ascii[128] = { };
		_itoa_s(result, ascii, 10);
		if (sStack.size() != 0) {
			sStack.push(std::string(ascii));
		}
	}
	std::cout << "Result = " << result << std::endl;
	return 0;
}
Unknown's avatar

Author: jamespatewilliamsjr

My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.

Leave a comment