(Note that this is just something I made up to illustrate the problem. I am well aware there are more efficient ways to parse an arithmetic expression and though the subject is fascinating, this has nothing to do with my actual question. It's just a semi-realistic example, if I might say so.
I agree the parser thing might make Вопрос кажется более сложным, но я не мог думать о более абстрактном примере < /em>). < /p>
Предположим, что вы хотите сделать простой анализатор выражения. Вы получите кусочки струн от токенизатора, некоторые из них, возможно, являются неоднозначными. Следует: < /p>
1) Определите отсортированный массив, описывающий все возможные операторы < /p>
(Note that this is just something I made up to illustrate the problem. I am well aware there are more efficient ways to parse an arithmetic expression and though the subject is fascinating, this has nothing to do with my actual question. It's just a semi-realistic example, if I might say so.
I agree the parser thing might make Вопрос кажется более сложным, но я не мог думать о более абстрактном примере < /em>). < /p>
Предположим, что вы хотите сделать простой анализатор выражения. Вы получите кусочки струн от токенизатора, некоторые из них, возможно, являются неоднозначными. Следует: < /p>
1) Определите отсортированный массив, описывающий все возможные операторы < /p>
[code]// types of operators enum class opType: char { unary, lasso, rasso, none };
// partial order comparison bool operator< (const opDesc& a) const { // unary operators first if (symbol == a.symbol) return type < a.type; return symbol < a.symbol; }
// comparison with strings static bool comp_desc_str (const opDesc& a, const string& s) { return a.symbol < s; } static bool comp_str_desc (const string& s, const opDesc& a) { return s < a.symbol; } };
// partial order comparison bool operator< (const opDesc& a) const { // unary operators first if (symbol == a.symbol) return type < a.type; return symbol < a.symbol; }
// comparison with strings static bool comp_desc_str (const opDesc& a, const string& s) { return a.symbol < s; } static bool comp_str_desc (const string& s, const opDesc& a) { return s < a.symbol; }