Is this an example of why C++ is not a nice grammar ( context free )?
A = B = ARR[i+1] = x+y*f(z) ;
No, this is not such an example. The above string can easily be
matched by some context free grammar consisting of a few production
rules governing the relationship of some simple operators.
C++ does in fact have a context-free grammar.
Its grammar, however, is ambiguous. Semantic information must be taken
into account in order to resolve ambiguities in the syntax.
This means that there are situations which require a C++ parser to
consider the emerging meaning of the translation unit being processed
before it can decide how to parse the remainder of it.
The parser doesn't know how many equal signs to expect, so it can only
That is not what context free means. A context-free grammar is one
whose production rules strictly expand single nonterminals.
For instance if you have the sentential form A B C, and there is a
rule B -> x y z for expanding B, you can derive the form A x y z
C. Consider that this B occurs in a context: it is surrounded by A
and C. But the rule can be applied without regard for context: it says
that any B whatsoever in a sentential form may be blindly replaced by
x y z. That is why a grammar which only has rules that expand a single
nonterminal symbol is called context-free. This expansion can be done
in reverse; you can identify the x y z as being a B, and reduce to
A B C, which is the essence of parsing. The x y z subsequence is
recognized as being a B without any context; A and C is not taken into
account, only the rule B -> x y z.
A non-context free grammar would have a rule with more than one symbol
in the left hand side. More than one symbol means that a pattern has
to be matched before the expansion takes place. For instance consider
the rule A B -> A x y z . This is very similar to B -> x y z
because the net effect is that B is replaced by x y z. But the A on
both sides means that this replacement can only take place for a B
that is preceded by an A. In other words, it can take place only /in
the context of/ A. Hence, not context-free. But this type of case is
still reasonably nice. In the general case, the rules can be
arbitrary, like A B -> C x y . This one means that some
nonterminal A followed by a nonterminal B, can derive a nonterminal C
followed by terminals x and y. This kind of thing would be a real
hairball to have in a programming language.
assume that each expression is an arithmetic expression in general,
since the last one does not have to be a left value ( variable name or
array expression ), but all the other ones must be a left value.
It is a semantic attribute, not a syntactic attribute, whether or not
an expression is an lvalue. As such it has nothing to do with whether
or not a grammar is context-free.