Syntax means grammar. Grammar is the the surface structure of a program: it says what you are allowed to write, and to some extent, how the compiler will interpret what you write. However, syntax does not say it all. There are additional rules that must be followed if the compiler is to understand your program. In this note, we will indicate some of the more important rules. Even if the syntax is correct, and you follow the additional rules, there is no guarentee that the program will function correctly. Syntax is the first of many steps to a correctly functioning program.
Syntax is simple. It has a simple mathematical means of description whereby a complex statement is broken down by a fixed set of rules into its primitive components. If a statement can be broken down by the rules, then the syntax is correct. This breaking down is also called parsing.
Here is the rule for an expression statement:
expression ;
;
An expression is any of the following:
Important: When forming two-character symbols such as ==, there must be no spaces between the characters.
Now we show by an example how to use these definitions to parse an expression statement.
i = 1 ;is an expression statement. We use the above rules of parse the statement as follows:
i
is a variable name, hence it is an expression.
1
is a constant, hence it is an expression.
i=1
is of the form expression assignment-operation
expression, hence it is an expression.
i=1;
is an expression statement, it is of the
form expression followed by a semi-colon.
The famous
cout <<"Hello World"line in the HellowWorld.C program is also an example of an expressions statement.
cout
is a variable name,
<<
is an operator, although not one that
we listed above, and "Hello World"
is a constant, although of type string, rather than type integer.
The point is that C++ code follows strict yet simple
rules of construction. It pays to understand the pattern.
Here's another example:
j = - ( i + 2 ) ;is an expression-statment:
Even though i = 1 ;
has correct syntax,
the compiler will still complain if it hasn't been previously
introducted to the variable name, usually with a declaration such as:
int i ;Syntax is only one element of getting your program to compile. More insidieous than using a variable before it is declared is the popular habit among beginning programmers to use the value of a variable before anything has ever been stored in the variable:
int i ; int j ; j = i + 1 ;The compiler will generally not complain about this situation, but it will cause the program to malfunction.
Whenenver you use a variable, mentally check that:
The syntax of an expression is often ambiguous, so that the text can be parsed in two different ways. Be very careful that you and the compiler agree on the parsing. Use parenthesis to make the intended meaning unambigous. For instance,
3 * 1 / 2is ambigouous. Is it
(3*1)/2
or 3*(1/2)
?
The first evaluates to 1, the second evaluates to 0.
It is very important to resolve this ambiguity.
These two parse sequences make clear the ambibuity:
Which parse sequence do you mean? Which does the compiler mean? Do all compilers agree with your compiler on the parsing? All very fascinating questions, all have good answers. However, I suggest talking to your compiler like this:(3 * 1) / 2This expression has correct syntax, and can be parsed in only one way, because of the parentheses.