I need a code generator that transforms a regular expression in any notation into C or C++ code, that allows recognition and extraction of data from the input.
-
Why not use a regex library and the notation that goes with it?– Steve BarnesCommented Aug 20, 2016 at 11:02
-
Because of bloat. My regex is constant.– user1095108Commented Aug 20, 2016 at 12:05
-
Well, you can probably convert it to BNF and then create a parser out of it. Not sure if that's worth getting rid of the "bloat" though. And we'll laugh our arses off if that single regex expands to multiple regex.– Maarten BodewesCommented Aug 20, 2016 at 17:18
-
It just needs to expand to code hehe :) My binary exploded 5x after using <regex>, generated code can easily beat that, I believe– user1095108Commented Aug 20, 2016 at 19:13
-
1nothing automatic but you can do this with gpt4 and lots lots lots of handholding– RainbCommented May 24, 2023 at 12:28
2 Answers
A few suggestions shamelessly copied from a similar SO question:
- re2c - "regular expression to C" says it's about generating lexers:
re2c is a lexer generator for C/C++. Its key features are:
Very fast lexers: the generated code is as good as a carefully tuned hand-crafted C/C++ lexer. It’s because re2c generates minimalistic hard-coded state machine (as opposed to full-featured table-based lexers).
but might still do what you want, if it actually takes regexps anywhere.
- regextl - dumps the state machine for a regular expression.
I haven't tried any of these so I can't actually recommend anything.
-
11. is a lexer, which I don't need, 2. is unfinished. I'll just make my own if this keeps up. Commented Aug 21, 2016 at 11:08
-
1Note that the fact that (1.) is a lexer does not mean it doesn't have the functionality you need. It might be using regexp matching for lexing. Anyway, if you do decide to write your own, please post an answer here with the link, I wouldn't mind having that. Commented Aug 21, 2016 at 11:13
Fo rmany years I have been extremely happy wiht RegEx Buddy. It is not free, but reasonably priced (and the OP does not ask for free).
Develop Efficient Software Quickly with Instant Code Snippets
Save time and code efficiently by using regular expressions when developing applications and scripts. With the proper regex, you can often do in a single line of code, or a few lines of code, what would otherwise require dozens or hundreds.
Rely on RegexBuddy to handle the details, such as which classes and function calls to use, and how to escape special characters. Just select the language you are working with, and the action you want to perform. Test whether a string matches a regex, extract matches from a string, search and replace, split a string, etc. RegexBuddy knows all the common regex actions and how to perform them with a variety of programming languages: C#, VB.NET, VB 6, Delphi, Java, Perl, PHP, JavaScript, VBScript, Python, Ruby and C/C++.
Read more about RegexBuddy's support for various programming languages Watch a video showing how to develop software with regular expressions
Plus many more features. My favo(u)rite being
Regex Tester and Debugger
Don't risk actual data with untested regexes. Copy and paste sample data into RegexBuddy, or open test files. Step through the search matches in the sample data, and get a detailed report about each match. Or highlight all matches to debug the regex in real time as you edit it.
If a regex doesn't do what you want, debug it by looking at the complete matching process, eliminating all guesswork.
Learn how you can avoid risk by testing and debugging regexes
Watch a video showing how to test and debug regular expressions
-
I don't see any code, make some C/C++ code and post it here please. Commented Oct 20, 2016 at 11:16
-
The code is in the first image. Why not download the trial and generate some C/C++ yourself?– MawgCommented Oct 20, 2016 at 11:54
-
1