Is it possible to reuse a Spirit Qi grammar as a Spirit Karma grammar?

后端 未结 1 1969
不思量自难忘°
不思量自难忘° 2021-01-15 05:31

I have a Qi grammar definition that I use to parse an input. Later I have a Karma generator to output in a way that should be similar to the input.

Is this possible

1条回答
  •  执笔经年
    2021-01-15 06:30

    Sadly it's not possible to achieve what you want in a general way (or at least I don't know how), but if you are willing to just use a limited subset of Spirit.Qi the approach below could work.

    The first thing to know is that when you use something like:

    int_ >> double_
    

    You just have a Boost.Proto expression that describes several terminals and how they are related. That expression by itself doesn't "know" anything about how to parse one int and then one double. Whenever you use parse/phrase_parse or assign one of these Proto expressions to a rule Spirit "compiles" that expression for a domain (Qi or Karma) and creates the parsers/generators that do the actual work.

    Here you can see a small example that shows the exact types of the Proto and compiled Qi expressions:

    Raw proto type:
    boost::proto::exprns_::expr const&, boost::spirit::terminal const&>, 2l>
    
    "Pretty" proto type:
    shift_right(
        terminal(boost::spirit::tag::int_)
      , terminal(boost::spirit::tag::double_)
    )
    
    Compiled Qi type:
    boost::spirit::qi::sequence, boost::fusion::cons >, boost::fusion::nil_> > >
    

    As long as you have access to the original expression you can use Proto transforms/grammars to convert it to a suitable Karma expression.

    In the example below I have used the following transformations:

    Qi          |Karma          |Reason  
    ------------|---------------|------  
    lexeme[expr]|verbatim[expr] | lexeme does not exist in Karma
    omit[expr]  |no_delimit[eps]| omit consumes an attribute in Karma
    a >> b      |a << b         |
    a > b       |a << b         | < does not exist in Karma
    a - b       |a              | - does not exist in Karma
    

    In order to achieve this transformations you can use boost::proto::or_ getting something similar to:

    struct Grammar : proto::or_<
                         proto::when,
                         proto::when,
                         Matcher3,
                         Matcher4
    >{};
    

    I'll try to explain how this works.
    MatcherN in the example below can be:

    • proto::terminal: matches only that specific terminal.
    • proto::terminal: matches any terminal not specifically matched before.
    • proto::subscript,proto::_>: matches omit[expr] where expr can be anything.
    • proto::shift_right: matches expr1 >> expr2 where expr1 and expr2 must recursively conform to the ToKarma grammar.
    • proto::nary_expr >: matches any n-ary (unary, binary or actually n-ary like a function call a(b,c,d,e)) where each one of the elements of the expression conforms to the ToKarma grammar.

    All the TransformN in this example are expression builders, here are some explanations:

    • _make_terminal(boost::spirit::tag::lexeme()): builds a proto::terminal (note that it is necessary to add () after the tag, you'll get an awful error if you forget them).
    • _make_subscript(_make_terminal(tag::no_delimit()), _make_terminal(tag::eps())): builds a proto::subscript, proto::terminal >, or the equivalent to no_delimit[eps].
    • _make_shift_left(ToKarma(proto::_left), ToKarma(proto::_right)): proto::_left means take the lhs of the original expression. ToKarma(proto::_left) means recursively apply the ToKarma grammar/transform to the lhs of the original expression. The whole _make_shift_left basically builds transformed_lhs << transformed_rhs.

    A MatcherN by itself (not inside proto::when) is a shorthand for build an expression of the same type using as elements the result of recursively applying the transform to the original elements.


    Full Sample (Running on WandBox)

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    namespace proto= boost::proto;
    
    
    struct ToKarma: proto::or_<
        //translation of directives
        proto::when, proto::_make_terminal(boost::spirit::tag::verbatim())>, //lexeme -> verbatim
        proto::when<
            proto::subscript,proto::_>, //omit[expr] -> no_delimit[eps]
            proto::_make_subscript(proto::_make_terminal(boost::spirit::tag::no_delimit()),proto::_make_terminal(boost::spirit::tag::eps()))
        >,
    
        proto::terminal, //if the expression is any other terminal leave it as is
    
        //translation of operators
        proto::when, proto::_make_shift_left(ToKarma(proto::_left),ToKarma(proto::_right)) >, //changes '>>' into '<<'
        proto::when, proto::_make_shift_left(ToKarma(proto::_left),ToKarma(proto::_right)) >, //changes '>' into '<<'
        proto::when, ToKarma(proto::_left)>, //changes 'expr-whatever' into 'expr'
    
        proto::nary_expr > //if it's anything else leave it unchanged and recurse into the expression tree
    >{};
    
    
    template 
    void test(const std::string& input, const Parser& parser)
    {
        std::cout << "Original: \"" << input << "\"\n";
    
        std::tuple attr;
    
        std::string::const_iterator iter = input.begin(), end = input.end();
    
        bool result = boost::spirit::qi::phrase_parse(iter,end,parser,boost::spirit::qi::space,attr);
    
        if(result && iter==end)
        {
            ToKarma to_karma;
            std::cout << "Generated: \"" << boost::spirit::karma::format_delimited(to_karma(parser), boost::spirit::karma::space, attr) << '"' << std::endl;
        }
        else
        {
            std::cout << "Parsing failed. Unparsed: ->" << std::string(iter,end) << "<-" << std::endl;
        }
    }
    
    
    
    int main(){
        using namespace boost::spirit::qi;
    
        test("Xx     1.233    pseudo", lexeme[+(char_-' '-'\n')] >> double_ >> lexeme[+(char_-' '-'\n')]);
        test("foo 1 2.5", omit[lexeme[+alpha]] > int_ > double_);
    }
    

    PS:
    Things that definitely won't work:

    • qi::rule
    • qi::grammar
    • qi::symbols

    Things that don't exist in Karma:

    • qi::attr
    • qi::matches
    • qi::hold
    • Permutation parser ^
    • Sequential Or parser ||

    Things that have different semantics in Karma:

    • qi::skip
    • And-predicate parser &
    • Not-predicate parser !

    0 讨论(0)
提交回复
热议问题