Python - EBNF and Railroad

 

Home

 

EBNF

Python - definitions

<real number>    ::= <sign><natural number> |
                     <sign><natural number>
'.'<digit sequence> |
                     <sign>
'.'<digit><digit sequence> |
                     <sign><real number>
'e'<natural number>
<sign>           ::= ‘’ | ‘+’ | ‘-‘
<natural number> ::= ‘
0’ | <nonzero digit><digit sequence>
<nonzero digit>  ::=
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<digit sequence> ::= ‘’ | <digit><digit sequence>
<digit>          ::=
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

 

From <https://stackoverflow.com/questions/14013792/how-to-implement-backus-naur-form-in-python>

 

letter

"A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"

 

special character (including "space")

" " | "*" | "/" | "=" | "<" | ">" | "[" | "]" | "." | "," | "(" | ")" | ":" | "^" | "@" | "{" | "}" | "$" | "#" | "&" | "%"

 

string-character

any character or digit except quote | "''"

 

string

<string-character > { <string-character > }

 

identifier                                                  (Identifiers are Variable names)

letter { letter | digit  }

 

Declaration of variables:

There is no such thing as "variable declaration" or "variable initialization" in Python.

There is simply what we call "assignment", but should probably just call "naming".

Assignment means "this name on the left-hand side now refers to the result of evaluating the right-hand side, regardless of what it referred to before (if anything)".

foo = 'bar' # the name 'foo' is now a name for the string 'bar'
foo =
2 * 3 # the name 'foo' stops being a name for the string 'bar',
# and starts being a name for the integer 6, resulting from the multiplication

As such, Python's names (a better term than "variables", arguably) don't have associated types; the values do. You can re-apply the same name to anything regardless of its type, but the thing still has behaviour that's dependent upon its type. The name is simply a way to refer to the value (object). This answers your second question: You don't create variables to hold a custom type. You don't create variables to hold any particular type. You don't "create" variables at all. You give names to objects.

Second point: Python follows a very simple rule when it comes to classes, that is actually much more consistent than what languages like Java, C++ and C# do: everything declared inside the class block is part of the class. So, functions (def) written here are methods, i.e. part of the class object (not stored on a per-instance basis), just like in Java, C++ and C#; but other names here are also part of the class. Again, the names are just names, and they don't have associated types, and functions are objects too in Python.

From <https://stackoverflow.com/questions/11007627/python-variable-declaration>

 

Concatenation:

Text block = " <string> "

Final statement =  [<Text block> ]  [+ {<identifier> | <Text block>}]

 

Example below:

Machine generated alternative text:

 

Message (print to screen) statements:

Text block = " <string> "

Final statement = print( [<Text block> ]  [, {<identifier> | <Text block>}] )

 

Machine generated alternative text:

 

Assignment:

 

<identifier> =  <Text block> | <identifier> {<Text block> | <identifier>}]

 

Machine generated alternative text:

 

 

Condition (evaluates to true or false):

 

(<identifier> ==|===|<|> <Text block> | <identifier> )

And you could have:

<condition> =(<condition> || <condition> AND <condition> )

( || means OR)

 

Machine generated alternative text:

 

Statement - block of code to be executed

Machine generated alternative text:
e ßlNT

 

 

 

 

"IF" statement: 

 

 

 

 

Railroad

Definitions

N 人 X ^ C ↓ 0 日 0 Z 1 ) 「 
く P 「 い C95 代 
′ ・ つ 物 当 っ ー 物 ・ つ ・ J 
ノ つ テ つ ′ つ 
X 9 ョ ョ 0 つ 日 > 
4 ! 6 ℃

 

 

String:

Machine generated alternative text:

 

 

Identifier                                                    (Identifiers are Variable names)

Machine generated alternative text:

letter 
letter 
( digit

 

 

Declaration of variables:

See: Declaration of variables:

 

Concatenation:

Machine generated alternative text:

Machine generated alternative text:

 

 

Message (print to screen) statements:

Machine generated alternative text:
4

 

Machine generated alternative text:

 

Assignment:

' top

Machine generated alternative text:

 

Condition (evaluates to true or false):

 

(<identifier> ==|===|<|> <Text block> | <identifier> )

And you could have:

<condition> =(<condition> || <condition> AND <condition> )

( || means OR)

 

Machine generated alternative text:

 

Statement - block of code to be executed

Machine generated alternative text:
e ßlNT

 

 

 

 

"IF" statement: