From 4fa66349760009ffb75ba730939524450cc311ee Mon Sep 17 00:00:00 2001 From: Blake Marshall Date: Mon, 4 Nov 2024 20:53:13 -0800 Subject: [PATCH] add base logic for abstract syntax tree --- Makefile | 6 +- .../programming_assignment_5-test_file_1.c | 25 ++++++ .../programming_assignment_5-test_file_2.c | 61 +++++++++++++++ .../programming_assignment_5-test_file_3.c | 54 +++++++++++++ .../programming_assignment_5-test_file_4.c | 33 ++++++++ .../programming_assignment_5-test_file_5.c | 76 +++++++++++++++++++ parser.h | 2 +- tree.cpp | 40 ++++++++++ tree.h | 17 +++++ 9 files changed, 310 insertions(+), 4 deletions(-) create mode 100644 Tests/Program5/programming_assignment_5-test_file_1.c create mode 100644 Tests/Program5/programming_assignment_5-test_file_2.c create mode 100644 Tests/Program5/programming_assignment_5-test_file_3.c create mode 100644 Tests/Program5/programming_assignment_5-test_file_4.c create mode 100644 Tests/Program5/programming_assignment_5-test_file_5.c create mode 100644 tree.cpp create mode 100644 tree.h diff --git a/Makefile b/Makefile index a9d25e1..fada806 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/Tests/Program5/programming_assignment_5-test_file_1.c b/Tests/Program5/programming_assignment_5-test_file_1.c new file mode 100644 index 0000000..2a9ed76 --- /dev/null +++ b/Tests/Program5/programming_assignment_5-test_file_1.c @@ -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); +} diff --git a/Tests/Program5/programming_assignment_5-test_file_2.c b/Tests/Program5/programming_assignment_5-test_file_2.c new file mode 100644 index 0000000..8ce2604 --- /dev/null +++ b/Tests/Program5/programming_assignment_5-test_file_2.c @@ -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); + } +} + diff --git a/Tests/Program5/programming_assignment_5-test_file_3.c b/Tests/Program5/programming_assignment_5-test_file_3.c new file mode 100644 index 0000000..c668ed8 --- /dev/null +++ b/Tests/Program5/programming_assignment_5-test_file_3.c @@ -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); + } + } +} + diff --git a/Tests/Program5/programming_assignment_5-test_file_4.c b/Tests/Program5/programming_assignment_5-test_file_4.c new file mode 100644 index 0000000..494fc1a --- /dev/null +++ b/Tests/Program5/programming_assignment_5-test_file_4.c @@ -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; diff --git a/Tests/Program5/programming_assignment_5-test_file_5.c b/Tests/Program5/programming_assignment_5-test_file_5.c new file mode 100644 index 0000000..5f58517 --- /dev/null +++ b/Tests/Program5/programming_assignment_5-test_file_5.c @@ -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"); + } + } +} + + diff --git a/parser.h b/parser.h index 170bc09..9dd85a8 100644 --- a/parser.h +++ b/parser.h @@ -46,7 +46,7 @@ extern std::string tokenType; //= TokenTypes::IDENTIFIER; // or TokenTypes::INTE class Parser { private: - std::vector reserved = {"printf", "int", "void", "char", "bool" "string", "procedure", "function"}; + std::vector reserved = {"printf", "int", "void", "char", "bool", "string", "procedure", "function"}; Token *head; // Vector holding all of the tokens in order from the tokenizer. diff --git a/tree.cpp b/tree.cpp new file mode 100644 index 0000000..f5b630f --- /dev/null +++ b/tree.cpp @@ -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; +} \ No newline at end of file diff --git a/tree.h b/tree.h new file mode 100644 index 0000000..aba8d36 --- /dev/null +++ b/tree.h @@ -0,0 +1,17 @@ +#ifndef TREE_H +#define TREE_H +#include +#include "token.h" + +const std::vector 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 \ No newline at end of file