RE2C(1)

NAME
re2c - convert regular expressions to C/C++ code

SYNOPSIS

re2c [OPTIONS] FILE

DESCRIPTION

re2c is a lexer generator for C/C++. It finds regular expression specifications inside of C/C++ comments and replaces them with a hard-coded DFA. The user must supply some interface code in order to control and customize the generated DFA.

OPTIONS

-? -h --help
    Show help message.

-b --bit-vectors
    Optimize conditional jumps using bit masks. Implies -s.

-c --conditions --start-conditions
    Enable support of Flex-like "conditions": multiple interrelated lexers within one block. Option --start-conditions is a legacy alias; use --conditions instead.

-d --debug-output
    Emit YYDEBUG in the generated code. YYDEBUG should be defined by the user in the form of a void function with two parameters: state (lexer state or -1) and symbol (current input symbol of type YYCTYPE).

-D --emit-dot
    Instead of normal output generate lexer graph in DOT format. The output can be converted to PNG with the help of Graphviz (something like dot -Tpng -odfa.png dfa.dot). Note that large graphs may crash Graphviz.

-e --ecb
    Generate a lexer that reads input in EBCDIC encoding. re2c assumes that character range is 0 -- 0xFF an character size is 1 byte.

-f --storable-state
    Generate a lexer which can store its inner state. This is useful in push-model lexers which are stopped by an outer program when there is not enough input, and then resumed when more input becomes available. In this mode users should additionally define YYGETSTATE () and YYSETSTATE (state) macros and variables yych, yyaccept and the state as part of the lexer state.

-F --flex-syntax
    Partial support for Flex syntax: in this mode named definitions don't need the equal sign and the terminating semicolon, and when used they must be surrounded by curly braces. Names without curly braces are treated as double-quoted strings.

-g --computed-gotos
    Optimize conditional jumps using non-standard "computed goto" extension (must be supported by C/C++ compiler). re2c generates jump tables only in complex cases with a lot of conditional branches. Complexity threshold can be configured with cgoto:threshold configuration. This option implies -b.

-i --no-debug-info
    Do not output #line information. This is useful when the generated code is tracked by some version control system.

-o OUTPUT --output=OUTPUT
    Specify the OUTPUT file.

-r --reusable
    Allows reuse of re2c rules with /*!rules:re2c */ and /*!use:re2c */ blocks. In this mode simple /*!re2c */ blocks are not allowed and exactly one /*!rules:re2c */ block must be present. The rules are saved and used by every /*!use:re2c */ block that follows (which may add rules of their own). This option allows to reuse the same set of rules with different configurations.

-s --nested-ifs
    Use nested if statements instead of switch statements in conditional jumps. This usually results in more efficient code with non-optimizing C/C++ compilers.

-t HEADER --type-header=HEADER
    Generate a HEADER file that contains enum with condition names. Requires -c option.

-T --tags
    Enable submatch extraction with tags.

-P --posix-captures
    Enable submatch extraction with POSIX-style capturing groups.

-u --unicode
    Generate a lexer that reads input in UTF-32 encoding. re2c assumes that character range is 0 -- 0x10FFFF and character size is 4 bytes. Implies -s.

-v --version
    Show version information.

-V --vernum
    Show version information in MMmmpp format (major, minor, patch).

-w --wide-chars
    Generate a lexer that reads input in UCS-2 encoding. re2c assumes that character range is 0 -- 0xFFFF and character size is 2 bytes. Implies -s.

-x --utf-16
    Generate a lexer that reads input in UTF-16 encoding. re2c assumes that character range is 0 -- 0x10FFFF and character size is 2 bytes. Implies -s.

-8 --utf-8
    Generate a lexer that reads input in UTF-8 encoding. re2c assumes that character range is 0 -- 0x10FFFF and character size is 1 byte.

--case-insensitive
    Treat single-quoted and double-quoted strings as case-insensitive.

--case-inverted
    Invert the meaning of single-quoted and double-quoted strings: treat single-quoted strings as case-sensitive and double-quoted strings as case-insensitive.

--no-generation-date
    Suppress date output in the generated file.

--no-lookahead
    Use TDFA(0) instead of TDFA(1). This option only has effect with --tags or --posix-captures options.

--no-optimize-tags
    Suppress optimization of tag variables (useful for debugging or benchmarking).

--no-version
    Suppress version output in the generated file.

--encoding-policy POLICY
    Define the way re2c treats Unicode surrogates. POLICY can be one of the following: fail (abort with an error when a surrogate is encountered), substitute (silently replace surrogates with the error code point 0xFFFD), ignore (default, treat surrogates as normal code points). The Unicode standard says that standalone surrogates are invalid, but real-world libraries and programs behave in different ways.

--input INPUT
    Specify re2c input API. INPUT can be either default or custom (enables the use of generic API).

-S --skeleton
    Ignore user-defined interface code and generate a self-contained "skeleton" program. Additionally, generate input files with strings derived from the regular grammar and compressed match results that are used to verify "skeleton" behavior on all inputs. This option is useful for finding bugs in optimizations and code generation.

--empty-class POLICY
    Define the way re2c treats empty character classes. POLICY can be one of the following: match-empty (match empty input: illogical, but default behavior for backwards compatibility reasons), match-none (fail to match on any input), error (compilation error).

--dfa-minimization ALGORITHM
    The internal algorithm used by re2c to minimize the DFA. ALGORITHM can be either moore (Moore algorithm, the default) or table (table filling algorithm). Both algorithms should produce the same DFA up to states relabeling; table filling is much slower and serves as a reference implementation.

--eager-skip
    Make the generated lexer advance the input position "eagerly": immediately after reading input symbol. By default this happens after transition to the next state. Implied by --no-lookahead.

--dump-nfa
    Generate representation of NFA in DOT format and dump it on stderr.

--dump-dfa-raw
    Generate representation of DFA in DOT format under construction and dump it on stderr.

--dump-dfa-det
    Generate representation of DFA in DOT format immediately after determinization and dump it on stderr.

--dump-dfa-tagopt
    Generate representation of DFA in DOT format after tag optimizations and dump it on stderr.

--dump-dfa-min
    Generate representation of DFA in DOT format after minimization and dump it on stderr.

--dump-adfa
    Generate representation of DFA in DOT format after tunneling and dump it on stderr.

-1 --single-pass
    Deprecated. Does nothing (single pass is the default now).

-W  Turn on all warnings.

-Werror
    Turn warnings into errors. Note that this option alone doesn't turn on any warnings; it only affects those warnings that have been turned on so far or will be turned on later.

-W<warning>
    Turn on warning.

-Wno-<warning>
    Turn off warning.

-Werror-<warning>
    Turn on warning and treat it as an error (this implies -W<warning>).

-Wno-error-<warning>
    Don't treat this particular warning as an error. This doesn't turn off the warning itself.

-Wcondition-order
    Warn if the generated program makes implicit assumptions about condition numbering. One should use either the -t, --type-header option or the /*!types:re2c*/ directive to generate a mapping of condition names to numbers and then use the autogenerated condition names.

-Wempty-character-class
    Warn if a regular expression contains an empty character class. Trying to match an empty character class makes no sense: it should always fail. However, for backwards compatibility reasons re2c allows empty character classes and treats them as empty strings. Use the --empty-class option to change the default behavior.

-Wmatch-empty-string
    Warn if a rule is nullable (matches an empty string). If the lexer runs in a loop and the empty match is unintentional, the lexer may unexpectedly hang in an infinite loop.

-Wswapped-range
    Warn if the lower bound of a range is greater than its upper bound. The default behavior is to silently swap the range bounds.

-Wundefined-control-flow
    Warn if some input strings cause undefined control flow in the lexer (the faulty patterns are reported). This is the most dangerous and most common mistake. It can be easily fixed by adding the default rule * which has the lowest priority, matches any code unit, and consumes exactly one code unit.

-Wunreachable-rules
    Warn about rules that are shadowed by other rules and will never match.

-Wuseless-escape
    Warn if a symbol is escaped when it shouldn't be. By default, re2c silently ignores such escapes, but this may as well indicate a typo or an error in the escape sequence.

-Wnondeterministic-tags
    Warn if a tag has n-th degree of nondeterminism, where n is greater than 1.

INTERFACE CODE

Below is the list of all symbols which may be used by the lexer in order to interact with the outer world. These symbols should be defined by the user, either in the form of inplace configurations, or as C/C++ variables, functions, macros and other language constructs. Which primitives are necessary depends on the particular use case.

yyaccept
    L-value of unsigned integral type that is used to hold the number of the last matched rule. Explicit definition by the user is necessary only with -f --storable-state option.

YYBACKUP ()
    Backup current input position (used only with --input custom option).

YYBACKUPCTX ()
    Backup current input position for trailing context (used only with --input custom option).

yych
    L-value of type YYCTYPE that is used to hold current input character. Explicit definition by the user is necessary only with -f --storable-state option.

YYCONDTYPE
    The type of condition identifiers (used only with -c --conditions option). Should be generated either with /*!types:re2c*/ directive, or with -t --type-header option.

YYCTXMARKER
    L-value of type YYCTYPE * that is used to backup input position of trailing context. It is needed only if regular expressions use the lookahead operator /.

YYCTYPE
    The type of the input characters (code units). Usually it should be unsigned char for ASCII, EBCDIC and UTF-8 encodings, unsigned short for UTF-16 or UCS-2 encodings, and unsigned int for UTF-32 encoding.

YYCURSOR
    L-value of type YYCTYPE * that is used as a pointer to the current input symbol. Initially YYCURSOR points to the first character and is advanced by the lexer during matching. When a rule matches, YYCURSOR points past the last character of the matched string.

YYDEBUG (state, symbol)
    A function-like primitive that is used to dump debug information (only used with -d --debug-output option). YYDEBUG should return no value and accept two arguments: state (either lexer state or -1) and symbol (current input symbol).

YYFILL (n)
    A function-like primitive that is called by the lexer when there is not enough input. YYFILL should return no value and supply at least n additional characters. Maximal value of n equals YYMAXFILL, which can be obtained with the /*!max:re2c*/ directive.

YYGETCONDITION ()
    R-value of type YYCONDTYPE that represents current condition identifier (used only with -c --conditions option).

YYGETSTATE ()
    R-value of signed integral type that represents current lexer state (used only with -f --storable-state option). Initial value of lexer state should be -1.

YYLESSTHAN (n)
    R-value of boolean type that is true if and only if there is less than n input characters left (used only with --input custom option).

YYLIMIT
    R-value of type YYCTYPE * that marks the end of input (YYLIMIT[-1] should be the last input character). Lexer compares YYCURSOR and YYLIMIT in order to determine if there is enough input characters left.

YYMARKER
    L-value of type YYCTYPE * used to backup input position of successful match. This might be necessary if there is an overlapping longer rule that might also match.

YYMTAGP (t)
    Append current input position to the history of m-tag t (used only with -T --tags option).

YYMTAGN (t)
    Append default value to the history of m-tag t (used only with -T --tags option).

YYMAXFILL
    Integral constant that denotes maximal value of YYFILL argument and is autogenerated by /*!max:re2c*/ directive.

YYMAXNMATCH
    Integral constant that denotes maximal number of capturing groups in a rule and is autogenerated by /*!maxnmatch:re2c*/ directive (used only with --posix-captures option).

yynmatch
    L-value of unsigned integral type that is used to hold the number of capturing groups in the matching rule. Used only with -P --posix-captures option.

YYPEEK ()
    R-value of type YYCTYPE that denotes current input character (used only with --input custom option).

yypmatch
    An array of l-values that are used to hold the values of s-tags corresponding to the capturing parentheses in the matching rule. The length of array must be at least yynmatch * 2 (ideally YYMAXNMATCH * 2). Used only with -P --posix-captures option.

YYRESTORE ()
    Restore input position (used only with --input custom option).

YYRESTORECTX ()
    Restore input position from the value of trailing context (used only with --input custom option).

YYRESTORETAG (t)
    Restore input position from the value of s-tag t (used only with --input custom option).

YYSETCONDITION (condition)
    Set current condition identifier to condition (used only with -c --conditions option).

YYSETSTATE (state)
    Set current lexer state to state (used only with -f --storable-state option). Parameter state is of signed integral type.

YYSKIP ()
    Advance input position to the next character (used only with generic API).

YYSTAGP (t)
    Save current input position to s-tag t (used only with -T --tags and --input custom option).

YYSTAGN (t)
    Save default value to s-tag t (used only with -T --tags and --input custom options).

SYNTAX

A program can contain any number of re2c blocks. Each block consists of a sequence of RULES, NAMED DEFINITIONS and INPLACE CONFIGURATIONS.

  RULES

Rules consist of a regular expression followed by a user-defined action: a block of C/C++ code that is executed in case of sucessful match. Action can be either an arbitrary block of code enclosed in curly braces { and } or a block of code without curly braces preceded with := and ended with a newline that is not followed by a whitespace.

If multiple rules match, re2c prefers the longest match. If rules match the same string, the earlier rule has priority.

There is one special kind of rule: the default rule with * instead of the regular expression. It always has the lowest priority, matches any code unit (either valid or invalid) and consumes exactly one code unit. Note that default rule is not the same as [^], which matches any valid code point and can consume multiple code units. In case of variable-length encodings * is the only possible way to match invalid input character.

If -c --conditions option is used, then rules have more complex form described in the section about conditions.

  NAMED DEFINITIONS

Named definitions are of the form name = regexp ; where name is an identifier that consists of letters, digits and underscores, and regexp is a regular expression. With -F --flex-syntax option named definitions are also of the form name regexp. Each name should be defined before it is used.

  INPLACE CONFIGURATIONS

re2c:cgoto:threshold = 9;
    With -g --computed-gotos option this value specifies the complexity threshold that triggers the generation of jump tables rather than nested if statements and bit masks.

re2c:cond:divider = '/* *********************************** */';
    Allows to customize the divider for condition blocks. One can use @@ to insert condition name.

re2c:cond:divider@cond = @@;
    Specifies the placeholder that will be replaced with condition name in re2c:cond:divider.

re2c:condenumprefix = yyc;
    Specifies the prefix used for condition identifiers.

re2c:cond:goto@cond = @@;
    Specifies the placeholder that will be replaced with condition label in re2c:cond:goto.

re2c:cond:goto = 'goto @@;';
    Allows to customize goto statements used with :=> style rules. One can use @@ to insert the condition name.

re2c:condprefix = yyc;
    Specifies the prefix used for condition labels.

re2c:define:YYBACKUPCTX = 'YYBACKUPCTX';
    Replaces YYBACKUPCTX identifier with the specified string.

re2c:define:YYBACKUP = 'YYBACKUP';
    Replaces YYBACKUP identifier with the specified string.

re2c:define:YYCONDTYPE = 'YYCONDTYPE';
    Enumeration type used for condition identifiers.

re2c:define:YYCTXMARKER = 'YYCTXMARKER';
    Replaces the YYCTXMARKER placeholder with the specified identifier.

re2c:define:YYCTYPE = 'YYCTYPE';
    Replaces the YYCTYPE placeholder with the specified type.

re2c:define:YYCURSOR = 'YYCURSOR';
    Replaces the YYCURSOR placeholder with the specified identifier.

re2c:define:YYDEBUG = 'YYDEBUG';
    Replaces the YYDEBUG placeholder with the specified identifier.

re2c:define:YYFILL@len = '@@';
    Any occurrence of this text inside of a YYFILL will be replaced with the actual argument.

re2c:define:YYFILL:naked = 0;
    Controls the argument in the parentheses after YYFILL and the following semicolon. If zero, both the argument and the semicolon are omitted. If non-zero, the argument is generated unless re2c:yyfill:parameter is set to zero; the semicolon is generated unconditionally.

re2c:define:YYFILL = 'YYFILL';
    Define a substitution for YYFILL. By default re2c generates an argument in parentheses and a semicolon after YYFILL. If you need to make YYFILL an arbitrary statement rather than a call, set re2c:define:YYFILL:naked to a non-zero value.

re2c:define:YYGETCONDITION:naked = 0;
    Controls the parentheses after YYGETCONDITION. If zero, the parentheses are omitted. If non-zero, the parentheses are generated.

re2c:define:YYGETCONDITION = 'YYGETCONDITION';
    Substitution for YYGETCONDITION. By default re2c generates parentheses after YYGETCONDITION. Set re2c:define:YYGETCONDITION:naked to non-zero in order to omit the parentheses.

re2c:define:YYGETSTATE:naked = 0;
    Controls the parentheses that follow YYGETSTATE. If zero, the parentheses are omitted. If non-zero, they are generated.

re2c:define:YYGETSTATE = 'YYGETSTATE';
    Substitution for YYGETSTATE. By default re2c generates parentheses after YYGETSTATE. Set re2c:define:YYGETSTATE:naked to non-zero to omit the parentheses.

re2c:define:YYLESSTHAN = 'YYLESSTHAN';
    Replaces YYLESSTHAN identifier with the specified string.

re2c:define:YYLIMIT = 'YYLIMIT';
    Replaces the YYLIMIT placeholder with the specified identifier.

re2c:define:YYMARKER = 'YYMARKER';
    Replaces the YYMARKER placeholder with the specified identifier.

re2c:define:YYMTAGN = 'YYMTAGN';
    Replaces YYMTAGN identifier with the specified string.

re2c:define:YYMTAGP = 'YYMTAGP';
    Replaces YYMTAGP identifier with the specified string.

re2c:define:YYPEEK = 'YYPEEK';
    Replaces YYPEEK identifier with the specified string.

re2c:define:YYRESTORECTX = 'YYRESTORECTX';
    Replaces YYRESTORECTX identifier with the specified string.

re2c:define:YYRESTORE = 'YYRESTORE';
    Replaces YYRESTORE identifier with the specified string.

re2c:define:YYRESTORETAG = 'YYRESTORETAG';
    Replaces YYRESTORETAG identifier with the specified string.

re2c:define:YYSETCONDITION@cond = '@@';
    Any occurrence of this text inside of YYSETCONDITION will be replaced with the actual argument.

re2c:define:YYSETCONDITION:naked = 0;
    Controls the argument in parentheses and the semicolon after YYSETCONDITION. If zero, both the argument and the semicolon are omitted. If non-zero, both the argument and the semicolon are generated.

re2c:define:YYSETCONDITION = 'YYSETCONDITION';
    Substitution for YYSETCONDITION. By default re2c generates an argument in parentheses followed by semicolon after YYSETCONDITION. If you need to make YYSETCONDITION an arbitrary statement rather than a call, set re2c:define:YYSETCONDITION:naked to non-zero.

re2c:define:YYSETSTATE:naked = 0;
    Controls the argument in parentheses and the semicolon after YYSETSTATE. If zero, both argument and the semicolon are omitted. If non-zero, both the argument and the semicolon are generated.

re2c:define:YYSETSTATE@state = '@@';
    Any occurrence of this text inside of YYSETSTATE will be replaced with the actual argument.

re2c:define:YYSETSTATE = 'YYSETSTATE';
    Substitution for YYSETSTATE. By default re2c generates an argument in parentheses followed by a semicolon after YYSETSTATE. If you need to make YYSETSTATE an arbitrary statement rather than a call, set re2c:define:YYSETSTATE:naked to non-zero.

re2c:define:YYSKIP = 'YYSKIP';
    Replaces YYSKIP identifier with the specified string.

re2c:define:YYSTAGN = 'YYSTAGN';
    Replaces YYSTAGN identifier with the specified string.

re2c:define:YYSTAGP = 'YYSTAGP';
    Replaces YYSTAGP identifier with the specified string.

re2c:flags:8 or re2c:flags:utf-8
    Same as -8 --utf-8 command-line option.

re2c:flags:b or re2c:flags:bit-vectors
    Same as -b --bit-vectors command-line option.

re2c:flags:case-insensitive = 0;
    Same as --case-insensitive command-line option.

re2c:flags:case-inverted = 0;
    Same as --case-inverted command-line option.

re2c:flags:d or re2c:flags:debug-output
    Same as -d --debug-output command-line option.

re2c:flags:dfa-minimization = 'moore';
    Same as --dfa-minimization command-line option.

re2c:flags:eager-skip = 0;
    Same as --eager-skip command-line option.

re2c:flags:e or re2c:flags:ecb
    Same as -e --ecb command-line option.

re2c:flags:empty-class = 'match-empty';
    Same as --empty-class command-line option.

re2c:flags:encoding-policy = 'ignore';
    Same as --encoding-policy command-line option.

re2c:flags:g or re2c:flags:computed-gotos
    Same as -g --computed-gotos command-line option.

re2c:flags:i or re2c:flags:no-debug-info
    Same as -i --no-debug-info command-line option.

re2c:flags:input = 'default';
    Same as --input command-line option.

re2c:flags:lookahead = 1;
    Same as inverted --no-lookahead command-line option.

re2c:flags:optimize-tags = 1;
    Same as inverted --no-optimize-tags command-line option.

re2c:flags:P or re2c:flags:posix-captures
    Same as -P --posix-captures command-line option.

re2c:flags:s or re2c:flags:nested-ifs
    Same as -s --nested-ifs command-line option.

re2c:flags:T or re2c:flags:tags
    Same as -T --tags command-line option.

re2c:flags:u or re2c:flags:unicode
    Same as -u --unicode command-line option.

re2c:flags:w or re2c:flags:wide-chars
    Same as -w --wide-chars command-line option.

re2c:flags:x or re2c:flags:utf-16
    Same as -x --utf-16 command-line option.

re2c:indent:string = '\t';
    Specifies the string to use for indentation. Requires a string that contains only whitespace (unless you need something else for external tools). The easiest way to specify spaces is to enclose them in single or double quotes. If you do not want any indentation at all, you can set this to ''.

re2c:indent:top = 0;
    Specifies the minimum amount of indentation to use. Requires a numeric value greater than or equal to zero.

re2c:labelprefix = 'yy';
    Allows to change the prefix of numbered labels. The default is yy. Can be set any string that is valid in a label name.

re2c:label:yyFillLabel = 'yyFillLabel';
    Overrides the name of the yyFillLabel label.

re2c:label:yyNext = 'yyNext';
    Overrides the name of the yyNext label.

re2c:startlabel = 0;
    If set to a non zero integer, then the start label of the next scanner block will be generated even if it isn't used by the scanner itself. Otherwise, the normal yy0-like start label is only generated if needed. If set to a text value, then a label with that text will be generated regardless of whether the normal start label is used or not. This setting is reset to 0 after a start label has been generated.

re2c:state:abort = 0;
    When not zero and the -f --storable-state switch is active, then the YYGETSTATE block will contain a default case that aborts and a -1 case will be used for initialization.

re2c:state:nextlabel = 0;
    Used when -f --storable-state is active to control whether the YYGETSTATE block is followed by a yyNext: label line. Instead of using yyNext, you can usually also use configuration startlabel to force a specific start label or default to yy0 as a start label. Instead of using a dedicated label, it is often better to separate the YYGETSTATE code from the actual scanner code by placing a /*!getstate:re2c*/ comment.

re2c:tags:expression = '@@';
    Allows to customize the way re2c addresses tag variables: by default it emits expressions of the form yyt<N>, but this might be inconvenient if tag variables are defined as fields in a struct, or for any other reason require special accessors. For example, setting re2c:tags:expression = p->@@ will result in p->yyt<N>.

re2c:tags:prefix = 'yyt';
    Allows to override prefix of tag variables.

re2c:variable:yyaccept = yyaccept;
    Overrides the name of the yyaccept variable.

re2c:variable:yybm = 'yybm';
    Overrides the name of the yybm variable.

re2c:variable:yych = 'yych';
    Overrides the name of the yych variable.

re2c:variable:yyctable = 'yyctable';
    When both -c --conditions and -g --computed-gotos are active, re2c will use this variable to generate a static jump table for YYGETCONDITION.

re2c:variable:yystable = 'yystable';
    Deprecated.

re2c:variable:yytarget = 'yytarget';
    Overrides the name of the yytarget variable.

re2c:yybm:hex = 0;
    If set to zero, a decimal table will be used. Otherwise, a hexadecimal table will be generated.

re2c:yych:conversion = 0;
    When this setting is non zero, re2c automatically generates conversion code whenever yych gets read. In this case, the type must be defined using re2c:define:YYCTYPE.

re2c:yych:emit = 1;
    Set this to zero to suppress the generation of yych.

re2c:yyfill:check = 1;
    This can be set to 0 to suppress the generations of YYCURSOR and YYLIMIT based precondition checks. This option is useful when YYLIMIT + YYMAXFILL is always accessible.

re2c:yyfill:enable = 1;
    Set this to zero to suppress the generation of YYFILL (n). When using this, be sure to verify that the generated scanner does not read beyond the available input, as allowing such behavior might introduce severe security issues to your programs.

re2c:yyfill:parameter = 1;
    Controls the argument in the parentheses that follow YYFILL. If zero, the argument is omitted. If non-zero, the argument is generated unless re2c:define:YYFILL:naked is set to non-zero.

  REGULAR EXPRESSIONS

re2c uses the following syntax for regular expressions:

•   "foo" case-sensitive string literal

•   'foo' case-insensitive string literal

•   [a-xyz], [^a-xyz] character class (possibly negated)

•   . any character except newline

•   R \ S difference of character classes R and S

•   R* zero or more occurrences of R

•   R+ one or more occurrences of R

•   R? optional R

•   R{n} repetition of R exactly n times

•   R{n,} repetition of R at least n times

•   R{n,m} repetition of R from n to m times

•   (R) just R; parentheses are used to override precedence or for POSIX-style submatch

•   R S concatenation: R followed by S

•   R | S alternative: R or S

•   R / S loohakead: R followed by S, but S is not consumed

•   name the regular expression defined as name (or literal string "name" in Flex compatibility mode)

•   {name} the regular expression defined as name in Flex compatibility mode

•   @stag an s-tag: saves the last input position at which @stag matches in a variable named stag

•   #mtag an m-tag: saves all input positions at which #mtag matches in a variable named mtag

    Character classes and string literals may contain the following escape sequences: \a, \b, \f, \n, \r, \t, \v, \\, octal escapes \ooo and hexadecimal escapes \xhh, \uhhhh and \Uhhhhhhhh.

SUBMATCH EXTRACTION

re2c supports two kinds of submatch extraction.

The first option is -P --posix-captures: it enables POSIX-compliant capturing groups. In this mode parentheses in regular expressions denote the beginning and the end of capturing groups; the whole regular expression is group number zero. The number of groups for the matching rule is stored in a variable yynmatch, and submatch results are stored in yypmatch array. Both yynmatch and yypmatch should be defined by the user; note that yypmatch size must be at least [yynmatch * 2]. re2c provides a directive /*!maxnmatch:re2c*/ that defines a constant YYMAXNMATCH: the maximal value of yynmatch among all rules. Note that re2c implements POSIX-compliant disambiguation: each subexpression matches as long as possible, and subexpressions that start earlier in regular expression have priority over those starting later.

Second option is -T --tags. With this option one can use standalone tags of the form @stag and #mtag instead of capturing parentheses, where stag and mtag are arbitrary used-defined names. Tags can be used anywhere inside of a regular expression; semantically they are just position markers. Tags of the form @stag are called s-tags: they denote a single submatch value (the last input position where this tag matched). Tags of the form #mtag are called m-tags: they denote multiple submatch values (the whole history of repetitions of this tag). All tags should be defined by the user as variables with the corresponding names. With standalone tags re2c uses leftmost greedy disambiguation: submatch positions correspond to the leftmost matching path through the regular expression.

With both --posix-captures and --tags options re2c generates a number of tag variables that are used by the lexer to track multiple possible versions of each tag (multiple versions are caused by possible ambiguity of submatch). When a rule matches, ambiguity is resolved and all tags of this rule (or capturing parentheses, which are also implemented as tags) are initialized with the values of appropriate tag variables. Note that there is no one-to-one correspondence between tag variables and tags: the same tag variable may be reused for different tags, and one tag may require multiple tag variables to hold all its ambiguous versions. The exact number of tag variables is unknown to the user; this number is determined by re2c. However, tag variables should be defined by the user, because it might be necessary to update them in YYFILL and store them between invocations of lexer with --storable-state option. Therefore re2c provides directives /*!stags:re2c ... */ and /*!mtags:re2c ... */ that can be used to declare, initialize and manipulate tag variables.

S-tags must support the following operations:

•   save input position to s-tag: t = YYCURSOR with default API, or user-defined operation YYSTAGP (t) with generic API

•   save default value to s-tag: t = NULL with default API, or user-defined operation YYSTAGN (t) with generic API

•   copy one s-tag to another: t1 = t2

    M-tags must support the following operations:

•   append input position to m-tag: user-defined operation YYMTAGP (t) with both default and generic API

•   append default value to m-tag: user-defined operation YYMTAGN (t) with both default and generic API

•   copy one m-tag to another: t1 = t2

    S-tags can be implemented as scalar values (pointers or offsets). M-tags need a more complex representation, as they need to store a sequence of tag values. The most naive and inefficient representation of m-tag is a list (array, vector) of tag values; a more efficient representation is to store all m-tags in a prefix-tree represented as array of nodes (v, p), where v is tag value and p is a pointer to parent node.

    For further details see http://re2c.org/examples/examples.html page on the website or re2c/examples/ subdirectory of re2c distribution.

STORABLE STATE

With -f --storable-state option re2c generates a lexer that can store its current state, return to the caller, and later resume operations exactly where it left off. The default mode of operation in re2c is a "pull" model, where the lexer "pulls" more input whenever it needs it. However, this mode of operation assumes that the lexer is the owner of the parsing loop, and that may not always be convenient.

Storable state is useful exactly for situations like that: it allows to construct lexers that work in a "push" model, where data is fed to the lexer chunk by chunk. When the lexer needs more input, it stores its state and returns to the caller. Later, when more input becomes available, it resumes operations exactly where it stopped.

Changes needed compared to the "pull" model:

•   Define YYSETSTATE () and YYGETSTATE (state).

•   Define yych, yyaccept and state variables as a part of persistent lexer state. state should be initialized to -1.

•   YYFILL should return to the outer program instead of trying to supply more input. Return code should indicate that lexer needs more input.

•   The outer program should recognize situations when lexer needs more input and respond appropriately.

•   Use /*!getstate:re2c*/ directive if it is necessary to execute any code before entering the lexer.

•   Use configurations state:abort and state:nextlabel to tweak the generated code.

CONDITIONS

Conditions are enabled with -c --conditions. This option allows to encode multiple interrelated lexers within the same re2c block.

Each lexer corresponds to a single condition. It starts with a label of the form yyc_name, where name is condition name and yyc prefix can be adjusted with configuration re2c:condprefix. Different lexers are separated with a comment /* *********************************** */ which can be adjusted with configuration re2c:cond:divider.

Furthermore, each condition has a unique identifier of the form yycname, where name is condition name and yyc prefix can be adjusted with configuration re2c:condenumprefix. Identifiers have the type YYCONDTYPE and should be generated with /*!types:re2c*/ directive or -t --type-header option. Users shouldn't define these identifiers manually, as the order of conditions is not specified.

Before all conditions re2c generates entry code that checks the current condition identifier and transfers control flow to the start label of the active condition. After matching some rule of this condition, lexer may either transfer control flow back to the entry code (after executing the associated action and optionally setting another condition with =>), or use :=> shortcut and transition directly to the start label of another condition (skipping the action and the entry code). Configuration re2c:cond:goto allows to change the default behavior.

Syntactically each rule must be preceded with a list of comma-separated condition names or a wildcard * enclosed in angle brackets < and >. Wildcard means "any condition" and is semantically equivalent to listing all condition names. Here regexp is a regular expression, default refers to the default rule *, and action is a block of C/C++ code.

•   <conditions-or-wildcard> regexp-or-default action

•   <conditions-or-wildcard> regexp-or-default => condition action

•   <conditions-or-wildcard> regexp-or-default :=> condition

    Rules with an exclamation mark ! in front of condition list have a special meaning: they have no regular expression, and the associated action is merged as an entry code to actions of normal rules. This might be a convenient place to peform a routine task that is common to all rules.

•   <!conditions-or-wildcard> action

    Another special form of rules with an empty condition list <> and no regular expression allows to specify an "entry condition" that can be used to execute code before entering the lexer. It is semantically equivalent to a condition with number zero, name 0 and an empty regular expression.

•   <> action

•   <> => condition action

•   <> :=> condition

ENCODINGS

re2c supports the following encodings: ASCII (default), EBCDIC (-e), UCS-2 (-w), UTF-16 (-x), UTF-32 (-u) and UTF-8 (-8). See also inplace configuration re2c:flags.

The following concepts should be clarified when talking about encodings. A code point is an abstract number that represents a single symbol. A code unit is the smallest unit of memory, which is used in the encoded text (it corresponds to one character in the input stream). One or more code units may be needed to represent a single code point, depending on the encoding. In a fixed-length encoding, each code point is represented with an equal number of code units. In variable-length encodings, different code points can be represented with different number of code units.

•   ASCII is a fixed-length encoding. Its code space includes 0x100 code points, from 0 to 0xFF. A code point is represented with exactly one 1-byte code unit, which has the same value as the code point. The size of YYCTYPE must be 1 byte.

•   EBCDIC is a fixed-length encoding. Its code space includes 0x100 code points, from 0 to 0xFF. A code point is represented with exactly one 1-byte code unit, which has the same value as the code point. The size of YYCTYPE must be 1 byte.

•   UCS-2 is a fixed-length encoding. Its code space includes 0x10000 code points, from 0 to 0xFFFF. One code point is represented with exactly one 2-byte code unit, which has the same value as the code point. The size of YYCTYPE must be 2 bytes.

•   UTF-16 is a variable-length encoding. Its code space includes all Unicode code points, from 0 to 0xD7FF and from 0xE000 to 0x10FFFF. One code point is represented with one or two 2-byte code units. The size of YYCTYPE must be 2 bytes.

•   UTF-32 is a fixed-length encoding. Its code space includes all Unicode code points, from 0 to 0xD7FF and from 0xE000 to 0x10FFFF. One code point is represented with exactly one 4-byte code unit. The size of YYCTYPE must be 4 bytes.

•   UTF-8 is a variable-length encoding. Its code space includes all Unicode code points, from 0 to 0xD7FF and from 0xE000 to 0x10FFFF. One code point is represented with a sequence of one, two, three, or four 1-byte code units. The size of YYCTYPE must be 1 byte.

    In Unicode, values from range 0xD800 to 0xDFFF (surrogates) are not valid Unicode code points. Any encoded sequence of code units that would map to Unicode code points in the range 0xD800-0xDFFF, is ill-formed. The user can control how re2c treats such ill-formed sequences with the --encoding-policy <policy> switch.

    For some encodings, there are code units that never occur in a valid encoded stream (e.g., 0xFF byte in UTF-8). If the generated scanner must check for invalid input, the only correct way to do so is to use the default rule (*). Note that the full range rule ([^]) won't catch invalid code units when a variable-length encoding is used ([^] means "any valid code point", whereas the default rule (*) means "any possible code unit").

GENERIC API

By default re2c operates on input using pointer-like primitives YYCURSOR, YYMARKER, YYCTXMARKER, and YYLIMIT. Normally pointer-like primitives are defined as variables of type YYCTYPE*, but it is possible to use STL iterators or any other abstraction as long as it syntactically fits into the following use cases:

•   ++YYCURSOR;

•   yych = *YYCURSOR;

•   yych = *++YYCURSOR;

•   yych = *(YYMARKER = YYCURSOR);

•   yych = *(YYMARKER = ++YCURSOR);

•   YYMARKER = YYCURSOR;

•   YYMARKER = ++YYCURSOR;

•   YYCURSOR = YYMARKER;

•   YYCTXMARKER = YYCURSOR + 1;

•   YYCURSOR = YYCTXMARKER;

•   if (YYLIMIT <= YYCURSOR) ...

•   if ((YYLIMIT - YYCURSOR) < n) ...

•   YYDEBUG (label, *YYCURSOR);

    If this input model is too restrictive, then it is possible to use generic input API enabled with --input custom option. In this mode all input operations are expressed in terms of the primitives below. These primitives can be defined in any suitable way; one doesn't have to stick to the pointer semantics. For example, it is possible to read input directly from file without any buffering, or to disable YYFILL mechanism and perform end-of-input checking on each input character from inside of YYPEEK or YYSKIP.

•   YYPEEK ()

•   YYSKIP ()

•   YYBACKUP ()

•   YYBACKUPCTX ()

•   YYSTAGP (t)

•   YYSTAGN (t)

•   YYMTAGP (t)

•   YYMTAGN (t)

•   YYRESTORE ()

•   YYRESTORECTX ()

•   YYRESTORETAG (t)

•   YYLESSTHAN (n)

    Default input model can be expressed in terms of generic API as follows (except for YMTAGP and YYMTAGN, which have no default implementation):

•   #define YYPEEK () *YYCURSOR

•   #define YYSKIP () ++YYCURSOR

•   #define YYBACKUP () YYMARKER = YYCURSOR

•   #define YYBACKUPCTX () YYCTXMARKER = YYCURSOR

•   #define YYRESTORE () YYCURSOR = YYMARKER

•   #define YYRESTORECTX () YYCURSOR = YYCTXMARKER

•   #define YYRESTORERAG (t) YYCURSOR = t

•   #define YYLESSTHAN (n) YYLIMIT - YYCURSOR < n

•   #define YYSTAGP (t) t = YYCURSOR

•   #define YYSTAGN (t) t = NULL

SEE ALSO

You can find more information about re2c at: http://re2c.org. See also: flex(1), lex(1), quex (http://quex.sourceforge.net).

AUTHORS

Originaly written by Peter Bumbulis in 1993; developed and maintained by Brain Young, Marcus Boerger, Dan Nuffer and Ulya Trofimovich. Below is a (more or less) full list of contributors retrieved from the Git history and mailing lists:

Abs62, asmwarrior, Ben Smith, Brian Young, CRCinAU, Dan Nuffer, Derick Rethans, Dimitri John Ledkov, Durimar, Eldar Zakirov, Emmanuel Mogenet, Hartmut Kaiser, jcfp, Jean-Claude Wippler, Jeff Trull, Jérôme Dumesnil, Jesse Buesking, joscherl, Julian Andres Klode, Marcus Boerger, Mike Gilbert, nuno-lopes, Oleksii Taran, paulmcq, Paulo Custodio, Perry E. Metzger, philippschaefer, Ross Burton, Rui Maciel, Ryan Mast, Samuel006, Sergei Trofimovich, sirzooro, Tim Kelly, Ulya Trofimovich

VERSION INFORMATION

This manpage describes re2c version 1.1.1, package date 30 Aug 2018.