Skip to content

Commit c947d3b

Browse files
Merge pull request #1 from Pip-Install-Party/officialblake-develop
add base logic for abstract syntax tree
2 parents c062491 + 4fa6634 commit c947d3b

9 files changed

+310
-4
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
table.exe: main.o commentDFA.o tokenizer.o parser.o table.o
2-
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o -o table.exe
1+
table.x: main.o commentDFA.o tokenizer.o parser.o table.o
2+
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o -o table.x
33

44
main.o: main.cpp commentDFA.h tokenizer.h parser.h testFiles.h
55
g++ -std=c++17 -g main.cpp -o main.o -c
@@ -17,4 +17,4 @@ table.o: table.cpp table.h
1717
g++ -std=c++17 -g table.cpp -o table.o -c
1818

1919
clean:
20-
rm -f table.exe *.o *.txt
20+
rm -f table.x *.o *.txt
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***************************************************
2+
// * CS460: Programming Assignment 5: Test Program 1 *
3+
// ***************************************************
4+
5+
function int sum_of_first_n_squares (int n)
6+
{
7+
int sum;
8+
9+
sum = 0;
10+
if (n >= 1)
11+
{
12+
sum = n * (n + 1) * (2 * n + 1) / 6;
13+
}
14+
return sum;
15+
}
16+
17+
procedure main (void)
18+
{
19+
int n;
20+
int sum;
21+
22+
n = 100;
23+
sum = sum_of_first_n_squares (n);
24+
printf ("sum of the squares of the first %d numbers = %d\n", n, sum);
25+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ***************************************************
2+
// * CS460: Programming Assignment 5: Test Program 2 *
3+
// ***************************************************
4+
5+
6+
7+
// ***********************************************************************************
8+
// * Hex digit converts a single character into its non-negative integer equivalent. *
9+
// * *
10+
// * Hex digit returns -1 upon error *
11+
// ***********************************************************************************
12+
function int hexdigit2int (char hex_digit)
13+
{
14+
int i, digit;
15+
16+
digit = -1;
17+
if ((hex_digit >= '0') && (hex_digit <= '9'))
18+
{
19+
digit = hex_digit - '0';
20+
}
21+
else
22+
{
23+
if ((hex_digit >= 'a') && (hex_digit <= 'f'))
24+
{
25+
digit = hex_digit - 'a' + 10;
26+
}
27+
else
28+
{
29+
if ((hex_digit >= 'A') && (hex_digit <= 'F'))
30+
{
31+
digit = hex_digit - 'A' + 10;
32+
}
33+
}
34+
}
35+
return digit;
36+
}
37+
38+
39+
40+
procedure main (void)
41+
{
42+
char hexnum[9];
43+
int i, digit, number;
44+
45+
number = 0;
46+
hexnum = "feed\x0";
47+
digit = 0;
48+
for (i = 0; (i < 4) && (digit > -1); i = i + 1)
49+
{
50+
digit = hexdigit2int (hexnum[i]);
51+
if (digit > -1)
52+
{
53+
number = number * 16 + digit;
54+
}
55+
}
56+
if (digit > -1)
57+
{
58+
printf ("Hex: 0x%s is %d decimal\n", hexnum, number);
59+
}
60+
}
61+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/***************************************************
2+
* CS460: Programming Assignment 5: Test Program 3 *
3+
***************************************************/
4+
5+
char announcement[2048];
6+
7+
8+
procedure main (void)
9+
{
10+
char name[100];
11+
12+
name = 'Robert\x0';
13+
announcement = "You've got mail!\x0";
14+
display_announcement (name);
15+
}
16+
17+
18+
function bool empty_string (char string[4096])
19+
{
20+
int i;
21+
int num_bytes_before_null;
22+
bool found_null;
23+
24+
found_null = FALSE;
25+
num_bytes_before_null = 0;
26+
i = 0;
27+
while ((i < 4096) && (!found_null))
28+
{
29+
if (string[i] == '\x0')
30+
{
31+
found_null = TRUE;
32+
}
33+
else
34+
{
35+
num_bytes_before_null = num_bytes_before_null + 1;
36+
}
37+
i = i + 1;
38+
}
39+
return (num_bytes_before_null == 0);
40+
}
41+
42+
43+
procedure display_announcement (char name[512])
44+
{
45+
if (!empty_string(name))
46+
{
47+
printf ("Welcome, %s\n\n", name);
48+
if (!empty_string(announcement))
49+
{
50+
printf ("%s\n", announcement);
51+
}
52+
}
53+
}
54+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/***************************************************
2+
* CS460: Programming Assignment 5: Test Program 4 *
3+
***************************************************/
4+
5+
char my_string[1024];
6+
7+
8+
procedure main (void)
9+
{
10+
result = TRUE;
11+
my_string[0] = '\x0';
12+
number = 3;
13+
}
14+
15+
16+
int number;
17+
18+
19+
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)
20+
{
21+
i = 1;
22+
j = 1000;
23+
k = 25;
24+
return TRUE;
25+
}
26+
27+
bool result;
28+
29+
procedure do_nothing (void)
30+
{
31+
}
32+
33+
int i, j, k;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// ***************************************************
2+
// * CS460: Programming Assignment 5: Test Program 5 *
3+
// ***************************************************
4+
5+
6+
7+
// *******************************************************************************************
8+
// * The fizzbuzz procedure outputs one of the following responses: *
9+
// * *
10+
// * If counter is divisible by three without remainder, display "Fizz". *
11+
// * If counter is divisible by five without remainder, display "Buzz". *
12+
// * If counter is divisible by both three and five without a remainder, display "Fizzbuzz". *
13+
// * If counter is NOT divisible by three or five, display the counter. *
14+
// *******************************************************************************************
15+
procedure fizzbuzz (int counter)
16+
{
17+
int state;
18+
19+
state = 0;
20+
if ((counter % 3) == 0)
21+
{
22+
state = 1;
23+
}
24+
if ((counter % 5) == 0)
25+
{
26+
state = state * 2 + 2;
27+
}
28+
if (state == 1)
29+
{
30+
printf ("Fizz");
31+
}
32+
else
33+
{
34+
if (state == 2)
35+
{
36+
printf ("Buzz");
37+
}
38+
else
39+
{
40+
if (state == 4)
41+
{
42+
printf ("Fizzbuzz");
43+
}
44+
else
45+
{
46+
printf ("%d", counter);
47+
}
48+
}
49+
}
50+
}
51+
52+
53+
54+
55+
56+
procedure main (void)
57+
{
58+
int counter;
59+
60+
counter = 1;
61+
while (counter <= 100)
62+
{
63+
fizzbuzz (counter);
64+
counter = counter + 1;
65+
if (counter <= 100)
66+
{
67+
printf (", ");
68+
}
69+
else
70+
{
71+
printf ("\n");
72+
}
73+
}
74+
}
75+
76+

parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern std::string tokenType; //= TokenTypes::IDENTIFIER; // or TokenTypes::INTE
4646
class Parser
4747
{
4848
private:
49-
std::vector<std::string> reserved = {"printf", "int", "void", "char", "bool" "string", "procedure", "function"};
49+
std::vector<std::string> reserved = {"printf", "int", "void", "char", "bool", "string", "procedure", "function"};
5050

5151
Token *head;
5252
// Vector holding all of the tokens in order from the tokenizer.

tree.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "tree.h"
2+
3+
// Prints the abstract syntax tree to the provided output stream
4+
void Tree::printTree(Token* head){
5+
if (head == nullptr){
6+
return;
7+
}
8+
if (head->getValue() == "{") {
9+
std::cout << "BEGIN BLOCK";
10+
} else if (head->getValue() == "}") {
11+
std::cout << "END BLOCK";
12+
} else if (head->getValue() == "procedure" || head->getValue() == "function") {
13+
std::cout << "DECLARATION";
14+
} else if (contains(head->getValue())) {
15+
std::cout << "DECLARATION";
16+
} else if (head->getType() == "IDENTIFIER") {
17+
if (head->getSibling() != nullptr && head->getSibling()->getValue() == "=") {
18+
std::cout << "ASSIGNMENT" << " ----> " << head->getValue();
19+
// This needs to break out to handleAssignment();
20+
// head = handleAssignment();
21+
}
22+
}
23+
if (head->getSibling() != nullptr) {
24+
head = head->getSibling();
25+
std::cout << " ----> ";
26+
} else if (head->getChild() != nullptr) {
27+
head = head->getChild();
28+
std::cout << "\n|\n|\n|\n|\nv ";
29+
}
30+
return printTree(head);
31+
}
32+
33+
bool contains(std::string type){
34+
for (const auto& reserved : varTypes) {
35+
if (type == reserved) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
}

tree.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef TREE_H
2+
#define TREE_H
3+
#include <iostream>
4+
#include "token.h"
5+
6+
const std::vector<std::string> varTypes = {"int", "void", "char", "bool", "string", "short", "long"};
7+
8+
class Tree {
9+
private:
10+
void printTree(Token*);
11+
bool contains(std::string);
12+
public:
13+
Tree(Token* head) { printTree(head); }
14+
~Tree();
15+
};
16+
17+
#endif // TREE_H

0 commit comments

Comments
 (0)