4

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.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53

1 Answers1

6

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
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thank you. I see that even the Standard hasn't defined it well enough, hence you have also answered why this thing is so less popular and not taught properly! Upvoted. – Ardent Coder Jan 31 '20 at 15:32
  • Although you didn't answer if those two terms could be used interchangeably – Ardent Coder Jan 31 '20 at 15:41
  • 1
    @ArdentCoder "Special symbol" is a way to describe punctuators. "Special symbol" is not a strictly defined term and its meaning can vary depending on context. For example, you could consider $ to be a special symbol, but it is not a punctuator in C++. – eerorika Jan 31 '20 at 15:44
  • Oh, the counterexample of $ seems very good. Thanks for all the valuable points :) – Ardent Coder Jan 31 '20 at 15:47