- CS320/08 Expressions and Assignment Statements
- : Preparation
- : Note 1: Expressions are not Statements
- : Note 2: Precedence and Associativity
- Class Work
- Lab Work
- Next

- expression::= simple_expression #( operator simple_expression),
- operator::= "+" | "*".
But this does not show the relative precedence of "+" and "*".

We show the precedence of "*" and "+" in algebra by replacing the above by:

- expression::= term #( "+" term),
- term::= simple_expression #( "*" simple_expression).
To express associativity we can replace the '#' by recursion. For example:

- expression::= term | expression "+" term, -- left associative.
- term::= simple_expression| simple_expression "*" term, -- right
associative.
Warning: it is unusual(and confusing) to have different associativities for multiplication and addition in a real language.

Prev [ 07.html ] | Data Types | Chapter 6 | lab07 C/C++ Data |

08 | Expressions etc | Chapter 7 | lab08 C/C++ Expressions |

Next [ 09.html ] | Control structures | Chapter 8 not section 5 | lab09 C/C++ Control |

(Close Table)

Answer review questions on chapter 7.

Hand in answers to 2 or more review questions.

Be careful to distinguish Expressions from Statements. Expressions are evaluated and return Values. Statements are executed and return a new State. Most languages have both.

You may want to express precedence and associativity in your project.

These are syntax not semantics. Express precedence and associativity in BNF/EBNF/XBNF not in the UML. It takes some extra definitions. See sections 3.3.1.7, 3.3.1.8 and 3.3.1.9 in the book.

For example, suppose we might define