Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example is not C #484

Open
createyourpersonalaccount opened this issue Aug 10, 2024 · 6 comments
Open

Example is not C #484

createyourpersonalaccount opened this issue Aug 10, 2024 · 6 comments

Comments

@createyourpersonalaccount

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
Copy link
Contributor

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
Copy link
Author

@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
Copy link
Owner

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
Copy link
Collaborator

helly25 commented Aug 11, 2024 via email

@createyourpersonalaccount
Copy link
Author

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
Copy link
Collaborator

helly25 commented Aug 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants