CTTL Home http://cttl.sourceforge.net/
"There is no certain date of victory, only a final state to which we must strive."
- Dedicated Open Source developer
cttl on
sourceforge |
download
latest |
cttl
docs index api |
lambda
docs index api |
utils
api |
sponsor
c-jump.com |
CTTL is a library of C++ classes and functions to parse and modify STL strings. The library is designed to manipulate std::basic_string template class, or a user-defined class with similar interface.
The library provides components for creating lexical analyzers making practical use of EBNF grammars. Template meta-programming and operator overloading offer features to write expressions that describe EBNF grammar rules directly in C++. No additional steps of parsing, compiling, or source code preprocessing are required. Compiled CTTL program implements LL(INF)-parser, the recursive-descent parser with infinite lookahead.
CTTL lexer generates a stream of substrings that correspond to parsed tokens. Substrings can be compared, inserted, deleted, or replaced without restrictions. CTTL substrings remain stable when content of the underlying string changes. Substring boundaries are adjusted according to the positional shifts of inserted or deleted text.
The library formalizes concept of text transformation as a series of mutating operations on substrings, executed in any order. More specifically, text transformation becomes first-hand operation during grammar evaluation. Since EBNF grammar productions are recursive by definition, text-manipulating programs can also be written as recursive algorithms.
Copyright © 1997-2009 by Igor Kholodov mailto:cttl@users.sourceforge.net
Common Text Transformation Library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Common Text Transformation Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
CTTL was not conceived as a regex replacement; CTTL was primarily designed as an extension of STL string. Since input pattern matching is essential for a text-processing library, adoption of an existing regular expression engine (or a proprietary regex implementation) was considered to be a viable option. At the same time, since hosting a regex interpreter poses a runtime performance setback, the decision was made in favor of a different solution.
CTTL approaches pattern-matching at a procedural level, adopting EBNF metasyntax notation to describe formal grammar productions. The following table compares regex and CTTL within a domain of problems solvable with std::string:
Regex | CTTL |
---|---|
Cryptic, unintuitive, "easier to write than read" syntax. | Descriptive, human-readable format. Just another C++ program. |
Interpreted at runtime. But how well is the expression optimized beyond rudimentary parsing? | Compile-time, native C++. Subject to optimization by the compiler. |
Multiple dialects. | Portable C++. |
Quickly grows in size. Becomes unreadable. | Easily divided into separate grammar rules. |
Sub-expression (backreference) syntax is obscure: "To figure out the number of a particular backreference, scan the regular expression from left to right and count the opening round brackets..." (from www.regular-expressions.info tutorial.) | EBNF grammars allow dividing complex expressions into a set of C++ functions that represent individual grammar productions. |
Method of reporting results of matching sub-expressions is using an old style C-array. Array elements are constant objects. | Substrings represent parsed tokens. Substrings are first-hand C++ objects, created by the user. Substrings can be instantly replaced, deleted, or inserted, adding more power to std::string class. |
Debugging methods are largely dependent on third-party tools. | Relatively easy to trace and debug with conventional C++ debugger. |
Mapping between regex metacharacters and CTTL lexer components is available here. However, the most significant difference between regex and CTTL is in the way of thinking: regex is a terse cipher of special characters, substituted according to a predetermined set of rules. By contrast, CTTL grammars are wordier, more methodical, and recursive by nature. CTTL improves the quality of pattern-matching code without sacrificing performance.
Every effort was made to keep CTTL architecture portable and thread safe.
CTTL has been fully tested with the following compilers:
GNU GCC Versions 3.2.x, 3.3.x, 3.4.x, 4.1.2 (Red Hat) 4.3.x (MinGW) MSVC 8.0 Version 14.00.50727.762 (2005) MSVC 7.1 Version 13.10.3077 (2003) Comeau C/C++ Version 4.3.3
The library documentation is organized into subordinate parts as follows:
CTTL manual explains principals of grammar parsing and functionality of substrings.
Lambda library documentation covers data manipulation by in-line semantic actions.
The API documentation includes doxygen −generated pages for
For additional resources see grammar reference page.
CTTL distribution includes only C++ headers and sample source files. The library is header-only: the header files contain template classes and functions, which do not require separately-compiled binaries. No installation or other special treatment is needed. Download and enjoy!
The sample programs in example/ subdirectory expect that library headers are available in relative subdirectories cttl/, lambda/, and utils/:
#include "cttl/cttl.h" #include "lambda/lambda.h" #include "utils/itos.h" // etc.
You may need to adjust the include paths in sample programs according to your local directory structure.
The library does not require any specific compiler switches or settings; with GNU compiler, your should be able to compile and link your CTTL programs by commands like
me@CTTL3 ~/cttl300 $ g++ -g -Wall -c -o main.o main.cpp ... me@CTTL3 ~/cttl300 $ g++ -o sample.exe main.o ... me@CTTL3 ~/cttl300 $
CTTL manual provides rational for various sample programs in example/ subdirectory of CTTL distribution. To find documentation for a particular program, locate its source file name in the index and follow the link:
If you would like to financially support CTTL development team, buy a copy of c-jump computer programming board game at http://www.c-jump.com/. The game introduces programming concepts to children and adults with no prior knowledge of the subject. Thank you for your support!
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.