2
$\begingroup$

I have found this reference for the C syntax Backus-Naur Form (BNF). I was wondering if there are any other ambiguities in this grammar other than the infamous "dangling else"? Also how we can modify the BNF grammar to avoid those ambiguities, including the known one mentioned above?

$\endgroup$
0

1 Answer 1

3
$\begingroup$

Not sure if this answers your question, but I will contribute two details regarding lexing/parsing ambiguities in C in general. I hope these are still helpful.

Consider the expression T ** c;. Since C supports type declarations via typedef, the parser itself cannot decide whether this is multiplication with variables T and *c or a variable declaration of a pointer to pointer c of type T**. We'll need information fed into the parser by the lexer to decide which situation we're parsing (the lexer will resolve each valid type name to a type).

Another situation similar to the situation above gives rise to an ambiguity in the C syntax: In general, statements like a +++ b are ambiguous, since both (a++) + b and a + (++b) are valid. In this kind of situation, the C compiler follows the "longest match" rule: the compiler will match against the longest sequence of characters it can match.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.