Search code examples
compiler-constructionflex-lexerlexical-analysis

Meaning of "\\" in flex - scanner generator


I'm reading a Start Conditions of the flex manual. Below is part of an example that is given on this page:

<str>\n        {
                 /* error - unterminated string constant */
                 /* generate error message */
               }
...
<str>\\n  *string_buf_ptr++ = '\n';
...

What is the difference between \n and \\n?

Any help is appreciated


Solution

    • \n matches a newline character (normally Ox0A). The rule triggers if a C-style quoted string is not terminated before a newline is reached, which it treats as an error.
    • \\n matches a backslash followed by the letter n. The rule is triggered if the quoted string includes the two-character sequence \n, which it replaces with a single newline character. (There should also be rules for the six other C-style letter-escape sequences, \a, \b,\f, \r, \t, and \v -- each substituted with the appropriate control character -- but for reasons I can't attest to, two of them are missing.)