Skip to content

update logic #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tree.exe: main.o commentDFA.o tokenizer.o parser.o table.o tree.o
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o tree.o -o tree.exe
tree.x: main.o commentDFA.o tokenizer.o parser.o table.o tree.o
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o tree.o -o tree.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 @@ -20,4 +20,4 @@ tree.o: tree.cpp tree.h
g++ -std=c++17 -g tree.cpp -o tree.o -c

clean:
rm -f tree.exe *.o *.txt
rm -f tree.x *.o *.txt
6 changes: 5 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ void abstractSyntaxTree(std::ifstream& testFile, std::ostringstream& outputFile,
Parser *parser = new Parser(tokenList);
parser->begin();

Tree* tree = new Tree(parser->getHead());
Table *table = new Table;

table->begin(parser->getHead());

Tree* tree = new Tree(parser->getHead(), table);

}

Expand Down
4 changes: 2 additions & 2 deletions parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ 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", "procedure", "function"};

Token *head;
// Vector holding all of the tokens in order from the tokenizer.
// Vector holding all of the tokens in order from the tokenizer.s
std::vector<Token> tokenList;

// Variable to track the current position in the token list.
Expand Down
1 change: 1 addition & 0 deletions table.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Table {
void begin(Token*);
void printTable();
void printParameters();
Entry* getHead() {return head;}
~Table(){};
};

Expand Down
90 changes: 74 additions & 16 deletions tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ void Tree::printTree(Token* head, Token* prevToken){
ignore = false;
std::cout << "RETURN";
head = head->getSibling();
while(head->getValue() == "("){
head = head->getSibling();
}
if (head->getSibling() != nullptr && contains(equationOperators, head->getSibling()->getValue())) {
std::cout << " ----> ";
head = handleAssignment(head);
prevToken = nullptr;
}
} else if (head->getValue() == "procedure" || head->getValue() == "function") {
std::cout << "DECLARATION\n|\n|\n|\n|\nv\n";
while(head->getSibling() != nullptr) {
Expand All @@ -33,7 +41,18 @@ void Tree::printTree(Token* head, Token* prevToken){
head = handleAssignment(head);
prevToken = nullptr;

} else if (head->getValue() == "printf"){
} else if (head->getValue() == "while"){
ignore = false;
std::cout << "WHILE ----> ";
// This needs to break out to handleAssignment();
head = head->getSibling();
while(head->getSibling()->getValue() == "("){
head = head->getSibling();
}
head = handleAssignment(head);
prevToken = nullptr;

}else if (head->getValue() == "printf"){
ignore = true;
std::cout << "PRINTF";
while (head->getSibling() != nullptr) {
Expand All @@ -60,7 +79,7 @@ void Tree::printTree(Token* head, Token* prevToken){
forCount++;
}

} else if (contains(varTypes, prevToken->getValue())) {
} else if (prevToken != nullptr && contains(varTypes, prevToken->getValue())) {
ignore = true; // this was false but i think should be true
std::cout << "DECLARATION";
if (head->getSibling() != nullptr && head->getSibling()->getValue() == ",") {
Expand All @@ -79,7 +98,17 @@ void Tree::printTree(Token* head, Token* prevToken){
// This needs to break out to handleAssignment();
head = handleAssignment(head);
prevToken = nullptr;
}
} else if (isFunction(head->getValue())){
ignore = false;
isCall = true;
std::cout << "CALL";
while(head->getSibling() != nullptr) {
head = head->getSibling();
if (head->getType() == "IDENTIFIER"){
std::cout << " ----> " << head->getValue();
}
}
}
} else if (head->getSibling() == nullptr && head->getChild() != nullptr && head->getValue() == ";"){
std::cout << "\n|\n|\n|\n|\nv\n";
}
Expand All @@ -93,6 +122,7 @@ void Tree::printTree(Token* head, Token* prevToken){
if (!ignore) {
std::cout << "\n|\n|\n|\n|\nv\n";
}
isCall = false;
return printTree(head->getChild(), head);
}
return;
Expand Down Expand Up @@ -138,26 +168,42 @@ Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, boo

Token* Tree::handleAssignment(Token* head) {
std::vector<Token*> equationAsVec;
bool isFunctionCall = false;

Token* prev = nullptr;
isCall = isFunction(head->getValue());

if (head->getValue() != "(") {
equationAsVec.push_back(head);
}


head = head->getSibling();

while(head->getValue() != ";" && (contains(equationOperators, head->getValue()) || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER" || head->getType() == "STRING" || head->getType() == "DOUBLE_QUOTE")) {
while(head->getValue() != ";" && (contains(equationOperators, head->getValue()) || head->getValue() != ";" || head->getValue() != "[" || head->getValue() != "]" || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER" || head->getType() == "STRING" || head->getType() == "DOUBLE_QUOTE")) {
// Check for function call (identifier with a L_PAREN)
if (head->getType() == "IDENTIFIER" && head->getSibling() != nullptr && head->getSibling()->getValue() == "(") {
if (isFunction(head->getValue())){
isCall = true;
}
if (head->getType() == "IDENTIFIER" && head->getSibling() != nullptr && head->getSibling()->getValue() == "(" && isCall) {
// Call to handleFunction since we encountered an identifier with a L_PAREN
auto temp = prev;
equationAsVec.push_back(head);
head = handleFunction(head, equationAsVec, isFunctionCall);
prev = head;
head = handleFunction(head, equationAsVec, isCall);
if(temp->getValue() == "!") {
equationAsVec.push_back(temp);
}
}
else {
equationAsVec.push_back(head);
else if (head->getValue() != "(" && head->getValue() != "!"){
if (prev != nullptr && prev->getValue() == "!"){
equationAsVec.push_back(head);
equationAsVec.push_back(prev);
} else {
equationAsVec.push_back(head);
}
}

if (head ->getSibling() != nullptr) {
prev = head;
head = head->getSibling();
}
else {
Expand All @@ -166,7 +212,7 @@ Token* Tree::handleAssignment(Token* head) {
}

// Convert infix to postfix
std::vector<Token*> postFix = infixToPostfix(equationAsVec, isFunctionCall);
std::vector<Token*> postFix = infixToPostfix(equationAsVec, isCall);

//std::cout << "Size<: " << postFix.size() << std::endl;
for (int i = 0; i < postFix.size(); i++) {
Expand Down Expand Up @@ -211,7 +257,7 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i
std::string tokenValue = t->getValue();

// If it's an operand, add it to the postfix output
if (tokenType == "INTEGER" || tokenType == "STRING" || tokenType == "CHARACTER" || tokenType == "IDENTIFIER" || tokenType == "SINGLE_QUOTE" || tokenType == "DOUBLE_QUOTE") {
if (tokenType == "INTEGER" || tokenType == "STRING" || tokenType == "CHARACTER" || tokenType == "IDENTIFIER" || tokenType == "SINGLE_QUOTE" || tokenType == "DOUBLE_QUOTE" || tokenValue == "!"){
postfix.push_back(t);
}
// If it's a left parenthesis, push it onto the stack
Expand Down Expand Up @@ -239,11 +285,11 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i
}
// Add array brackets here if it is a function call, if not, it is a normal operator.
else if (tokenType == "L_BRACKET" || tokenType == "R_BRACKET") {
if (isFunctionCall) {
//if (isFunctionCall) {
postfix.push_back(t);
} else {
operators.push(t);
}
// } else {
// operators.push(t);
// }
}
// If it's an operator
else if (isOperator(tokenValue) || tokenType == "GT_EQUAL" || tokenType == "LT_EQUAL" || tokenType == "GT" || tokenType == "LT" || tokenType == "BOOLEAN_EQUAL" || tokenType == "BOOLEAN_AND" || tokenType == "BOOLEAN_NOT") { // can code this to check for all explicit tokens like prev if checks above... can add other tokens to operators too
Expand All @@ -266,3 +312,15 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i

return postfix;
}


bool Tree::isFunction(std::string tokenName){
Entry* tableHead = symbolTable->getHead();
while(tableHead != nullptr) {
if ((tableHead->getIDType() == "procedure" || tableHead->getIDType() == "function") && tableHead->getIDName() == tokenName) {
return true;
}
tableHead = tableHead->getNext();
}
return false;
}
8 changes: 6 additions & 2 deletions tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
#define TREE_H
#include <iostream>
#include "token.h"
#include "table.h"
#include <vector>

const std::vector<std::string> varTypes = {"int", "void", "char", "bool", "string", "short", "long"};
const std::vector<std::string> equationOperators = {"+", "-", "/", "%", "*", "(", ")", "=", "'", "<", ">", "<=", ">=", "&&"};
const std::vector<std::string> equationOperators = {"+", "-", "/", "%", "*", "(", ")", "=", "==", "'", "<", ">", "<=", ">=", "&&"};

class Tree {
private:
Table* symbolTable;
bool isCall = false;
void printTree(Token*, Token*);
bool contains(const std::vector<std::string>, std::string);
bool isFunction(std::string tokenName);
Token* handleAssignment(Token*);
Token* handleFunction(Token *,std::vector<Token*>& equationAsVec, bool& isFunctionCall);
int getPrecedence(std::string op);
Expand All @@ -19,7 +23,7 @@ class Tree {


public:
Tree(Token* head) { printTree(head, nullptr); }
Tree(Token* head, Table* table) { symbolTable = table; printTree(head, nullptr); }
~Tree();
};

Expand Down