Arithmetic Expression Evaluator in C++ Implemented by James Pate Williams, Jr.

I translated to C++ and enhanced a Pascal arithmetic expression evaluator program found in “Applied Data Structures Using Pascal” by Guy J. Hale and Richard E. Easton. The original code used single digit numbers. As an exercise I enhanced the application to utilize multiple digit numbers. Below is my test file and output from my program. I used the C++ standard library stack data structure.

20 + 3 * 4 + 50 * 4 * 2 + 6 * 2 - 8 / 2 + 2 ^ 5
#pragma once
#include <fstream>
#include <stack>
using namespace std;

class Expression
{
public:
	char ch, sign, termOp;
	int number;

	stack<int> stk;

	void GetChar(fstream& inps);
	void GetExpression(fstream& inps);
	void GetFactor(fstream& inps);
	void GetTerm(fstream& inps);
};
#include "pch.h"
#include "Expression.h"
#include <math.h>
#include <fstream>
#include <stack>
#include <string>
using namespace std;

void Expression::GetChar(
	fstream& inps)
{
	while (!inps.eof())
	{
		ch = (char)inps.get();

		if (inps.eof())
			exit(1);

		if (ch >= '0' && ch <= '9')
			return;

		if (ch == '^' || ch == '*' || ch == '/')
			return;

		if (ch == '+' || ch == '-')
			return;

		if (ch == ' ' || ch == '\t' || ch == '\n')
			continue;

		if (ch == ';')
			return;
	}
}

void Expression::GetExpression(fstream& inps)
{
	int num, num1, num2;

	if (ch == '+' || ch == '-')
	{
		sign = ch;

		GetChar(inps);		
		GetTerm(inps);

		if (sign == '-')
		{
			num = stk.top();
			stk.pop();
			num = -num;
			stk.push(num);
		}
	}

	GetTerm(inps);
	
	while (ch == '+' || ch == '-')
	{
		termOp = ch;
		
		GetChar(inps);
		GetTerm(inps);
		
		num2 = stk.top();
		stk.pop();
		num1 = stk.top();
		stk.pop();

		if (termOp == '+')
			num = num1 + num2;
		else
			num = num1 - num2;

		stk.push(num);
	}
}

void Expression::GetFactor(fstream& inps)
{
	if (ch >= '0' && ch <= '9')
	{
		string str;

		while (ch >= '0' && ch <= '9' && !inps.eof())
		{
			str += ch;
			ch = (char)inps.get();

			if (ch == ' ' || ch == '\t' || ch == '\n')
				break;

			else if (ch == '^' || ch == '+' || ch == '-' ||
				ch == '*' || ch == '/')
				break;
		}

		if (inps.eof())
			exit(-1);

		while (ch == ' ' || ch == '\t' || ch == '\n')
			ch = (char)inps.get();

		number = atoi(str.c_str());
		stk.push(number);
		return;
	}
	else
	{
		GetChar(inps);
		GetExpression(inps);
		GetChar(inps);
	}
}

void Expression::GetTerm(fstream& inps)
{
	char factOp;
	int num, num1, num2;

	GetFactor(inps);

	while (ch == '*' || ch == '/' || ch == '^')
	{
		factOp = ch;

		GetChar(inps);
		GetFactor(inps);

		num2 = stk.top();
		stk.pop();
		num1 = stk.top();
		stk.pop();

		if (factOp == '*')
			num = num1 * num2;
		else if (factOp == '/')
			num = num1 / num2;
		else if (factOp == '^')
			num = (int)pow(num1, num2);

		stk.push(num);
	}
}
#include "pch.h"
#include "Expression.h"
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    bool validExp;
    int expVal;
    fstream inps;
    Expression ex;

    inps.open("TestExp.txt", fstream::in);
    
    if (!inps.eof())
    {
        validExp = true;
        ex.GetChar(inps);
        ex.GetExpression(inps);
        expVal = ex.stk.top();
        ex.stk.pop();
        cout << "Value = " << expVal << endl;
    }

    else
    {
        validExp = false;
        expVal = 0;
    }

    return 0;
}

An Assignment Statement Syntactic Scanner for a Language resembling ADA or Pascal Implemented by James Pate Williams, Jr.

I am working my way through two compiler textbooks: “Design of Compilers Techniques of Programming Language Translation” by Karen A. Lemone and “Modern Compiler Implementation in Java” by Andrew W. Appel. My first exercise is a single line by line assignment statement parser.

Here is my source code and my translation structures:

X1:=a+bb*12;
X2:=a/2+bb*12;

Identifiers:
X1
a
bb
Literals:
12
Operators:
:=
+
*
Punctuation:
;

Identifiers:
X2
a
bb
Literals:
2
12
Operators:
:=
/
+
*
Punctuation:
;
#pragma once
#include "RegularExpAssignStm.h"
#include <string>
#include <vector>
using namespace std;

class RegularExpAssignStm
{
public:
	string punctuation[3] = { ";", "(", ")" };
	string upperCase =
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string lowerCase =
		"abcdefghijklmnopqrstuvwxyz";
	string dig = "0123456789";
	string ops[5] = { "+", "-", "*", "/", ":="};
	vector<string> identifier;
	vector<string> liter;
	vector<string> oper;
	vector<string> punc;

	RegularExpAssignStm() {	};
	bool IdContains(char key);
	size_t Search(size_t pos, string key, string match);
	size_t SingleCharSearch(char key, size_t index, string match[]);
	bool GetIdentifier(string assignStm);
	bool GetLiteral(string assignStm);
	bool Parse(string assignStm);
};

#include "pch.h"
#include "RegularExpAssignStm.h"
#include <functional>
#include <iostream>
#include <string>
using namespace std;

bool RegularExpAssignStm::IdContains(char key)
{
	bool dg = false, uc = false, lc = false;

	for (size_t i = 0; !uc && i < upperCase.size(); i++)
		uc = key == upperCase[i];

	if (uc)
		return true;

	for (size_t i = 0; !lc && i < lowerCase.size(); i++)
		lc = key == lowerCase[i];

	if (lc)
		return true;

	for (size_t i = 0; !dg && i < dig.size(); i++)
		dg = key == dig[i];

	if (dg)
		return true;

	return false;
}

size_t RegularExpAssignStm::Search(size_t pos, string key, string match)
{
	bool found = false;
	size_t i;

	for (i = 0; !found && i < match.size(); i++)
		found = key[pos] == match[i];

	if (!found)
		i = 4294967295;
	else
		i--;

	return i;
}

size_t RegularExpAssignStm::SingleCharSearch(
	char key, size_t index, string match[])
{
	bool found = false;
	size_t i;

	for (i = 0; !found && i < match[index].size(); i++)
		found = key == match[index].c_str()[i];

	if (!found)
		i = 4294967295;
	else
		i--;

	return i;
}

bool RegularExpAssignStm::GetIdentifier(string assignStm)
{
	string idStr;

	for (size_t i = 0; i < dig.size(); i++)
		if (assignStm[0] == dig[i])
			return false;

	for (size_t i = 0; i < assignStm.size(); i++)
	{
		if (IdContains(assignStm[i]))
			idStr.push_back(assignStm[i]);
		else
			break;
	}

	if (idStr.size() > 0)
		identifier.push_back(idStr);

	return idStr.size() > 0;
}

bool RegularExpAssignStm::GetLiteral(string assignStm)
{
	bool start = false;
	string litStr;

	for (size_t i = 0; !start && i < assignStm.size(); i++)
	{
		if (assignStm[0] == dig[i])
			start = true;
	}
	
	if (start)
	{
		liter.push_back("");

		for (size_t i = 0; i < assignStm.size(); i++)
		{
			if (assignStm[i] >= '0' && assignStm[i] <= '9')
				liter[liter.size() - 1].push_back(assignStm[i]);
			else
				return liter.size() > 0;
		}
	}

	return false;
}

bool RegularExpAssignStm::Parse(string assignStm)
{
	if (GetIdentifier(assignStm))
		assignStm.erase(0, identifier[identifier.size() - 1].size());
	else
		return false;

	size_t assignOpPos = Search(0, assignStm, ops[4]);

	if (assignOpPos != 4294967295)
	{
		assignStm.erase(0, ops[4].size());
		oper.push_back(ops[4]);
	}

	else
		return false;

	while (true)
	{
		if (GetLiteral(assignStm))
		{
			assignStm.erase(0, liter[liter.size() - 1].size());

			if (assignStm.size() <= 0)
				return false;
		}

		else if (GetIdentifier(assignStm) &&
			identifier[identifier.size() - 1].size() != 0)
		{
			assignStm.erase(0, identifier[identifier.size() - 1].size());
			
			if (assignStm.size() <= 0)
				return false;
		}

		size_t plusPos, minusPos, timesPos, divPos;

		plusPos = SingleCharSearch(assignStm[0], 0, ops);
		minusPos = SingleCharSearch(assignStm[0], 1, ops);
		timesPos = SingleCharSearch(assignStm[0], 2, ops);
		divPos = SingleCharSearch(assignStm[0], 3, ops);

		if (plusPos != 4294967295)
		{
			oper.push_back(ops[0]);
			assignStm.erase(0, 1);
		}
		else if (minusPos != 4294967295)
		{
			oper.push_back(ops[1]);
			assignStm.erase(0, 1);
		}
		else if (timesPos != 4294967295)
		{
			oper.push_back(ops[2]);
			assignStm.erase(0, 1);
		}
		else if (divPos != 4294967295)
		{
			oper.push_back(ops[3]);
			assignStm.erase(0, 1);
		}
		else
			return false;

		if (assignStm.size() <= 0)
			return false;

		if (GetLiteral(assignStm))
		{
			assignStm.erase(0, liter[liter.size() - 1].size());
			
			if (assignStm.size() <= 0)
				return false;
		}

		else if (GetIdentifier(assignStm) &&
			identifier[identifier.size() - 1].size() != 0)
		{
			assignStm.erase(0, identifier[identifier.size() - 1].size());
			
			if (assignStm.size() <= 0)
				return false;
		}

		size_t puns = SingleCharSearch(assignStm[0], 0, punctuation);

		if (puns != 4294967295)
		{
			punc.push_back(punctuation[puns]);
			assignStm.erase(0, punc[punc.size() - 1].size());

			if (assignStm.size() <= 0)
				return false;
		}
	}

	return true;
}

Almost a Lifetime of Computer Programming (aka Software Development) by James Pate Williams, Jr.

As I have mentioned before on this website (blog), I taught myself BASIC (Beginner’s All-purpose Symbolic Instruction Code) in the summer of 1978. I went on to undergraduate college courses in BASIC, FORTRAN (Formula Translator) IV, Intel 8085 or 8086 assembly and machine language programming, C, COBOL (Common Business Oriented Language kudos to Rear Admiral Grace Hopper), and Pascal. In between my two undergraduate careers, I taught myself Amiga BASIC, Modula-2, Motorola 68000 macro-assembly language, and Pecan Pascal on my ever-faithful 1988 Commodore Amiga 2000. After my second graduation from LaGrange College, I taught myself C++ in 1996 and client/server Internet programming in C also in 1996. As a graduate student at Auburn University during my tenure as a student, I had formal courses in Java, Common LISP, and Scheme in 1999 and Palm Operating System C. later in my studies. In the late-2000s I taught myself C#.

Procedural Languages: C, COBOL, FORTRAN IV, Pascal

Functional Languages: Common LISP (List Processor) and Scheme

In between procedural and object-oriented languages: Modula-2

Object-Oriented Languages: C++, Common LISP, and Java

Create an Index also Known as a Symbol Table Using C++ and the Vector Data Structure Designed and Implemented by James Pate Williams, Jr.

The hash node consists of a char position within a line, a line number, an ASCII character array symbol, and a hash value.  ASCII characters have the decimal value of 0 to 127. The hash function is the value of the first symbol character * 128 + the value of the second symbol character. There can be hash value collisions. The symbol table is defined as a vector<HashNode> table[128 * 128 + 128]. This is a very elementary method of handling collisions. The hash table generated by this application is as follows:

This		1	1	
The		2	12	
The		1	39	
a		1	9	
dummy		2	31	
definitions	2	37	
file		1	48	
generator	1	28	
has		1	53	
is		1	6	
is		2	28	
index		1	22	
lines		2	5	
line		2	23	
my		1	19	
of		1	16	
second		2	16	
two		2	1	
text		1	43	
test		1	11

There are two collision generating elements namely “The” and “is”. See my previous blog for the original data file. I sacrifice memory for functional complexity. Collisions require sorting of an element. I use TreeSort3. In the map-based indexer I use Quick Sort. Here is a pertinent comment:

// Insertion sort, heap sort, and quick sort algorithms
// From "Algortihms" by Thomas H. Cormen, et. al.
// Selection sort, Singleton's sort, and Tree Sort 3
// From "Sorting and Sort Systems" by Harold Lorin

Create an Index Using C++ and the Map Data Structure Designed and Implemented by James Pate Williams, Jr.

I recall that way back in the early to mid-1980s I had the pleasure of perusing a copy of the source code for a Pascal compiler. It was probably created directly under the inventor Nicklaus Wirth in Switzerland. I partially implemented a Pascal emulator for a Data General Eclipse minicomputer.

Here are some of the phases required for the creation of a Pascal computer program:

  1. Parse the source code.
  2. Create a symbol table.
  3. Interpret the symbols.
  4. Create P-Code for the interpreter.

Running the interpreter code involves translation of the P-Code to a computer readable bit string. Every computer scientist should at some time in her/his formal education should implement an assembler and a compiler.

Yesterday, April 11, 2023, I created a word index C++ application that takes a text file, parses the words, and creates an index also known as an English language symbol table. The app utilizes a C++ map that consists of integer keys and a node containing information about the words and their order in the text file. Below are the indexable text file and the symbol table (index).

This is a test of my index generator. The text file has

two lines. The second line is dummy definitions.

This is a test of my index generator. The text file has
two lines. The second line is dummy definitions.
The first number is the line number and the second the position within a line.

The         1             39          
The         2             12          
This        1             1            
a              1             9            
definitions           2             37          
dummy 2             31          
file          1             48          
generator            1             28          
has         1             53          
index     1             22          
is             2             28          
is             1             6            
line         2             23          
lines       2             5            
my          1             19          
of            1             16          
second  2             16          
test        1             11          
text        1             43          
two        2             1

Prime Number Binary Search Tree Implemented by James Pate Williams, Jr.

#pragma once

#include <cinttypes>
#include <vector>
using namespace std;

// See "Introduction to Algorithms"
// Thomas H. Cormen Among Others
// Chapter 13 Binary Search Trees
// Translated from Pascal found in
// "Applied Data Structures Using
// Pascal" by Guy J. Hale and
// Richard J. Easton Chapter 6
// Introduction to Trees

typedef struct treeNode
{
	uint32_t key;
	treeNode* lt, * rt;
} TREENODE, * PTREENODE;

class BinarySearchTree
{
public:
	static void InitTree(
		PTREENODE root, uint32_t key);
	static void CreateTree(
		PTREENODE& root, vector<uint32_t>& data,
		unsigned int seed, size_t bound);
	static void InOrderTreeWalk(PTREENODE x);
	static PTREENODE TreeSearch(PTREENODE x, uint32_t k,
		uint32_t& depth);
	static PTREENODE IterativeTreeSearch(
		PTREENODE x, uint32_t k);
	static PTREENODE TreeMinimum(PTREENODE x);
	static PTREENODE TreeMaximum(PTREENODE x);
	static PTREENODE TreeSuccessor(PTREENODE x);
	static void InsertTree(
		PTREENODE &root, uint32_t key);
};
#include "pch.h"
#include "BinarySearchTree.h"
#include "SieveOfEratosthenes.h"
#include <iostream>
#include <vector>
using namespace std;

void BinarySearchTree::InitTree(
	PTREENODE root, uint32_t key)
{
	root->key = key;
	root->lt = root->rt = NULL;
}

void BinarySearchTree::CreateTree(
	PTREENODE& root, vector<uint32_t>& keys,
	unsigned int seed, size_t bound)
{
	SieveOfEratosthenes::Initialization(bound);

	for (uint32_t i = 0; i < bound; i++)
	{
		uint32_t prime = SieveOfEratosthenes::GetNextPrime(bound);

		if (prime != -1)
			keys.push_back(prime);
	}

	srand(seed);

	unsigned int number;
	uint32_t key = -1;

	number = rand() % keys.size();
	key = keys[number];
	InitTree(root, key);
	keys.erase(keys.begin() + number, keys.begin() + number + 1);
	size_t count = 0, start = keys.size();

	for (size_t count = 1; count < start; count++)
	{
		bool found = false;

		do
		{
			number = rand() % start;

			if (number < keys.size())
			{
				key = keys[number];
				found = true;
			}
		} while (!found);
		
		keys.erase(keys.begin() + number, keys.begin() + number + 1);
		BinarySearchTree::InsertTree(root, key);
	}

	delete[] SieveOfEratosthenes::sieve;
}

void BinarySearchTree::InOrderTreeWalk(PTREENODE x)
{
	if (x != NULL)
	{
		InOrderTreeWalk(x->lt);
		cout << x->key << endl;
		InOrderTreeWalk(x->rt);
	}
}

PTREENODE BinarySearchTree::TreeSearch(
	PTREENODE x, uint32_t k, uint32_t &depth)
{
	depth++;

	if (x == NULL || x->key == k)
		return x;
	if (k < x->key)
		return TreeSearch(x->lt, k, depth);

	return TreeSearch(x->rt, k, depth);
}

PTREENODE BinarySearchTree::IterativeTreeSearch(
	PTREENODE x, uint32_t k)
{
	while (x != NULL && x->key != k)
	{
		if (k < x->key)
			x = x->lt;
		else
			x = x->rt;
	}

	return x;
}

PTREENODE BinarySearchTree::TreeMinimum(PTREENODE x)
{
	while (x->lt != NULL)
		x = x->lt;
	return x;
}

PTREENODE BinarySearchTree::TreeMaximum(PTREENODE x)
{
	while (x->rt != NULL)
		x = x->rt;
	return x;
}

PTREENODE BinarySearchTree::TreeSuccessor(PTREENODE x)
{
	if (x->rt != NULL)
		return TreeSuccessor(x->rt);
	PTREENODE y = x;
	while (y != NULL && x == y->rt)
		x = y;
	return x;
}

void BinarySearchTree::InsertTree(
	PTREENODE& root, uint32_t key)
{
	bool inserted = false;
	PTREENODE node = new TREENODE();
	PTREENODE oneNode = root;

	while (!inserted)
	{
		if (key <= oneNode->key)
		{
			if (oneNode->lt != NULL)
				oneNode = oneNode->lt;
			else
			{
				oneNode->lt = node;
				inserted = true;
			}
		}

		else
		{
			if (oneNode->rt != NULL)
				oneNode = oneNode->rt;
			else
			{
				oneNode->rt = node;
				inserted = true;
			}
		}
	}

	node->key = key;
	node->lt = node->rt = NULL;
}
// PrimeNumberBST.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Create a prime number search tree
// James Pate Wiliams, Jr. (c) 2023

#include "pch.h"
#include "BinarySearchTree.h"
#include "SieveOfEratosthenes.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	while (true)
	{
		uint32_t bound = 50;
		size_t start = 0;
		unsigned int seed = 1;
		PTREENODE root = new TREENODE();

		cout << "Key Bound = ";
		cin >> bound;

		if (bound == 0)
			break;

		cout << "PRNG seed = ";
		cin >> seed;
		cout << endl;

		vector<uint32_t> data;
		uint32_t depth = 0, key;
		BinarySearchTree::CreateTree(
			root, data, seed, bound);
		BinarySearchTree::InOrderTreeWalk(root);
		cout << endl;
		cout << "Search key = ";
		cin >> key;

		PTREENODE x = BinarySearchTree::TreeSearch(
			root, key, depth);

		cout << "Search depth = ";
		cout << depth << endl;
		cout << "Found key = ";

		if (x != NULL)
			cout << x->key << endl;
		else
			cout << "Key not found" << endl;
	
		cout << endl;
	}

	return 0;
}