Skip to content

Commit 585b04e

Browse files
committed
format
1 parent d5c5413 commit 585b04e

File tree

10 files changed

+325
-377
lines changed

10 files changed

+325
-377
lines changed

backend/grammar.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "grammar.hpp"
2020
#include "symbol_table.hpp"
2121
#include <algorithm>
22-
#include <ranges>
2322
#include <iostream>
23+
#include <ranges>
2424
#include <unordered_map>
2525
#include <vector>
2626

@@ -41,28 +41,25 @@ Grammar::Grammar(
4141
}
4242
}
4343
}
44-
axiom_ = "S";
45-
g_ = grammar;
44+
axiom_ = "S";
45+
g_ = grammar;
4646
g_[axiom_] = {{"A", st_.EOL_}};
4747
st_.PutSymbol(axiom_, false);
4848
}
4949

50-
void Grammar::SetAxiom(const std::string& axiom)
51-
{
50+
void Grammar::SetAxiom(const std::string& axiom) {
5251
axiom_ = axiom;
5352
}
5453

55-
bool Grammar::HasEmptyProduction(const std::string& antecedent) const
56-
{
54+
bool Grammar::HasEmptyProduction(const std::string& antecedent) const {
5755
auto rules{g_.at(std::string{antecedent})};
5856
return std::ranges::find_if(rules, [&](const auto& rule) {
5957
return rule[0] == st_.EPSILON_;
6058
}) != rules.end();
6159
}
6260

63-
std::vector<std::pair<const std::string, production>> Grammar::FilterRulesByConsequent(
64-
const std::string& arg) const
65-
{
61+
std::vector<std::pair<const std::string, production>>
62+
Grammar::FilterRulesByConsequent(const std::string& arg) const {
6663
std::vector<std::pair<const std::string, production>> rules;
6764
for (const auto& [nt, prods] : g_) {
6865
for (const production& prod : prods) {
@@ -76,7 +73,7 @@ std::vector<std::pair<const std::string, production>> Grammar::FilterRulesByCons
7673

7774
// GCOVR_EXCL_START
7875
// LCOV_EXCL_START
79-
void Grammar::Debug() const //NOSONAR
76+
void Grammar::Debug() const // NOSONAR
8077
{
8178
std::cout << "Grammar:\n";
8279
for (const auto& entry : g_) {
@@ -103,26 +100,25 @@ std::vector<std::string> Grammar::Split(const std::string& s) {
103100
return {st_.EPSILON_};
104101
}
105102
std::vector<std::string> splitted;
106-
std::string str;
107-
unsigned start{0};
108-
unsigned end{1};
103+
std::string str;
104+
unsigned start{0};
105+
unsigned end{1};
109106

110107
while (end <= s.size()) {
111108
str = s.substr(start, end - start);
112109

113110
if (st_.In(str)) {
114111
unsigned lookahead = end + 1;
115112
while (lookahead <= s.size()) {
116-
if (std::string extended =
117-
s.substr(start, lookahead - start);
113+
if (std::string extended = s.substr(start, lookahead - start);
118114
st_.In(extended)) {
119115
end = lookahead;
120116
}
121117
++lookahead;
122118
}
123119
splitted.push_back(s.substr(start, end - start));
124120
start = end;
125-
end = start + 1;
121+
end = start + 1;
126122
} else {
127123
++end;
128124
}

backend/grammar.hpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ using production = std::vector<std::string>;
3535

3636
/**
3737
* @struct Grammar
38-
* @brief Represents a context-free grammar, including its rules, symbol table, and starting symbol.
38+
* @brief Represents a context-free grammar, including its rules, symbol table,
39+
* and starting symbol.
3940
*
40-
* This structure encapsulates all components required to define and manipulate a grammar,
41-
* including production rules, the associated symbol table, and metadata such as the start symbol.
42-
* It supports construction, transformation, and analysis of grammars.
41+
* This structure encapsulates all components required to define and manipulate
42+
* a grammar, including production rules, the associated symbol table, and
43+
* metadata such as the start symbol. It supports construction, transformation,
44+
* and analysis of grammars.
4345
*/
4446
struct Grammar {
4547

@@ -48,7 +50,6 @@ struct Grammar {
4850
const std::unordered_map<std::string, std::vector<production>>&
4951
grammar);
5052

51-
5253
/**
5354
* @brief Sets the axiom (entry point) of the grammar.
5455
*
@@ -82,40 +83,43 @@ struct Grammar {
8283
* Searches for rules in which the specified token is part of the consequent
8384
* and returns those rules.
8485
*/
85-
std::vector<std::pair<const std::string, production>> FilterRulesByConsequent(
86-
const std::string& arg) const;
86+
std::vector<std::pair<const std::string, production>>
87+
FilterRulesByConsequent(const std::string& arg) const;
8788

8889
/**
8990
* @brief Prints the current grammar structure to standard output.
9091
*
9192
* This function provides a debug view of the grammar by printing out all
9293
* rules, the axiom, and other relevant details.
9394
*/
94-
void Debug() const; //NOSONAR
95+
void Debug() const; // NOSONAR
9596

9697
/**
97-
* @brief Adds a production rule to the grammar and updates the symbol table.
98-
*
99-
* This function inserts a new production of the form A → α into the grammar,
100-
* where `antecedent` is the non-terminal A and `consequent` is the sequence α.
101-
* It also updates the internal symbol table to reflect any new symbols introduced.
102-
*
103-
* @param antecedent The left-hand side non-terminal of the production.
104-
* @param consequent The right-hand side sequence of grammar symbols.
105-
*/
98+
* @brief Adds a production rule to the grammar and updates the symbol
99+
* table.
100+
*
101+
* This function inserts a new production of the form A → α into the
102+
* grammar, where `antecedent` is the non-terminal A and `consequent` is the
103+
* sequence α. It also updates the internal symbol table to reflect any new
104+
* symbols introduced.
105+
*
106+
* @param antecedent The left-hand side non-terminal of the production.
107+
* @param consequent The right-hand side sequence of grammar symbols.
108+
*/
106109
void AddProduction(const std::string& antecedent,
107110
const std::vector<std::string>& consequent);
108111

109112
/**
110-
* @brief Splits a string into grammar symbols using the current symbol table.
111-
*
112-
* This function tokenizes the input string `s` into a sequence of grammar symbols
113-
* based on the known entries in the symbol table. It uses a greedy approach,
114-
* matching the longest valid symbol at each step.
115-
*
116-
* @param s The input string to split.
117-
* @return A vector of grammar symbols extracted from the string.
118-
*/
113+
* @brief Splits a string into grammar symbols using the current symbol
114+
* table.
115+
*
116+
* This function tokenizes the input string `s` into a sequence of grammar
117+
* symbols based on the known entries in the symbol table. It uses a greedy
118+
* approach, matching the longest valid symbol at each step.
119+
*
120+
* @param s The input string to split.
121+
* @return A vector of grammar symbols extracted from the string.
122+
*/
119123
std::vector<std::string> Split(const std::string& s);
120124

121125
/**

backend/grammar_factory.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
#include "ll1_parser.hpp"
2121
#include "slr1_parser.hpp"
2222
#include <algorithm>
23-
#include <ranges>
2423
#include <iostream>
2524
#include <queue>
2625
#include <random>
26+
#include <ranges>
2727

2828
void GrammarFactory::Init() {
2929
items.emplace_back(
@@ -394,7 +394,8 @@ GrammarFactory::NullableSymbols(const Grammar& grammar) const {
394394
} else {
395395
bool all_nullable = true;
396396
for (const std::string& sym : prod) {
397-
if (!nullable.contains(sym) && sym != grammar.st_.EOL_) {
397+
if (!nullable.contains(sym) &&
398+
sym != grammar.st_.EOL_) {
398399
all_nullable = false;
399400
break;
400401
}
@@ -476,7 +477,8 @@ void GrammarFactory::LeftFactorize(Grammar& grammar) {
476477

477478
std::vector<std::string> new_production = common_prefix;
478479
new_production.push_back(new_non_terminal);
479-
factored_productions.emplace_back(std::move(new_production));
480+
factored_productions.emplace_back(
481+
std::move(new_production));
480482

481483
std::vector<production> new_remaining_productions;
482484
for (const auto& prod : remaining_productions) {
@@ -486,9 +488,11 @@ void GrammarFactory::LeftFactorize(Grammar& grammar) {
486488
prod.end());
487489
if (remaining_part.empty()) {
488490
remaining_part.push_back(grammar.st_.EPSILON_);
489-
grammar.st_.PutSymbol(grammar.st_.EPSILON_, true);
491+
grammar.st_.PutSymbol(grammar.st_.EPSILON_,
492+
true);
490493
}
491-
new_remaining_productions.emplace_back(std::move(remaining_part));
494+
new_remaining_productions.emplace_back(
495+
std::move(remaining_part));
492496
} else {
493497
factored_productions.emplace_back(prod);
494498
}
@@ -566,8 +570,8 @@ GrammarFactory::FactoryItem::FactoryItem(
566570
g_ = grammar;
567571
}
568572

569-
void GrammarFactory::NormalizeNonTerminals(FactoryItem& item,
570-
const std::string& nt) const {
573+
void GrammarFactory::NormalizeNonTerminals(FactoryItem& item,
574+
const std::string& nt) const {
571575
std::unordered_map<std::string, std::vector<production>> updated;
572576
for (auto& [old_nt, prods] : item.g_) {
573577
for (auto& prod : prods) {
@@ -600,7 +604,7 @@ void GrammarFactory::AdjustTerminals(FactoryItem& base, const FactoryItem& cmb,
600604
}
601605
std::vector<std::string> remaining(alphabet.begin(), alphabet.end());
602606
std::uniform_int_distribution<size_t> dist(0, remaining.size() - 1);
603-
std::string new_terminal = remaining[dist(gen)];
607+
std::string new_terminal = remaining[dist(gen)];
604608

605609
std::vector<std::string> base_terms(base.st_.terminals_wtho_eol_.begin(),
606610
base.st_.terminals_wtho_eol_.end());
@@ -622,8 +626,8 @@ void GrammarFactory::AdjustTerminals(FactoryItem& base, const FactoryItem& cmb,
622626
base_terms.assign(base.st_.terminals_wtho_eol_.begin(),
623627
base.st_.terminals_wtho_eol_.end());
624628
base_dist = std::uniform_int_distribution<size_t>(0, base_terms.size() - 1);
625-
to_replace = *std::next(base.st_.terminals_wtho_eol_.begin(),
626-
base_dist(gen));
629+
to_replace =
630+
*std::next(base.st_.terminals_wtho_eol_.begin(), base_dist(gen));
627631
for (auto& [nt, prods] : base.g_) {
628632
for (auto& prod : prods) {
629633
for (auto& symbol : prod) {

backend/grammar_factory.hpp

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ struct GrammarFactory {
5252
* grammar.
5353
* @param grammar The grammar to initialize the FactoryItem with.
5454
*/
55-
explicit FactoryItem(const std::unordered_map<std::string, std::vector<production>> &grammar);
55+
explicit FactoryItem(
56+
const std::unordered_map<std::string, std::vector<production>>&
57+
grammar);
5658
};
5759

5860
/**
@@ -206,14 +208,17 @@ struct GrammarFactory {
206208
* @param graph The directed graph.
207209
* @return true if grammar has cycle.
208210
*/
209-
bool HasCycle(const std::unordered_map<std::string, std::unordered_set<std::string>>& graph) const;
211+
bool HasCycle(
212+
const std::unordered_map<std::string, std::unordered_set<std::string>>&
213+
graph) const;
210214

211215
/**
212216
* @brief Find nullable symbols in a grammar.
213217
* @param grammar The grammar to check.
214218
* @return set of nullable symbols.
215219
*/
216-
std::unordered_set<std::string> NullableSymbols(const Grammar& grammar) const;
220+
std::unordered_set<std::string>
221+
NullableSymbols(const Grammar& grammar) const;
217222

218223
/**
219224
* @brief Removes direct left recursion in a grammar.
@@ -307,41 +312,47 @@ struct GrammarFactory {
307312
const std::string& base);
308313

309314
/**
310-
* @brief Replaces all non-terminal symbols in a grammar item with a single target non-terminal.
311-
*
312-
* This function is used during grammar combination to normalize the non-terminal
313-
* symbols in a given FactoryItem, so that they are consistent and compatible
314-
* with another item.
315-
*
316-
* @param item The grammar item whose non-terminals will be renamed.
317-
* @param nt The new non-terminal symbol that will replace all existing ones.
318-
*/
315+
* @brief Replaces all non-terminal symbols in a grammar item with a single
316+
* target non-terminal.
317+
*
318+
* This function is used during grammar combination to normalize the
319+
* non-terminal symbols in a given FactoryItem, so that they are consistent
320+
* and compatible with another item.
321+
*
322+
* @param item The grammar item whose non-terminals will be renamed.
323+
* @param nt The new non-terminal symbol that will replace all existing
324+
* ones.
325+
*/
319326
void NormalizeNonTerminals(FactoryItem& item, const std::string& nt) const;
320327

321328
/**
322-
* @brief Adjusts the terminal symbols between two grammar items.
323-
*
324-
* This function modifies the terminal symbols of a base grammar item so that
325-
* they do not conflict with those of the item being combined. It also renames
326-
* terminals to ensure consistency and inserts the target non-terminal where appropriate.
327-
*
328-
* @param base The base grammar item to adjust.
329-
* @param cmb The grammar item being combined with the base.
330-
* @param target_nt The target non-terminal symbol used for replacement.
331-
*/
329+
* @brief Adjusts the terminal symbols between two grammar items.
330+
*
331+
* This function modifies the terminal symbols of a base grammar item so
332+
* that they do not conflict with those of the item being combined. It also
333+
* renames terminals to ensure consistency and inserts the target
334+
* non-terminal where appropriate.
335+
*
336+
* @param base The base grammar item to adjust.
337+
* @param cmb The grammar item being combined with the base.
338+
* @param target_nt The target non-terminal symbol used for replacement.
339+
*/
332340
void AdjustTerminals(FactoryItem& base, const FactoryItem& cmb,
333341
const std::string& target_nt) const;
334342

335343
/**
336-
* @brief Merges the grammar rules of two grammar items into a single grammar.
337-
*
338-
* This function performs a raw combination of the production rules from both
339-
* grammar items, resulting in a single grammar map that contains all productions.
340-
*
341-
* @param base The first grammar item.
342-
* @param cmb The second grammar item.
343-
* @return A merged grammar map containing all production rules from both inputs.
344-
*/
344+
* @brief Merges the grammar rules of two grammar items into a single
345+
* grammar.
346+
*
347+
* This function performs a raw combination of the production rules from
348+
* both grammar items, resulting in a single grammar map that contains all
349+
* productions.
350+
*
351+
* @param base The first grammar item.
352+
* @param cmb The second grammar item.
353+
* @return A merged grammar map containing all production rules from both
354+
* inputs.
355+
*/
345356
std::unordered_map<std::string, std::vector<production>>
346357
Merge(const FactoryItem& base, const FactoryItem& cmb) const;
347358

0 commit comments

Comments
 (0)