File: action.qbk

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (100 lines) | stat: -rw-r--r-- 3,893 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
[/==============================================================================
    Copyright (C) 2001-2011 Joel de Guzman
    Copyright (C) 2001-2011 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:action Semantic Actions with Parsers]

[heading Description]

Semantic actions may be attached to any point in the grammar specification. 
They allow to call a function or function object in order to provide the value 
to be output by the parser attached to the semantic action.  Semantic 
actions are associated with a parser using the syntax `p[]`, where `p` is an
arbitrary parser expression.

[heading Header]

    // forwards to <boost/spirit/home/qi/action.hpp>
    #include <boost/spirit/include/qi_action.hpp>

Also, see __include_structure__.

[heading Model of]

[:__unary_parser_concept__]

[variablelist Notation
    [[`a`, `p`][Instances of a parser, `P`]]
    [[`A`]     [Attribute type exposed by a parser, `a`]]
    [[`fa`]    [A (semantic action) function with signature `void(Attrib&, Context, bool&)`.
               The third parameter is a boolean flag that can be set to false to
               force the parser to fail. Both `Context` and the boolean flag are
               optional. For more information see below.]]
    [[`Attrib`][The attribute obtained from the parse.]]
    [[`Context`]  [The type of the parser execution context. For more 
                information see below.]]
]

[heading Expression Semantics]

Semantics of an expression is defined only where it differs from, or is not
defined in __unary_parser_concept__.

[table
    [[Expression]       [Semantics]]
    [[`p[fa]`]          [If `p` is successful, call semantic action, `fa`. The function
                         or function object `fa` is provided the attribute value
                         parsed by the parser `p`, plus some more context information
                         and a mutable bool flag which can be used to fail parsing.]]
]

The possible signatures for functions to be used as semantic actions are:

    template <typename Attrib>
    void fa(Attrib& attr);

    template <typename Attrib, typename Context>
    void fa(Attrib& attr, Context& context);

    template <typename Attrib, typename Context>
    void fa(Attrib& attr, Context& context, bool& pass);

The function or function object is expected to return the value to generate 
output from by assigning it to the first parameter, `attr`. Here `Attrib` is 
the attribute type of the parser attached to the semantic action. 

The type `Context` is the type of the parser execution context. This type is 
unspecified and depends on the context the parser is invoked in. The value, 
`context` used by semantic actions written using __phoenix__ to access various 
context dependent attributes and values. For more information about __phoenix__ 
placeholder expressions usable in semantic actions see __qi_nonterminal__.

The third parameter, `pass`, can be used by the semantic action to force the 
associated parser to fail. If pass is set to `false` the action parser 
will immediately return `false` as well, while not invoking `p` and not 
generating any output.

[heading Attributes]

[table
    [[Expression]       [Attribute]]
    [[`a[fa]`]          [`a: A --> a[fa]: A`]]
]

[heading Complexity]

The complexity of the action parser is defined by the complexity of the 
parser the semantic action is attached to and the complexity of the function
or function object used as the semantic action.

[heading Example]

Examples for semantic actions can be found here: 
[link spirit.qi.tutorials.semantic_actions.examples_of_semantic_actions Examples of Semantic Actions].

[endsect] [/ Action]