解析数学EX pression在C / C ++ [英] parsing math expression in c/c++

查看:157
本文介绍了解析数学EX pression在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆