Re2c

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

re2c is a free and open-source lexer generator for C, C++, D, Go, Haskell, Java, JavaScript, OCaml, Python, Rust, V and Zig. 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.

re2c is a free and open-source lexer generator for C, C++, D, Go, Haskell, Java, JavaScript, OCaml, Python, Rust, V and Zig. 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. Rules have the form REGEXP { CODE } 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; (and also NAME { REGEXP } in Flex compatibility mode). 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. include static int lex(const char YYCURSOR[]) { const char* YYMARKER; /*!re2c re2c:define:YYCTYPE = char; re2c:yyfill:enable = 0; end = "\x00"; hex = "0x" [0-9a-fA-F]+; * { printf("err\n"); return 1; } hex end { printf("hex\n"); return 0; } */ } int main(int argc, char* argv[]) { for (int i = 1; i Given that, re2c -is -o example.c example.re generates the code below (example.c). The contents of the comment /*!re2c ... */ are substituted with a deterministic finite automaton encoded 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 (as in this example with -s option), 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 static int lex(const char *YYCURSOR) { const char *YYMARKER; { char yych; yych = *YYCURSOR; if (yych == '0') goto yy4; ++YYCURSOR; yy3: { printf("err\n"); return 1; } yy4: yych = *(YYMARKER = ++YYCURSOR); if (yych != 'x') goto yy3; yych = *++YYCURSOR; if (yych >= 0x01) goto yy8; yy6: YYCURSOR = YYMARKER; goto yy3; yy7: yych = *++YYCURSOR; yy8: if (yych

Text derived from https://en.wikipedia.org/w/index.php?oldid=1376309127 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

19 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.