Re2c
re2c is a free and open-source lexer generator for C, C++ and Go. It compiles declarative regular expression specifications to deterministic finite automata. Originally written by Peter Bumbulis and described in his paper, re2c was put in public domain and has been since maintained by volunteers. It is the lexer generator adopted by projects such as PHP, SpamAssassin, Ninja build system and others. Together with the Lemon parser generator, re2c is used in BRL-CAD. This combination is also used with STEPcode, an implementation of ISO 10303 standard.
Philosophy
The main goal of re2c is generating fast lexers:at least as fast as reasonably optimized C lexers coded by hand.
Instead of using traditional table-driven approach, re2c
encodes the generated finite state machine directly in the form of conditional jumps and comparisons.
The resulting program is faster than its table-driven counterpart
and much easier to debug and understand.
Moreover, this approach often results in smaller lexers,
as re2c applies a number of optimizations such as DFA minimization and the construction of tunnel automaton.
Another distinctive feature of re2c is its flexible interface:
instead of assuming a fixed program template,
re2c lets the programmer write most of the interface code and adapt the generated lexer to any particular environment.
The main idea is that re2c should be a zero-cost abstraction for the programmer:
using it should never result in a slower program than the corresponding hand-coded implementation.
Features
- Submatch extraction: re2c supports both POSIX-compliant capturing groups and standalone tags . The implementation is based on the lookahead-TDFA algorithm.
- Encoding support: re2c supports ASCII, UTF-8, UTF-16, UTF-32, UCS-2 and EBCDIC.
- Flexible user interface: the generated code uses a few primitive operations in order to interface with the environment ; users can redefine these primitives to whatever they need.
- Storable state: re2c supports both pull-model lexers and push-model lexers.
- Start conditions: re2c can generate multiple interrelated lexers, where each lexer is triggered by a certain condition in program.
- Self-validation: re2c has a special mode in which it ignores all used-defined interface code and generates a self-contained skeleton program. Additionally, re2c generates two files: one with the input strings derived from the regular grammar, and one with compressed match results that are used to verify lexer behavior on all inputs. Input strings are generated so that they extensively cover DFA transitions and paths. Data generation happens right after DFA construction and prior to any optimizations, but the lexer itself is fully optimized, so skeleton programs are capable of revealing any errors in optimizations and code generation.
- Warnings: re2c performs static analysis of the program and warns its users about possible deficiencies or bugs, such as undefined control flow, unreachable code, ill-formed escape symbols and potential misuse of the interface primitives.
- Debugging. Besides generating human-readable lexers, re2c has a number of options that dump various intermediate representations of the generated lexer, such as NFA, multiple stages of DFA and the resulting program graph in DOT format.
Syntax
/*!re2c... */
blocks.Each block consists of a sequence of rules, definitions and configurations
.
Rules have the form
REGEXP
or REGEXP := CODE;
where REGEXP
is a regular expression and CODE
is a block of C code. When REGEXP
matches the input string, control flow is transferred to the associated CODE
. There is one special rule: the default rule with *
instead of REGEXP
; it is triggered if no other rules matches. re2c has greedy matching semantics: if multiple rules match, the rule that matches longer prefix is preferred; if the conflicting rules match the same prefix, the earlier rule has priority.Definitions have the form
NAME = REGEXP;
.Configurations have the form
re2c:CONFIG = VALUE;
where CONFIG
is the name of the particular configuration and VALUE
is a number or a string.For more advanced usage see the official re2c manual.
Regular expressions
re2c uses the following syntax for regular expressions:"foo"
case-sensitive string literal'foo'
case-insensitive string literal,
character class
.
any character except newlineR \ S
difference of character classesR*
zero or more occurrences ofR
R+
one or more occurrences ofR
R?
optionalR
R
repetition ofR
exactlyn
timesR
repetition ofR
at leastn
timesR
repetition ofR
fromn
tom
timesjust
R
; parentheses are used to override precedence or for POSIX-style submatchR S
concatenation:R
followed byS
R | S
alternative:R
orS
R / S
lookahead:R
followed byS
, butS
is not consumedname
the regular expression defined asname
@stag
an s-tag: saves the last input position at which@stag
matches in a variable namedstag
#mtag
an m-tag: saves all input positions at which#mtag
matches in a variable namedmtag
\a
, \b
, \f
, \n
, \r
, \t
, \v
, \\
, octal escapes \ooo
and hexadecimal escapes \xhh
, \uhhhh
and \Uhhhhhhhh
.Example
Here is a very simple program in re2c.It checks that all input arguments are hexadecimal numbers.
The code for re2c is enclosed in comments
/*!re2c... */
, all the rest is plain C code.See the official re2c website for more complex examples.
- include
int main
Given that,
re2c -is -o example.c example.re
generates the code below.The contents of the comment
/*!re2c... */
are substituted with a deterministic finite automatonencoded in the form of conditional jumps and comparisons; the rest of the program is copied verbatim into the output file.
There are several code generation options; normally re2c uses
switch
statements,but it can use nested
if
statements,or generate bitmaps and jump tables.
Which option is better depends on the C compiler;
re2c users are encouraged to experiment.
/* Generated by re2c 1.2.1 on Fri Aug 23 21:59:00 2019 */
- include
int main