actions.qbk

来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 104 行

QBK
104
字号
[/==============================================================================    Copyright (C) 2001-2008 Joel de Guzman    Copyright (C) 2001-2008 Hartmut Kaiser    Distributed under the Boost Software License, Version 1.0. (See accompanying    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)===============================================================================/][section Semantic Actions]Our parser above is really nothing but a recognizer. It answers the question"did the input match our grammar?", but it does not do anything other than that.It does not extract any information from what was parsed. For example, wheneverwe parse a real number, we wish to store the parsed number after a successfulmatch.Enter Semantic actions. Semantic actions may be attached to any point in thegrammar specification. These actions are C++ functions or function objects thatare called whenever a part of the parser successfully recognizes a portion ofthe input. Say you have a parser `P`, and a C++ function `F`, you can make theparser call `F` whenever it matches an input by attaching `F`:    P[F]The expression above links `F` to the parser, `P`.The function/function object signature depends on the type of the parser towhich it is attached. The parser `double_` passes the parsed number. Thus, if wewere to attach a function `F` to `double_`, we need `F` to be declared as:    void F(double n);There are actually 2 more arguments being passed (the parser context and areference to a boolean 'hit' parameter). We don't need these, for now, but we'llsee more on these other arguments later. Spirit.Qi allows us to bind a singleargument function, like above. The other arguments are simply ignored.Presented are various ways to attach semantic actions:* Using plain function pointer* Using simple function object* Using __boost_bind__ with a plain function* Using __boost_bind__ with a member function* Using __boost_lambda__[import ../../example/qi/actions.cpp]Given:[tutorial_semantic_action_functions]Take note that with function objects, we need to have an `operator()` with 3arguments. Since we don't care about the other two, we can use `unused_type` forthese. We'll see more of `unused_type` elsewhere. Get used to it. `unused_type`is a Spirit supplied support class.All examples parse inputs of the form:    "{integer}"An integer inside the curly braces.The first example shows how to attach a plain function:[tutorial_attach_actions1]What's new? Well `int_` is the sibbling of `double_`. I'm sure you can guesswhat this parser does.The next example shows how to attach a simple function object:[tutorial_attach_actions2]We can use __boost_bind__ to 'bind' member functions:[tutorial_attach_actions4]Likewise, we can also use __boost_bind__ to 'bind' plain functions:[tutorial_attach_actions3]Yep, we can also use __boost_lambda__:[tutorial_attach_actions5]There are more ways to bind semantic action functions, but the examples aboveare the most common. Attaching semantic actions is the first hurdle one hasto tackle when getting started with parsing with Spirit. Familiarize yourselfwith this task and get intimate with the tools behind it such as __boost_bind__and __boost_lambda__.The examples above can be found here: [@../../example/qi/actions.cpp][heading Phoenix]__phoenix__, a companion library bundled with Spirit, is specifically suitedfor binding semantic actions. It is like __boost_lambda__ in steroids, withspecial custom features that make it easy to integrate semantic actions withSpirit. If your requirements go beyond simple to moderate parsing, I suggest youuse this library. Examples presented henceforth shall be using the libraryexclusively[endsect]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?