Skip to content

Commit

Permalink
Make most of the C/C++ examples truly C compatible.
Browse files Browse the repository at this point in the history
Some of the examples use C++ data structures like `std::ifstream`, `std::vector`
etc. Leave them as is for now.

Update docs.

This addresses bug #484 "Example is not C".
  • Loading branch information
skvadrik committed Nov 23, 2024
1 parent a75eb2b commit 3a40b3d
Show file tree
Hide file tree
Showing 25 changed files with 421 additions and 432 deletions.
243 changes: 117 additions & 126 deletions bootstrap/doc/re2c.1

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/split_man.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
src_ext = b'c'
hdr_ext = b'h'
lang_name = b'C/C++'
disclaimer = b'Note: examples are in C++ (but can be easily adapted to C).'
disclaimer = b'Note: some examples are in C++ (but can be adapted to C).'
elif lang == b'd':
src_ext = b'd'
lang_name = b'D'
Expand Down
8 changes: 4 additions & 4 deletions examples/c/01_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// re2c $INPUT -o $OUTPUT -i --case-ranges
#include <assert.h>

bool lex(const char *s) {
int lex(const char *s) {
const char *YYCURSOR = s;

{
Expand All @@ -14,20 +14,20 @@ bool lex(const char *s) {
}
yy1:
++YYCURSOR;
{ return false; }
{ return 1; }
yy2:
yych = *++YYCURSOR;
switch (yych) {
case '0' ... '9': goto yy2;
default: goto yy3;
}
yy3:
{ return true; }
{ return 0; }
}

}

int main() {
assert(lex("1234"));
assert(lex("1234") == 0);
return 0;
}
8 changes: 4 additions & 4 deletions examples/c/01_basic.re
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// re2c $INPUT -o $OUTPUT -i --case-ranges
#include <assert.h>

bool lex(const char *s) {
int lex(const char *s) {
const char *YYCURSOR = s;
/*!re2c
re2c:yyfill:enable = 0;
re2c:define:YYCTYPE = char;
[1-9][0-9]* { return true; }
* { return false; }
[1-9][0-9]* { return 0; }
* { return 1; }
*/
}

int main() {
assert(lex("1234"));
assert(lex("1234") == 0);
return 0;
}
17 changes: 7 additions & 10 deletions examples/c/conditions/parse_u32_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

static const uint64_t ERROR = UINT64_MAX;

static void add(uint32_t base, uint64_t &number, char digit) {
number = number * base + digit;
if (number > UINT32_MAX) number = ERROR;
}
#define CHECK(n) if (n > UINT32_MAX) return ERROR;

static uint64_t parse_u32(const char *s) {
const char *YYCURSOR = s, *YYMARKER;
Expand Down Expand Up @@ -116,7 +113,7 @@ static uint64_t parse_u32(const char *s) {
{ return ERROR; }
yy13:
++YYCURSOR;
{ add(2, u, YYCURSOR[-1] - '0'); goto bin; }
{ u = u * 2 + (YYCURSOR[-1] - '0'); CHECK(u); goto bin; }
}

oct:
Expand Down Expand Up @@ -144,7 +141,7 @@ static uint64_t parse_u32(const char *s) {
{ return ERROR; }
yy17:
++YYCURSOR;
{ add(8, u, YYCURSOR[-1] - '0'); goto oct; }
{ u = u * 8 + (YYCURSOR[-1] - '0'); CHECK(u); goto oct; }
}

dec:
Expand Down Expand Up @@ -174,7 +171,7 @@ static uint64_t parse_u32(const char *s) {
{ return ERROR; }
yy21:
++YYCURSOR;
{ add(10, u, YYCURSOR[-1] - '0'); goto dec; }
{ u = u * 10 + (YYCURSOR[-1] - '0'); CHECK(u); goto dec; }
}

hex:
Expand Down Expand Up @@ -216,13 +213,13 @@ static uint64_t parse_u32(const char *s) {
{ return ERROR; }
yy25:
++YYCURSOR;
{ add(16, u, YYCURSOR[-1] - '0'); goto hex; }
{ u = u * 16 + (YYCURSOR[-1] - '0'); CHECK(u); goto hex; }
yy26:
++YYCURSOR;
{ add(16, u, YYCURSOR[-1] - 'A' + 10); goto hex; }
{ u = u * 16 + (YYCURSOR[-1] - 'A' + 10); CHECK(u); goto hex; }
yy27:
++YYCURSOR;
{ add(16, u, YYCURSOR[-1] - 'a' + 10); goto hex; }
{ u = u * 16 + (YYCURSOR[-1] - 'a' + 10); CHECK(u); goto hex; }
}

}
Expand Down
17 changes: 7 additions & 10 deletions examples/c/conditions/parse_u32_blocks.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

static const uint64_t ERROR = UINT64_MAX;

static void add(uint32_t base, uint64_t &number, char digit) {
number = number * base + digit;
if (number > UINT32_MAX) number = ERROR;
}
#define CHECK(n) if (n > UINT32_MAX) return ERROR;

static uint64_t parse_u32(const char *s) {
const char *YYCURSOR = s, *YYMARKER;
Expand All @@ -29,27 +26,27 @@ static uint64_t parse_u32(const char *s) {
bin:
/*!re2c
end { return u; }
[01] { add(2, u, YYCURSOR[-1] - '0'); goto bin; }
[01] { u = u * 2 + (YYCURSOR[-1] - '0'); CHECK(u); goto bin; }
* { return ERROR; }
*/
oct:
/*!re2c
end { return u; }
[0-7] { add(8, u, YYCURSOR[-1] - '0'); goto oct; }
[0-7] { u = u * 8 + (YYCURSOR[-1] - '0'); CHECK(u); goto oct; }
* { return ERROR; }
*/
dec:
/*!re2c
end { return u; }
[0-9] { add(10, u, YYCURSOR[-1] - '0'); goto dec; }
[0-9] { u = u * 10 + (YYCURSOR[-1] - '0'); CHECK(u); goto dec; }
* { return ERROR; }
*/
hex:
/*!re2c
end { return u; }
[0-9] { add(16, u, YYCURSOR[-1] - '0'); goto hex; }
[a-f] { add(16, u, YYCURSOR[-1] - 'a' + 10); goto hex; }
[A-F] { add(16, u, YYCURSOR[-1] - 'A' + 10); goto hex; }
[0-9] { u = u * 16 + (YYCURSOR[-1] - '0'); CHECK(u); goto hex; }
[a-f] { u = u * 16 + (YYCURSOR[-1] - 'a' + 10); CHECK(u); goto hex; }
[A-F] { u = u * 16 + (YYCURSOR[-1] - 'A' + 10); CHECK(u); goto hex; }
* { return ERROR; }
*/
}
Expand Down
32 changes: 21 additions & 11 deletions examples/c/conditions/parse_u32_conditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ enum YYCONDTYPE {
};


static void add(uint32_t base, uint64_t &number, char digit) {
number = number * base + digit;
if (number > UINT32_MAX) number = ERROR;
}

static uint64_t parse_u32(const char *s) {
const char *YYCURSOR = s, *YYMARKER;
int c = yycinit;
Expand Down Expand Up @@ -52,6 +47,7 @@ static uint64_t parse_u32(const char *s) {
}
yy1:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return ERROR; }
yy2:
yych = *(YYMARKER = ++YYCURSOR);
Expand Down Expand Up @@ -128,13 +124,16 @@ static uint64_t parse_u32(const char *s) {
}
yy11:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return u; }
yy12:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return ERROR; }
yy13:
++YYCURSOR;
{ add(2, u, YYCURSOR[-1] - '0'); goto yyc_bin; }
{ if (u > UINT32_MAX) return ERROR; }
{ u = u * 2 + (YYCURSOR[-1] - '0'); goto yyc_bin; }
/* *********************************** */
yyc_dec:
yych = *YYCURSOR;
Expand All @@ -154,13 +153,16 @@ static uint64_t parse_u32(const char *s) {
}
yy15:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return u; }
yy16:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return ERROR; }
yy17:
++YYCURSOR;
{ add(10, u, YYCURSOR[-1] - '0'); goto yyc_dec; }
{ if (u > UINT32_MAX) return ERROR; }
{ u = u * 10 + (YYCURSOR[-1] - '0'); goto yyc_dec; }
/* *********************************** */
yyc_hex:
yych = *YYCURSOR;
Expand Down Expand Up @@ -192,19 +194,24 @@ static uint64_t parse_u32(const char *s) {
}
yy19:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return u; }
yy20:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return ERROR; }
yy21:
++YYCURSOR;
{ add(16, u, YYCURSOR[-1] - '0'); goto yyc_hex; }
{ if (u > UINT32_MAX) return ERROR; }
{ u = u * 16 + (YYCURSOR[-1] - '0'); goto yyc_hex; }
yy22:
++YYCURSOR;
{ add(16, u, YYCURSOR[-1] - 'A' + 10); goto yyc_hex; }
{ if (u > UINT32_MAX) return ERROR; }
{ u = u * 16 + (YYCURSOR[-1] - 'A' + 10); goto yyc_hex; }
yy23:
++YYCURSOR;
{ add(16, u, YYCURSOR[-1] - 'a' + 10); goto yyc_hex; }
{ if (u > UINT32_MAX) return ERROR; }
{ u = u * 16 + (YYCURSOR[-1] - 'a' + 10); goto yyc_hex; }
/* *********************************** */
yyc_oct:
yych = *YYCURSOR;
Expand All @@ -222,13 +229,16 @@ static uint64_t parse_u32(const char *s) {
}
yy25:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return u; }
yy26:
++YYCURSOR;
{ if (u > UINT32_MAX) return ERROR; }
{ return ERROR; }
yy27:
++YYCURSOR;
{ add(8, u, YYCURSOR[-1] - '0'); goto yyc_oct; }
{ if (u > UINT32_MAX) return ERROR; }
{ u = u * 8 + (YYCURSOR[-1] - '0'); goto yyc_oct; }
}

}
Expand Down
18 changes: 7 additions & 11 deletions examples/c/conditions/parse_u32_conditions.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
static const uint64_t ERROR = UINT64_MAX;
/*!conditions:re2c*/

static void add(uint32_t base, uint64_t &number, char digit) {
number = number * base + digit;
if (number > UINT32_MAX) number = ERROR;
}

static uint64_t parse_u32(const char *s) {
const char *YYCURSOR = s, *YYMARKER;
int c = yycinit;
Expand All @@ -32,12 +27,13 @@ static uint64_t parse_u32(const char *s) {
<bin, oct, dec, hex> "\x00" { return u; }
<bin> [01] { add(2, u, YYCURSOR[-1] - '0'); goto yyc_bin; }
<oct> [0-7] { add(8, u, YYCURSOR[-1] - '0'); goto yyc_oct; }
<dec> [0-9] { add(10, u, YYCURSOR[-1] - '0'); goto yyc_dec; }
<hex> [0-9] { add(16, u, YYCURSOR[-1] - '0'); goto yyc_hex; }
<hex> [a-f] { add(16, u, YYCURSOR[-1] - 'a' + 10); goto yyc_hex; }
<hex> [A-F] { add(16, u, YYCURSOR[-1] - 'A' + 10); goto yyc_hex; }
<bin> [01] { u = u * 2 + (YYCURSOR[-1] - '0'); goto yyc_bin; }
<oct> [0-7] { u = u * 8 + (YYCURSOR[-1] - '0'); goto yyc_oct; }
<dec> [0-9] { u = u * 10 + (YYCURSOR[-1] - '0'); goto yyc_dec; }
<hex> [0-9] { u = u * 16 + (YYCURSOR[-1] - '0'); goto yyc_hex; }
<hex> [a-f] { u = u * 16 + (YYCURSOR[-1] - 'a' + 10); goto yyc_hex; }
<hex> [A-F] { u = u * 16 + (YYCURSOR[-1] - 'A' + 10); goto yyc_hex; }
<!*> { if (u > UINT32_MAX) return ERROR; }
*/
}

Expand Down
Loading

0 comments on commit 3a40b3d

Please sign in to comment.