Итак, у меня есть грамматика:
S -> A B #
S -> C #
C -> c #
S -> a #
A -> a A #
B -> b #
##
И первые наборы всех нетерминалов должны быть
FIRST(S) = { c, a }
FIRST(A) = { a }
FIRST(B) = { b }
FIRST(C) = { c }
, но я получаю вывод как First (S) = {a, c}
Итак, мне нужно отсортировать его в порядке их появления в грамматике, и поэтому у меня есть вектор, в котором я храню все символы, называемые "_veverything", истрока с именем «tempstr», в которой хранятся ПЕРВЫЕ наборы грамматики.
for (auto& nt : _vnonterminals) //for each of the non-terminals
{ //create a tempstr
string tempstr = "";
//append all elements of first set of this non-terminal into the string
for (auto& el : _mapFirst[nt])
{
tempstr = tempstr + " " + el + ",";
}
//remove the trailing comma "," if it exists
if (tempstr.at(tempstr.length() - 1) == ',')
{
//tempstr.pop_back();
tempstr = tempstr.substr(0, tempstr.size() - 1);
}
//print this NT's first set beautifully
cout << "FIRST(" << nt << ") = {" << tempstr << " }";
if (nt != _vnonterminals.back())
{
cout << endl;
}
}
Как реализовать код, чтобы все элементы в tempstr сортировались в том виде, в котором они отображаются в грамматике _veverything?