Search code examples
c++punctuator

What are Punctuators in C++?


I don't seem to get my head around punctuators in C++. My college didn't even mention about it while teaching tokens, they referred to it as 'special symbols' and just skimmed through it. Are the two terms used interchangeably? How can I write about punctuators in some 80-100 words if it gets asked in my exam? I may want to know about the ways it interacts with the compiler, its difference from operators, or other things which could build a good short note on punctuators.

Note:

I have got this after surfing the net for a quality answer. But that is a very short answer which is not what I'm looking for.

Edit:

Even a few points would do upon which I can build a short note.


Solution

  • The C++ standard document actually appears to not define what punctuator means, other than punctuators being a lexical token. Specifically:

    [gram.lex]

    token:
      identifier
      keyword
      literal
      operator
      punctuator
    

    Here is the definition from the C language:

    6.4.6 Punctuators

    Syntax

    punctuator: one of

    [ ] ( ) { } . ->
    ++ -- & * + -  ~ !
    / % << >> < > <= >= == != ^ | && ||
    ? : ; ... = *=  /=  %=  > +=  -=  <<=  >>=  &= 
    ^= |= , # ## <: :> <% %> %: %:%:
    

    Semantics

    A punctuator is a symbol that has independent syntactic and semantic significance. Depending on context, it may specify an operation to be performed (which in turn may yield a value or a function designator, produce a side effect, or some combination thereof) in which case it is known as an operator (other forms of operator also exist in some contexts). An operand is an entity on which an operator acts.


    The grammar of C++ has similar list (quote from latest standard draft; the list includes the new operator <=> which will be in C++20):

    [lex.operators]

    preprocessing-op-or-punc: one of

      {        }        [        ]        #        ##       (        )
      <:       :>       <%       %>       %:       %:%:     ;        :        ...
      new      delete   ?        ::       .        .*       ->       ->*      ~
      !        +        -        *        /        %        ^        &        |
      =        +=       -=       *=       /=       %=       ^=       &=       |=
      ==       !=       <        >        <=       >=       <=>      &&       ||
      <<       >>       <<=      >>=      ++       --       ,
      and      or       xor      not      bitand   bitor    compl     and_eq  
      or_eq    xor_eq   not_eq