1
$\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$
1
  • 1
    $\begingroup$ I posted an answer that addresses parsing issues in C in general, but not specifically for your grammar. It is a bit too long to be posted as solely a comment. If you found it to be not helpful or off topic, I will delete the answer. Thanks! $\endgroup$ – BearAqua Jun 21 '20 at 0:47
2
$\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, privacy policy and cookie policy

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