1

I read this documentation: http://docs.python.org/reference/simple_stmts.html

Now, I want to create statements like it describes. For example, a statement that concat a multiple assert and print statements. The syntax is unclear. How would I use the ::= operator?

I will be grateful for a clear example.

4
  • By syntext do you mean syntax? Commented Oct 17, 2012 at 23:22
  • There is no ::= operator in python. It's part of the meta-grammer used to define a grammer element in terms of other grammer elements. Commented Oct 17, 2012 at 23:24
  • ::= is not a Python operator, it is part of the definition of the language syntax, meaning "is defined to be". Commented Oct 17, 2012 at 23:24
  • It's unclear what you mean when you say you want to create a statement. I think you mean you want to create a function that will perform multiple asserts and print, but I'm not sure. Could you clarify what you're hoping to accomplish? Commented Oct 17, 2012 at 23:31

1 Answer 1

14

I think you are confusing the Python grammar reference with examples of actual Python code. The sections with ::= are formally describing the structure of Python statements in Backus–Naur Form. The other examples show actual Python code, and how the formal grammar looks in practice.

For example, the grammar element assert_stmt has the form:

assert_stmt ::= "assert" expression ["," expression]

This describes the structure of an actual Python assert statement, for example:

assert (2 + 2 == 4), "The world is ending!"

The quoted elements in the grammar, called terminals, appear literally in the Python syntax. These include, for example, the assert keyword and the comma. The unquoted parts refer to other grammar elements, called nonterminals; for example, expression corresponds to a Python expression returning a value. Grammar elements in brackets [] denote optionality, so expression ["," expression] refers to a comma-separated list of one or two expressions.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.