skvadrik / re2c

Lexer generator for C, C++, Go and Rust.
https://re2c.org
Other
1.09k stars 170 forks source link

Example is not C #484

Open createyourpersonalaccount opened 2 months ago

createyourpersonalaccount commented 2 months ago

This example https://github.com/skvadrik/re2c/blob/f2395c299886cfc5639f841f6445ab9ad0fa26f9/examples/c/conditions/parse_u32_conditions.re is transpiled as C but it contains C++ templates.

It can be trivially translated into C e.g. with

uint64_t add(uint64_t u, unsigned d, unsigned base) {
    u = u * base + d;
    if (u > UINT32_MAX) {
      return UINT64_MAX;
    } else {
      return u;
    }
}

and if it stays as C++ the template is excessive for this simple example and should probably be converted to the function above. If C++ stays, the deprecated C headers should be converted, e.g. <limits.h> should be converted to <climits>.

pmetzger commented 2 months ago

I noticed this years ago and thought little of it because re2c is a preprocessor and doesn't really care if the input is C or C++. It seemed like it was good to make it clear to the reader that either syntax would be fine in context, but maybe it needs to be more explicit? I do agree that more modern C++ syntax is a good idea though.

createyourpersonalaccount commented 2 months ago

@pmetzger the repository contains the transpilation as C source code with the .c suffix, e.g. https://github.com/skvadrik/re2c/blob/f2395c299886cfc5639f841f6445ab9ad0fa26f9/examples/c/conditions/parse_u32_conditions.c so that is another confusion.

On the front page it says "User manual (C)" and so I wouldn't expect C++ examples. Something needs to be fixed to make this presentation consistent, obviously it's not important though.

skvadrik commented 2 months ago

Thank for reporting @createyourpersonalaccount. The mismatch is caused by the fact that re2c has one backend for both C and C++, and the testing scripts / manual generators / everything assumes that there's one file extension per language (and it's a reasonable assumption for most languages). You make a valid point, but so far it seemed enough to place a note on the manual page https://re2c.org/manual/manual_c.html saying Note: examples are in C++ (but can be easily adapted to C).

On the front page it says "User manual (C)" and so I wouldn't expect C++ examples.

I should change this to say C/C++ (it's not a trivial change, as the manuals are autogenerated from one language-independent source, and the change requires regenerating them and inspecting the diffs). I'll address this later when I update all docs before the next release.

helly25 commented 2 months ago

Thanks for the report indeed.

Imo it makes perfect sense to keep the generator for both C and C++ the same as long as feasible. Nonetheless, we could: a) call that out clearly in the documentation, b) separate the examples into C and C++ respectively, and c) as pointed out in the original report the template is not even necessary, so we could also change the example to C.

For b) I assume here that we go from extension to backend somewhere in our testing. That simply needs to have the ability to map multiple extensions to the same backend. Then we can rename the C++ examples into .cpp and keep the C ones as .c. This will also work for other languages with multiple extensions or cases where multiple languages share a single backends.

my 2c

On Sun, Aug 11, 2024 at 6:31 PM Ulya Trofimovich @.***> wrote:

Thank for reporting @createyourpersonalaccount https://github.com/createyourpersonalaccount. The mismatch is caused by the fact that re2c has one backend for both C and C++, and the testing scripts / manual generators / everything assumes that there's one file extension per language (and it's a reasonable assumption for most languages). You make a valid point, but so far it seemed enough to place a note on the manual page https://re2c.org/manual/manual_c.html saying Note: examples are in C++ (but can be easily adapted to C).

On the front page it says "User manual (C) https://re2c.org/manual/manual_c.html" and so I wouldn't expect C++ examples.

I should change this to say C/C++ (it's not a trivial change, as the manuals are autogenerated from one language-independent source, and the change requires regenerating them and inspecting the diffs). I'll address this later when I update all docs before the next release.

— Reply to this email directly, view it on GitHub https://github.com/skvadrik/re2c/issues/484#issuecomment-2282813204, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQ7NSJUJMUGO452GSDYA5DZQ6GVBAVCNFSM6AAAAABMJT6LYKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBSHAYTGMRQGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

createyourpersonalaccount commented 2 months ago

Let me say that I personally like the examples shown and if they become too decoupled (i.e. in separate pages) I think that would be a net loss to the presentation. I'm not sure if that was implied somewhere in the suggestions above, just a remark.

I totally missed the note at the top of the page. I did not realize all the examples are in C++, because the first few are valid C.

Re2c is a very nice tool, I don't think these quirks are important to waste your sleep over them, using re2c with lemon was certainly less painful than trying to figure out flex/bison with reentrancy. I wrote a small project in CMake here https://github.com/createyourpersonalaccount/lemon_example for demonstration purposes because I noticed that I couldn't find a complete example online combining the two (lemon and re2c).

helly25 commented 2 months ago

Nice. Having that combination work and documented in the open is very valuable. There have been rumors for way over a decade that the combo is in fact the best combo. But I haven't seen any open documentation or discussions.

On Sun, Aug 11, 2024, 22:10 Nikolaos Chatzikonstantinou < @.***> wrote:

Let me say that I personally like the examples shown and if they become too decoupled (i.e. in separate pages) I think that would be a net loss to the presentation. I'm not sure if that was implied somewhere in the suggestions above, just a remark.

I totally missed the note at the top of the page. I did not realize all the examples are in C++, because the first few are valid C.

Re2c is a very nice tool, I don't think these quirks are important to waste your sleep over them, using re2c with lemon was certainly less painful than trying to figure out flex/bison with reentrancy. I wrote a small project in CMake here https://github.com/createyourpersonalaccount/lemon_example for demonstration purposes because I noticed that I couldn't find a complete example online combining the two (lemon and re2c).

— Reply to this email directly, view it on GitHub https://github.com/skvadrik/re2c/issues/484#issuecomment-2282875247, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQ7NSP7HHNQCWBTUN7TYZTZQ7ALLAVCNFSM6AAAAABMJT6LYKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBSHA3TKMRUG4 . You are receiving this because you commented.Message ID: @.***>