我有一个关于解析树的问题:
I have a question about Parsing Trees:
我有一个字符串(数学表达式 estring),例如:(a+b)*c-(d-e)*f/g.我必须在树中解析该表达式:
I have a string (math expresion estring), for example: (a+b)*c-(d-e)*f/g. I have to parse that expression in a Tree:
class Exp{};
class Term: public Exp{
int n_;
}
class Node: Public Exp{
Exp* loperator_;
Exp* roperator_;
char operation; // +, -, *, /
}
我可以使用什么算法来构建表示上述表达式字符串的树?
What algorithm can I use to build a tree which represents the expresion string above?
使用 Shunting-yard 算法.维基百科的描述相当全面,我希望它足够了.
Use the Shunting-yard algorithm. The wikipedia description is quite comprehensive, I hope it will suffice.
您也可以尝试编写形式化语法,例如解析表达式语法,并使用工具生成解析器.这个关于 PEG 的站点列出了 3 个用于 PEG 解析的 C/C++ 库.
You can also try to write a formal grammar, for example a parsing-expression grammar, and use a tool to generate a parser. This site about PEGs lists 3 C/C++ libraries for PEG parsing.
这篇关于在 C++ 中解析数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
编译器如何处理编译时分支?What do compilers do with compile-time branching?(编译器如何处理编译时分支?)
我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?)
在 C/C++ 中检查空指针Checking for NULL pointer in C/C++(在 C/C++ 中检查空指针)
比较运算符的数学式链接-如“if((5<j<=1))&quMath-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比较运算符的数学式链接-如“if((5<j<=1)))
“if constexpr()"之间的区别与“if()"Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之间的区别与“if())
C++,'if' 表达式中的变量声明C++, variable declaration in #39;if#39; expression(C++,if 表达式中的变量声明)