I was browsing 🫣 the latest working draft of the ISO C++ Standard (https://eel.is/c++draft/) and then I found that what a single semicolon is called grammatically depends on where it appears. Cool beans 😎!
Here’s a screenshot illustrating my findings (engorgio):
The top half is a piece of valid C++ code stored in sc.cpp
and the bottom half is its Clang AST dump
with unimportant parts removed.
The ;
in the global scope on line 1 of sc.cpp
is an empty-declaration
as noted in [decl.pre]p1.
The grammar production goes:
translation-unit
-> declaration-seq
-> declaration
-> empty-declaration
-> ;
The ;
in the function-body
on line 3 of sc.cpp
is an expression-statement
also called a null statement in this case,
as noted in [stmt.expr]p1.
The grammar production goes:
translation-unit
-> declaration-seq
-> declaration
-> function-definition
-> function-body
-> compound-statement
-> statement-seq
-> statement
-> expression-statement
-> ;
Notice how the Clang AST dump align with this.
That’s it for this C++ trivia hope you like it as much as I do 🙃.