Boost logo

Boost Users :

Subject: Re: [Boost-users] [xpressive] questions about semantic actions
From: Eric Niebler (eric_at_[hidden])
Date: 2012-08-05 13:01:56


On 8/5/2012 12:58 AM, Nathan Ridge wrote:
>>> I am trying to use semantic actions in Boost.Xpressive, and I have a
>>> couple of questions:
>>>
>>> 1. When a semantic action is attached to a subexpression of a regex, what
>>> is the meaning of _, s1, s2 etc. in the semantic action? For example,
>>> suppose you have the following regex:
>>> ((s1 = xpr1)[action]) >> (s2 = xpr2)
>>> Inside 'action':
>>> - is it valid to use '_'?
>>> - if so does it refer to just the portion matched by xpr1?
>>> - is it valid to use 's1'?
>>> - is it valid to use 's2'?
>>
>> Yes, this is all described in the docs.
>>
>> http://www.boost.org/doc/libs/1_50_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.semantic_actions_and_user_defined_assertions
>
> I think I see now. Please let me know if I understand correctly:
>
> '_' always refers to the subexpression to which the semantic action
> is attached (call this E)
>
> 's1', 's2' etc. refer to the matches in the enclosing sregex (which
> may be larger than E). Some of these matches may not yet have been
> filled in at the time the semantic action is executed, but otherwise
> they are all available to the semantic action, even if the capture
> took place outside E.

That's right. Some care is needed when accessing s1, s2, etc within an
action because they might refer to incomplete matches. Buyer beware.

>>> 2. Is there a way to access the regex_id of the regex to which a semantic
>>> action belongs from the semantic action? More generally, can a semantic
>>> action be written as a custom function object that takes as argument
>>> some sort of environment (perhaps a match_results<>)?
>>
>> Please see the sections in the docs on refering to local and non-local
>> variables from within semantic actions. Non-local variables will do what
>> you want, I think.
>>
>> http://www.boost.org/doc/libs/1_50_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables
>
> Apologies if my description wasn't very clear.
>
> In the examples in the documentation, all of the semantic actions take the
> form of expression templates. Presumably the object that gets built is
> a function object which takes as arguments some environment in the context
> of which it can interpret things like 's1' (I suspect this environment
> is a match_results<>, and perhaps a few other things). Algorithms like
> regex_match() then call the function object with the appropriate environment.
>
> I would like to write my own function object to be used as a semantic
> action, rather than be limited to the forms that the expression template
> allows. In particular, I am hoping to get access to the match_results<>
> object that gives meaning to 's1', 's2', etc. in the semantic action,
> for the purpose of calling regex_id() on it to recover the regex id
> of the regex to which the semantic action belongs.
>
> If this is not possible, but there is a more direct way of getting at
> that regex_id, that would be fine as well.

The action does not receive the match_results. The only way to get data
into and out of an action is the way described in the docs. You will
have to use xpressive::value, xpressive::reference or
xpressive::placeholder to hold either the regex_id, a reference to the
regex_id, or a promise to return a regex_id respectively. You can use
those within the action.

HTH,

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net