+ 3

Matching Compiler Flags using regex

I wish to generate preprocessor and assembly files from a c++ source file using a c++ program which will invoke the compiler. Now, for some codes, additional flags may be required like -std=c++17, -masm=intel, etc. I can take the user input for such flags, but at the same time, I wish to prevent the user from entering flags that may not be relevant in this case, as the program is not supposed to generate executable files or rename the generated .s and .ii files. The flags that I can allow the user to type are: -masm=<arg>, -std=c++<ver>, -S, -l<lib> and -save-temps For this, I thought of using the following regex : regex("((-std=c\\+\\+(03|11|14|17|20|0x|1y|1z|2a) )?(-l\\w+ )?(-masm=\\w+ )?)-S -save-temps"); However, if I change the order of the flags, like "-std=c++17 -S -save-temps" to "-S -save-temps -std=c++17", the regex fails to match, which is expected. But the compiler will treat the two strings as equivalent, and that is what I want to achieve. How can I create the regex that matches the flags in the desired way? Is there a way or am I forced to keep the strings in order, by splitting them into individual flags, and sorting them in order?

11th Aug 2018, 10:03 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
3 odpowiedzi
+ 1
i'm pretty sure you need to make a regex for each flag and then check all possible arrangements because if you were to try it with one regex it would be unnecessarily long (you would have to just put every possible arrangement of flags into one regex)
11th Aug 2018, 1:17 PM
hinanawi
hinanawi - avatar
+ 1
Kinshuk Vasisht that could work too, just requires knowing how the strings will be sorted which is pretty easy to just check and then you can create one regex based on that
11th Aug 2018, 3:54 PM
hinanawi
hinanawi - avatar
0
hinanawi Thank you very much for your reply. If I attempt to make a regex for all combinations, I will have to make around 32 regex strings, which will be tedious. If I check for individual flags, the regex will still match flags that are unwanted. Eg = regex(".*(-l\\w+).*"); matches : -luser32 -o -c, and -o -c should have made the regex return false. I think the best option in this case then is to split the string to individual flags, sort the flags in ascending order, and run a single regex on a sorted string of flags.
11th Aug 2018, 3:51 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar