0

I started my journey into recursive parsers, and was looking into C Grammar, trying to understand how it works to replicate it in my code.

Then I saw this:

assignmentExpression
:   conditionalExpression
|   unaryExpression assignmentOperator assignmentExpression
|   DigitSequence // for
;

and this part in particular:

unaryExpression assignmentOperator assignmentExpression

With my (poor and probably wrong) understanding, a unary expression can be a Constant (if you follow the rest of the grammar), then it looks like this 1 = 1 is valid, which is obviously wrong. I searched for C parsers, even the GCC source code, and the assignmentExpression functions never really have any code related to the unaryExpression part.

So I'm really confused; I'm probably missing something very important here.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Patric
  • 763
  • 1
  • 8
  • 17

1 Answers1

4

C is not completely described by its grammar; a valid C program must conform to the grammar, but it also must conform to many other rules, e.g. rules relating to the type system.

So, you're right: 1 = 1 conforms to the rule you've quoted, but it's invalid anyway, because the left-hand-side is not an lvalue.

(Note that some compilers may actually treat 1 = 1 as a syntax error, because they may roll some of these other restrictions into the grammar that they apply. That's allowed; the spec gives compilers a lot of flexibility in how they implement things, as long as they handle valid programs correctly.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Don't these rules belong to the grammar (like *lvalue* on the left), precisely? – Déjà vu May 01 '19 at 05:09
  • @RingØ Yes, no and a little bit. The grammar in EBNF specifies the syntactical rules which means it specifies the structure of the code. But the type system for example specifies semantic rules which are not possible to describe in EBNF (with a context-sensitive grammar it would be possible but parser ugly to write with them). So in C you will usually need a semantic checker and therefore, stuff which are easier handled in the semantic checker are not part of the grammar. – user6556709 May 01 '19 at 06:07
  • @RingØ: For example, in C, `a = 1;` is invalid if `a` names an array or a function. That's impossible to express in a context-free grammar. – rici May 01 '19 at 14:51