In this video, we introduce one new kind of expression,

the binary expression and one new kind of token, the operator token.

We will add the binary expression to our expression syntax diagram.

We've already introduced three of the five kinds of Python tokens;

delimiters, literals, and identifiers.

In this lesson, we introduce the operator token.

Consider this program whose third statement contains

a binary expression and an operator token called plus.

This program prompts the user to enter two values and binds

the identifier's first value and

second value to the two string objects returned by the input function.

I will run this Python program.

I will enter 27 and 65,

the answer is 2,765.

What happened? Every statement except for

statement three contains tokens and expressions that you have already studied.

All of these statements have valid lexics and syntax.

What about statement three?

Let's apply lexical analysis,

syntax analysis, and semantic analysis to statement three.

Statement three contains these tokens;

the identifier sum, assignment delimiter,

and the identifier first underscore value.

The next character is a plus and

no token kind that we have described so far starts with the plus character.

The plus character is a new kind of token called an operator token.

An operator is used to compute a result object from one or more operands.

Since there are only 19 operator tokens,

we list them in a lexical table instead of using a lexical rule.

Some mathematical operators are represented by the same operator token in Python.

Some are represented by different operator tokens

and some are not represented by any operator token.

Plus sign and minus sign are the same in Python as in mathematics.

In mathematics, we use a product symbol or

a dot to represent multiplication and in Python,

we use the star symbol.

The mathematical comparison operator less than or

equal sign is represented by two symbols in Python,

the less-than sign followed by the equal sign.

In mathematics, exponentiation is denoted by a superscript.

But in Python, the star star operator token is used.

Equal sign is an equality operator in mathematics,

but in Python, the equality operator is equal sign followed by another equal sign.

Recall that equal sign by itself is actually the assignment delimiter token,

not an operator token.

It is a common programming error to use

the assignment delimiter token when you should use the equality operator.

The factorial operator uses an exclamation mark in mathematics,

but there's no factorial operator token in Python.

You just saw that the operator token table contains both star star and star.

If the lexical analyzer encounters star star,

does it create a single operator token star star,

or two operator tokens star and star?

We can apply the longest token rule to operator tokens just like we

applied it to literal integers and identifiers in the lexical analysis lesson.

So, the answer is one long token star star.

The last token in statement three is the identifier second underscore value.

Statement three is an assignment statement where

the expression is a new kind of expression, a binary expression.

Here is the syntax diagram for a binary expression.

This syntax diagram contains a new non-terminal binary operator.

For now, we will use this syntax diagram for the non-terminal state binary operator.

Any operator token except for tilda,

can be used as a binary operator.

In future lessons, we will generalize

binary operator to include some other tokens that are not operator tokens.

The expression in statement three is a binary expression since

first value matches the first expression as an identifier.

The plus sign token matches

the binary operator state and

the second value matches the second expression as an identifier.

A binary expression is used to apply an operator function to two operand objects.

The function that is applied depends both on

the binary operator and the type of the first operand.

The semantics of a binary expression are:

Evaluate the left expression to get the first operand object.

Evaluate the right expression to get the second operand object.

Identify the operator function based on

the binary operator and the type of the two operands.

Apply the binary operator's function.

In a future lesson,

you will discover that some binary operator functions may ignore the second expression.