首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在grammar<Iterator中添加qi::symbols,double()>?

如何在grammar<Iterator中添加qi::symbols,double()>?
EN

Stack Overflow用户
提问于 2012-01-09 03:40:08
回答 1查看 1.6K关注 0票数 2

我正在尝试使用Boost.Spirit (V.2.5)库来创建一个迷你计算器。我想实现的功能:-基本的微积分(+,-,/,*),可以工作的-一些函数(比如min,max),也可以工作-声明/赋值双变量,但有问题...当我添加"vars.add“时,我得到了编译错误(模板参数不明确)。我试过"add(char_(_1)","add(_1)",...但似乎什么都不起作用。我显然遗漏了一些东西(实际上没有理解一些东西)。如果有人能在这方面帮助我,我将不胜感激!

以下是源代码:

代码语言:javascript
复制
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/home/phoenix/statement/if.hpp>
#include <boost/spirit/home/phoenix/bind/bind_function.hpp>

#include <iostream>
#include <string>

namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    namespace phx = boost::phoenix;

    struct vars_ : qi::symbols<char, double> {
        vars_() {
            add("ans" , 0);
        }
    } vars;

    template <typename Iterator>
    struct parser : qi::grammar<Iterator, double()>
    {
        parser() : parser::base_type(function)
        {
            using qi::eps;
            using qi::lit;
            using qi::_val;
            using qi::_1;
            using ascii::char_;
            using qi::double_;
            using qi::string;
            using qi::lexeme;
            using boost::phoenix::if_;
            using qi::alpha;
            using qi::alnum;

            MAX = lexeme[string("max") | string("MAX")]; //define max symbol

            MIN = lexeme[string("min") | string("MIN")]; //define min symbol

            D   = lexeme[string("d") | string("D")];     //define distance symbol

            ANS = lexeme[string("ans") | string("ANS")]; //not working yet

            function =
                expression                                      [_val = _1]
                | declaration
                | assignment
                | ( MAX >> "(" >> function                      [_val = _1] >> 
                        +(',' >> function                       [if_(_1 > _val)[_val = _1]]) >> ')') // call : max(function,...)
                | ( MIN >> "(" >> function                      [_val = _1] >>
                        +(',' >> function                       [if_(_1 < _val)[_val = _1]]) >> ')') // call : min(function,...)
                | ( D   >> "(" >> (function >> ',' >> function) >> ')');                             // call : d(point1,point2) not implemented yet

            expression =
                term                            [_val = _1]
                >> *(   ('+' >> term            [_val += _1])
                    |   ('-' >> term            [_val -= _1]));

            term =
                factor                          [_val = _1]
                >> *(   ('*' >> factor          [_val *= _1])
                    |   ('/' >> factor          [_val /= _1]));
            factor = 
                double_                         [_val = _1]
                | (vars                         [_val += _1] )
                |   '(' >> expression           [_val = _1] >> ')'
                |   ('-' >> factor              [_val = -_1])
                |   ('+' >> factor              [_val = _1])
                | declaration;
            ;

            assignment =
                vars >> '=' >> function;

            var_decl =
                lexeme [ qi::raw [  ( alpha >> *( alnum | '_' ) ) - vars  ] ] //[ phx::bind(vars.add, _1) ]
                    ;


            declaration =
                "var " >> var_decl >> *( ',' >> var_decl );
        }

        qi::rule<Iterator, double()> MAX, MIN, D, ANS, expression, term, factor, 
                                        function, assignment, var_decl, declaration;
    };
}

///////////////////////////////////////////////////////////////////////////////
//  Main program
///////////////////////////////////////////////////////////////////////////////
int main()
{
    std::cout << "**********************************************************" << std::endl;
    std::cout << "*                                                        *" << std::endl;
    std::cout << "*          Command interface for VideoTraction4          *" << std::endl;
    std::cout << "*                                                        *" << std::endl;
    std::cout << "**********************************************************" << std::endl << std::endl;
    std::cout << "Type an expression...or [q or Q] to quit" << std::endl << std::endl;

    typedef std::string::const_iterator iterator_type;
    typedef client::parser<iterator_type> parser;

    parser _parser; // Our grammar

    std::string str;
    double result;
    while (std::getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();

        bool r = parse(iter, end, _parser, result);

        if (r && iter == end)
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing succeeded\n";
            std::cout << "result = " << result << std::endl;
            std::cout << "-------------------------\n";
            client::vars.remove("ans");
            client::vars.add("ans",result);
        }
        else
        {
            std::string rest(iter, end);
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \": " << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

我想做一些事情,比如:

代码语言:javascript
复制
var i,j
i = 1
j = max(2*(i+1),5)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-09 05:32:32

嗯,我自己从来没有在语义动作中直接添加过符号(我更喜欢构建ASTs,然后遍历它们)。

你这样写的事实和没有SSCCE的事实让我大吃一惊。事实证明,我应该忽略间接的“证据”,直接选择docs + win:

代码语言:javascript
复制
var_decl =
    qi::as_string [ lexeme [ ( ( alpha >> *( alnum | '_' ) ) - vars ) ] ]
        [ phx::bind(vars.add, _1) ];

代码语言:javascript
复制
var_decl =
     lexeme [ qi::raw [  ( alpha >> *( alnum | '_' ) ) - vars  ] ]
        [ phx::bind(vars.add, _1) ];

一些类似的咒语也会适用

编辑这里是完整的源代码,在MSVC2010上编译,Boost1.47:

输出:

代码语言:javascript
复制
T:\>cl /EHsc /I "c:\Program Files (x86)\boost\boost_1_47" test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

T:\>.\test.exe
**********************************************************
*                                                        *
*          Command interface for VideoTraction4          *
*                                                        *
**********************************************************

Type an expression...or [q or Q] to quit

3*4+7
-------------------------
Parsing succeeded
result = 19
-------------------------

在一个稍微不相关的注释中:它看起来很友好-有趣的是,var_decl规则的公开属性似乎是char

您可能希望阅读有关自动规则和%=操作符的信息;如果没有%=,语义操作的存在将抑制所有自动属性传播。这在所需的定制点不存在的情况下很有帮助。如上所述,公开的属性将始终是未赋值的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8780604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档