Search code examples
clexical-analysis

Can we write comments within variable names?


int main()
{
     i/*nt*/a = 10;
     return 0;
}

If I have the above code and I want to count the tokens, will it be 14 or 13 tokens?

Is it valid to write a comment within a variable name? You can assume that the int i, int a, int ia are globally defined.


Solution

  • The comments are removed during phase 3 of program translation1: each comment is replaced by one space character. so the comment /*nt*/ is definitely not a token.

    If none of int, main, i, a or return are defined as preprocessing macros, parsing the program produces 14 tokens (not 13):

    int main ( ) { i a = 10 ; return 0 ; }

    Unless i is defined as a type with a typedef statement, there is a syntax error as i a does not match a rule in the C grammar.

    So you cannot write comments inside variable names, the comment splits the identifier into 2 separate tokens. This is true for any preprocessing and C language token2.

    Note however that you can insert comments in unusual places such as between unary operators and their operand or between the # and the preprocessing directive and its arguments:

    /**/#/**/include/**/<stdio.h>/**///////////////////////
    /**/#/**/define/**/STAT/**/(/**/a/**/)/**/-/**/1/**////
    /**/#/**/ifdef/**/STAT/**//////////////////////////////
    /**/int/**/main/**/(/**/)/**/{/**//////////////////////
    /**/int/**/a/**/=/**/+/**/1/**/;/**////////////////////
    /**/printf/**/(/**/"Hello "/**/"world!\n"/**/)/**/;/**/
    /**/return/**/STAT/**/;/**/////////////////////////////
    /**/}/**///////////////////////////////////////////////
    /**/#/**/endif/**//////////////////////////////////////
    

    But the above macro definition does not define a function-like macro but a regular macro STAT that expands to ( a ) - 1.

    Variable names, like any other token can be split by escaped newlines. Escaped newlines are sequences or \ immediately followed by a newline. These sequences are removed from the source code during phase 2 of program translation. Their main purpose is to break long macro definitions on multiple lines.

    Below is a code fragment3 that produces the same 14 tokens:

    \
    i\
    nt\
     ma\
    in()
    {\
    i/\
    *nt\
    */a \
    = 10;
    r\
    et\
    urn\
     0;}
    
    

    Notice how the code colorizer missed the sliced and diced keywords and comment :)


    1) This behavior was specified in ANSI-C aka C89. Some ancient compilers had subtly different behavior resulting in token pasting, but such peculiarities are of historical interest only.

    2) You can almost insert a comment inside a string constant by taking advantage of the fact that adjacent string constants are concatenated in phase 6 of program translation: printf("Hello "/* my name is Luca */"world!\n");

    3) This Christmas Tree presentation style is not meant to be used in real programs, it illustrates how to abuse C's input handling capabilities. More elaborate tricks have won The International Obfuscated C Code Contest