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#bo...
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.
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#bo...
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. Thanks, Nate