1

I'm currently writing a compiler for the C-language, and stumbled upon something rather peculiar. I know that the increment operator ++ can be prefix or postfix, I know that it can be used in expressions.

  • So the line: a++, where a is an integer variable, should be legal.
  • The line: (a++)-(++b) should also be legal.
  • The line: a++-++b is also legal.
  • The line: a++/++b is also legal.

This leads me to conclude that: <variable><++><operator><++><variable>, where operator is any binary operator should be legal.

What confuses me, is that the line a+++++b is not legal. Why is this not legal? Is it something to do with how GCC lexes the expression a+++++b?


I'm running GCC 7.2.0.

Napoleon
  • 340
  • 3
  • 13
  • I'm afraid I flagged this by mistake. This is about the lexicographical analysis of C, not the semantics. – Cong Ma Feb 26 '18 at 12:41
  • 1
    This is not the correct duplicate. This is the one to do with the fact that ++++a is not valid C (but is valid C++). – Bathsheba Feb 26 '18 at 12:41
  • Explaining grotcode is not useful to future SO users/visitors:( – Martin James Feb 26 '18 at 12:47
  • I'm sorry if this is a duplicate, I didn't know what to search for in order to find the any duplicate. – Napoleon Feb 26 '18 at 12:47
  • 3
    This is not a duplicate. The problem is that C tokenizer must consume as much characters as possible, so `a+++++b` is lexed as `a++ ++ + b` which is not valid C. – Cong Ma Feb 26 '18 at 12:49
  • 3
    Yes it is a duplicate and also a somewhat common FAQ. See the nice answer by Shafik Yaghmour in the linked duplicate. I've added the linked post to the [SO C FAQ](https://stackoverflow.com/tags/c/info) now. – Lundin Feb 26 '18 at 14:23

0 Answers0