I need a regular expression for inline comment in C for a compiler's lexical analyser. I tried this ScreenShot but it is of no use as it comments out the regular expression.
Asked
Active
Viewed 160 times
-1
-
1Post your code as text in the question please. – Retired Ninja Mar 25 '17 at 23:30
-
Most of the part disappear as it contain lots of \\\\ – Khurshid Mar 25 '17 at 23:43
-
It is simple Lex code for creating Tokens. – Khurshid Mar 25 '17 at 23:43
-
2Handling comments reliably in all their glory is surprisingly hard. In theory, you need to know about trigraphs (because `??/` maps to a backslash), but compilers like GCC ignore them by default, and C++17 eliminates them. You also need to know about backslash-newline rules; these matter, because `//` comments can continue onto subsequent lines if the last character on each line is a backslash. Both the start and end comment symbols can be broken by backslash-newline too. Most of the time, you won't run into such esoterically formatted comments, but a compiler must take them all into account. – Jonathan Leffler Mar 26 '17 at 00:56
-
There is a flex regular expression in [this answer](http://stackoverflow.com/a/25396611/1566221) but @JonathanLeffler's comment is also correct; it depends on your having previously dealt with trigraphs and \-newline sequences before feeding the input into your lexer. – rici Mar 26 '17 at 05:51
1 Answers
0
For catching C style comments better use start conditions. Example for flex you can find in documentation.
There is simple example for ignoring C stype comments by using exclusive start condition:
%x comment
"/*" BEGIN(comment);
<comment>[^*\n]* /* eat anything that's not a '*' */
<comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
<comment>"*"+"/" BEGIN(INITIAL);

komar
- 861
- 5
- 8
-
thank you for the reply but I need the regular expression for "//" inline comments – Khurshid Mar 26 '17 at 10:38
-
My code is working fine for block comments. why I need to use this (your one) method? – Khurshid Mar 26 '17 at 10:42
-
For one line comments you need put your regular expression (something like `"//".*\n`) **before** rule what ignore newline `[ \t\n]+` – komar Mar 26 '17 at 11:43
-
@komar: There is no need to put those two rules in any particular order, since they cannot both match at any point in the input. Also, it is certainly not necessary to use a state machine to ignore block comments; a simple regular expression suffices. – rici Mar 26 '17 at 19:38