Increased semantic actions causing parsing problems

I worked with the Boost mini compiler example. Here is the root of the source code: http://www.boost.org/doc/libs/1_59_0/libs/spirit/example/qi/compiler_tutorial/mini_c/

The fragment that interests me is in statement_def.hpp

The problem I am facing is that if you attach semantic actions, such as

statement_ =
                variable_declaration[print_this_declaration]
            |   assignment
            |   compound_statement
            |   if_statement
            |   while_statement
            |   return_statement
            ;

And then run the compiler mini_cin an example program, for example:

int foo(n) {
    if (n == 3) { }
    return a;
}

int main() {
    return foo(10);
}

It launches the "Duplicate Function Error" found in the file "compile.cpp" (found using the link above). Here is a snippet for quick reference:

    if (functions.find(x.function_name.name) != functions.end())
    {
        error_handler(x.function_name.id, "Duplicate function: " + x.function_name.name);
        return false;
    }

In life, I can’t understand why.

, , , - , , ( ).

, - , - ( ).

, , , ( Boost, ), - //Iterator , t ( SA), - .

+3
1

[...] , - , ,

, ... :) .

, - , ( ).

. "" , . . .

:

  • Qi, , . , .

    . . : /

    :

  • - , statement_, ast::statement:

    qi::rule<Iterator, ast::statement(), skipper<Iterator> > statement_;
    
  • ast::statement , :

    typedef boost::variant<
            variable_declaration
        , assignment
        , boost::recursive_wrapper<if_statement>
        , boost::recursive_wrapper<while_statement>
        , boost::recursive_wrapper<return_statement>
        , boost::recursive_wrapper<statement_list>
        >
    statement;
    
  • , variable_declaration!

    struct variable_declaration {
        identifier lhs;
        boost::optional<expression> rhs;
    };
    

    , , statement_, AST " identifier name """. ( , (rhs) ).

    , , , " ".

?

, .

operator%= operator=, :

    statement_ %=
            variable_declaration [print_this_declaration]
        |   assignment
        |   compound_statement
        |   if_statement
        |   while_statement
        |   return_statement
        ;

.

+6

Source: https://habr.com/ru/post/1606758/


All Articles