I know that C is not a context-free language, a famous example is:
int foo;
typedef int foo;
foo x;
In this case the lexer doesn't know, whether foo
in the 3rd line, is an identifier, or typedef
.
My question is, is this the only reason that makes C a Context-Sensitive Language?
I mean, if we get rid of typedef
, would it become context-free language? Or there are other reasons (examples) that prevent it from being so?
+
depends on context: in40 + 2
it means integer addition; in40 + 2.0
it means floating point addition. – Shirleeshirleen+
(and-
) are context-sensitive since they can both mean two different operators depending on context: Unary or binary plus/minus. So from the lexers point of view it's impossible to say which operator is meant. – Trichroismfoo
weren't a typedef,foo x;
would simply be an error, so there's no syntactic ambiguity there. I mean sure, the lexer can't tell whetherfoo
is an identifier or a typename, but the example doesn't explain why it would need to know that (in fact the lexer doesn't necessarily have to know that - that's just one possible way of solving the problem). The common example would be the expression(foo)(x)
, which could either be a function call or a cast, depending on whetherfoo
is a type or not. – Cossackint foo; int bar = (foo)(3);
is not valid C, whiletypedef int foo; int bar = (foo)(3);
is valid. There is an argument (which I don't totally agree with) that we can ignore that error as "semantic", but there is no doubt that the C standard forbids the first excerpt, so that the best we can say is that the (potential) CFG describes a superset of C. (Ignoring the preprocessor.) ... – Rations