Skip to content

Commit 98c387e

Browse files
authored
Merge pull request #24 from TheSYNcoder/parser-end-token-fix
Adding $ in constructor
2 parents 6d663b7 + f74b302 commit 98c387e

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/include/parser/parsing_table.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace jucc {
1414
namespace parser {
1515

1616
const char SYNCH_TOKEN[] = "synch";
17-
1817
const char ERROR_TOKEN[] = "error";
1918

2019
class ParsingTable {
@@ -61,10 +60,12 @@ class ParsingTable {
6160

6261
/**
6362
* Used for setting synchronization tokens in the parsing table calculated from the
64-
* follow set.
63+
* follow set. Adds "$" to terminals_.
6564
*/
6665
ParsingTable(std::vector<std::string> terms, std::vector<std::string> non_terms)
67-
: terminals_(std::move(terms)), non_terminals_(std::move(non_terms)) {}
66+
: terminals_(std::move(terms)), non_terminals_(std::move(non_terms)) {
67+
terminals_.emplace_back(utils::STRING_ENDMARKER);
68+
}
6869

6970
/**
7071
* Builds the parsing table from the firsts and follows

test/parser/parser_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ TEST(parser, Parser1) {
3535
auto firsts = utils::CalcFirsts(grammar, nullables);
3636
auto follows = utils::CalcFollows(grammar, firsts, nullables, "E");
3737

38-
std::vector<std::string> terminals = {"id", "+", "*", "(", ")", "$"};
38+
std::vector<std::string> terminals = {"id", "+", "*", "(", ")"};
3939
std::vector<std::string> non_terminals = {"E", "E'", "T", "T'", "F"};
4040

4141
ParsingTable table = ParsingTable(terminals, non_terminals);

test/parser/parsing_table_test.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ TEST(parser, ParsingTable1) {
3131
auto firsts = utils::CalcFirsts(grammar, nullables);
3232
auto follows = utils::CalcFollows(grammar, firsts, nullables, "S");
3333

34-
std::vector<std::string> terminals = {"a", "b", "c", "d", "$"};
34+
std::vector<std::string> terminals = {"a", "b", "c", "d"};
3535
std::vector<std::string> non_terminals = {"A", "S", "B"};
3636

3737
ParsingTable table = ParsingTable(terminals, non_terminals);
@@ -90,7 +90,7 @@ TEST(parser, ParsingTable2) {
9090
auto firsts = utils::CalcFirsts(grammar, nullables);
9191
auto follows = utils::CalcFollows(grammar, firsts, nullables, "S");
9292

93-
std::vector<std::string> terminals = {"a", "b", "c", "$"};
93+
std::vector<std::string> terminals = {"a", "b", "c"};
9494
std::vector<std::string> non_terminals = {"C", "S", "B"};
9595

9696
ParsingTable table = ParsingTable(terminals, non_terminals);
@@ -112,15 +112,15 @@ TEST(parser, ParsingTable2) {
112112
ASSERT_EQ(p.first, 2);
113113
ASSERT_EQ(p.second, 0);
114114

115-
p = table.GetEntry("S", "$");
115+
p = table.GetEntry("S", utils::STRING_ENDMARKER);
116116
ASSERT_EQ(p.first, 0);
117117
ASSERT_EQ(p.second, 1);
118118

119-
p = table.GetEntry("B", "$");
119+
p = table.GetEntry("B", utils::STRING_ENDMARKER);
120120
ASSERT_EQ(p.first, 1);
121121
ASSERT_EQ(p.second, 1);
122122

123-
p = table.GetEntry("C", "$");
123+
p = table.GetEntry("C", utils::STRING_ENDMARKER);
124124
ASSERT_EQ(p.first, 2);
125125
ASSERT_EQ(p.second, 1);
126126
}
@@ -149,7 +149,7 @@ TEST(parser, ParsingTable3) {
149149
auto firsts = utils::CalcFirsts(grammar, nullables);
150150
auto follows = utils::CalcFollows(grammar, firsts, nullables, "S");
151151

152-
std::vector<std::string> terminals = {"a", "b", "c", "$"};
152+
std::vector<std::string> terminals = {"a", "b", "c"};
153153
std::vector<std::string> non_terminals = {"C", "S", "B"};
154154

155155
ParsingTable table = ParsingTable(terminals, non_terminals);
@@ -171,15 +171,15 @@ TEST(parser, ParsingTable3) {
171171
ASSERT_EQ(p.first, 2);
172172
ASSERT_EQ(p.second, 0);
173173

174-
p = table.GetEntry("S", "$");
174+
p = table.GetEntry("S", utils::STRING_ENDMARKER);
175175
ASSERT_EQ(p.first, 0);
176176
ASSERT_EQ(p.second, 1);
177177

178-
p = table.GetEntry("B", "$");
178+
p = table.GetEntry("B", utils::STRING_ENDMARKER);
179179
ASSERT_EQ(p.first, 1);
180180
ASSERT_EQ(p.second, 1);
181181

182-
p = table.GetEntry("C", "$");
182+
p = table.GetEntry("C", utils::STRING_ENDMARKER);
183183
ASSERT_EQ(p.first, 2);
184184
ASSERT_EQ(p.second, 1);
185185
}
@@ -208,7 +208,7 @@ TEST(parser, ParsingTable4) {
208208
auto firsts = utils::CalcFirsts(grammar, nullables);
209209
auto follows = utils::CalcFollows(grammar, firsts, nullables, "S");
210210

211-
std::vector<std::string> terminals = {"a", "b", "$"};
211+
std::vector<std::string> terminals = {"a", "b"};
212212
std::vector<std::string> non_terminals = {"A", "S", "B"};
213213

214214
ParsingTable table = ParsingTable(terminals, non_terminals);
@@ -234,19 +234,19 @@ TEST(parser, ParsingTable4) {
234234
ASSERT_EQ(p.first, 2);
235235
ASSERT_EQ(p.second, 0);
236236

237-
p = table.GetEntry("S", "$");
237+
p = table.GetEntry("S", utils::STRING_ENDMARKER);
238238
ASSERT_EQ(p.first, 0);
239239
ASSERT_EQ(p.second, 0);
240240

241-
p = table.GetEntry("A", "$");
241+
p = table.GetEntry("A", utils::STRING_ENDMARKER);
242242
ASSERT_EQ(p.first, 1);
243243
ASSERT_EQ(p.second, 1);
244244

245245
p = table.GetEntry("A", "b");
246246
ASSERT_EQ(p.first, 1);
247247
ASSERT_EQ(p.second, 1);
248248

249-
p = table.GetEntry("B", "$");
249+
p = table.GetEntry("B", utils::STRING_ENDMARKER);
250250
ASSERT_EQ(p.first, 2);
251251
ASSERT_EQ(p.second, 1);
252252
}

0 commit comments

Comments
 (0)