Reputation: 6390
simple-declaration:
decl-specifier-seq init-declarator-listopt ;
attribute-specifier-seq decl-specifier-seq init-declarator-list ;
<====
attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [
identifier-list]
initializer ;
Note that the attribute-specifier-seq is required in this definition. When does that happen?
Upvotes: 2
Views: 88
Reputation: 283684
Given that we want our grammar to accept:
DSS;
DSS IDL;
ASS DSS IDL;
(plus accept array forms, which this answer will not deal with any further)
but not
ASS DSS;
That is, if the attribute-specifier(s) are provided, the init-declarator-list becomes required.
The grammar productions as shown in the question provide for this and importantly only parse any legal simple-declaration one way.
Every declaration which does not have an attribute-specifier-seq is parsed using the first case. Every declaration which does is parsed using the second case. There's no overlap between the two cases.
If the second case were
attribute-specifier-seqopt decl-specifier-seq init-declarator-list
;
then the same inputs would be allowed, but the rules overlap -- inputs of the form DSS IDL;
would match both the first and the second, creating an ambiguous parse.
Overlapping rules are not wanted.
There's more than one way to solve this with non-overlapping rules. The following would also be ok:
decl-specifier-seq
;
attribute-specifier-seqopt decl-specifier-seq init-declarator-list
;
In effect, this comes from the Karnaugh map for the truth table for the implication operator (ASS provided implies IDL provided), which has three True cells in an L pattern. One solution arises from using a vertical circle, the other solution arises from using a horizontal circle.
In digital logic, overlap prevents glitching (good!). In language grammar, overlap produces parsing ambiguity (bad!).
Upvotes: 5