I am studying on how to make a bytecode interpreter (the language i am studing is clox at the site https://craftinginterpreters.com/). In it a valid clox program is defined as a list of declarations. A declaration is defined as either a class, function or variable declaration OR as a statement.
Now in C, i know there are different kinds of declarations and there are different kinds of statements but none of the types of declarations are a statement and none of the type of statements are a declaration. I think any possible line of C code is either one or the other so how do the standard define a C program ?
A list of lines that can be either a definition or a statement ?
The C standard defines a translation unit (effectively the source file being compiled, including all the files it includes with #include
) as a list of external-declaration items, and each external-declaration is either a function-definition or a declaration (C 2018 6.9 1).
So, fundamentally, each source file of a C program is a list of declarations, including function definitions.
Each function-definition has, after the part that declares the type(s) of the function and its parameter(s), a compound-statement (6.9.1 1). A compound-statement is a list of declaration or statement items enclosed in {
and }
(6.8.2 1).
Each declaration tells us about (usually1) one or more identifiers (6.8 2 and 5). A statement is some sort of instruction for the computer to “do something,” such as iterate a loop or evaluate an expression. In the C grammar, there is no overlap between declarations and statements.
Further information on declarations is in clause 6.7 of the C 2018 standards, and further information on statements is in clause 6.8.
1 In the C grammar, a static_assert-declaration is also a declaration. It does not declare any identifier but creates a compile-time assertion that is tested and that produces an error message if the assertion fails.