Skip to content

Commit

Permalink
Merge pull request #1 from Pip-Install-Party/officialblake-develop
Browse files Browse the repository at this point in the history
add base logic for abstract syntax tree
  • Loading branch information
officialblake authored Nov 5, 2024
2 parents c062491 + 4fa6634 commit c947d3b
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
table.exe: main.o commentDFA.o tokenizer.o parser.o table.o
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o -o table.exe
table.x: main.o commentDFA.o tokenizer.o parser.o table.o
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o -o table.x

main.o: main.cpp commentDFA.h tokenizer.h parser.h testFiles.h
g++ -std=c++17 -g main.cpp -o main.o -c
Expand All @@ -17,4 +17,4 @@ table.o: table.cpp table.h
g++ -std=c++17 -g table.cpp -o table.o -c

clean:
rm -f table.exe *.o *.txt
rm -f table.x *.o *.txt
25 changes: 25 additions & 0 deletions Tests/Program5/programming_assignment_5-test_file_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***************************************************
// * CS460: Programming Assignment 5: Test Program 1 *
// ***************************************************

function int sum_of_first_n_squares (int n)
{
int sum;

sum = 0;
if (n >= 1)
{
sum = n * (n + 1) * (2 * n + 1) / 6;
}
return sum;
}

procedure main (void)
{
int n;
int sum;

n = 100;
sum = sum_of_first_n_squares (n);
printf ("sum of the squares of the first %d numbers = %d\n", n, sum);
}
61 changes: 61 additions & 0 deletions Tests/Program5/programming_assignment_5-test_file_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ***************************************************
// * CS460: Programming Assignment 5: Test Program 2 *
// ***************************************************



// ***********************************************************************************
// * Hex digit converts a single character into its non-negative integer equivalent. *
// * *
// * Hex digit returns -1 upon error *
// ***********************************************************************************
function int hexdigit2int (char hex_digit)
{
int i, digit;

digit = -1;
if ((hex_digit >= '0') && (hex_digit <= '9'))
{
digit = hex_digit - '0';
}
else
{
if ((hex_digit >= 'a') && (hex_digit <= 'f'))
{
digit = hex_digit - 'a' + 10;
}
else
{
if ((hex_digit >= 'A') && (hex_digit <= 'F'))
{
digit = hex_digit - 'A' + 10;
}
}
}
return digit;
}



procedure main (void)
{
char hexnum[9];
int i, digit, number;

number = 0;
hexnum = "feed\x0";
digit = 0;
for (i = 0; (i < 4) && (digit > -1); i = i + 1)
{
digit = hexdigit2int (hexnum[i]);
if (digit > -1)
{
number = number * 16 + digit;
}
}
if (digit > -1)
{
printf ("Hex: 0x%s is %d decimal\n", hexnum, number);
}
}

54 changes: 54 additions & 0 deletions Tests/Program5/programming_assignment_5-test_file_3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/***************************************************
* CS460: Programming Assignment 5: Test Program 3 *
***************************************************/

char announcement[2048];


procedure main (void)
{
char name[100];

name = 'Robert\x0';
announcement = "You've got mail!\x0";
display_announcement (name);
}


function bool empty_string (char string[4096])
{
int i;
int num_bytes_before_null;
bool found_null;

found_null = FALSE;
num_bytes_before_null = 0;
i = 0;
while ((i < 4096) && (!found_null))
{
if (string[i] == '\x0')
{
found_null = TRUE;
}
else
{
num_bytes_before_null = num_bytes_before_null + 1;
}
i = i + 1;
}
return (num_bytes_before_null == 0);
}


procedure display_announcement (char name[512])
{
if (!empty_string(name))
{
printf ("Welcome, %s\n\n", name);
if (!empty_string(announcement))
{
printf ("%s\n", announcement);
}
}
}

33 changes: 33 additions & 0 deletions Tests/Program5/programming_assignment_5-test_file_4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***************************************************
* CS460: Programming Assignment 5: Test Program 4 *
***************************************************/

char my_string[1024];


procedure main (void)
{
result = TRUE;
my_string[0] = '\x0';
number = 3;
}


int number;


function bool random_long_parameter_list (int ensity, char ter, int rospective, int egrity, char latan, char coal, int elligent, bool lean, char treuse, char ming, int uitive)
{
i = 1;
j = 1000;
k = 25;
return TRUE;
}

bool result;

procedure do_nothing (void)
{
}

int i, j, k;
76 changes: 76 additions & 0 deletions Tests/Program5/programming_assignment_5-test_file_5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// ***************************************************
// * CS460: Programming Assignment 5: Test Program 5 *
// ***************************************************



// *******************************************************************************************
// * The fizzbuzz procedure outputs one of the following responses: *
// * *
// * If counter is divisible by three without remainder, display "Fizz". *
// * If counter is divisible by five without remainder, display "Buzz". *
// * If counter is divisible by both three and five without a remainder, display "Fizzbuzz". *
// * If counter is NOT divisible by three or five, display the counter. *
// *******************************************************************************************
procedure fizzbuzz (int counter)
{
int state;

state = 0;
if ((counter % 3) == 0)
{
state = 1;
}
if ((counter % 5) == 0)
{
state = state * 2 + 2;
}
if (state == 1)
{
printf ("Fizz");
}
else
{
if (state == 2)
{
printf ("Buzz");
}
else
{
if (state == 4)
{
printf ("Fizzbuzz");
}
else
{
printf ("%d", counter);
}
}
}
}





procedure main (void)
{
int counter;

counter = 1;
while (counter <= 100)
{
fizzbuzz (counter);
counter = counter + 1;
if (counter <= 100)
{
printf (", ");
}
else
{
printf ("\n");
}
}
}


2 changes: 1 addition & 1 deletion parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern std::string tokenType; //= TokenTypes::IDENTIFIER; // or TokenTypes::INTE
class Parser
{
private:
std::vector<std::string> reserved = {"printf", "int", "void", "char", "bool" "string", "procedure", "function"};
std::vector<std::string> reserved = {"printf", "int", "void", "char", "bool", "string", "procedure", "function"};

Token *head;
// Vector holding all of the tokens in order from the tokenizer.
Expand Down
40 changes: 40 additions & 0 deletions tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "tree.h"

// Prints the abstract syntax tree to the provided output stream
void Tree::printTree(Token* head){
if (head == nullptr){
return;
}
if (head->getValue() == "{") {
std::cout << "BEGIN BLOCK";
} else if (head->getValue() == "}") {
std::cout << "END BLOCK";
} else if (head->getValue() == "procedure" || head->getValue() == "function") {
std::cout << "DECLARATION";
} else if (contains(head->getValue())) {
std::cout << "DECLARATION";
} else if (head->getType() == "IDENTIFIER") {
if (head->getSibling() != nullptr && head->getSibling()->getValue() == "=") {
std::cout << "ASSIGNMENT" << " ----> " << head->getValue();
// This needs to break out to handleAssignment();
// head = handleAssignment();
}
}
if (head->getSibling() != nullptr) {
head = head->getSibling();
std::cout << " ----> ";
} else if (head->getChild() != nullptr) {
head = head->getChild();
std::cout << "\n|\n|\n|\n|\nv ";
}
return printTree(head);
}

bool contains(std::string type){
for (const auto& reserved : varTypes) {
if (type == reserved) {
return true;
}
}
return false;
}
17 changes: 17 additions & 0 deletions tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef TREE_H
#define TREE_H
#include <iostream>
#include "token.h"

const std::vector<std::string> varTypes = {"int", "void", "char", "bool", "string", "short", "long"};

class Tree {
private:
void printTree(Token*);
bool contains(std::string);
public:
Tree(Token* head) { printTree(head); }
~Tree();
};

#endif // TREE_H

0 comments on commit c947d3b

Please sign in to comment.