<<< R1-R2, binary set complement | Table Of Contents | R1|R2, binary set union operator >>> |
Common Text Transformation Library http://cttl.sourceforge.net/
Category:
Format:
R1 & R2
where operands R1 and R2 are valid CTTL grammar expressions, representing two arbitrary sets of string matches.
Algorithm:
Overloaded binary bitwise AND operator specifies matches of R1 that are also matches of R2.
Intersection construct (R1&R2) has left-to-right associativity: the expression R1 is evaluated first, and then R2 is evaluated. If evaluation of R1 fails, the expression R2 is never evaluated, realizing a short-circuit type of evaluation.
Parseable substring, presented to the R2 operand for evaluation, is restricted by the boundaries of the token that matched R1.
The evaluation of R2 operand does not consume any input characters. The implementation
Result of (R1&R2) fits the description of the set intersection from the set theory:
Usage notes:
Space sensitivity:
The space sensitivity of R1 operand is enabled.
The R2 operand is evaluated in strict grammar evaluation mode. Space sensitivity of R2 is controlled by the space policy object, constructed along with the temporary copy of the parseable substring. The temporary copy of substring is created prior to R2 evaluation as an instance of strict_edge_T, which is a member typedef of CTTL substring classes.
Searchability:
Search grammar evaluation algorithms
are enabled for (R1&R2) construct. The terminal search is transitive: the expressions
!(R1 & R2) (!R1) & R2
are equivalent.
The following sample demonstrates a non-terminal search using the intersection operator:
#define CTTL_TRACE_EVERYTHING
#include "cttl/cttl.h"
using namespace cttl;
int main()
{
std::string inp = "++x1y2z3 ++X1Y2Z3";
const_edge<> substring( inp );
const_edge<> token = substring;
size_t result = token(
entity( isalnum ) & first( isupper )
).bang_find( substring );
assert( result != std::string::npos );
assert( token == "X1Y2Z3" );
return 0;
}
Example:
The following example uses expression
entity( isalnum ) & first( isupper )
to match an alphanumeric multi-character entity and assert that its first character is alphabetic and uppercase:
#define CTTL_TRACE_EVERYTHING #include "cttl/cttl.h" using namespace cttl; int main() { std::string inp = "X1Y2Z3"; const_edge<> substring( inp ); const_edge<> token = substring; size_t result = token( entity( isalnum ) & first( isupper ) ).match( substring ); assert( result != std::string::npos ); assert( token == inp ); return 0; }
Trace output format:
The trace symbol of (R1&R2) construct is the ampersand sign '&', enclosed in a pair of symmetrical braces. The above example generates the following trace:
------------------@X1Y2Z3?{e 0-6 :3:1 ------------------@X1Y2Z3? {& 0-6 ------------------X1Y2Z3@| $ 6-6 isalnum -----------------------X@| F 1-6 isupper } ------------------@X1Y2Z3| e 0-6 }
Copyright © 1997-2009 Igor Kholodov mailto:cttl@users.sourceforge.net.
Permission to copy and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
<<< R1-R2, binary set complement | Table Of Contents | R1|R2, binary set union operator >>> |