TECH Signal 182
sizeof is surprisingly difficult to parse in c
Engineers building C parsers, linters, or static analysis tools must handle this edge case correctly; naive backtracking approaches fail when compound literals and postfix operators follow a parenthesized type name, and conflating cast-expression parsing with unary-expression parsing produces incorrect results for expressions like `sizeof(int)+1`.
Written by elseif from the cluster below · every claim links back to a sourceThe three things worth knowing
The operand of `sizeof` is either a unary expression or a parenthesized type name, and only type names require parentheses.
Compound literals such as `sizeof(int){0}` make parsing difficult because a parenthesized type name can be followed by a compound literal and then arbitrary postfix operators like `.x[0]()`.
Combining unary-expression and cast-expression parsing into a single function does not work because `sizeof(int)+1` must parse as addition, not as the size of a cast expression.
THE CLUSTER