[xpressive] questions about semantic actions
Hello, 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'? 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<>)? Thanks, Nate
On 8/4/2012 11:57 PM, Nathan Ridge wrote:
Hello,
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...
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... -- Eric Niebler BoostPro Computing http://www.boostpro.com
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
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#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.
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#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.
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
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<>)?
<snip>
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.
All of these require providing the regex_id *manually*. For example, with
placeholders, I can do this:
placeholder
On 8/5/2012 1:21 PM, Nathan Ridge wrote:
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<>)?
<snip>
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.
All of these require providing the regex_id *manually*.
<snip> Right.
What I would like to do is this:
sregex rx = xpr[cout << _regex_id_]; // _regex_id_ is a special symbol
smatch what; // no need to provide anything here! _regex_id_ will be replaced by the // regex_id of rx regex_match(str, what, rx);
The reason this is important is because the action may be attached to a subregex of the regex on which the match is being done. For example:
placeholder
_regex_id; sregex sub = xpr[cout << _regex_id]; sregex rx = sub >> another_xpr; smatch what; what.let(_regex_id = ???); // sub.regex_id() needs to go here, but this // code knows only about rx, not about sub! regex_match(str, what, rx);
Is there no way to accomplish this?
Not at present, no. I don't think it would be hard to add. Could you file a feature request? -- Eric Niebler BoostPro Computing http://www.boostpro.com
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<>)?
<snip>
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.
All of these require providing the regex_id *manually*. <snip>
Right.
What I would like to do is this:
sregex rx = xpr[cout << _regex_id_]; // _regex_id_ is a special symbol
smatch what; // no need to provide anything here! _regex_id_ will be replaced by the // regex_id of rx regex_match(str, what, rx);
The reason this is important is because the action may be attached to a subregex of the regex on which the match is being done. For example:
placeholder
_regex_id; sregex sub = xpr[cout << _regex_id]; sregex rx = sub >> another_xpr; smatch what; what.let(_regex_id = ???); // sub.regex_id() needs to go here, but this // code knows only about rx, not about sub! regex_match(str, what, rx);
Is there no way to accomplish this?
Not at present, no. I don't think it would be hard to add. Could you file a feature request?
I filed https://svn.boost.org/trac/boost/ticket/7197 . Thanks, Nate
participants (2)
-
Eric Niebler
-
Nathan Ridge