Skip to content

Commit

Permalink
make use of token position while reporting a syntax error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedelsa3eed committed Dec 24, 2023
1 parent d989d19 commit 88c6d5f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 45 deletions.
1 change: 0 additions & 1 deletion src/Lex/InputReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ void InputReader::addPunctuations(std::string pString) {
pos++;
} else if ((pString)[pos] == '\\') {
std::string punctuation;
// punctuation += pString[pos];
punctuation += pString[pos + 1];
rules->addRule(RuleType::PUNCTUATION, punctuation);
pos += 2;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void Parser::buildPredictiveTable() {
}

void Parser::buildPredictiveTopDownParser() {
std::cout << "\nLL(1) parsing:\n";
std::cout << "\nLL(1) parser\n";
predictiveTopDownParser = new PredictiveTopDownParser(*lex, *predictiveTable,
firstAndFollowGenerator->getNonTerminals());
}
48 changes: 5 additions & 43 deletions src/Parser/PredictiveTopDownParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ void PredictiveTopDownParser::parseInputTokens() {
while (!stk.empty()) {
auto top = stk.top();

const bool is_parsing_complete = handleParsingCompletion(top, curr_token);
if (is_parsing_complete) {
return;
}

std::cout << "stack top: " << top.token << std::endl;
std::cout << "curr token: " << *(curr_token->getKey()) << std::endl;

Expand All @@ -42,43 +37,8 @@ void PredictiveTopDownParser::parseInputTokens() {
// Handle non-terminal at stack top
handleNonTerminal(top, curr_token);
}
}

bool PredictiveTopDownParser::handleParsingCompletion(const StackItem &top, Token *&curr_token) {
if (curr_token == nullptr) { // $ is the end of input
while (!stk.empty()) {
if (top.isTerminal) {
if (top.token == "$") {
std::cout << "Accept!" << std::endl;
stk.pop();
return true;
} else {
handleMissingTerminal(top);
}
} else {
handleNonTerminalAtEndOfInput(top);
}
}
return true;
}
return false;
}

void PredictiveTopDownParser::handleNonTerminalAtEndOfInput(const StackItem &top) {
const CellValue *cellValue = predictive_table.lookUp(top.token, "$"); // Look up using lookahead of $
ParsingTableEntryType entryType = cellValue->getPredictiveTableEntryType();

switch (entryType) {
case ParsingTableEntryType::EMPTY:
std::cerr << "Error:(illegal " << top.token << ") – Wrong Grammar!" << std::endl;
return; // Reject grammar
case ParsingTableEntryType::SYNC:
handleSyncEntry(top);
break;
case ParsingTableEntryType::VALID_PRODUCTION:
handleValidProduction(top, cellValue);
break;
}
// Accept the grammar if the stack is empty
std::cout << "Accept!" << std::endl;
}

bool PredictiveTopDownParser::handleMatchOrError(const StackItem &top, Token *&curr_token) {
Expand Down Expand Up @@ -125,7 +85,9 @@ void PredictiveTopDownParser::handleMissingTerminal(const StackItem &top) {
}

void PredictiveTopDownParser::handleEmptyEntry(const StackItem &top, Token *&curr_token) {
std::cerr << "Error:(illegal " << top.token << ") – discard " << curr_token << std::endl;
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 = lex.getNextToken();
}

Expand Down

0 comments on commit 88c6d5f

Please sign in to comment.