Skip to content
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

Fixing end of input while parsing stack not empty #39

Merged
merged 13 commits into from
Dec 25, 2023
4 changes: 2 additions & 2 deletions include/Parser/PredictiveTopDownParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class PredictiveTopDownParser {
bool handleMatchOrError(const StackItem& top, Token*& curr_token);
void handleMatch(const StackItem& top, Token*& curr_token);
void handleNonTerminal(const StackItem& top, Token*& curr_token);
void handleMissingTerminal(const StackItem& top);
void handleMissingTerminalOrSyncEntry(const StackItem& top);
void handleEmptyEntry(const StackItem& top, Token*& curr_token);
void handleSyncEntry(const StackItem& top);
void handleValidProduction(const StackItem& top, const CellValue* cellValue);
void handleEndOfInput();
void pushProductionToStack(const std::vector<std::string>& production);
void setNextDerivation(const StackItem& top, std::vector<std::string> &curr_production);
};
Expand Down
42 changes: 33 additions & 9 deletions program.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
int x;
x = 5;
if (x > 2)
{
x = 0;
}
else
!!!!!
@@@@@
1.5E-6
int sum , count , pass , mnt; whi!=
le (pass !1.5E !
10 )
{
x = 1;
}
pass = pass + 1;
}
boolean float int intx!
!
whi!
@int sum , count , pass , mnt; whi! 1.5E-6
do!
1.5E-6
@int sum , count , pass , mnt; whi!
le (pass ! !
10 )
}
boolean float int intx!
!
whi!

do!
dou!
whi!le
do!
1.5E while
1.5E 123
1.5E
1.5E@
do!
@
1.5E
43 changes: 25 additions & 18 deletions src/Parser/PredictiveTopDownParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ PredictiveTopDownParser::PredictiveTopDownParser(
}
}

PredictiveTopDownParser::~PredictiveTopDownParser() {
}
PredictiveTopDownParser::~PredictiveTopDownParser() {}

void PredictiveTopDownParser::parseInputTokens() {
std::cout << "Parsing input tokens..." << std::endl;
Expand All @@ -49,8 +48,6 @@ void PredictiveTopDownParser::parseInputTokens() {
handleNonTerminal(top, curr_token);
parsingFile << "\n";
}
// Accept the grammar if the stack is empty
std::cout << "Accept -_-" << std::endl;
parsingFile.close();
}

Expand All @@ -60,10 +57,14 @@ bool PredictiveTopDownParser::handleMatchOrError(const StackItem &top, Token *&c
handleMatch(top, curr_token);
return true;
} else {
handleMissingTerminal(top);
handleMissingTerminalOrSyncEntry(top);
return true;
}
}
if (*(curr_token->getKey()) == "$") {
handleEndOfInput();
return true;
}
return false;
}

Expand All @@ -72,9 +73,19 @@ void PredictiveTopDownParser::handleMatch(const StackItem &top, Token *&curr_tok

std::cout << "Match " << *(curr_token->getKey()) << std::endl;
stk.pop();
if (*(curr_token->getKey()) == "$") {
handleEndOfInput();
return;
}
curr_token = lex.getNextToken(); // Advance to the next token
}

void PredictiveTopDownParser::handleEndOfInput() {
while (!stk.empty()) {
handleMissingTerminalOrSyncEntry(stk.top());
Bazina marked this conversation as resolved.
Show resolved Hide resolved
stk.pop();
}
}

void PredictiveTopDownParser::handleNonTerminal(const StackItem &top, Token *&curr_token) {
const CellValue *cellValue = predictive_table.lookUp(top.token, *(curr_token->getKey()));
Expand All @@ -85,7 +96,7 @@ void PredictiveTopDownParser::handleNonTerminal(const StackItem &top, Token *&cu
handleEmptyEntry(top, curr_token);
break;
case ParsingTableEntryType::SYNC:
handleSyncEntry(top);
handleMissingTerminalOrSyncEntry(top);
break;
case ParsingTableEntryType::VALID_PRODUCTION:
handleValidProduction(top, cellValue);
Expand All @@ -96,31 +107,27 @@ void PredictiveTopDownParser::handleNonTerminal(const StackItem &top, Token *&cu
}

// Helper functions for error handling and stack operations
void PredictiveTopDownParser::handleMissingTerminal(const StackItem &top) {
parsingFile << "Error: missing " << top.token << ", discarded" << " |";
void PredictiveTopDownParser::handleMissingTerminalOrSyncEntry(const StackItem &top) {
parsingFile << "Error: missing " << top.token << " discarded" << " |";

std::cerr << "Error: missing " << top.token << ", discarded" << std::endl;
std::cerr << "Error: missing " << top.token << " discarded" << std::endl;
stk.pop();
}

/**
* @brief discard the current token (input symbol) and advance to the next token
* */
void PredictiveTopDownParser::handleEmptyEntry(const StackItem &top, Token *&curr_token) {
parsingFile << "Error:(illegal " << top.token << ") at line("
<< curr_token->getPosition()->line_number << ") column("
<< curr_token->getPosition()->column_number << ") discard " << curr_token << " |";
<< curr_token->getPosition()->column_number << ") - discard " << *(curr_token->getKey()) << " |";

std::cerr << "Error:(illegal " << top.token << ") at line("
<< curr_token->getPosition()->line_number << ") column("
<< curr_token->getPosition()->column_number << ") discard " << curr_token << std::endl;
<< curr_token->getPosition()->column_number << ") - discard " << *(curr_token->getKey()) << std::endl;
curr_token = lex.getNextToken();
}

void PredictiveTopDownParser::handleSyncEntry(const StackItem &top) {
parsingFile << "Error: missing " << top.token << ", discarded" << " |";

std::cerr << "Error: missing " << top.token << ", discarded" << std::endl;
stk.pop();
}

void PredictiveTopDownParser::handleValidProduction(const StackItem &top, const CellValue *cellValue) {
stk.pop();
auto production = cellValue->getProduction().productions[0];
Expand Down