Implementing “NOT” in boost::spirit mini_c

别来无恙 提交于 2019-12-11 19:15:31

问题


I tried to modify the mini_c example of boost::spirit to match to my existing vocabulary.

I therefore added a operator "NOT that should behave equal as "!":

unary_expr =
        primary_expr
    |   ("NOT" > primary_expr           [op(op_not)]) // This does not work
    |   ('!' > primary_expr             [op(op_not)])
    |   ('-' > primary_expr             [op(op_neg)])
    |   ('+' > primary_expr)
    ;

I can compile the modified source code, but when i try to execute it it fails to parse. How can i solve this?

EDIT: As my want to access external variables, i had made another modification in order to build a list of these variables when compiling:

identifier %=
    raw[lexeme[alpha >> *(alnum | '§' | '_' | '.' | '-' )]]
    ;
variable =
       identifier      [add_var(_1)]
    ;

Where add_var and identifier are defined as

rule<Iterator, std::string(), white_space> identifier;
function<var_adder> add_var;

If i don't use this modification, "NOT" can be used. With the modification, using "NOT" generates a parsing error.

EDIT 2: The following conditional expressions do work though:

logical_expr =
    relational_expr
    >> *(  ("AND" > relational_expr     [op(op_and)])
        |   ("OR" > relational_expr     [op(op_or)])
        )
    ;

回答1:


With your change the small test:

int main()
{
    return NOT 1;
}

parses successfully and returns 0. So it is not obvious to me what doesn't work for you. Could you provide a failing input example as well, please?



来源:https://stackoverflow.com/questions/3591533/implementing-not-in-boostspirit-mini-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!