Search code examples
c++compiler-constructionsyntax-errorlvalue

Assigning a value to a constant syntax or semantic error?


Is the second line of code considered as a syntax error or a semantic error in C++?

 int a = 7;
 3 = a;

In standard C++ context-free grammar I found this statement syntactically valid.


Solution

  • It is not a syntax error, as the grammar can derive from assignment-expression (5.17) up to integer_literal

    It is then a semantic error, as stated in 5.17:

    All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.

    lvalue is a semantic concept, not a syntactic one.