Skip to content

Commit

Permalink
Switch this->m_lexer to m_lexer by using Parser::m_lexer.
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafMohamed committed Dec 9, 2024
1 parent ae64f64 commit 9153a7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/log_surgeon/Lalr1Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ class Lalr1Parser : public Parser<TypedNfaState, TypedDfaState> {

auto symbol_is_token(uint32_t s) -> bool { return m_terminals.find(s) != m_terminals.end(); }

using Parser<TypedNfaState, TypedDfaState>::m_lexer;

std::set<uint32_t> m_terminals;
std::set<uint32_t> m_nullable;
std::map<std::set<Item>, std::unique_ptr<ItemSet>> m_lr0_item_sets;
Expand Down
46 changes: 23 additions & 23 deletions src/log_surgeon/Lalr1Parser.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::add_rule(
std::unique_ptr<finite_automata::RegexAST<TypedNfaState>> rule
) {
Parser<TypedNfaState, TypedDfaState>::add_rule(name, std::move(rule));
m_terminals.insert(this->m_lexer.m_symbol_id[name]);
m_terminals.insert(m_lexer.m_symbol_id[name]);
}

template <typename TypedNfaState, typename TypedDfaState>
Expand Down Expand Up @@ -115,9 +115,9 @@ auto Lalr1Parser<TypedNfaState, TypedDfaState>::add_production(
std::vector<std::string> const& body,
SemanticRule semantic_rule
) -> uint32_t {
if (this->m_lexer.m_symbol_id.find(head) == this->m_lexer.m_symbol_id.end()) {
this->m_lexer.m_symbol_id[head] = this->m_lexer.m_symbol_id.size();
this->m_lexer.m_id_symbol[this->m_lexer.m_symbol_id[head]] = head;
if (m_lexer.m_symbol_id.find(head) == m_lexer.m_symbol_id.end()) {
m_lexer.m_symbol_id[head] = m_lexer.m_symbol_id.size();
m_lexer.m_id_symbol[m_lexer.m_symbol_id[head]] = head;
}
uint32_t n = m_productions.size();
auto it = m_productions_map.find(head);
Expand All @@ -131,13 +131,13 @@ auto Lalr1Parser<TypedNfaState, TypedDfaState>::add_production(
}
std::unique_ptr<Production> p(new Production);
p->m_index = n;
p->m_head = this->m_lexer.m_symbol_id[head];
p->m_head = m_lexer.m_symbol_id[head];
for (std::string const& symbol_string : body) {
if (this->m_lexer.m_symbol_id.find(symbol_string) == this->m_lexer.m_symbol_id.end()) {
this->m_lexer.m_symbol_id[symbol_string] = this->m_lexer.m_symbol_id.size();
this->m_lexer.m_id_symbol[this->m_lexer.m_symbol_id[symbol_string]] = symbol_string;
if (m_lexer.m_symbol_id.find(symbol_string) == m_lexer.m_symbol_id.end()) {
m_lexer.m_symbol_id[symbol_string] = m_lexer.m_symbol_id.size();
m_lexer.m_id_symbol[m_lexer.m_symbol_id[symbol_string]] = symbol_string;
}
p->m_body.push_back(this->m_lexer.m_symbol_id[symbol_string]);
p->m_body.push_back(m_lexer.m_symbol_id[symbol_string]);
}
p->m_semantic_rule = std::move(semantic_rule);
m_non_terminals.insert(std::pair<int, std::vector<Production*>>(p->m_head, {}));
Expand All @@ -152,7 +152,7 @@ auto Lalr1Parser<TypedNfaState, TypedDfaState>::add_production(

template <typename TypedNfaState, typename TypedDfaState>
void Lalr1Parser<TypedNfaState, TypedDfaState>::generate() {
this->m_lexer.generate();
m_lexer.generate();
assert(!m_productions.empty());
generate_lr0_kernels();
generate_first_sets();
Expand Down Expand Up @@ -435,7 +435,7 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::generate_lalr1_action() {
for (std::map<std::set<Item>, std::unique_ptr<ItemSet>>::value_type const& kv : m_lr1_item_sets)
{
ItemSet* item_set_ptr = kv.second.get();
item_set_ptr->m_actions.resize(this->m_lexer.m_symbol_id.size(), false);
item_set_ptr->m_actions.resize(m_lexer.m_symbol_id.size(), false);
for (Item const& item : item_set_ptr->m_closure) {
if (!item.has_dot_at_end()) {
if (m_terminals.find(item.next_symbol()) == m_terminals.end()
Expand All @@ -453,7 +453,7 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::generate_lalr1_action() {
}
std::string conflict_msg{};
conflict_msg += "For symbol ";
conflict_msg += this->m_lexer.m_id_symbol[item.next_symbol()];
conflict_msg += m_lexer.m_id_symbol[item.next_symbol()];
conflict_msg += ", adding shift to ";
conflict_msg
+= std::to_string(item_set_ptr->m_next[item.next_symbol()]->m_index);
Expand All @@ -465,10 +465,10 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::generate_lalr1_action() {
} else {
conflict_msg += "shift-reduce conflict with reduction ";
conflict_msg
+= this->m_lexer.m_id_symbol[std::get<Production*>(action)->m_head];
+= m_lexer.m_id_symbol[std::get<Production*>(action)->m_head];
conflict_msg += "-> {";
for (uint32_t symbol : std::get<Production*>(action)->m_body) {
conflict_msg += this->m_lexer.m_id_symbol[symbol] + ",";
conflict_msg += m_lexer.m_id_symbol[symbol] + ",";
}
conflict_msg += "}\n";
}
Expand All @@ -486,12 +486,12 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::generate_lalr1_action() {
if (!std::holds_alternative<bool>(action)) {
std::string conflict_msg{};
conflict_msg += "For symbol ";
conflict_msg += this->m_lexer.m_id_symbol[item.m_lookahead];
conflict_msg += m_lexer.m_id_symbol[item.m_lookahead];
conflict_msg += ", adding reduction ";
conflict_msg += this->m_lexer.m_id_symbol[item.m_production->m_head];
conflict_msg += m_lexer.m_id_symbol[item.m_production->m_head];
conflict_msg += "-> {";
for (uint32_t symbol : item.m_production->m_body) {
conflict_msg += this->m_lexer.m_id_symbol[symbol] + ",";
conflict_msg += m_lexer.m_id_symbol[symbol] + ",";
}
conflict_msg += "} causes ";
if (std::holds_alternative<ItemSet*>(action)) {
Expand All @@ -501,11 +501,11 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::generate_lalr1_action() {
} else {
conflict_msg += "reduce-reduce conflict with reduction ";
conflict_msg
+= this->m_lexer
+= m_lexer
.m_id_symbol[std::get<Production*>(action)->m_head];
conflict_msg += "-> {";
for (uint32_t symbol : std::get<Production*>(action)->m_body) {
conflict_msg += this->m_lexer.m_id_symbol[symbol] + ",";
conflict_msg += m_lexer.m_id_symbol[symbol] + ",";
}
conflict_msg += "}\n";
}
Expand Down Expand Up @@ -605,12 +605,12 @@ auto Lalr1Parser<TypedNfaState, TypedDfaState>::report_error() -> std::string {
error_type += "'";
if (auto* regex_ast_literal
= dynamic_cast<finite_automata::RegexASTLiteral<TypedNfaState>*>(
this->m_lexer.get_rule(i)
m_lexer.get_rule(i)
))
{
error_type += unescape(char(regex_ast_literal->get_character()));
} else {
error_type += this->m_lexer.m_id_symbol[i];
error_type += m_lexer.m_id_symbol[i];
}
error_type += "',";
}
Expand Down Expand Up @@ -660,14 +660,14 @@ void Lalr1Parser<TypedNfaState, TypedDfaState>::reset() {
m_parse_stack_matches.pop();
}
m_input_buffer.reset();
this->m_lexer.reset();
m_lexer.reset();
}

template <typename TypedNfaState, typename TypedDfaState>
auto Lalr1Parser<TypedNfaState, TypedDfaState>::get_next_symbol() -> Token {
if (m_next_token == std::nullopt) {
Token token;
if (ErrorCode error = this->m_lexer.scan(m_input_buffer, token);
if (ErrorCode error = m_lexer.scan(m_input_buffer, token);
ErrorCode::Success != error)
{
throw std::runtime_error("Error scanning in lexer.");
Expand Down

0 comments on commit 9153a7c

Please sign in to comment.