|
|
|
|
|
by weinzierl
182 days ago
|
|
In C, the token sequence T * x;
can be interpreted either as a declaration or as an expression (multiplication). Resolving this ambiguity requires context, namely whether T is a typedef name or an object name, which cannot be determined from syntax alone.One could argue that it is acceptable to produce an ambiguous parse and defer the decision, and in practice compilers do rely on semantic information to resolve such cases. However, this is not what context-free means in the Chomsky sense: a context-free grammar must be able to determine the syntactic structure without reference to prior declarations or semantic state. |
|
In contrast, languages like Java, JavaScript or C# would be context-free: The string
is not a "valid" JavaScript program, because of the undefined identifiers, but it does have a non-ambiguous AST.