2

I am reading a book namad A Retargetable C Compiler: Design and Implementation. In this book, the C language grammar is specified like this:

expression:
  assignment-expression { , assignment-expression }
assignment-expression:
  conditional-expression
  unary-expression assign-operator assignment-expression
assign-operator:
  one of= += -= *= /= %= <<= >>= &= A= I=
conditional-expression:
  binary-expression [ ? expression : conditional-expression ]
binary-expression:
  unary-expression { binary-operator unary-expression }
binary-operator:
  one of || && '|' A & == ! = < > <= >= << >> + - * | %
unary-expression:
  postfix-expression
  unary-opera tor unary-expression
  '(' type-name ')' unary-expression
  sizeof unary-expression
  sizeof '(' type-name ')'
unary-operator:
  one of ++ -- & * + - - !
postfix-expression:
  primary-expression { postfix-operator }
postfix-operator:
  '[' expression ']'
  '(' [ assignment-expression { , assignment-expression } ] ')'
. identifier
-> identifier
++
--
primary-expression:
  identifier
  constant
  string-literal
  '(' expression ')'

I have a question about something I observed with:

expression:
    assignment-expression

I put unary-expression assign-operator assignment-expression for the assignment-expression.

I choose "sizeof '(' type-name ')'" for the unary-expression.

Then I choose "=" for the assign-operator.

Then I chose "conditional-expression" for the assignment-expression.

Then I derive like this:

conditional-expression -> binary-expression -> unary-expression ->postfix-expression -> primary-expression -> identifier

As a result of all the above, I can generate an expression like this: "sizeof(int) = 7".

But this expression is not possible in C language. Is there a problem with the above grammar listing, or am I producing this expression in the wrong way?

E. T.
  • 847
  • 10
  • 26
  • 1
    That something is grammatically correct doesn't mean it is logically correct. – KamilCuk Sep 28 '20 at 11:51
  • A grammar is not sufficient to decide if a program is legal or not. `3=4` will also be accepted by that grammar. `a=2` will be accepted even if `a` is undeclared. A compiler has a semantic phase to determine whether a program is legal. Think of a grammar as a sieve that gets out many, but not all, of any impurities. – Mark Plotnick Sep 28 '20 at 11:56
  • @MarkPlotnick thank you. your examples are very clear – kernel_panic Sep 28 '20 at 12:12

1 Answers1

2

That something is grammatically correct doesn't mean it is logically correct. The expression sizeof(int) = 7 may be grammatically correct, but it doesn't make much sense. So your compiler instead of spilling error: syntax error will tokenize and interpret the statement properly and tell you error: cannot assign to result of sizeof.

On the topic you may be interested in the Annex A.1 Lexical grammar in C11 standard draft.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111