解析数学EX pression在C / C ++ [英] parsing math expression in c/c++
问题描述
我有一个关于解析树的问题:
I have a question about Parsing Trees:
我有一个字符串(数学前presion公司为EString),例如:(A + B)* C-(D-E)* F / G
。我必须在解析一棵树前pression:
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; // +, -, *, /
}
我可以使用哪些算法来构建一个树,重新presents上述前presion的字符串?
What algorithm can I use to build a tree which represents the expresion string above?
推荐答案
使用调度场算法。维基百科的描述是相当COM prehensive,我希望这就够了。
Use the Shunting-yard algorithm. The wikipedia description is quite comprehensive, I hope it will suffice.
您也可以尝试写一个正式的语法,例如解析-EX pression语法一>和使用工具以生成解析器。 本网站有关的PEG 列出3 C / C ++库PEG解析。
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.
这篇关于解析数学EX pression在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!